
	/*
	JS
	--------------------------------------------------------------------------------------------
	@site			sho.com/site
	@project		970
	@package		movies
	@file			MoviesPage.js
	@author			dpaul
	@author			ncavanagh
	@modified		10.02.09	
	@desc			Sets up features glider and tabbed content for /movies.do
	@depend			prototype, scriptaculous, glider
	
	/* =:MoviesPage
	--------------------------------------------------------------------------------------------*/
	ns('sho.movies');
	sho.movies.MoviesPage = Class.create(
	{	 
		
		_atoz:null,
		_airingCnt:null,
		AIRINGS_URL:'/site/schedules/product-airings-json.do',
		CAROUSEL_URL:'/site/movies/tiles/carousel.jsp',
		
		initialize:function()
		{   
			this.initAToZList();
			this.initInfoBox();
			this.initSortUI();
			this.getFeaturedMovies();

			if ($('nav-movies')) {  
				new ProtoFish('nav-movies', '200', 'on', false, true, true); 
			}
			
			if ($('list-results')) { this.initSortUI(); }
		},
		
		initAToZList:function()
		{	
			if (!$('all-movies')) return;
			this._atoz = new sho.movies.AToZList($('all-movies'));
		},
		
		initInfoBox:function()
		{	
			if ($('featured-movies')) {
				sho.movies.InfoBox.init({
					duration:0.3,
					classNames: {
						set: '.movie-set',
						container:'#featured-movies'
					}
				});
			}
			
			if ($('list-results')) {
				sho.movies.InfoBox.init({
					duration:0.3,
					classNames: {
						set: '.movie-list',
						container:'#browse'
					}
				});
			}
			
			if ($('thumbnail-results')) {
				sho.movies.InfoBox.init({
					duration:0.3,
					classNames: {
						set: '.movie-set',
						container:'#browse'
					}
				});
			}
			
			if ($('quotes')) {
				sho.movies.InfoBox.init({
					duration:0.3,
					classNames: {
						set: '.movie-set',
						container:'#quotes'
					}
				});
			}
		},
		
		initSortUI:function()
		{	
			/*
			var _params = window.location.search.toString();
			if (_params.include('sort=')) {
				var _sort = (_params.split("sort=")[1]);
				if (_sort[0] == 'a') {
					$('sort-by-airing').addClassName('selected');
					$('sort-by-title').removeClassName('selected');
					if (_sort[2] == 'd') {
						$('sort-by-airing').addClassName('desc');
					}
				}
				else { 
					$('sort-by-title').addClassName('selected');
					if (_sort[2] == 'd') {
						$('sort-by-title').addClassName('desc');
					}
				}	
			}
			*/
			if ($('thumbnail')) {
				$('thumbnail').observe('click', this.toggleView);
				$('list').observe('click', this.toggleView);
			}
		},
		
		toggleView:function(e) 
		{ 		
				var toggleClicked = e.findElement('a'); 
				if (!toggleClicked.hasClassName('selected')) {
					var toggleLink = toggleClicked.readAttribute('id');
					toggleLink = toggleLink + '-results';
					$('browse').select('.results').each(function(r,idx){ 
						r.hide();
					});
					$(toggleLink).show();
					$('browse').select('.toggle').each(function(t,idx){ 
						t.toggleClassName('selected');
					});
				}
				e.stop();
		},
		
		getFeaturedMovies:function()
		{	
			if (!$('featured-movies')) return;
			var featuredMovies = $('featured-movies');
			
			var modules = $w(featuredMovies.className);
			modules = modules.without('mod'); 

			var idx = Math.floor( Math.random() * modules.length );
			$(modules[idx]).show();  
			featuredMovies.show();
		},
				
		getCarouselMovies:function()
		{	
			if (!$('movies-carousel')) return;
			
			var req = new Ajax.Request( this.CAROUSEL_URL, {
					onSuccess:this.parseCarouselMovies.bind(this),
					onFailure:function(){
						log('a connection error has occurred');
					}
				});
		},
		
		parseCarouselMovies:function(r)
		{
			//var data = r.responseText;
			var xmlDoc = r.responseXML.documentElement;  
			//var products = xmlDoc.getElementsByTagName("product"); 
			
		},
		
		getUnique:function(total, maxValue)
		{
			var found = false;
			var count = 0;
			var rndArray = new Array(total);
			for (var i=0; count<total; count++)
			{
			  found = false;
			  var rndValue = this.getRandom(maxValue)
			  var j=0;
			  for (j=0;j<rndArray.length;j++)
			  {
				if (rndArray[j] == rndValue)
				{
				  found = true;
				  break;
				}
			  }
			  if (found) {
				count--;
			  } 
			  else {
				rndArray[count] = rndValue;
			  }
			}  
			return rndArray;
		 },
		  
		getRandom:function(maxValue)
		{
			var ranNum = Math.round(Math.random()*maxValue);
			return ranNum;
		}
		
	});
	
	
	// Add to onload stack
	document.observe("dom:loaded",  function() {  
		var _moviesPage = new sho.movies.MoviesPage();
	});	