/*
	Available properties
		previwerType
		fileUrl
		mediaType
		onDoneCallBack
		onStartCallBack
*/
var LIB_mediapreview_playList = Array();
var LIB_mediapreview_playingClipNumber = 1;


/*
 * play multiple items
 *
 *@param array playList array with playlist properties
 *
*/
function LIB_mediapreview_showMultiple(playList) {
	LIB_mediapreview_playingClipNumber = 1;
	LIB_mediapreview_playList = playList;

	var url2 = '';

	if (LIB_mediapreview_playList[0]['imageUrl']) {
		url2 = LIB_mediapreview_playList[0]['imageUrl'];
	}

	LIB_mediapreview_showPreview(LIB_mediapreview_playList[0]['previwerType'], LIB_mediapreview_playList[0]['fileUrl'], LIB_mediapreview_playList[0]['mediaType'],url2);
}

/*
 * this function is called on clip done
 *
*/
function LIB_mediapreview_onClipDone() {
	// sometimes there are timing issues and onDone is called two times
	if (LIB_mediapreview_playingClipNumber == 0) {
		return;
	}

	numOfVideos = LIB_mediapreview_playList.length;
	doneItemArrIndex = LIB_mediapreview_playingClipNumber - 1;
	nextItemArrIndex = LIB_mediapreview_playingClipNumber;

	if (LIB_mediapreview_playList[doneItemArrIndex]['onDoneCallBack']) {
		eval(LIB_mediapreview_playList[doneItemArrIndex]['onDoneCallBack']);
	}

	if (numOfVideos > LIB_mediapreview_playingClipNumber) {
		LIB_mediapreview_playingClipNumber++;

		var url2 = '';

		if (LIB_mediapreview_playList[nextItemArrIndex]['imageUrl']) {

			url2 = LIB_mediapreview_playList[nextItemArrIndex]['imageUrl'];
		}

		LIB_mediapreview_showPreview(LIB_mediapreview_playList[nextItemArrIndex]['previwerType'], LIB_mediapreview_playList[nextItemArrIndex]['fileUrl'], LIB_mediapreview_playList[nextItemArrIndex]['mediaType'],url2);
	}
	else {
		// reset the global counter
		LIB_mediapreview_playingClipNumber = 0;

		// check if we have onDoneAll callback
		if (LIB_PreviewMedia_onDoneAllCallback) {
			eval(LIB_PreviewMedia_onDoneAllCallback);
		}
	}
}


