/* *** LOADING PLAYER *** */

function startLoading(){
	buildPlaylistContent();
	$('#divScrollBarBox').jScrollPane({showArrows:true, scrollbarWidth:11, scrollbarMargin:11});

	//wait before we start playing... lets check if we're using a groovy URL.
	var videoIndex = 0;
	if(queryVideoIndex != ""){ videoIndex = queryVideoIndex; }
	if(queryMetaDataID != ""){ videoIndex = getMetaDataIndex(queryMetaDataID); }

	playVideo(videoIndex,'content');

}

function getMetaDataIndex(metadataID){
	var playingItems;
	var contentLength = 0;
	if(currentlyShowing == 'content'){ playingItems = contentArray; }
	if(currentlyShowing == 'search'){ playingItems = searchContentArray; }
	
	contentLength = playingItems.length;

	for(var i=0;i < contentLength;i++){	
		if(playingItems[i].ContentMetaDataID == metadataID){
			return i;
		}
	}
	return 0;
}



function initPlayer(index) {
	var playingItem;
	var isContentAvailable = true;
	if(currentlyShowing == 'content'){ playingItem = contentArray[index]; }
	if(currentlyShowing == 'search'){ playingItem = searchContentArray[index]; }

	if(playingItem == null){
		isContentAvailable = false;
		alert("isContentAvailable:" + isContentAvailable);
	}else{
	  theFile = playingItem.VideoPath;
	  theImage = playingItem.ThumbnailPath;
	}

	if(isContentAvailable){
		deletePlayer('videoHolder', 'videoWindow', 'player1'); 
		createPlayer('videoWindow', 'player1', theFile, theImage);
	}else{
		//TODO: show something saying there is no content
	}
}

function deletePlayer(theWrapper, thePlaceholder, thePlayerId) { 
	swfobject.removeSWF(thePlayerId);
	var tmp = document.getElementById(theWrapper);
	if (tmp){ tmp.innerHTML = "<div id=" + thePlaceholder + "></div>"; }
	playerReady('player1'); // Player Status Listeners. Detected when video finished
}

function createPlayer(thePlaceholder, thePlayerId, theFile, theImage) {
	getURL(thePlaceholder, thePlayerId, theFile, theImage); // calls control-cdn.js
}

function doCreatePlayer(thePlaceholder, thePlayerId, theFile, theImage, streamFile, streamURL) 
{
	var flashvars;

	//Paso de par‡metros desde los servicios	
	if(queryWidth != "")
		width = queryWidth;
	else
		width = '600';

	if(queryHeight != "")
		height = queryHeight;
	else
		height = '337';

	//Montamos las flashvars, params y atributos
	if(streamURL != "")
	{
		//RTMP STREAM FILE
		flashvars = {
						streamer:streamURL,
						file:streamFile,
						type:'rtmp',
						autostart: videoPlayer_IsAutoStart,
						skin:'vidplayer/skins/psoe.swf',
						abouttext:'vivocomtech',
						aboutlink:'http://www.vivocomtech.com',
						controlbar: videoPlayer_ControlBar,
						stretching:'uniform',
						bufferlength:'2',
						wmode:'opaque',
						image:theImage
					}
	}
	else
	{
		//PROGRESSIVE
		flashvars = {
						file:theFile,
						autostart: videoPlayer_IsAutoStart,
						provider:'http',
						skin:'vidplayer/skins/psoe.swf',
						abouttext:'vivocomtech',
						aboutlink:'http://www.vivocomtech.com',
						controlbar: videoPlayer_ControlBar,
						stretching:'uniform',
						bufferlength:'5',
						wmode:'opaque',
						image:theImage
					}
	}

	var params = {
					allowfullscreen:'true', 
					allowscriptaccess:'always',
					wmode: 'transparent'
				 }
				 
	var attributes = {
						id:thePlayerId,  
						name:thePlayerId
					 }

	//Objeto video
	swfobject.embedSWF('vidplayer/player.swf', thePlaceholder, width, height, '9.0.115', false, flashvars, params, attributes);

	// We have to have this line for google chrome. (gains access to the player)
	try
	{
		var tmp = document.getElementById('player1');tmp.innerHTML;
	}
	catch(Exception){}
	 
}
