
	/*
	JS
	--------------------------------------------------------------------------------------------  
	@site			sho.com/site
	@project		sho (970)
	@package		schedules
	@file			HDModule.js
	@author			ncavanagh
	@author			dpaul
	@modified		08.17.09
	@desc			Global promotional module which shows a dropdown of providers.
	@note			recoded to allow for more better tracking/grouping of providers into categories
	@depend			prototype, HDModuleProviderData.js, OmniHelp
	
	/* =:HDModule
	--------------------------------------------------------------------------------------------*/  
	namespace('sho.schedules');
	
	sho.schedules.HDModule = Class.create(
	{
		ui:{},
		data:{},
		useHDLinks:'',
		
		PROVIDERS_URL:'/site/sho/js/com/sho/schedules/HDModuleProviderData.js',
		SCHEDULEPAGE_WRAPPER:'hd-mod',
		ORDERPAGE_WRAPPER:'order-providers',
		EXIT_LINK_PREFIX:'Affiliate_Exit',
		DROPDOWN_LABEL:'-- Choose Provider --',

		/* =:Startup
		---------------------------------------------------------------------------------------*/ 
		initialize:function()
		{
			if($(this.SCHEDULEPAGE_WRAPPER )){ this.useHDLinks = true; }
			if($(this.ORDERPAGE_WRAPPER )){ this.useHDLinks = false; }
			if(!$(this.SCHEDULEPAGE_WRAPPER) && !$(this.ORDERPAGE_WRAPPER )) return;
			
			this.drawContainer();
			this.setHandlers();
			this.loadProviders();
		},
		
		drawContainer:function()
		{
			this.ui.container = $('all-providers-dd');
			this.ui.container.update(['',
			'<form name="HDModuleProviders">',
				'<select>',
				'</select>',
			''].join(''));
			
			this.ui.dropdown = this.ui.container.select('select')[0]; 	
		},
		
		setHandlers:function()
		{
			this.ui.dropdown.observe('change', 
				this.selectProvider.bindAsEventListener(this));
		},


		/* =:Load
		---------------------------------------------------------------------------------------*/ 
		loadProviders:function()
		{
			new Ajax.Request( this.PROVIDERS_URL, {
				onSuccess:this.onload.bind(this),
				onFailure:this.loadFailure.bind(this)
			})
		},
		
		onload:function(ajx)
		{
			this.data = ajx.responseText.evalJSON();		
			this.drawProviderMenu();
			this.ui.container.removeClassName('loading');
		},
		
		loadFailure:function()
		{
			// Debugger.t('a connection error has ocurred');
		},
		
		drawProviderMenu:function()
		{
			var dd = this.data.collect(function(p,idx){
				return '<option value="'+idx+'">' + p.name +'</option>'
			});
			
			// focus dummy option
			var fix = (function(){ this.ui.dropdown.options.selectedIndex = 0; }).bind(this);
			fix.delay(0.25);
			
			this.ui.dropdown.update('<option value="0" selected="selected">'+this.DROPDOWN_LABEL+'</option>');
			this.ui.dropdown.insert({ bottom:dd.join('')});
		},
		
		/* =:Runtime
		---------------------------------------------------------------------------------------*/ 
		selectProvider:function(e)
		{
			if(Event.findElement(e, 'select'))
			{
				var idx = Event.findElement(e, 'select').selectedIndex; Event.stop(e);
				if(idx !== 0 ){ this.exit(idx); }
			}
			else
			{
				log('error: bad select option');
			}
		},
		
		exit:function(i)
		{
			if(i > 0)
			{
				i--;
				var p = this.data[i];
				var url = this.useHDLinks ? p.url_hd : p.url;
				var cat = p.category;
				var name = p.name;
				
				// perform analytics
				if( typeof OmniHelp !== "undefined" )
				{
					OmniHelp.tc([this.EXIT_LINK_PREFIX,cat,name].join(':'));
				}
				
				if( typeof pageTracker !== "undefined" )
				{
					var slug = [this.EXIT_LINK_PREFIX,cat,name].join('/');
					slug += this.useHDLinks ? '_HD' : '';
					pageTracker._trackPageview(slug);
				}
				window.location = url;
			}
			
		}
	
	});
	
	/* =:Add to Onload Stack
	--------------------------------------------------------------------------------------------*/ 
	document.observe("dom:loaded", function() { 
		new sho.schedules.HDModule();
	});
	
	