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

function startLoading(){
	buildPlaylistContent();

//	$('#playlist_scroller').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;
	if(streamURL != ""){
		//RTMP STREAM FILE
		 flashvars = {
			streamer:streamURL,
			file:streamFile,
			type:'rtmp',
			autostart: 'false',
		//	skin:'vidplayer/skins/modieus/modieus.swf',
		  skin:'vidplayer/skins/giraldatv.swf',
			abouttext:'vivocomtech',
			aboutlink:'http://www.vivocomtech.com',
			controlbar: videoPlayer_ControlBar,
		//stretching:'exactfit',
        stretching:'uniform',
			bufferlength:'2',
			wmode:'opaque',
			image:theImage
			/*image:theImage,
			displayclick:'none'*/
		
			
		}
	}else{
		//PROGRESSIVE--------ok!
		 flashvars = {
			file:theFile,
			autostart: videoPlayer_IsAutoStart,
		//	skin:'vidplayer/skins/modieus/modieus.swf',
			provider:'http',
		  skin:'vidplayer/skins/giraldatv.swf',
			abouttext:'vivocomtech',
			aboutlink:'http://www.vivocomtech.com',
			controlbar: videoPlayer_ControlBar,
				//stretching:'exactfit',
        stretching:'uniform',
			bufferlength:'5',
				wmode:'opaque',
			image:theImage,
			plugins:'gapro-1&gapro.accountid=UA-15536741-3&gapro.trackstarts=true&gapro.trackpercentage=true&gapro.tracktime=true'
		/*	image:theImage,
			displayclick:'none'*/
		 }
	}
	var params = {
		allowfullscreen:'true', 
		allowscriptaccess:'always',
			wmode: 'transparent'
	}
	var attributes = {
		id:thePlayerId,  
		name:thePlayerId
	}

	swfobject.embedSWF('vidplayer/player.swf', thePlaceholder, '576', '324', '9.0.115', false, flashvars, params, attributes);
	try{var tmp = document.getElementById('player1');tmp.innerHTML;}catch(Exception){} // We have to have this line for google chrome. (gains access to the player)
}

