
	/*
	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, SHO.Overlay
	
	/* =:SimpleVideo
	--------------------------------------------------------------------------------------------*/
	if( SHO == undefined ) var SHO = {};
	SHO.SimpleVideo = function()
	{	
		var _playerid = 1873832296;
		var _videowidth = 640 + 6; 	// footprint+chrome
		var _videoheight = 360 + 52;	// 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>',
		'</div>'
		].join("\n\t"));
		
		var _icon = new Template (['',
			'<div class="videoIcon" style="background: transparent no-repeat url(#{src}) #{left};">',
				'<img class="icon" src="/site/beta/image-panel/global/clear.png" width="#{width}" height="#{height}" />',
			'</div>',
		''].join("\n\t"));
		
		var _listeners = [];
		
		/* =:Startup
		  ---------------------------------------------------------------------------------------*/
		function init()
		{
			if( typeof SHO.Overlay == "undefined" ){ return; }
		    SHO.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 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(1).split(':');
			var mode = ( props[0] == 'player' || props[0] == 'lineup' || props[0] == 'episode' ) ? props[0] : false;
			var id = Number( props[1] ) ;
			if( !mode || isNaN( id )) return;
			
			Event.observe(a, 'click', function(e){ Event.stop(e); play( mode, id ); });	
		}
		
		/* =:Runtime 
		  ---------------------------------------------------------------------------------------*/
		function play (mode, id)
		{
			SHO.Overlay.update( _embed.evaluate({ 
				bgcolor:'#000000',
				width:_videowidth,
				height:_videoheight,
				video:id,
				player:_playerid
			}));
			SHO.Overlay.resize(_videowidth, _videoheight);
			SHO.Overlay.open({
				onOpen:( function(){ callSleep(); showVideo(); }),
				onClose:( function(){ callWake(); })
			});
		}
		
		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() { 
		SHO.SimpleVideo.init();
	});	
