
	/*
	JS
	--------------------------------------------------------------------------------------------
	@site			sho.com (v5)
	@file			SimpleVideo.js
	@author			dpaul
	@modified		12.05.08
	@desc			Brightcove wrapper for global video calls
	@note			First recode with Overlay manager
	@depend			prototype, scriptaculous, SHO.Utils, Series.Overlay
	
	/* =:SimpleVideo
	--------------------------------------------------------------------------------------------*/
	if( Series == undefined ) var Series = {};
	Series.SimpleVideo = function()
	{	
		var _playerid = 26396052001; // v1 1873832296;
		var _videowidth = 640; 	// footprint+chrome
		var _videoheight = 496;	// footprint+chrome
		var _embed = new Template([ '',
		'<div id="video-wrap" style="display:none;">',
			'<embed',
				'src="http://c.brightcove.com/services/viewer/federated_f9/#{player}?isVid=1&publisherID=63128"',
				'bgcolor="#000000"',
				'flashVars="playerID=#{player}&domain=embed&@videoPlayer=#{video}"',
				'base="http://admin.brightcove.com"',
				'name="flashObj"',
				'width="#{width}"',
				'height="#{height}"',
				'seamlesstabbing="false"',
				'type="application/x-shockwave-flash"',
				'allowFullScreen="true"',
				'swLiveConnect="true"',
				'pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">',
			'</embed>',
			'<!--a href="http://www.sho.com/site/video/brightcove/series/title.do?bcpid=14033849001">Watch more Californication videos &raquo;</a-->',
		'</div>'
		].join("\n\t"));

		var _icon = new Template (
			'<img class="icon" src="/site/locknload/season1/image-panel/ui/video-icon.jpg" />');
		
		var _listeners = [];
		
		/* =:Startup
		  ---------------------------------------------------------------------------------------*/
		function init()
		{	
			if( typeof Series.Overlay == "undefined" ){ return; }
		    Series.Overlay.setPurge(true); // so flash audio doesn't persist after close
			findVideoLinks();
		}
		
		function findVideoLinks()
		{  
			$$('a[rel=brightcove]').each( function(a){  
				setClickHandler(a); 
				//addIcon(a);
			});
		}
		
		function addIcon(a)
		{	
			if( !a.select('img')[0] ) { return; }
			
			//var image = a.select('img')[0].hide();
			var image = a.select('img')[0];
			var icon = { src: image.readAttribute('src') };  
			//icon.width = image.getWidth();
			//icon.height = image.getHeight();
			icon.left = image.getStyle('margin-left');
			icon.top = image.getStyle('margin-top');
			a.insert(_icon.evaluate(icon));
		}
		
		function setClickHandler(a)
		{  
			var props = a.readAttribute('href').substr(0).split('#');  
			props.reverse();
			var nonsense = props[0].substr(0).split(':'); 
			var id = nonsense[1];
			var mode = 'player'; 
			if( !mode || isNaN( id )) return;
			
			Event.observe(a, 'click', function(e){ Event.stop(e); play( mode, id ); });	
		}
		
		/* =:Runtime 
		  ---------------------------------------------------------------------------------------*/
		function play (mode, id)
		{   
			Series.Overlay.update( _embed.evaluate({ 
				bgcolor:'#000000',
				width:_videowidth,
				height:_videoheight,
				video:id,
				player:_playerid
			}));
			Series.Overlay.resize(_videowidth, _videoheight);
			Series.Overlay.open({
				onOpen:( function(){ showVideo(); }),
				onClose:( function(){ })
			});
		}
		
		function showVideo()
		{   
			$('video-wrap').setStyle({display:'block'});	
		}
		
		function addListener( movie )
		{  
			if( _listeners.join(' ').indexOf( movie ) !== -1 ) return;
			_listeners.push( movie );
		}
		
		function callSleep()
		{    
			_listeners.each(function(f){ getFlash(f).sleep() });
		}
		
		function callWake()
		{
			_listeners.each(function(f) { getFlash(f).wake(); })
		}
		
		function getFlash( name ){
			return SHO.Utils.isIE() ? window[ name ] : document[ name ];
		}
		
		/* =:Reveal as Public
		  ---------------------------------------------------------------------------------------*/
		return {
			init:init,
			play:play,
			addListener:addListener,
			getFlash:getFlash
		}
	
	}();
	
	document.observe("dom:loaded", function() { 
		Series.SimpleVideo.init();
	});	