
	/*
	JS
	--------------------------------------------------------------------------------------------
	@site			sho.com/site
	@project		970
	@package		movies
	@file			InfoBox.js
	@author			dpaul
	@author			ncavanagh
	@modified		03.25.10	
	@desc			Shows product info in netflix style floating box
	
	/* =:InfoBox
	--------------------------------------------------------------------------------------------*/
	ns('sho.movies');
	sho.movies.InfoBox = function()
	{	 
		var _ui = {};
		var _options;
		var _data;
		var _id;
		var _load;
		var	_title = new Template(''+
			'<h4>#{title}</h4>'+
				'<p class="desc">#{description}</p>'+
				'<strong>Rating:</strong> #{rating}<br/>'+
				'<strong>Genres:</strong> #{genres}<br/>'+
				'<strong>Duration:</strong> #{duration}<br/>'+
				'#{year}'
			);
		var _onDemand = new Template(''+
			'<strong>Available On Demand:</strong><br />#{odStart} until #{odEnd}<br />'
	        );
		var _nextAiring = new Template(''+
			'<p><strong>Next Airing:</strong><br/>'+ 
			    '#{airDay} #{airDate} #{airTime} on #{channelName}</p>'
	        );
		var _queue = [];
		var _anchor;
		var _results = {};
		var _onTonightPadding = 10;
		
		var PRODUCT_URL = '/site/movies/product.do?seriesid=0&seasonid=0&episodeid=';
		
		// trended 'slots' for infobox to appear in
		var _xx = {};
		_xx.FourUp = [ 130,268,406,543 ]; // 4 up, landing page
		_xx.TwoUp = [ 130, 0, 390, 0 ]; // 2 up, landing page (zeroes are no-box links)
		_xx.FiveUp = [ 127, 123 ]; // 5 up, genre thumbnail landing page   (Was 75, 124)
		_xx.OnTonight = [ 255 ]; 
		_xx.OnDemand = [ 180 ]; 
		_xx.BrowseList = [ 42 ]; 
		_xx.Quotes = [ 278 ];
		_xx.FiveUpFeatured = [ 204,390,576,193,378 ];
		
		var _yy = {};
		_yy.FeaturedMovies = [ 36, 375 ];
		_yy.OnTonight = [ -45 ];
		_yy.OnDemand = [ -45 ];
		_yy.BrowseList = [ -42 ];
		_yy.Quotes = [ 43 ];
		_yy.FeaturedGenre = [ 40 ];
		
		var DELAY = 0.5;
		var TWEEN = 0.5;
        var AIRINGS_URL = '/site/schedules/product-airings-json.do';
		
		/* =:Startup
		----------------------------------------------------------------------------------------*/
		function initialize( cfg )
		{	
			if(cfg.classNames) {
				_options = Object.extend({
					duration:0.3,
					classNames:{
						set: '.movie-set',
						container:'.c7.primary'
					}
				},cfg);
			}
			
			if ($('on-tonight-container')) {
				var OnTonightHeight = $('on-tonight-container').getHeight() + _onTonightPadding; 
				_yy.OnDemand[1] = OnTonightHeight + _yy.OnDemand[0];  
			}
			
			if(sho.core.Utils.isIE6() ) { 
				_yy.OnTonight[0] = (_yy.OnTonight[0])+3; 
				_yy.OnDemand[1] = (_yy.OnDemand[1])+9; 
			}

			build();
			setHandlers();
		}
		
		function build()
		{	
			_ui.container = $$( _options.classNames.container )[0];  
			if (!$('movie-info')) {
				_ui.container.insert({ bottom:	['',
				'<div id="movie-info" style="display:none;">',
					'<div id="tail"></div>',
					'<div id="movie-info-inner">',
						'<div id="movie-info-content">',
						'</div>',
						'<div id="next-airing"></div>',
						'<div id="on-demand-info"></div>',
					'</div>',
				'</div>',
				''].join('') });
			}
			
			_ui.box = $('movie-info');
			_ui.content = $('movie-info-content');
			_ui.nextAiring = $('next-airing');
			_ui.onDemand = $('on-demand-info');
		}
		
		function setHandlers()
		{	
			// for each set
			$$(_options.classNames.set).each(function(s)
			{	
				s.select('a').each(function(t,idx){     
					if (!(t.hasClassName('no-box'))) { 
						// for setting box tail
						t._index = idx;     
						t._isTop5 = s.up().className == 'mod' ? true : false;   
						t._isTwoUp = s.hasClassName('two-up') ? true : false;  
						if ($('list-results')) {
							t._isList = t.descendantOf('list-results') ? true : false; 
						}
						if ($('thumbnail-results')) {
							t._isFiveUp = t.descendantOf('thumbnail-results') ? true : false; 
						}
						if ($('sidebar')) {
							t._isSideBar = t.descendantOf('sidebar') ? true : false;
						}
						if ($('featured-genre')) {
							t._isFeaturedGenre = t.descendantOf('featured-genre') ? true : false;
						}
						t.observe('mouseover', over);
						t.observe('mouseout', out); 
					}
				});
			});
		}
		
		/* =:Runtime
		----------------------------------------------------------------------------------------*/
		function over(e)
		{	
			_load = true; 
			while (_queue.length) { // abandon any queued rollout requests
				clearTimeout(_queue[0]); _queue.shift();
			}
			
			//if(!sho.core.Utils.isIE() ) { fadeDown(); }

			_anchor = e.findElement('a'); 
			_anchor.dimensions = _anchor.getDimensions(); 
			
			try {
			_id = _anchor.href.match(/episodeid=(\d{2,10})/)[1];
			
			var titleAjax = new Ajax.Request ( PRODUCT_URL + _id, {
				method:'GET',
				onSuccess: function(transport) { 
						infoLoaded(transport.responseText.evalJSON()); 
					},
					
	   			onFailure: function(){  }
	  		});   
			}
			catch(err) {}
		}
		
		function infoLoaded(t)
		{	
			_results.title = t.title;
			_results.episodeID = t.episodeID;
			_results.rating = t.rating;
			_results.description = t.description;
			_results.duration = t.duration;
			_results.year = t.year;
			_results.genres = '';
			
			t.genres.each( function(genre, idx){  
					_results.genres += t.genres[idx].genre;
					if ((idx+1) < t.genres.length) { _results.genres += ', '; }
				}); 
			
			if (t.genres.length < 1) { _results.genres = 'Unknown'; }
			
			if (t.year == '') { 
				_results.year = ''; 
			}
			else {
				_results.year = '<strong>Year:</strong> ' + t.year + '<br/>';
			}
			
			if (_results.episodeID == _id && (_load)) { 
				if (t.odEnd) { 
					_results.odStart = t.odStartInfoBox;
					_results.odEnd = t.odEndInfoBox;
					_ui.onDemand.update( _onDemand.evaluate(_results));
					_ui.onDemand.show();
				}
				else 
				{
					_ui.onDemand.hide();
				}
				
				if (t.airings.length > 0) {
					_results.airDate = t.airings[0].airDateInfoBox;
					_results.airDay = t.airings[0].airDay;
					_results.airTime = t.airings[0].airTime;
					_results.channelName = t.airings[0].channelName;
					_ui.nextAiring.update(_nextAiring.evaluate(_results));
				}
				
				_ui.content.update( _title.evaluate(_results));			
				_ui.box.className = getTail(); 
				moveBoxTo(getPosition()); 
				fadeUp(); 
			}
		}
		
		function out(e)
		{	 
			var box = _anchor;
			try 
			{
				if (_anchor.descendantOf('thumbnail-results')) {   
						var box = _anchor.getOffsetParent(); 
				}
			}
			catch(err) {}
			
			if(!Position.within(box,Event.pointerX(e),Event.pointerY(e)))
			{  
				_load = false; 
				_queue.push( fadeDown.delay(0.5));
			}
		}
		
		function getTail()
		{
			if (_anchor._isSideBar ) { var t = 'east'; return t;  } 
			if (_anchor._isList ) { var t = 'west'; return t;  } 
			if (_anchor._isFiveUp ) {
				var ancestor = _anchor.getOffsetParent(); 
				var position = ancestor.positionedOffset(); 
				var t = position.left > 450 ? 'east' : 'west';
				return t;
			}
			var tail = 4;
			if (_anchor._isFeaturedGenre) { tail = 3; } 
			var t = _anchor._index < tail ? 'west' : 'east'; 
			return t;
		}
		
		function getPosition()
		{	
			try 
			{
				if (_anchor.descendantOf('featured-movies')) { 
					return {
						x: _anchor._isTwoUp ? _xx.TwoUp[_anchor._index] : _xx.FourUp[_anchor._index],
						y: _yy.FeaturedMovies[0]
					};
				}
				else if (_anchor.descendantOf('on-tonight')) {
					var position = _anchor.positionedOffset();
					var newLeft = _xx.OnTonight[0];
					var newTop = (position.top + (_yy.OnTonight[0]));
					return { 
						x:newLeft,
						y:newTop
					}
				}
				else if (_anchor.descendantOf('on-demand')) {
					var position = _anchor.positionedOffset();
					var newLeft = _xx.OnDemand[0];
					var newTop = (position.top + (_yy.OnDemand[1]));
					return { 
						x:newLeft,
						y:newTop
					}
				}
				
			}
			catch(err) {}
			
			try
			{ 
				if (_anchor.descendantOf('list-results')) {  
					var position = _anchor.positionedOffset();
					var newLeft = _anchor.dimensions.width + position.left + (_xx.BrowseList[0]);
					var newTop = position.top + (_yy.BrowseList[0]);
					if ($('featured-genre')) {   
						var featuredArea = $('featured-genre').getDimensions(); 
						newTop = newTop + featuredArea.height + 30; 
					}
					return { 
						x:newLeft,
						y:newTop
					}
				}
				else if (_anchor.descendantOf('quotes')) {   
					var position = _anchor.positionedOffset();   
					var newLeft = _xx.Quotes[0];
					var newTop = (position.top + (_yy.Quotes[0])); 
					return { 
						x:newLeft,
						y:newTop
					}
				}
			}	
			catch(err) { }
			
			try
			{ 
				if (_anchor.descendantOf('thumbnail-results')) {   
					var ancestor = _anchor.getOffsetParent(); 
					var position = ancestor.positionedOffset();
					var newLeft = (position.left + (_xx.FiveUp[0]));  
					if (position.left > 450) {	newLeft = _xx.FiveUp[1]; }
					var newTop = (position.top + (_yy.FeaturedMovies[0])); 
					if ($('featured-genre')) {   
						var featuredArea = $('featured-genre').getDimensions(); 
						newTop = newTop + featuredArea.height; 
					}
					return { 
						x: newLeft,
						y: newTop
					}
				}
				if (_anchor.descendantOf('featured-genre')) {   
					return {
						x: _xx.FiveUpFeatured[_anchor._index],
						y: _yy.FeaturedGenre[0]
					};
				}
			}	
			catch(err) { }
			
		}
		
		function moveBoxTo(coords)
		{		
			_ui.box.setStyle({
				top:coords.y+'px',
				left:coords.x+'px'
			})
		}
		
		function fadeUp()
		{  
			Effect.Appear( _ui.box, { 
				duration:TWEEN
			});
		}
		
		function fadeDown()
		{
			Effect.Fade( _ui.box, { 
				duration:TWEEN
			});
		}
    	
        /* =:Reveal as Public
		----------------------------------------------------------------------------------------*/   
		return {
			init:initialize
		}
	
	}();
