$(function() {

    var zindex = 10;

    var config = {
        sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
        interval: 0,  // number = milliseconds for onMouseOver polling interval    
        over: doOpen,   // function = onMouseOver callback (REQUIRED)    
        timeout: 100,   // number = milliseconds delay before onMouseOut    
        out: doClose    // function = onMouseOut callback (REQUIRED)    
    };

    function doOpen() {

        $("ul.nav-bar:first").mouseover(function(e) {
            var relativeX = e.pageX - this.offsetLeft + 200;
            var viewportWidth = $(window).width();

            if (relativeX > viewportWidth) {
                $('ul.nav-bar li ul, ul.nav-bar li ul li ul').addClass("openLeft");
            } else {
                $('ul.nav-bar li ul, ul.nav-bar li ul li ul').removeClass("openLeft");
            }
        });

        zindex++;
        $(this).addClass("hover");
        $(this).css('z-index', zindex);
        $('ul:first', this).slideDown(300);
    }

    function init() {
        doOpen();
    }

    function doClose() {
        $(this).removeClass("hover");
        $('ul:first', this).fadeOut(200);
    }

    $("ul.nav-bar li").hoverIntent(config);

    $('ul.nav-bar:first li a').each(function() {
        if (window.location.href.toLowerCase() == window.location.protocol + '//' + window.location.host + '/' ||
            window.location.href.toLowerCase() == window.location.protocol + '//' + window.location.host + '/default.aspx') {

            $('ul.nav-bar li:first a').addClass('active');

            return false;
        }
        else if (window.location.pathname.indexOf($(this).text().replace(/ /gi, '_')) != -1) {
            $(this).addClass('active');

            return false;
        }
    });

    /*$("ul.nav-bar li ul li:has(ul)").find("a:first")
    .css("background-image", "url('/_common/img/icon-arrow-r.gif')")
    .css("background-repeat", "no-repeat")
    .css("background-position", "145px 15px");*/

    init();
});