function LIB_mediapreview_showPreview(previewType, url, mediaType, url2, protectImage, mediaParams) {

  if ( document.getElementById('lightbox1')) {
    fromemms = 1;
  } else { fromemms=0;}

	if (!mediaParams) {
		var mediaParams = {};
		mediaParams.width = null;
		mediaParams.height = null;
	}

	if (!url2) {
		url2 = '';
	}

	if (!protectImage) {
		protectImage = false;
	}

	if (!mediaType) {
		mediaType = 'Video'
	}
	/*
		previewType
			1 -> high bit rate media files
			2 -> normal bit rate media files
			3 -> flash files
			4 -> image
			5 -> swf
			6 -> windows media server publishing point
			7 -> mp3 file
	*/

	// hide windows media player
	document.getElementById('LIB_MediaPreview_mediaContainerDiv').style.display = 'none';

	// stoping the media player
	if (document.getElementById('LIB_MediaPreview_mediaObject')) {
		LIB_mediapreview_stopWindowsMediaPlayer();
		document.getElementById('LIB_MediaPreview_mediaObject').style.display = 'none';
	}
	// end hiding windows media player

	// hide the image object
	document.getElementById('LIB_MediaPreview_imageObject').style.display = 'none';
	// hide the empty object
	document.getElementById('LIB_MediaPreview_emptyObject').style.display = 'none';
	// hide the swf container
	document.getElementById('LIB_MediaPreview_swfDiv').style.display = 'none';

	// hide flash player
	if (previewType != 3) {
		document.getElementById('LIB_MediaPreview_flashContainerDiv').style.display = 'none';

		// stop the player
		LIB_mediapreview_stopFlashPlayer();
	}
	// end hiding the flash player

	/*
		flow player

		document.getElementById('LIB_MediaPreview_flvPlayerIframe').style.display = 'none';
		// reset the player
		if (typeof document.getElementById('LIB_MediaPreview_flvPlayerIframe').contentWindow.stopMedia == 'function' && previewType != 3)  {
			document.getElementById('LIB_MediaPreview_flvPlayerIframe').contentWindow.stopMedia();
		}
	*/

	// high bit rate or low bit rate
	if (previewType == 1 || previewType == 2 || previewType == 6) {
		document.getElementById('LIB_MediaPreview_mediaContainerDiv').style.display = '';

		// LIB_MediaPreview_mediaObjectObj = document.getElementById('LIB_MediaPreview_mediaObject');

		// LIB_MediaPreview_mediaObjectObj.style.display = '';
		// LIB_MediaPreview_mediaObjectObj.AutoStart = true;
		// LIB_MediaPreview_mediaObjectObj.Filename = url;
		// LIB_MediaPreview_mediaObjectObj.style.height = LIB_PreviewMedia_conteinerHeight + 45;
		// LIB_MediaPreview_mediaObjectObj.ShowControls = true;
		// LIB_MediaPreview_mediaObjectObj.ShowStatusBar = true;

		windowsMediaPlayerHTML = '<object name="LIB_MediaPreview_mediaObject" id="LIB_MediaPreview_mediaObject" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" width="'+LIB_PreviewMedia_conteinerWidth+'" height="'+LIB_PreviewMedia_conteinerHeight+'">';
		windowsMediaPlayerHTML += '	<param name="Filename" value="' + url + '">';
		windowsMediaPlayerHTML += '	<param name="ShowStatusBar" value="true">';
		windowsMediaPlayerHTML += '	<param name="AutoSize" value="1">';
		windowsMediaPlayerHTML += '	<param name="DisplaySize" value="2">';
		windowsMediaPlayerHTML += '	<embed id="LIB_MediaPreview_mediaObject" AutoSize="1" DisplaySize="2" width="' + LIB_PreviewMedia_conteinerWidth + '" height="' + LIB_PreviewMedia_conteinerHeight + '" src="' + url + '" autoplay="true">';
		windowsMediaPlayerHTML += '</object>';


		//document.getElementById('LIB_MediaPreview_mediaContainerDiv').innerHTML = windowsMediaPlayerHTML;
		document.getElementById('LIB_MediaPreview_mediaContainerDivInner').innerHTML = windowsMediaPlayerHTML;
	}
	// if we are going to preview flv
	else if (previewType == 3 || previewType == 7) {

		// flow player
		// document.getElementById('LIB_MediaPreview_flvPlayerIframe').style.display = '';

		document.getElementById('LIB_MediaPreview_flashContainerDiv').style.display = '';

		// this is used by flow player 1.7
		// flashVars = 'videoFile=' + url +'&loop=false&autoPlay=true&videoHeight='+(LIB_PreviewMedia_conteinerHeight - 30);

		var forceAdVideoFormat = '';
		
		if (mediaType == 'Ad') {
			controlEnabled = false;
			videoCategory = 'Ad';
			if (url.match(/fourspots/i)) {
				forceAdVideoFormat = 'flv';
			}
		}
		else {
			controlEnabled = true;
			videoCategory = '';
		}

		// flow player
		// flashVars = "config={playList: [{ url: '" + url + "',controlEnabled: "+ controlEnabled + "}],loop:false,useNativeFullScreen:true,menuItems: [ true, true, true, true, true, false],initialScale:'fit',progressBarColor1:'0xadadad',progressBarColor2:'0xd9d9d9',bufferBarColor1:'0xa09f9f',bufferBarColor2:'0xadadad'}";

		flashVars = 'file='+url+'&height='+LIB_PreviewMedia_conteinerHeight+'&width='+LIB_PreviewMedia_conteinerWidth+'&autostart=true&enablejs=true&bcategory='+videoCategory;
		if (LIB_PreviewMedia_flvPlayerVizType == 2) {
			flashVars = flashVars + '&frontcolor=0xCCFF00&backcolor=0x000000&lightcolor=0xFFFF00';
		}
		else {
		  if (fromemms == 0) {
        	flashVars = flashVars + '&backcolor='+LIB_PreviewMedia_flvPlayerBackColor;
        	} else {
        	flashVars = flashVars + '&backcolor=0x979797';
          }
		}

		if (forceAdVideoFormat!='') {
			flashVars = flashVars + '&type='+forceAdVideoFormat;
		}

		if (previewType == 7) {

			flashVars = flashVars + '&image=' + url2;
		}

		flashContainerHeight = LIB_PreviewMedia_conteinerHeight;

		if (document.getElementById("LIB_MediaPreview_flashObject")) {
			jwPlayer = system_getMovieName('LIB_MediaPreview_flashObject');

			if (previewType == 7) {
				jwPlayerConf = {file:url,image:url2,category:videoCategory};
			} else {
				jwPlayerConf = {file:url,category:videoCategory};
			}

			setTimeout('jwPlayer.loadFile(jwPlayerConf);',500);
			setTimeout('jwPlayer.sendEvent(\'playpause\')',1000);

			// flow player
			/*
				flowPlayer = document.getElementById("LIB_MediaPreview_flashObject");

				fpConf = {
					bufferLength: 20,
					loop: false,
					useNativeFullScreen: true,
					menuItems: [ true, true, true, true, true, false],
					initialScale:'fit',
					progressBarColor1:'0xFFFF00',
					progressBarColor2:'0xFFFF00',
					playList: [{name:'clip',url:url,controlEnabled:controlEnabled}]
				};

				flowPlayer.setConfig(fpConf);
			*/

		}
		else {

			var LIB_flash_SWFObject = new SWFObject(LIB_PreviewMedia_flvPlayer, 'LIB_MediaPreview_flashObject',LIB_PreviewMedia_conteinerWidth,LIB_PreviewMedia_conteinerHeight,8, "");
			LIB_flash_SWFObject.skipDetect = true;
			LIB_flash_SWFObject.addParam('wmode', 'Window');
			LIB_flash_SWFObject.addParam('flashvars', flashVars);
			LIB_flash_SWFObject.addParam('allowscriptaccess', 'always');
			LIB_flash_SWFObject.addParam('allownetworking', 'all');
			LIB_flash_SWFObject.addParam('allowfullscreen', 'true');
			LIB_flash_SWFObject.addParam('menu', 'false');
	   	LIB_flash_SWFObject.write("LIB_MediaPreview_flashContainerDiv");

	   	// flashVars = 'file='+url+'&height='+LIB_PreviewMedia_conteinerHeight+'&width='+LIB_PreviewMedia_conteinerWidth+'&autostart=true&enablejs=true';
		}

		// document.getElementById('LIB_MediaPreview_flvPlayerIframe').contentWindow.playMedia(LIB_PreviewMedia_flvPlayer,LIB_PreviewMedia_conteinerWidth,flashContainerHeight,flashVars,url,controlEnabled);

		/*
   		//when i use this method it gives me js error on browser close
			var FO = {movie:LIB_PreviewMedia_flvPlayer, width:LIB_PreviewMedia_conteinerWidth, height:flashContainerHeight, majorversion:"6", build:"0", xi:"true", flashvars:flashVars,wmode:"window",allowscriptaccess:"always", allownetworking:"all", allowfullscreen:"true",id:"LIB_MediaPreview_flashObject"};
			UFO.create(FO, "LIB_MediaPreview_flashContainerDiv");
		*/

		// when i use this method it doesn't work at Firefox
		// document.getElementById('LIB_MediaPreview_flashContainerDiv').innerHTML = flashPlayerHTML;

		/*
			flashPlayerHTML = '';
			flashPlayerHTML += '<object style="background:#000000" id="LIB_MediaPreview_flashObject" name="LIB_MediaPreview_flashObject" width="' + LIB_PreviewMedia_conteinerWidth + '" height="' + flashContainerHeight + '" type="application/x-shockwave-flash" data="'+ LIB_MediaPreview_flashObject +'" codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0">';
			flashPlayerHTML += '	<param name="movie" value="' + LIB_PreviewMedia_flvPlayer + '" />';
			flashPlayerHTML += '	<param name="quality" value="high" />';
			flashPlayerHTML += '	<param name="wmode" value="transparent" />';
			flashPlayerHTML += '	<param name="flashvars" value="' + flashVars +'" />';
			flashPlayerHTML += '	<embed flashvars="'+ flashVars +'" wmode="transparent" src="' + LIB_PreviewMedia_flvPlayer + '" quality="high" pluginspage="https://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + LIB_PreviewMedia_conteinerWidth + '" height="' + LIB_PreviewMedia_conteinerHeight + '"></embed>';
			flashPlayerHTML += '</object>';
		*/

	} else if (previewType == 5) {

		LIB_MediaPreview_swfDiv.style.display = '';
		LIB_MediaPreview_swfIframe.window.location = LIB_PreviewMedia_swfIframeSrc + '?width=' + LIB_PreviewMedia_conteinerWidth + '&height=' + LIB_PreviewMedia_conteinerHeight + '&movie=' + url;

	} else if (previewType == 4) {

		// show image
		if (document.getElementById('LIB_MediaPreview_imageObjectInner')) {

			document.getElementById('LIB_MediaPreview_imageObject').style.display = '';
			var imgObject = document.getElementById('LIB_MediaPreview_imageObjectInner');
			imgObject.style.display = '';
			imgObject.src = url;

			if (mediaParams.height && mediaParams.width) {
				var newWidth;
				var newHeight;

				var picContainerWidth = LIB_PreviewMedia_conteinerWidth;
				var picContainerHeight = LIB_PreviewMedia_conteinerHeight-30; //because ot the Prev & Next buttons bar

				if (mediaParams.width > picContainerWidth || mediaParams.height > picContainerHeight) {
					if ((mediaParams.width / mediaParams.height) >= (picContainerWidth / picContainerHeight)) {
						newWidth = picContainerWidth;
						newHeight = (mediaParams.height * picContainerWidth / mediaParams.width);
					} else {
						newWidth = (mediaParams.width * picContainerHeight / mediaParams.height);
						newHeight = picContainerHeight;
					}
				} else { // don't resize if the picture is smaller than the viewing area
					newWidth = mediaParams.width;
					newHeight = mediaParams.height;
				}

				imgObject.style.height =  newHeight + "px";
				imgObject.style.width = newWidth + "px";
				var picMargin = (picContainerHeight - newHeight)/2;
				imgObject.style.marginTop = picMargin + "px";
				imgObject.style.marginBottom = picMargin + "px";
			}


		} else {

			//document.getElementById('LIB_MediaPreview_imageObject').innerHTML = '<table cellpadding="0" cellspacing="0" border="0" style="width:100%;height:100%;"><tr><td style="height:100%;"><center><a href="'+url2+'" target="_blank"><img '+(protectImage ? 'oncontextmenu="return false;" ' : '')+'src="'+url+'" alt="" border="0" /></a></center></td></tr><tr><td><center><div class="pictures_fullsizetext">Click on the picture to see it in full size.</div></center></td></tr></table>';
			//document.getElementById('LIB_MediaPreview_imageObject').style.display = '';
		}
	}

	// call callback if any
	if (typeof(LIB_mediapreview_playList[LIB_mediapreview_playingClipNumber - 1]) !== "undefined" && typeof(LIB_mediapreview_playList[LIB_mediapreview_playingClipNumber - 1]['onStartCallBack']) !== "undefined") {
		eval(LIB_mediapreview_playList[LIB_mediapreview_playingClipNumber - 1]['onStartCallBack']);
	}
}

