/************************************************************************************************************************/
/* Media Gallery    					                                                              	*/
/* 31 october 2007  					   							      											*/
/* Last modification : 16 January 2008 - improve the component to manage several media galleries on the same page 	*/
/* Last modification : 21 February 2008 - improve the component to add an hyperlink on the image item, from the url of the linkgroup item	*/
/* Last modification : 18 March 2008 - replace the div for the title by a span	*/
/* Last modification : 23 May 2008 - add .asx extention */
/************************************************************************************************************************/

// detect browser
var NavigatorName;
var ResizeImageHtml;

if(navigator.userAgent.indexOf("Firefox")!=-1)
{
  versionindex=parseInt(navigator.userAgent.charAt(navigator.userAgent.indexOf("Firefox")+8))+navigator.userAgent.charAt(navigator.userAgent.indexOf("Firefox")+9)+parseInt(navigator.userAgent.charAt(navigator.userAgent.indexOf("Firefox")+10));
  NavigatorName = "Firefox";
}
if(navigator.userAgent.indexOf("MSIE")!=-1)
{
  NavigatorName = "MSIE";
}
if(navigator.userAgent.indexOf("Opera")!=-1)
{
  NavigatorName = "Opera";
}



/* Media Gallery constructor, send an unique identifie */
function MediaGallery(pID) {
	MediaGallery(pID, false);
}

function MediaGallery(pID, pOnFly) {
	this.Id = pID;
	this.MediasList = new Array(); // list of medias
	this.TitleList = new Array(); //list of links
	this.AltList = new Array(); //list of Alternative Text
	this.AbsList = new Array(); //list of abstracts
	this.HyperlinkList = new Array(); //list of hyperlinks
	this.MediaIndex = 0;
	this.resized = false;
	this.width = 230;
	this.height = 189;
	this.Original_width = 230;
	this.Original_height = 189;
	this.isImage = false;
	this.mustRefresh = false;
	this.onFly = pOnFly;
	
	this.AddMedia = mediagallery_AddMedia; // assign function to insert media
	this.Generate = mediagallery_generator;  // assign function to generate the player
	this.Display = mediagallery_show;  // assign function to display the video
	this.Resize = resizeScene; //assign function to resize the video.
	this.Next = mediagallery_next;//assign function to go to the next video.
	this.Previous = mediagallery_previous;//assign function to go to the previous video.

	this.insertRealPlayer = insertRealPlayer;
	this.insertFlashPlayer = insertFlashPlayer;
	this.insertWMVPlayer = insertWMVPlayer;
 	this.insertQuickTimePlayer = insertQuickTimePlayer;
	this.insertImage = insertImage;
	this.insertYoutube = insertYoutube;
	this.insertAV = insertAV;
	this.insertDailymotion = insertDailymotion;
}

// function to add media, source of image, alternative text, link
// Mandatory parameter : Media Source
function mediagallery_AddMedia(pMedia, pTitle, pAlt, pAbstract, pHyperlink) {

	// detect supported format
	var lplayer = mediagallery_DetectPlayer(pMedia);
	
	if(lplayer != '') {
		this.MediasList[this.MediasList.length] = pMedia;
		this.TitleList[this.TitleList.length] = pTitle;
		this.AltList[this.AltList.length] = pAlt;
		this.AbsList[this.AbsList.length] = pAbstract;
		this.HyperlinkList[this.HyperlinkList.length] = pHyperlink;
	}
}
/* function to generate the player */
function mediagallery_generator(pWidth, pHeight, pAllowResize, pLeftImageUrl, pResizeImageUrl, pRightImageUrl) {
	mediagallery_generator(pWidth, pHeight, pAllowResize, pLeftImageUrl, pResizeImageUrl, pRightImageUrl, null);
}

