if (typeof nftr == 'undefined') nftr = new Object();

nftr = {

    /* included a cross-browser onenter and onleave events */
    /* July, 25 - 2008 - http://blog.stchur.com/2007/03/15/mouseenter-and-mouseleave-events-for-firefox-and-other-non-ie-browsers/ */
	evtHash: [],

	ieGetUniqueID: function(_elem) 	{
		if (_elem === window) { return 'theWindow'; }
		else if (_elem === document) { return 'theDocument'; }
		else { return _elem.uniqueID; }
	},

	addEvent: function(_elem, _evtName, _fn, _useCapture) {
		if (typeof _elem.addEventListener != 'undefined')
		{
			if (_evtName == 'mouseenter')
				{ _elem.addEventListener('mouseover', nftr.mouseEnter(_fn), _useCapture); }
			else if (_evtName == 'mouseleave')
				{ _elem.addEventListener('mouseout', nftr.mouseEnter(_fn), _useCapture); } 
			else
				{ _elem.addEventListener(_evtName, _fn, _useCapture); }
		}
		else if (typeof _elem.attachEvent != 'undefined')
		{
			var key = '{FNKEY::obj_' + nftr.ieGetUniqueID(_elem) + '::evt_' + _evtName + '::fn_' + _fn + '}';
			var f = nftr.evtHash[key];
			if (typeof f != 'undefined')
				{ return; }
			
			f = function()
			{
				_fn.call(_elem);
			};
		
			nftr.evtHash[key] = f;
			_elem.attachEvent('on' + _evtName, f);
	
			// attach unload event to the window to clean up possibly IE memory leaks
			window.attachEvent('onunload', function()
			{
				_elem.detachEvent('on' + _evtName, f);
			});
		
			key = null;
			//f = null;   /* DON'T null this out, or we won't be able to detach it */
		}
		else
			{ _elem['on' + _evtName] = _fn; }
	},	

	removeEvent: function(_elem, _evtName, _fn, _useCapture) {
		if (typeof _elem.removeEventListener != 'undefined')
			{ _elem.removeEventListener(_evtName, _fn, _useCapture); }
		else if (typeof _elem.detachEvent != 'undefined')
		{
			var key = '{FNKEY::obj_' + nftr.ieGetUniqueID(_elem) + '::evt' + _evtName + '::fn_' + _fn + '}';
			var f = nftr.evtHash[key];
			if (typeof f != 'undefined')
			{
				_elem.detachEvent('on' + _evtName, f);
				delete nftr.evtHash[key];
			}
		
			key = null;
			//f = null;   /* DON'T null this out, or we won't be able to detach it */
		}
	},
	
	mouseEnter: function(_pFn) {
		return function(_evt)
		{
			var relTarget = _evt.relatedTarget;				
			if (this == relTarget || nftr.isAChildOf(this, relTarget))
				{ return; }

			_pFn.call(this, _evt);
		}
	},
	
	isAChildOf: function(_parent, _child) {
		if (_parent == _child) { return false };
		
		while (_child && _child != _parent)
			{ _child = _child.parentNode; }
		
		return _child == _parent;
	},
    
    /* end onenter and onleave events */
    
    home_animation: function() {
        if (jQuery("#HomePageContent").size()>0) {
            var so = new SWFObject(this.media_path + "flash/nftr/nftr_home.swf", "MainNavFlash", "920", "452", "9", "#ffffff");
            so.addParam("allowScriptAccess", "always");
            so.addParam("scale", "noscale");
            so.addParam("salign", "T");
            so.addParam("wmode", "transparent");
            so.addVariable("xmlPath", nftr.mainNavXMLPath);
            so.addVariable("xmlHome", nftr.homeSpecialPath);
            so.addVariable("itemSelected", nftr.mainNavItemSelected);
            so.write("MainImage");
            
            jQuery("#MainNav").remove();
        }
    },

    set_logo : function() {
        if (jQuery("#Logo").size()>0) {
            var so = new SWFObject(this.media_path + "flash/nftr/logo.swf", "LogoFlash", "173", "158", "8", "#ffffff");
            so.addParam("scale", "noscale");
            so.addParam("salign", "T");
            so.addParam("wmode", "transparent");
            so.write("Logo");
        }
    },
    
    init_main_nav : function() {
        if (jQuery("#MainNav").size() > 0) {
            jQuery("#MainNav ul li a:eq("+nftr.mainNavItemSelected+")").attr('class','active');
            
            var so = new SWFObject(this.media_path + "flash/nftr/main_nav.swf", "MainNavFlash", "720", "21", "9", "#ffffff");
            so.addParam("allowScriptAccess", "always");
            so.addParam("scale", "noscale");
            so.addParam("salign", "T");
            so.addParam("wmode", "transparent");
            so.addVariable("xmlPath", nftr.mainNavXMLPath);
            so.addVariable("itemSelected", nftr.mainNavItemSelected);
            so.write("MainNav");

        }
    },

	sifr : function() {
		if (typeof sIFR == "function") {
            //headers
            sIFR.replaceElement(named({sSelector:"h1.section-title", sFlashSrc:this.media_path + "flash/nftr/NexusMixBold.swf", sColor:"#6c2602", sLinkColor:"#6c2602", sBgColor:"#FFFFFF", sHoverColor:"#6c2602", sWmode:"transparent", nPaddingTop:0, nPaddingBottom:0}));
            sIFR.replaceElement(named({sSelector:".related-title h4", sFlashSrc:this.media_path + "flash/nftr/NexusMixBold.swf", sColor:"#6c2602", sLinkColor:"#6c2602", sBgColor:"#FFFFFF", sHoverColor:"#6c2602", sWmode:"transparent", nPaddingTop:0, nPaddingBottom:0}));

		};
	},

    dynamic_archive_links : function() {
        if (jQuery('#NewsStoryArchive').size() > 0) {
            jQuery('#NewsStoryArchive span').attr('class','news-archive-year');
            jQuery('#NewsStoryArchive ul').attr('class','news-archive-months');
            
            jQuery('ul.news-archive-months').hide().parent().find('.news-archive-year').prepend('<img src="'+this.media_path+'img/arrow_right.png" alt="hide months" style="display:none;" class="img-arrow-hide" /><img src="'+this.media_path+'img/arrow_down.png" alt="show months" class="img-arrow-show" />&nbsp;').css('cursor', 'pointer').click(function(){
                jQuery(this).find('+ ul.news-archive-months').toggle();
                jQuery(this).find('img').toggle();
            });
        }
    },
	
	set_up_tune_in : function() {
	   if ((jQuery("#ThisWeekSchedule").size() > 0) && (jQuery("#CompleteSchedule").size() > 0)) {
	       jQuery("#Content").prepend('<div id="ScheduleSelectBar"></div>');
	       jQuery("#ScheduleSelectBar").append(jQuery("#ThisWeekSchedule h3").wrapInner('<span id="ThisWeek" class="schedule-title"></span>').remove().html());
	       jQuery("#ScheduleSelectBar").append(jQuery("#CompleteSchedule h3").wrapInner('<span id="Complete" class="schedule-title"></span>').remove().html());
	       
	       jQuery('.schedule-title').css('cursor', 'pointer').click(function(){
	           id_to_show = jQuery(this).attr('id');
	           id_to_hide = jQuery("#ScheduleSelectBar span[id!='"+id_to_show+"']").attr('id');

	           jQuery("#"+id_to_hide+"Schedule").fadeOut("normal", function(){
	               jQuery("#"+id_to_show+"Schedule").fadeIn("fast");
	           });
	       });
	       
	       jQuery("#CompleteSchedule").hide();
	   }
	},

    discography_ecommerce_links : function() {
        if (jQuery("#AlbumList .ecommerce-links").size() > 0) {
            jQuery("#AlbumList .ecommerce-links").each(function(i){
                jQuery(this).parent().find('div.itbox-text').append('<div id="ELink'+i+'" class="e-link">buy it now</div>');
                var container = jQuery(this).parent().find('div.itbox-text').append('<div id="ELinkBox'+i+'" class="e-link-box"></div>');
                jQuery(this).find('div span a').clone().wrapInner('<div class"e-link-line"></div>').appendTo(jQuery(container).find('.e-link-box'));
                jQuery(this).remove();
            });
            jQuery("#AlbumList .e-link-box").hide();
            jQuery("#AlbumList .e-link").attr("style", "pointer:cursor").click(function(){
                var current_id = jQuery(this).attr('id');
                jQuery("#AlbumList .e-link").each(function(){
                    if (jQuery(this).attr('id') != current_id) {
                        jQuery(this).next().hide();
                        jQuery(this).attr('style', 'border: none;');
                    }
                });
                jQuery(this).next().toggle();
                if (jQuery(this).next().css('display')!='hidden') {
                    jQuery(this).attr('style', 'border: 1px solid #6c2602;');
                }
            });


            hide_elements = function() {
                jQuery(".e-link-box").hide();
            }

            var theList = document.getElementById('AlbumList');
            /* nftr.addEvent(theList, 'mouseenter', enter, false); */
            nftr.addEvent(theList, 'mouseleave', hide_elements, false);

        }
    },
    
    
    init : function() {
        //Fix ie transparency problem
        //jQuery("#Logo").pngFix();
        this.set_logo();
        
        this.home_animation();
        this.init_main_nav();
        this.sifr();
        //this.dynamic_archive_links();
        this.set_up_tune_in();
        this.discography_ecommerce_links();
    }    
};

jQuery(document).ready(function(){
    nftr.init();
});

