﻿function addMega() {
    $(this).addClass("hovering");
    $(this).addClass("over");
    if ($(this).hasClass('first')) {
        $('#nav_left').addClass('hover');
    } else if ($(this).hasClass('last')) {
        $('#nav_right').addClass('hover');
    }
}

function removeMega() {
    $(this).removeClass("hovering");
    $(this).removeClass("over");
    if ($(this).hasClass('first')) {
        $('#nav_left').removeClass('hover');
    } else if ($(this).hasClass('last')) {
        $('#nav_right').removeClass('hover');
    }
}

$(document).ready(function() {

    var megaConfig = {
        interval: 100,
        sensitivity: 4,
        over: addMega,
        timeout: 300,
        out: removeMega
    };

    $(".mega").hoverIntent(megaConfig);

    if ($('#img-display').length > 0) {
        $('#img-display').cycle({
            fx: 'fade',
            pause: 1,
            speed: 700,
            timeout: 0,
            pager: '#img-thumbs',
            pagerEvent: 'mouseover',
            pagerAnchorBuilder: function(idx, slide) {
                // return selector string for existing anchor 
                return '#img-thumbs li:eq(' + idx + ') a';
            }
        });
    }

    //$.preloadCssImages();
    // Resize the sides of the subnav
    //console.log("Width: " + $('.main_subnav_bottom').width());
    //var $('.main_subnav_bottom').width()
    $('#nav_content ul > li').click(function(e) {
		e.stopPropagation();
		if($(this).attr("target") != undefined && $(this).attr("target") != "") {
			var this_window = window.open($(this).children("a").attr("href"), '_self');
			this_window.focus();
		}
    });
    if ($.browser.msie && $.browser.version.substr(0,1) > 6) {
        // Calculate the width so the nav is equal in all browsers
        var nav_items = 0;
        var nav_text_space = 0;
        $("#nav_content > ul > li").each(function(index) {
            nav_text_space += parseInt($(this).children("a").width());
            nav_items++;
        });

        var nav_empty_space = $("#divMain").width() - nav_text_space;
        var nav_item_space = Math.floor((nav_empty_space / nav_items));

        var nav_item_width = 0;
        $("#nav_content > ul > li").each(function(index) {
            nav_item_width = $(this).children("a").width() + nav_item_space;
            $(this).css({ 'width': (nav_item_width - 4) + 'px' });
        });

    }
    $('.main_subnav_bottom').each(function(i, obj) {
        $(this).css({
            'width': ($(this).parent().width() - 8) + 'px'
        });
    });
    $('.main_subnav_right').each(function(i, obj) {
        $(this).css({
            'height': $(this).parent().height() + 'px'
        });
    });

    $('#search_submit').click(function() {
        window.open('http://www.libertymedical.com/search/node/' + $('#search_field').val(), '_self');
    });

});
