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

function startLoading()
{
	if (parametros == 0)
	{
		var vervideo = buildPlaylistContentDirecto();
		displayTagCloud();

		//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); }

		//playVideoDirecto(videoIndex,'content');
		playVideoDirecto(vervideo,'schcontent');
	}
	else
	{
		buildPlaylistContent();
		displayTagCloud();

		//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;
	var i=0;

	if(currentlyShowing == 'content'){ playingItems = contentArray; }
	if(currentlyShowing == 'schcontent'){ playingItem = schArray[index]; }
	if(currentlyShowing == 'search'){ playingItems = searchContentArray; }
	
	contentLength = playingItems.length;
	
	for (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 == 'schcontent'){ playingItem = schArray[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;
	var theSkin = '';
	
	if (currentlyShowing == 'schcontent'){
		theSkin = 'vidplayer/skins/canalclubdirecto.zip';
	}
	else {
		theSkin = 'vidplayer/skins/canalclub.zip';
	}
	
	videoPlayer_ControlBar = 'bottom';
		
	if(streamURL != "")
	{
		//RTMP STREAM FILE
		jwplayer("videoHolder").setup({
	        flashplayer: "vidplayer/player.swf",
	        streamer:streamURL,
			file:streamFile,
			type:'rtmp',
	        autostart: videoPlayer_IsAutoStart,
	        skin: theSkin,
	        abouttext: "vivocomtech",
	        aboutlink: "http://www.vivocomtech.com",
	        controlbar: videoPlayer_ControlBar,
	        stretching: "exactfit",
	        bufferlength: 5,
	        wmode: "opaque",
	        image: theImage,
	        plugins: { gapro: {
           		accountid: "UA-3247148-46" ,
				trackstarts: "true",
				trackpercentage: "true",
				tracktime: "true"
       			}
       		},
       		id: thePlayerId,  
			name: thePlayerId,
	        height: 358,
	        width: 634
	    });		
	}
	else
	{
		 jwplayer("videoHolder").setup({
	        flashplayer: "vidplayer/player.swf",
	        file: theFile,
	        autostart: videoPlayer_IsAutoStart,
	        provider: "http",
	        skin: theSkin,
	        abouttext: "vivocomtech",
	        aboutlink: "http://www.vivocomtech.com",
	        controlbar: videoPlayer_ControlBar,
	        stretching: "exactfit",
	        bufferlength: 5,
	        wmode: "opaque",
	        image: theImage,
	        plugins: { gapro: {
           		accountid: "UA-3247148-46" ,
				trackstarts: "true",
				trackpercentage: "true",
				tracktime: "true"
       			}
       		},
       		id: thePlayerId,  
			name: thePlayerId,
	        height: 358,
	        width: 634
	    });
	}
}