function mediagallery_generator(pWidth, pHeight, pAllowResize, pLeftImageUrl, pResizeImageUrl, pRightImageUrl, pContainerID) {
	
	this.width = pWidth;
	this.height = pHeight;
	this.Original_width = pWidth;
	this.Original_height = pHeight;
	
	if(this.MediasList.length>0) {

	var lHtmlPlayer = '<table id="'+ this.Id +'_container" align="center" cellspacing="0" cellpadding="0" class="mediagallery" border="0" style="position:relative; top:0px; left: 0px;"><tr><td align="center"><table border="0"><tr><td><div class="mediagallery_media" id="'+ this.Id +'_media" style="float:left;_margin-right:-3px;" width="'+ this.width + '" height="'+ this.height +'"></div>';
	
	if(pAllowResize == 'Y' && NavigatorName != 'Opera'){
		lHtmlPlayer += '</td><td valign="top" align="left"><a href="javascript:void(0)" class="mediagallery_Resize" id="'+ this.Id +'_Resize" onclick="'+this.Id+'.Resize();return false;" style="float:left;"><img id="'+ this.Id +'_imgresize" src="'+pResizeImageUrl+'" border="0" align="middle"/></a>';
	}
	lHtmlPlayer +='</td></tr></table></td></tr>';

	//if there is only one media and no title -> generate mini player without div 'content'
	if(this.MediasList.length > 1 || this.TitleList[0] != '' ) {
		lHtmlPlayer += '<tr><td><div class="mediagallery_content" id="'+ this.Id +'_content"><span class="mediagallery_Title" id="'+ this.Id +'_Title"></span><div class="mediagallery_Abstract" id="'+ this.Id +'_Abstract"></div></div></td></tr>';
	}

	//if there is only one media and no resize -> generate mini player without div 'browser'
	if(this.MediasList.length > 1){

		lHtmlPlayer += '<tr><td><div class="mediagallery_browser" id="'+ this.Id +'_browser">';

		if(this.MediasList.length > 1){
			lHtmlPlayer += '<a href="javascript:void(0)" class="mediagallery_PreviousLink" id="'+ this.Id +'_PreviousLink" onclick="'+this.Id+'.Previous();return false;"><img src="'+pLeftImageUrl+'" border="0" align="middle"></a><input type="text" class="mediagallery_Counter" id="'+ this.Id +'_Counter" size="4" readonly=""/><a href="javascript:void(0)" class="mediagallery_NextLink" onclick="'+this.Id+'.Next();return false;"><img src="'+pRightImageUrl+'" border="0" align="middle"></a>';
		}
		lHtmlPlayer += '</div></td></tr>';
	}

	lHtmlPlayer += '</table>';
	
	if(pContainerID) {
	  var lContainer=document.getElementById(pContainerID);
	  if(lContainer){
            lContainer.innerHTML = lHtmlPlayer;
	  }
	}
	else{
	 document.write(lHtmlPlayer);
	}
		
	this.Display(0);
	}
}

