var ie = (navigator.userAgent.indexOf('MSIE')>=0) ? true : false;
var ie6 = (navigator.userAgent.indexOf('MSIE 6')>=0) ? true : false;

/*** JavaScript Enabled ***/

var jsEnabled = function(){
	$$('body')[0].removeClassName('no-js');
};

/*** Cufón Configuration ***/

var startCufon = function(){
	Cufon.replace('ul#nav-main > li > a',{
		color: '-linear-gradient(#4a4a4a, #6b6b6b)',
		fontFamily: 'eau',
		textShadow: '1px 1px rgba(0, 0, 0, 0.05)'
	});

	Cufon.replace('ul#nav-main > li > a.over',{
		color: '#ffffff'
	});

	Cufon.replace('ul#nav-footer li h4',{
		fontFamily: 'eau',
		letterSpacing: '-1px'
	});

	Cufon.replace('ul#nav-tab-home li a.tab-1 span.copy',{
		color: '-linear-gradient(#e3e3e3, #f1f1f1)',
		fontFamily: 'eau'
	});

	Cufon.replace('ul#nav-tab-home li a.tab-2 span.copy',{
		color: '-linear-gradient(#973333, #bd4f4f)',
		fontFamily: 'eau'
	});

	Cufon.replace('ul#nav-tab-home li a.tab-3 span.copy',{
		color: '-linear-gradient(#3682cd, #59b2e0)',
		fontFamily: 'eau'
    	});

	Cufon.replace('ul#nav-tab-home li a.tab-4 span.copy', {
    		color: '-linear-gradient(#816E4B, #988158)',
    		fontFamily: 'eau'
	});

	Cufon.replace('div#tab-connect h3',{
		color: '-linear-gradient(#3682cd, #59b2e0)',
		fontFamily: 'eau'
	});

	Cufon.replace('h3.section-title',{
		color: '-linear-gradient(#606060, #3c3c3c)',
		fontFamily: 'eau'
	});

	Cufon.replace('div#news-ticker h3',{
		color: '-linear-gradient(#3a3a3a, #646454)',
		fontFamily: 'eau',
		textShadow: '1px 1px rgba(255, 255, 255, 0.2)'
	});

	Cufon.replace('ul.slider-select li.slider-item',{
		color: '#666666',
		fontFamily: 'eau'
	});

	Cufon.replace('ul.slider-select li.slider-item.selected',{
		color: '#a22525',
		fontFamily: 'eau'
	});

	Cufon.replace('div#tab-whats_on h3.category',{
		color: '#a22525',
		fontFamily: 'eau'
	});

	Cufon.replace('div.section-whats_on div.column-search-form ol li > label',{
		color: '#666666',
		fontFamily: 'eau'
	});

	Cufon.replace('div.section-whats_on div.column-search-form ol li > span.pseudo-label',{
		color: '#666666',
		fontFamily: 'eau'
	});

	Cufon.replace('table.yacal thead tr.nav th.title',{
		color: '#222222',
		fontFamily: 'eau'
	});

	Cufon.replace('table.ajaxCal tbody tr td table.title tbody tr td:nth-child(2)', {
		color: '#222222',
		fontFamily: 'eau'
	});

	Cufon.replace('table.ajaxCal tbody tr th.days', {
		color: '#222222',
		fontFamily: 'eau'
	});
	
	Cufon.replace('table.yacal thead tr.dow th abbr',{
		color: '#222222',
		fontFamily: 'eau'
	});

	Cufon.replace('a.btn-cufon span.copy',{
		color: '#ffffff',
		fontFamily: 'eau'
	});

	Cufon.now();
}

/*** Navigation Mega Drop Down ***/

