
	/*
	JS
	--------------------------------------------------------------------------------------------
	@site			sho.com/site
	@project		970
	@package		movies
	@file			AToZList.js
	@author			dpaul
	@author			ncavanagh
	@modified		10.07.09	
	@desc			Sets up features glider and tabbed content for /movies.do
	@depend			prototype, scriptaculous, sho.core.Utils
	
	/* =:AToZList
	--------------------------------------------------------------------------------------------*/
	ns('sho.movies');
	sho.movies.AToZList = Class.create(
	{	
		initLetter:'A',
		activeLetter:null,
		ui:null,
		busy:false,
		results:null,
		
		FEED_URL:"/site/schedules/atozxml.do",
		PRODUCT_URL:"/site/schedules/product.do?seriesid=0&seasonid=0&episodeid=",
		
		/* =:Startup
		---------------------------------------------------------------------------------------*/
		initialize:function(c )
		{   
			this.ui = {
				container : c,
				picker : $('movie-picker'),
				results : $('movie-results-inner')
			}
			this.ui.picker.observe('click', this.click.bindAsEventListener(this));
			this.load(this.initLetter);
		},
		
		load:function(letter)
		{
			if(this.busy) return;
			
			this.activeLetter = letter; 
			this.busy = true; 
			this.update();
			var a = new Ajax.Request( this.FEED_URL + '?letter=' + letter.toUpperCase(),
			{
				method:'GET',
				onSuccess: this.parseList.bind(this),
				onFailure:function(){
					// log('Sorry, a connection error has occured');
				}
			});
		},
		
		parseList:function( ajx )
		{
			/*<movies>
				<movie>
					<title>a/k/a Tommy Chong</title>
					<episodeid>133777</episodeid>
				</movie>
				<movie>
					<title>Ace of Hearts</title>
					<episodeid>135044</episodeid>
				</movie>
			</movies>*/
			
			this.results = [];
			var doc = ajx.responseXML; 
			var movies = doc.getElementsByTagName('movie'); 
			for(var i=0; i<movies.length; i++)
			{
				this.results.push({
					title: this.__value(movies[i].childNodes[0].firstChild),
					id: this.__value(movies[i].childNodes[1].firstChild)
				});
			}
			
			this.busy = false; 
			this.update();
		},
		
		__value:function(node)
		{
			return sho.core.Utils.isIE() ? node.nodeValue : node.textContent;
		},
		
		/*
		=:Runtime
		---------------------------------------------------------------------------------------*/		
		update:function()
		{
			if(this.busy) this.ui.container.addClassName('loading');
			else this.ui.container.removeClassName('loading');
			
			this.updatePicker();
			if(!this.busy) this.updateResults();
		},
		
		updateResults:function()
		{
			var p = this.PRODUCT_URL;
			var out = '';
			if(this.results.length == 0)
			{
				out = '<p>No titles currently airing.</p>';
			}
			else
			{
				out = '<ul>';
				this.results.each(function(m){
					out += (['',
						'<li>',
							'<a href="',p, m.id, '">',
								m.title,
							'</a>',
						'</li>',
					'']).join('');
				});
				out += '</ul>'
			}
			this.ui.results.update(out);
		},
		
		updatePicker:function()
		{
			var aktiv = this.activeLetter;
			this.ui.picker.select('a').each( function(a){
				a.className = a.innerHTML == aktiv ? 'on' : '';
			});
		},
		
		click:function(e)
		{
			var el =  Event.findElement(e,'a'); e.stop();
			this.load(el.innerHTML);
		}
	});
	
	
	