/* function to detect the player from the extension of the url */
function mediagallery_DetectPlayer(pUrl) {
	
	var lFileExtension;
	
	//get file extension
	if(pUrl.lastIndexOf('.')>0) {	
	 lFileExtension = pUrl.substring(pUrl.lastIndexOf('.'));
	}
	//remove http parameters
	if(lFileExtension.lastIndexOf('?')>0) {	
	 lFileExtension = lFileExtension.substring(0, lFileExtension.length - lFileExtension.lastIndexOf('?'));
	}
	//alert(lFileExtension);
	var lFormat;
	
	switch(lFileExtension)
	{
	case '.rm':
	  lFormat = 'RealPlayer';
	  break;
	case '.flv':
	case '.mp4':
	  lFormat = 'FlashVideo';
	  break;
 	case '.wmv': 
	  lFormat = 'WindowsMedia';
	  break;
	case '.asx': 
	  lFormat = 'WindowsMedia';
	  break;
 	case '.avi': 
	  lFormat = 'WindowsMedia';
	  break;
	case '.mpg':
	  lFormat = 'WindowsMedia';
	  break;
 	case '.mov':
	  lFormat = 'QuickTime';
	  break;
	case '.jpg':
	  lFormat = 'Image';
	  break;
	case '.bmp':
	  lFormat = 'Image';
	  break;
  	case '.gif':
	 lFormat = 'Image';
	 break; 
  	case '.png':
	 lFormat = 'Image';
	 break;
  	case '.jpeg':
	 lFormat = 'Image';
	 break;
  	case '.jpe': // image extension of AVS
	 lFormat = 'Image';
	 break;
	 case '.cfm': // image extension of AVS
	 lFormat = 'AV';
	 break;
  	case '.smq': // image extension of AVS
	 lFormat = 'Image';
	 break;
	 
	default:
		// if no extension, detect youtube url
		if( pUrl.lastIndexOf('youtube.')>0) {
			lFormat = 'YouTube';break;
		}
		if( pUrl.lastIndexOf('.cfm?idFile')>0) {
			lFormat = 'AV';break;
		}
		// or dailymotion url
		if( pUrl.lastIndexOf('dailymotion.')>0) {
			lFormat = 'DailyMotion';break;
		}
	}
	
	return lFormat;
}
function mediagallery_show(pIndex) {

	if(document.getElementById(this.Id+'_media'))
	{
		//previous loop
		if(pIndex < 0)pIndex=this.MediasList.length - 1;
		
		//next loop
		if(pIndex > this.MediasList.length - 1)pIndex=0;
		
		var url = this.MediasList[pIndex];
		
		var lPlayer = mediagallery_DetectPlayer(url);
		
		//insert media for the file extension
		switch(lPlayer)
		{
		case 'RealPlayer':
		  this.insertRealPlayer(url);
		  break; 
		case 'FlashVideo':
		  this.insertFlashPlayer(url);
		  break;
		case 'WindowsMedia':
		  this.insertWMVPlayer(url);
		  break;
		case 'QuickTime':
		  this.insertQuickTimePlayer(url);
		  break;
		case 'Image':
		  this.insertImage(url, this.AltList[pIndex], this.HyperlinkList[pIndex]);
		  break;
		case 'YouTube':
		  this.insertYoutube(url);
		  break;  
		case 'AV':
		  this.insertAV(url);
		  break; 
		case 'DailyMotion':
		  this.insertDailymotion(url);
		  break;
		}
		//insert title
		if(document.getElementById(this.Id+'_Title')) {
			document.getElementById(this.Id+'_Title').innerHTML = this.TitleList[pIndex];
		}
		//insert abstract
		if(document.getElementById(this.Id+'_Abstract')) {
			document.getElementById(this.Id+'_Abstract').innerHTML = this.AbsList[pIndex];
		}
		if(this.MediasList.length > 1){
			//set the counter
			var counter = (1*pIndex) + 1;
			if(document.getElementById(this.Id+'_Counter')) {
				document.getElementById(this.Id+'_Counter').value = counter + '/' + this.MediasList.length;
			}
		}	
		//save current Index
		this.MediaIndex=pIndex;
	}
}

// goto the next media
function mediagallery_next() {
	if(this.MediaIndex < this.MediasList.length - 1){
		this.Display(this.MediaIndex + 1);
	}
else {
	this.Display(0);} 
}

//goto the previous media
function mediagallery_previous() {
	if(this.MediaIndex > 0 ){
		this.Display(this.MediaIndex - 1);}
else {
	this.Display(this.MediasList.length - 1);} 
}

// insert image
function insertImage(pUrl, pAlt, pHyperlink) {
	if(pHyperlink) {
		document.getElementById(this.Id+'_media').innerHTML = '<a title="'+pAlt+'" href="'+pHyperlink+'" ><img src="'+pUrl+'" alt="'+pAlt+'" border="0" /></a>';
	}else{
		document.getElementById(this.Id+'_media').innerHTML = '<img src="'+pUrl+'" alt="'+pAlt+'" border="0" />';
	}
	this.isImage = true;
}

