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

function startLoading()
{
	buildPlaylistContent();

	//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)
	{
		//device detection
		if (navigator.userAgent.match(/iPad/i) != null)
		{
			if(queryWidth != "")
				width = queryWidth;
			else
				width = '560';
		
			if(queryHeight != "")
				height = queryHeight;
			else
				height = '345';
			
			var ipad='';
			ipad+="<object classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B' codebase='http://www.apple.com/qtactivex/qtplugin.cab' height='"+height+"' width='"+width+"'><param name='src' value='"+theFile+"' autoplay='true'><param name='autoplay' value='true'><param name='type' value='video/quicktime' height='"+height+"' width='"+width+"'><embed src='"+theFile+"'  autoplay='true' height='"+height+"' width='"+width+"' autoplay='true' type='video/quicktime' pluginspage='http://www.apple.com/quicktime/download/' flashvars='autostart=true'></object>";
		
			$("#divMedia").html(ipad);
		}
		else
		{
			deletePlayer('divMedia', '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 = '560';

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

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

		if ((theFile.indexOf(".xml")) != -1)
		{
			flashvars = {
						playlistfile:theFile,
						autostart: 'true',
						skin:'vidplayer/skins/icex.zip',
						abouttext:'vivocomtech',
						aboutlink:'http://www.vivocomtech.com',
						controlbar: 'bottom',
						stretching:'uniform',
						bufferlength:'5',
						wmode:'opaque',
						//plugins:'qualitymonitor-2', 
						image:theImage
					}
		
		} else {
	
		//PROGRESSIVE
		flashvars = {
						file:theFile,
						autostart: 'true',
						provider:'http',
						skin:'vidplayer/skins/icex.zip',
						abouttext:'vivocomtech',
						aboutlink:'http://www.vivocomtech.com',
						controlbar: 'bottom',
						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){}
}

