var swfContainerType = "";
var videoQueue = Array();


function loadPlayer() {
	var flvVars = {
	        title: "TITLE", 
	        height: "400",
	        displayheight: "380",
	        enablejs: "true",
	        javascriptid: "redbullmovie",
	        overstretch: "false",
	        autostart: "false",
	        callback: "eventCallback",
	        bufferlength: "2",
	        image: video.Thumbnail,
	        file: video.Url,
	        title: video.Title,
	        backcolor: "0x000000",
	        frontcolor: "0xffba00"
	        };
	var flvParams = {
	        allowscriptaccess: "always",
	        allowfullscreen: "true",
	        hideMenu: "true",
	        wmode: "transparent"};
	var attrib = { id: "movie1" };
	var vidObject = swfobject.embedSWF(video.SwfUrl, "flashVideo", "534", "400", "9.0.115", "/public/shared/swf/expressInstall.swf", flvVars, flvParams, attrib);
}

function eventCallback(obj) {
	
	if (obj["state"] == "start") {	
		
		pageTracker1._trackPageview("/wsop_video/start/"+ obj["title"] + ".html");
		pageTracker2._trackPageview("/wsop_video/start/"+ obj["title"] + ".html");
		pageTracker3._trackPageview("/wsop_video/start/"+ obj["title"] + ".html");

	}
}
		
function getUpdate(typ,pr1,pr2,pid) {
	if (typ == "time") { 
		currentPosition = pr1; 
	} else if (typ == "volume") { 
		currentVolume = pr1; }
	else if (typ == "item") { 
		currentItem = pr1; 
		setTimeout("getItemData(currentItem)",100); 
	}
	var id = document.getElementById(typ);
	id.innerHTML = typ+ ": "+Math.round(pr1);
	pr2 == undefined ? null: id.innerHTML += ", "+Math.round(pr2);
};

function getItemData(idx) {
	var obj = thisMovie("movie1").itemData(idx);
	var nodes = "";
	for(var i in obj) { 
		nodes += i +": "+obj[i]+"<br>"; 
	}

	trackVideo();
	
}

function trackVideo() {
	var url = '/ajax/trackVideo';
	var pars = 'video_id='+ video.Id;
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete: function(obj,json) {
			var json = obj.responseText.evalJSON(true);
		}
	});
}

function thisMovie(swf) {
  if(navigator.appName.indexOf("Microsoft") != -1) {
    return window[swf];
  } else {
    return document[swf];
  }
};

function voteVideo() {

	var url = '/ajax/voteVideo';
	var pars = 'video_id='+ video.Id;
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete: function(obj,json) {
			var json = obj.responseText.evalJSON(true);
			if (json.success == false) {		
				alert(json.message);
				return;
			} else {
				$('voteButton').innerHTML = json.html;
			}
		}
	});
}

function init() {
	loadPlayer();
	checkVideoVotes();	
}


function checkVideoVotes() {
	var url = '/ajax/checkVideoVotes.html';
	var myAjax = new Ajax.Request(url, {method: 'post',  onComplete: function(obj,json) {
			var json = obj.responseText.evalJSON(true);
			$('voteButton').innerHTML = json.html;
		}
	});	
}