// insert youtube video
function insertYoutube(pUrl) {

	var lUrlTube;
	
	// playlist url
	if(pUrl.lastIndexOf('p=')>0){
		var lYoutubeId = pUrl;
		lYoutubeId = lYoutubeId.substring(lYoutubeId.lastIndexOf('p=')+2);
		// build youtube playlist video
		lUrlTube = 'http://www.youtube.com/p/'+lYoutubeId;
	}else{
		var lYoutubeId = pUrl;

		  // extract video ID
		if(lYoutubeId.lastIndexOf('=')>0) {
		  lYoutubeId = lYoutubeId.substring(lYoutubeId.lastIndexOf('=')+1);
		  // build youtube flash video
		  lUrlTube = 'http://www.youtube.com/v/'+lYoutubeId+'&rel=1';
		}
	}
	var playerhtml = '<embed class="mediagalleryVideo_emb" id="'+this.Id+'_videoemb" width="'+ this.width +'" wmode="transparent" height="'+ this.height+'" src="'+lUrlTube+'" type="application/x-shockwave-flash" />';
	
	document.getElementById(this.Id+'_media').innerHTML = playerhtml ;
	this.isImage = false;
	this.mustRefresh = false;
}

// insert dailymotion video
function insertDailymotion(pUrl) {

	var lUrlDaily;
	
	// playlist url
	if(pUrl.lastIndexOf('/video/')>0){
		// build dailymotion video address
		lUrlDaily = pUrl.replace('/video/', '/swf/');
	}
	else
	{	lUrlDaily = pUrl;	}
		
	var playerhtml = '<embed class="mediagalleryVideo_emb" id="'+this.Id+'_videoemb" width="'+ this.width +'" wmode="transparent" height="'+ this.height+'" src="'+lUrlDaily+'" type="application/x-shockwave-flash" />';
	
	document.getElementById(this.Id+'_media').innerHTML = playerhtml ;
	this.isImage = false;
	this.mustRefresh = false;
}

// insert RealPlayer video
function insertRealPlayer(pUrl) {

	var playerhtml = '<embed autoPlay="false" id="'+this.Id+'_videoemb" pluginspage="http://www.real.com/freeplayer/?rppr=rnwk" pluginurl="http://www.real.com/freeplayer/?rppr=rnwk" controls="imagewindow,ControlPanel" class="mediagalleryVideo_emb" width="'+ this.width +'" height="'+ this.height+'" src="'+pUrl+'" type="audio/x-pn-realaudio-plugin" />';
	document.getElementById(this.Id+'_media').innerHTML = playerhtml ;
	this.isImage = false;
	this.mustRefresh = false;
}

// insert QuickTime video
function insertQuickTimePlayer(pUrl) {

	var playerhtml = '<embed scale="tofit" id="'+this.Id+'_videoemb" PLUGINSPAGE="http://www.apple.com/quicktime/download/" autoPlay="false" controller="true" class="mediagalleryVideo_emb" width="'+ this.width +'" height="'+ this.height+'" src="'+pUrl+'"/>';
	document.getElementById(this.Id+'_media').innerHTML = playerhtml ;
	this.isImage = false;
	this.mustRefresh = false;
}

// insert Windows Media video
function insertWMVPlayer(pUrl) {
	var playerhtml = '<embed autoStart="0" class="mediagalleryVideo_emb" id="'+this.Id+'_videoemb" width="'+ this.width +'" height="'+ this.height+'" src="'+pUrl+'" type="application/x-mplayer2"/>';
	document.getElementById(this.Id+'_media').innerHTML = playerhtml ;
	this.isImage = false;
	this.mustRefresh = true;
}

// insert Flash video
function insertFlashPlayer(pUrl) {

	var flash_vars = "video1="+pUrl+"&&param_connexion=14&&totalVideo=1&&className="+this.Id;
	
	/*var playerhtml = '<embed play="false" id="'+this.Id+'_videoemb" wmode="transparent" class="mediagalleryVideo_emb" width="'+ this.width +'" height="'+ this.height+'" flashvars="'+flash_vars+'" menu="false" quality="high" bgcolor="none" style="" src="/wel/swf/representations/player_big_blue.swf" type="application/x-shockwave-flash"/>';*/
	var playerhtml = '<embed play="false" id="'+this.Id+'_videoemb" wmode="transparent" class="mediagalleryVideo_emb" width="'+ this.width +'" height="'+ this.height+'" flashvars="'+flash_vars+'" menu="false" quality="high" bgcolor="none" style="" src="/wel/swf/representations/player_for_avs_mp4.swf" type="application/x-shockwave-flash"/>';
	
	document.getElementById(this.Id+'_media').innerHTML = playerhtml;
	this.isImage = false;
	this.mustRefresh = false;
}