var navDropDown = Class.create({
    initialize: function (options) {
        this.options = Object.extend({
            elNavContainer: $('nav-main'),
            elNavItem: 'a.target-link',
            elNavSub: 'div.nav-sub',
            elNavSubColumn: 'div.column',
            elNonNavLink: '#content a',
            elLastNavLink: '#nav-main a:last'
        }, options || {});

        var navMouseOver = this.__navMouseOver.bindAsEventListener(this),
			navMouseOut = this.__navMouseOut.bindAsEventListener(this),
			navSubMouseOver = this.__navSubMouseOver.bindAsEventListener(this),
			navSubMouseOut = this.__navSubMouseOut.bindAsEventListener(this);

        this.columnCount();
        $$(this.options.elNavSub).invoke('hide');
        $$(this.options.elNavSub).invoke('removeClassName', 'invisible');
        $$(this.options.elNavItem).invoke('observe', 'mouseenter', navMouseOver).invoke('observe', 'mouseleave', navMouseOut).invoke('observe', 'focus', navMouseOver); //.invoke('observe', 'keypress', navMouseOver);
        $$(this.options.elNavSub).invoke('observe', 'mouseenter', navSubMouseOver).invoke('observe', 'mouseleave', navSubMouseOut).invoke('observe', 'blur', navMouseOut).invoke('observe', 'focus', navMouseOver);
        $$(this.options.elNonNavLink).invoke('observe', 'focus', navMouseOut);

        $($$(this.options.elLastNavLink)[$$(this.options.elLastNavLink).length - 1]).observe('blur', this.hideAllMenus);


    },
    hideAllMenus: function () {
        jQuery('.nav-sub').fadeOut(50);
        jQuery('a.over').removeClass('over');
        Cufon.refresh('ul#nav-main > li > a', 'ul#nav-main > li > a.over');
    },
    columnCount: function () {
        var navSubColumns = $$(this.options.elNavSubColumn);

        for (var i = 0; i < this.options.elNavSub.length; i++) {
            var elNavSub = $$(this.options.elNavSub)[i];

            if (elNavSub) {
                var navSubColumns = elNavSub.childElements();

                if (navSubColumns.length > 4) {
                    elNavSub.addClassName('nav-sub-large');
                }
            }
        }
    },
    showDropDown: function (elTarget) {
        var elNavSub = elTarget.adjacent(this.options.elNavSub);
        //var otherNav = $$('a.other');
        //if (otherNav) {
        $$('a.over').invoke('removeClassName', 'over');
        //}
        elTarget.addClassName('over');

        clearTimeout(this.fadeDelay);

        this.hoverDelay = setTimeout(function () {
            elNavSub[0].appear({
                duration: 0.15
            });
        } .bind(this), 150);

        Cufon.refresh('ul#nav-main > li > a', 'ul#nav-main > li > a.over');
    },
    hideDropDown: function (elTarget) {
        var elNavSub = elTarget.adjacent(this.options.elNavSub);

        clearTimeout(this.hoverDelay);

        elTarget.removeClassName('over');

        this.fadeDelay = setTimeout(function () {
            elNavSub[0].fade({
                duration: 0.05,
                from: 1,
                to: 0
            });
        } .bind(this), 50);

        Cufon.refresh('ul#nav-main > li > a', 'ul#nav-main > li > a.over');
    },
    __navMouseOver: function (e) {
        var elTarget = e.findElement('a');

        $$(this.options.elNavSub).invoke('hide');

        for (var i = 0; i < this.options.elNavItem.length; i++) {
            if (elTarget == $$(this.options.elNavItem)[i]) {
                this.showDropDown(elTarget);
                break;
            }
        }
    },
    __navMouseOut: function (e) {
        var elTarget = e.findElement('a');

        for (var i = 0; i < this.options.elNavItem.length; i++) {
            if (elTarget == $$(this.options.elNavItem)[i]) {
                this.hideDropDown(elTarget);
                break;
            }
        }
    },
    __navSubMouseOver: function (e) {
        var elTarget = e.findElement(this.options.elNavSub).adjacent('a.nav-item')[0];

        for (var i = 0; i < this.options.elNavItem.length; i++) {
            if (elTarget == $$(this.options.elNavItem)[i]) {
                this.showDropDown(elTarget);
                break;
            }
        }
    },
    __navSubMouseOut: function (e) {
        var elTarget = e.findElement(this.options.elNavSub).adjacent('a.nav-item')[0];

        for (var i = 0; i < this.options.elNavItem.length; i++) {
            if (elTarget == $$(this.options.elNavItem)[i]) {
                this.hideDropDown(elTarget);
                break;
            }
        }
    }
});

/*** Widget On Change Event ***/

document.observe('widget:onChange', function(){
	Cufon.refresh();
});

/*** Load On DOM Ready ***/

document.observe('dom:loaded', function(){
	mainNavDropDown = new navDropDown({
		elNavContainer: $('nav-main'),
		elNavItem: 'ul#nav-main > li > a.nav-item',
		elNavSub: 'div.nav-sub'
	});
	jsEnabled();
	startCufon();
});
