$(document).ready(function () {
    var StopTimeOut;
    var StopMainTimeOut;
    $("body").addClass("js");

    $(".subExposureAreaTwo .box:last").addClass("last");
    $(".productTab").parent("ul").addClass("productTabList");

    // whatDoINeedBox
    $('#buyProduct .whatDoINeedBox').hide();
    $('#buyProduct a.whatDoINeed').click(function () {
        $('#buyProduct .whatDoINeedBox').slideToggle(300);
        return false;
    });

    $('.subExposureArea .box .textWrapper .text').hide();
    $('.subExposureArea .box a.textWrapper').parent().bind('mouseenter', function () {
        $(this).find('.text').slideDown(300);
    }).bind('mouseleave', function () {
        $(this).find('.text').slideUp(300);
    }).click(function () {
        window.location = $(this).find('.textWrapper').attr("href");
    });

    $("#siteHeader nav a.mainNavLink").bind("mouseenter", function () {
        var element = $(this);
        if (element.hasClass('over')) {
            //stops the slideup function when you go from submenu to the parent mainNavLink
            clearTimeout(StopTimeOut);
        } else {
            //stops the hide of submenu when you go from mainNavLink to mainNavLink
            clearTimeout(StopMainTimeOut);
            //hides the submenu when you go from mainNavLink to mainNavLink
            $("#siteHeader .submenu").hide();


            //removes the "hover" effects from mainNavLink when you go from submenu to another mainNavLink
            clearTimeout(StopTimeOut);
            $("#siteHeader nav a.mainNavLink").removeClass("over");

            //puts the right width on the submenu, depending on how many ul is in it
            var submenu = element.siblings(".submenu");
            var n = submenu.children("div").size();
            if (n == 1) {
                submenu.attr("style", "width:330px");
            }
            else if (n == 3) {
                submenu.attr("style", "width:730px");
            }

            // a position which is relative to mainNavLink
            var submenuPosition = -45;
            var shift = $(window).width() - element.offset().left - submenu.width() - submenuPosition;
            // if submenu is cut by windowSize then shift menu to left
            if (shift < 50)
                submenu.css("left", shift - 100);
            //slides the menu down
            submenu.show();
        }
    }).bind("mouseleave", function () {
        StopMainTimeOut = setTimeout(function () {
            $(".submenu").hide();
            $(".mainNavLink").removeClass("over");
        }, 200);
    });

    $(".submenu").bind("mouseenter", function () {
        //stops the hide of sibling menu if you go to sibling submenu from mainNavLink
        clearTimeout(StopMainTimeOut);
        //stops slideup function when you go back to submenu
        clearTimeout(StopTimeOut);
        //adds "hover" class to parent mainNavLink
        $(this).siblings("a").addClass("over");
    }).bind("mouseleave", function () {
        //starts slideup function when you go outside submenu
        var menu = this;
        StopTimeOut = setTimeout(function () {
            $(menu).hide();
            $(menu).siblings("a").removeClass("over");
        }, 1000);
    });

    //box follow scroll
    var followScroll = $('#followScroll');
    if (followScroll.length > 0) {
        followScroll.css('position', 'relative');
        $(window).scroll(function (event) {
            var top = followScroll.parent().offset().top;
            var h = followScroll.height() + 70;
            var bottom = top + $('.product').height() - h;
            bottom = bottom > top ? bottom : top;
            // what the y position of the scroll is
            var y = $(this).scrollTop();

            // whether that's below the form
            if (y >= top) {
                // whether that's below the form
                if (y >= bottom)
                    followScroll.css({ position: 'absolute', top: bottom - top + 'px' });
                else
                    followScroll.css({ position: 'fixed', top: 0 + 'px' });
            }
            else
                followScroll.css({ position: 'absolute', top: '0px' });
        });
    }

    //last-child for ie other get from css last-child
    if (jQuery.browser.msie && jQuery.browser.version <= 8) {
        $("#siteFooter .siteLinks li:last-child").addClass("last");
        $(".subExposureAreaTwo li:last-child").addClass("last");
        $(".subExposureAreaTwo .box:last-child").addClass("last");
        $(".subExposureArea .box:last-child").addClass("last");
    }

    // search panel
    var handleSearch = function(e) {
        e.stopPropagation();
        e.preventDefault();
        var input = $(this).parent().find('input');
        if (!window.__searchLink || jQuery.trim(input.val()) == "")
            return;
        document.location = window.__searchLink + (window.__searchLink.indexOf("?") > 0 ? "&" : "?") + "query=" +
            encodeURIComponent(input.val());
    };
    
    $(".search button, #search").parent().keypress(function (e) {
        if (e.which == 13)
            handleSearch.apply($(this).find('button'), arguments);
    });

    $(".search button, #search").click(handleSearch);
});
function slideToQuestion(qid) {
    $(document).ready(function () {
        if ($("#" + qid).length > 0)
            window.scrollTo(0, $("#" + qid).parent().offset().top);
    });
}