// insert Flash video
function insertAV(pUrl) {

	var flash_vars = "config="+pUrl+"&&param_connexion=14&&totalVideo=1&&className="+this.Id;
	
	var playerhtml = '<embed play="false" id="'+this.Id+'_videoemb" wmode="transparent" class="mediagalleryVideo_emb" width="'+ this.width +'" height="'+ this.height+'" flashvars="'+flash_vars+'" menu="false" quality="high" bgcolor="none" style="" src="http://ec.europa.eu/avservices/player/jwplayer/player44198.swf" type="application/x-shockwave-flash"/>';
	
	document.getElementById(this.Id+'_media').innerHTML = playerhtml;
	this.isImage = false;
	this.mustRefresh = false;
}

// resize the player
function resizeScene() {

	// get main table of the player
	var lObjectGallery = document.getElementById(this.Id+'_container');
	
	if(lObjectGallery) { 
		if (this.resized == false){
			this.resized = true;
			if(document.getElementById(this.Id+"_imgresize"))
			{
			  document.getElementById(this.Id+"_imgresize").src = document.getElementById(this.Id+"_imgresize").src.replace("resize","resize_smaller");
			}
			// double resize
			this.width = this.Original_width *2;
			this.height = this.Original_height *2;
			
			if(this.onFly)
			{
				// get the left and top position of the player
				var curleft = 0;
				var curtop = 0;
				var obj = lObjectGallery;
				if (obj.offsetParent)
				{
					while (obj.offsetParent)
					{
						curleft += obj.offsetLeft
						obj = obj.offsetParent;
					}
					obj = lObjectGallery;
					while (obj.offsetParent)
					{
						curtop += obj.offsetTop
						obj = obj.offsetParent;
					}
				}
				
				if( (curleft + this.width) > screen.width ){
					// set the absolute position, open to left direction
					lObjectGallery.style.left = (curleft-this.Original_width) +'px';
				}
				else{
					// set the absolute position, open to right direction
					lObjectGallery.style.left = curleft +'px';
				}
				// set the absolute position
				lObjectGallery.style.top = curtop +'px';
				
				lObjectGallery.style.position = 'absolute';	
				
				lObjectGallery.style.zIndex = '99';
			}

		}
		else if (this.resized == true){
			this.resized = false;

			if(document.getElementById(this.Id+"_imgresize"))
			{
			  document.getElementById(this.Id+"_imgresize").src = document.getElementById(this.Id+"_imgresize").src.replace("resize_smaller","resize"); "/wel/images/mediagallery_resize.gif";
			}
			
			//restore the original size
			this.width = this.Original_width;
			this.height = this.Original_height;
			lObjectGallery.style.width = parseInt(this.Original_width)+19; 
			lObjectGallery.style.height = this.Original_height;

			//restore the relative position
			lObjectGallery.style.zIndex = '0';
			lObjectGallery.style.position = 'relative';
			lObjectGallery.style.top ='0px';
			lObjectGallery.style.left = '0px';
			
		}
		
		// change the media size only if the media is not an image.
		if(this.isImage == false){
			if(this.mustRefresh == true && NavigatorName == "Firefox")
			{
				this.Display(this.MediaIndex);
			}
			else
			{
				document.getElementById(this.Id+"_videoemb").width = this.width; 
				document.getElementById(this.Id+"_videoemb").height = this.height
				lObjectGallery.style.width = parseInt(this.width)+19; 
				lObjectGallery.style.height = this.height;
			}

		}
	}
}