/* ******************************************************************************
  JavaScript Gallery Transition controller
	
 	Copyright (c) 2008 David Turner (davidturner.info)
 	Date: 30/07/2008	 
		 
****************************************************************************** */

document.observe('dom:loaded', function(){	
	var 	swfParams = {"allowfullscreen":"true","wmode":"transparent"};

	// load flash movies
	if($$('div.VideoContainer').size()>0){
		$$('div.VideoContainer').each(function(videoCont){
			
			videoCont.setStyle({'display':'inline-block'});
			
			var vidSrc = 		videoCont.readAttribute('title').match(/\(([^)]+)\)$/)[1]; 
			if(videoCont.readAttribute('title').match(/(^[^(]+) \(/)){
				var vidTitle = 	videoCont.readAttribute('title').match(/(^[^(]+) \(/)[1];
		  }else{
				var vidTitle = 	"No Title";
		  }
			videoCont.writeAttribute('title',vidTitle);
			
			if(vidSrc.match(/vimeo./)){
				//insert vimeo

				vidSrc = vidSrc.replace(/vimeo.([a-z.]{2,6})\/([0-9]+)/,'vimeo.$1/moogaloop.swf?clip_id=$2&amp;color=11677C&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;var=test');
				
				var swfInsert = new swfobject.embedSWF(vidSrc,videoCont.childElements()[1].identify(),"100%","100%","9.0.0","/images_site/media-player/expressInstall.swf",'',swfParams);
				
				videoCont.childElements()[0].remove();
			} else if(vidSrc.match(/youtube.co/)){
				//inser youtube
				youtubeMatch = vidSrc.match(/youtube.([a-z.]{2,6})\/watch\??v=([A-Za-z0-9^\?]+)/);
				vidSrc = 'http://youtube.'+youtubeMatch[1]+'/v/'+youtubeMatch[2];
				var swfInsert = new swfobject.embedSWF(vidSrc,videoCont.childElements()[1].identify(),"100%","100%","9.0.0","/images_site/media-player/expressInstall.swf",'',swfParams);
				videoCont.childElements()[0].remove();
			}else{				
				//insert local
				vidSrc = vidSrc.match(/^[^?]+/)[0]; 
				
				//console.log(vidSrc);
				//alert(vidSrc)
				if($$('#'+videoCont.identify()+' img').size()>0){
					var bgImage = $$('#'+videoCont.identify()+' img')[0].readAttribute('src');
				}else{
					var bgImage = "";
				}//
				//console.log(videoCont.getHeight())
				var swfSRC = "/images_site/media-player/MediaPlayer.swf";
				var flashVars = {'mediaTitle':vidTitle,'mediaURL':vidSrc,'bgImage':bgImage,'fsEnable':'TRUE','colHL':'FF2829'}
				//videoCont.setStyle({'backgroundImage':bgImage});

				var swfInsert = new swfobject.embedSWF(swfSRC,videoCont.childElements()[1].identify(),"100%","100%","9.0.0","/images_site/media-player/expressInstall.swf",flashVars,swfParams);
				videoCont.childElements()[0].remove();
					
			}
			//console.info(videoCont.identify())
			//swfInsert.write(videoCont.identify());
		});
	}
	
	// load audio player
	if($$('div.AudioContainer').size()>0){
		$$('div.AudioContainer').each(function(audioCont){
			var audSrc = 		audioCont.readAttribute('title').match(/\(([^)]+)\)$/)[1]; 
			var audTitle = 	audioCont.readAttribute('title').match(/(^[^(]+) \(/)[1];
			audioCont.writeAttribute('title',audTitle);
			
				audSrc = audSrc.match(/^[^?]+/)[0]; 
				//alert(audSrc)
				
				var flashVars = {'mediaTitle':audTitle,'mediaURL':audSrc,'fsEnable':'FALSE','colHL':'039DE3'}
				var swfInsert = new swfobject.embedSWF(swfSRC,audioCont.identify(),"100%",audioCont.getHeight()-18,"9.0.0","/images_site/media-player/expressInstall.swf",flashVars,swfParams);
				
			var PlayerCont = new Element('div');
			audioCont.insert({'top':PlayerCont});
			swfInsert.write(PlayerCont.identify());
		});
	}
	
	
});
			