/*
 * this is a callback function of the flv player. Unfortunately i cannot rename it
 *
 *@param array clip info about the finished clip 	(name,baseUrl,fileName,start,end,protected,linkUrl,linkWindow,controlEnabled)
 *
*/

function onClipDone(clip) {
	LIB_mediapreview_onClipDone();
}

/*
 * This function is called from the jw player on few seconds intervals
*/
function getUpdate(typ,pr1,pr2,pid) {
	if (typ == 'state') {
		// if completed
		if (pr1 == 3) {
			LIB_mediapreview_onClipDone();
		}
	}
};

/**
 *  This function is called when the previewer is closed to stop all players
 */
function LIB_mediapreview_closePreviwer() {
	// stop flash player
	LIB_mediapreview_stopFlashPlayer();

	// stop windows media player
	LIB_mediapreview_stopWindowsMediaPlayer();
}

/**
 *  Stop flash player if exists
 */
function LIB_mediapreview_stopFlashPlayer() {
	// stop flash player if exists
	if (document.getElementById("LIB_MediaPreview_flashObject")) {
		if (typeof document.getElementById('LIB_MediaPreview_flashObject').sendEvent == 'function')  {
			system_getMovieName('LIB_MediaPreview_flashObject').sendEvent('stop');
		}
	}
}

/**
 *  Stop Windows Media Player if exists
 */
function LIB_mediapreview_stopWindowsMediaPlayer() {
	// stoping the media player
	if (document.getElementById('LIB_MediaPreview_mediaObject')) {
		if (navigator.appName.indexOf("Microsoft") != -1) {
			LIB_MediaPreview_mediaObject.stop();
		}
	}
}



