var setupMouseMove = 1;
var setupMouseLeave = 1;

function initExpandCollapse() {
    $('#mainMenuColumn').hover(function() {
        $('#mainMenu,#contactDetails').animate({ width: "150px" }, { queue: false, duration: 600, easing: "swing" });
    },
    function() {
        $('#mainMenu,#contactDetails').animate({ width: "15px" }, { queue: false, duration: 600, easing: "swing" });
    });
}

function checkHeight() {
    if ($.browser.opera) {
        var windowHeight = window.innerHeight;
    }
    else {
        var windowHeight = $(window).height();
    }
    if (windowHeight < 600) {
        $('#mainMenuColumnInner').addClass('relative');
    }
    else {
        $('#mainMenuColumnInner').removeClass('relative');
    }
}

$(document).ready(function() {
    checkHeight();
    $(window).scroll(function () {
        checkHeight();
    });
    $(window).resize(function () {
        checkHeight();
    });
    if (!$('body').hasClass('home') && !$('body').hasClass('error404') && !navigator.userAgent.match(/iPhone|iPad|iPod|BlackBerry|Android/)) {
        if (!$('body').hasClass('page-child')) {
            $('#mainMenu,#contactDetails').animate({ width: "15px" }, 600, "swing", function() {
                $(document).mousemove(function(e) {
                    if (setupMouseMove == 1) {
                        if (e.pageX < 31) {
                            $('#mainMenu').mouseleave(function() {
                                if (setupMouseLeave == 1) {
                                    initExpandCollapse();
                                    setupMouseLeave = 0;
                                }
                            });
                        }
                        else {
                            initExpandCollapse();
                        }
                        setupMouseMove = 0;
                    }
                });
            }, false);
        }
        else {
            initExpandCollapse();
        }
    }
    var textHeight = $('#pageMainText').height();
    $('#pageMainText').css('height', textHeight+'px');
    if ($('#mainBackgroundImage').length) {
        var backgroundSource = $('#mainBackgroundImage img').attr('src');
        $('body').css('background-image','url('+backgroundSource+')');
    }
    $('#pageMoreDetails li .linkDetails').each(function() {
        $(this).show();
        var height = $(this).height();
        if ($(this).hasClass('hidden')) {
            $(this).hide();
        }
        $(this).css('height', height+'px');
    });
    $('#pageMoreDetails li a').click(function() {
        if (!$(this).hasClass('active')) {
            $('#pageMoreDetails li a.active').removeClass('active').siblings('.linkDetails').slideUp(300);
            $(this).addClass('active').siblings('.linkDetails').slideDown(300,function() {
                var image = $(this).siblings('img').attr('src');
                $('body').css('background-image','url('+image+')');
            });
            if ($('.videoWrapper').length) {
                var videoPosition = $('.videoWrapper').attr('id').replace(/video/gi, '');
                var currentItem = $(this).parent().attr('class').replace(/item/gi, '');
                if (videoPosition == currentItem) {
                    $('.videoWrapper').show();
                }
                else {
                    $('.videoWrapper').hide();
                }
            }
        }
        return false;
    });
    if (navigator.userAgent.match(/iPhone|iPad|iPod|BlackBerry|Android/)) {
        $('.page-child #mainMenu, .page-child #contactDetails').css({width: "150px"});
    }
});
