var Cobalt = (Cobalt === undefined) ? {}:Cobalt;

Cobalt.BaseNavigation = function(){
    return this;
};
Cobalt.BaseNavigation.setIE = function() {
    /*@cc_on
     @if(@_jscript_version > 5)
     return true;
     @else@*/
    return false;
    /*@end
     @*/
};

Cobalt.BaseNavigation.prototype.ieHeightOffset =  0;
Cobalt.BaseNavigation.prototype.heightOffset = 0;
Cobalt.BaseNavigation.prototype.isIE = Cobalt.BaseNavigation.setIE();
/**
 * Add hover functionality with jQuery since CSS :hover is poorly supported
 */
Cobalt.BaseNavigation.prototype.addHover = function(element) {
    var dropul = jQuery(element).children("ul").eq(0);
    var dropiframe = jQuery(element).children("iframe");
    var me = this;
    jQuery(element).hover(
            function() {
                var heightOffset = dropul.height();
                if(me.isIE){
                    heightOffset += me.ieHeightOffset;
                } else {
                    heightOffset += me.heightOffset;
                }
                dropul.css("display", "block");
                dropiframe.css("display", "block");
                dropiframe.css("height", heightOffset + "px");
            },
            function() {
                dropul.css("display", "none");
                dropiframe.css("display", "none");
            }
            );
};

Cobalt.BaseNavigation.prototype.extendHoverState = function() {
    jQuery('#pmenu li.main').hover(
            function() {
                jQuery(this).children('a.mainAnchor').addClass('over');
            },
            function() {
                jQuery(this).children('a.mainAnchor').removeClass('over');
            }
            );
    jQuery('#pmenu li.sub').hover(
            function() {
                jQuery(this).children('a.subAnchor').addClass('over');
            },
            function() {
                jQuery(this).children('a.subAnchor').removeClass('over');
            }
            );
};

Cobalt.BaseNavigation.prototype.checkOverflow = function(selector) {
    var ieWildCard = (this.isIE) ? 38 : 0;
    var element = jQuery(selector);
    element.show(); // give the element a display property so we can grab the width and offset
    var rightPos = element.offset().left + element.width();
    element.hide();
    if (rightPos - 1000 > 0) element.css("left", (rightPos - (1000 - ieWildCard)) * -1 + "px");
};

Cobalt.BaseNavigation.prototype.init = function() {
    var me = this;
   jQuery('<iframe src="about:blank"></iframe>').insertAfter('ul#pmenu li.parent a.main');
    this.extendHoverState();
    jQuery('#pmenu li.parent').each(function() {
        me.addHover(this);
    });
    jQuery('#pmenu ul.sub').each(function() {
        me.checkOverflow(this);
    });
};

jQuery(window).bind("load", function(){
    var nav = new Cobalt.BaseNavigation;
    nav.init();
});