function initSwfUpload(uType) {
	
	Lightview.show({
	  href: '/ajax/pseudo_progress.html',
	  rel: 'ajax',
	  title: uType.substr(0,1).toUpperCase() + uType.substr(1) +" Upload Status",

	  options: {
	    width: 500,
	    height: 120,
	    topclose: true,
	    ajax: {
	      method: 'post',
		  onComplete: function() {
		  	var pseudoDiv = $('pseudo_progressDiv');
				  	
			pseudoDiv.style.width = "500px";
			pseudoDiv.style.height = "120px";
			pseudoDiv.style.lineHeight = "normal"
			pseudoDiv.style.padding = "5px"
			
					  
			//Effect.BlindDown(utypeContainer);
			var uProgress = document.createElement("div");
			uProgress.style.margin = "20px auto";
			uProgress.style.textAlign = "center";
			uProgress.id = "uploadProgressContainer";

			var uploadProcessDiv = document.createElement("div");
			uploadProcessDiv.style.height = "60px";
			uploadProcessDiv.style.textAlign = "left";
			uploadProcessDiv.style.margin = "5px";
			uploadProcessDiv.id = "uploadProcess";
			uProgress.appendChild(uploadProcessDiv);
			var thumbnailsDiv = document.createElement("div");
			thumbnailsDiv.style.textAlign = "center";
			thumbnailsDiv.id = "thumbnails";
			uProgress.appendChild(thumbnailsDiv);
			var divStatus = document.createElement("div");
			divStatus.style.textAlign = "center";
			divStatus.style.clear = "both";
			divStatus.id = "divStatus";
			uProgress.appendChild(divStatus);

			
			//utypeContainer.appendChild(uProgress);
			swfContainerType = uType;
		
			$('pseudo_progressDiv').innerHTML = uProgress.innerHTML;
			

			var post_params = swfU_Vid.settings.post_params;
			post_params.videoTitle = $('video_title').value;
			swfU_Vid.setPostParams(post_params);
			if (uType == "video") {
				setTimeout('swfU_Vid.selectFiles()', 1100);
			} else {
				setTimeout('swfU.selectFiles()', 1100);
			}
		
			iTime = "";			  
			  
			}		  	
		  } 
	    }
	  });
	

}


function showHelpInfo(hType) {
	 Effect.BlindDown(hType, { duration : 2.0 });
	 eval("$(hType + '_link').onclick = function () {};");
}

function hideHelpInfo(hType) {
	 Effect.BlindUp(hType, { duration : 2.0 });
	 eval("$(hType + '_link').onclick = function () { showHelpInfo('"+ hType +"'); };");
}

function hideProgress() {
	Lightview.hide();
	new Effect.Fade($('overlay'), { duration: 0.3 });	
}

function converTmpFile2Input(fn) {
	$('submit_continue').disabled = 'disabled';
	createHiddenInput("videoFile[fileSession]", tmpFile.fileSession, fn);
	createHiddenInput("videoFile[fileName]", tmpFile.fileName, fn);
	createHiddenInput("videoFile[fileDuration]", tmpFile.fileDuration, fn);
	createHiddenInput("videoFile[fileFromCodec]", tmpFile.fileFromCodec, fn);
	createHiddenInput("videoFile[thumbNail]", tmpFile.thumbNail, fn);
	createHiddenInput("videoFile[thumbNailSmall]", tmpFile.thumbNailSmall, fn);
	window.onbeforeunload = "";	
	
	fn.submit();
	
}

function encodingProgressChecker() {
	var url = '/ajax/encodingProgressChecker.html';

	var myPeriodicalAjax = new Ajax.PassivePeriodicalUpdater(url, {method: 'post',  decay : 1,
			onSuccess : function (obj) {
							var json = obj.responseText.evalJSON(true);
							var queues = json.queues;
							for (i=0; i<queues.length; i++) {
								var queue = queues[i];
								var videoId = queue.videoId;
								
								$('vQueue_' + videoId).className = "progressContainer blue";
								//$('vQueue_' + videoId).style.height = "45px";
								$('vQueueProgress_' + videoId).className = "progressBarInProgress";
								$('vQueueProgress_' + videoId).style.width = queue.encodingPercent + "%";
							//	$('vQueueProgress_' + videoId).style.height = '45px';
							//	$('vQueueProgress_' + videoId).style.bottom = '15px';
								$('vQueueProgress_' + videoId).innerHTML = "&nbsp;";
								
								$('vQueueUploadTime_' + videoId).innerHTML = queue.videoUploadDate;
								$('vQueueTitle_' + videoId).innerHTML = queue.videoTitle;
								$('vQueueProgressTimeRemaining_'+ videoId).innerHTML = queue.encodingTimeLeft;
								$('vQueueProgressTimeElapsed_'+ videoId).innerHTML = queue.encodingTimeElapsed;
							
								$('vQueueStatus_' + videoId).innerHTML = queue.sessionStatus;
							}
						}
					});
								
}

function updateVideoThumbnail(videoId, thumbId) {
	var url='/ajax/updateVideoThumbnail.html';
	var pars = 'video_id='+ videoId +'&thumbnail_id='+ thumbId;
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete: function(obj,json) {
			var json = obj.responseText.evalJSON(true);
			$('videoThumbnail_'+ videoId).src = json.thumbnailUrl;
		}
	});	
}
