var swfContainerType = "";
var videoQueue = Array();

function videoList() {
	this.vid = new Array();
	this.pid = new Array();
	this.title = new Array();
	this.category = new Array();
	this.rating = new Array();
	this.video_owner = new Array();
	this.profile_link = new Array();
	this.num_rated = new Array();
	this.current = 0;
	this.previous = 0;
	this.Id = 0;
}


var thisPlayer = null; 
function playerReady(thePlayer) { 
	thisPlayer = window.document[thePlayer.id]; 
	if (typeof onLoadFunc != 'undefined') {
		for(var i in onLoadFunc) {
			thisPlayer.addViewListener('LOAD', onLoadFunc[i]);		
		}				
	}
	if (typeof onPlayFunc != 'undefined') {
		for(var i in onPlayFunc) {
			thisPlayer.addViewListener('PLAY', onPlayFunc[i]);		
		}				
	}
}



function loadPlayer() {
	
	// Create child for swfobject
	var flvVideoDiv = document.createElement('div');
	flvVideoDiv.id = 'flashVideo';
	$('flashVideoContainer').appendChild(flvVideoDiv);
	
	var playerWidth = (video.Width != undefined) ? video.Width : "534";
	var playerHeight = (video.Height != undefined) ? video.Height : "400";
	var displayHeight = playerHeight - 30;
	
	var flvVars = {
	     
	        
	        displayheight: displayHeight,
	        enablejs: "true",
	        javascriptid: "coastmovie",
	        stretching: "none",
      		repeat: "none",
	        logo: "http://coast.bnqt.com/public/frontend/styles/coast/images/coastEye.png",
	        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", name: "movie1" };
	var vidObject = swfobject.embedSWF(video.SwfUrl, "flashVideo", playerWidth, playerHeight, "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 rateVideo(videoRating) {
	if (video.canRate == "true") {
		var url = '/ajax/rateVideo';
		var pars = 'video_id='+ video.Id + '&rating='+ videoRating;
		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 {

					video.canRate = "false";
					video.numRatings = json.numRatings;
					video.totalRating = json.totalRating;
					$('videoRaterContainer').innerHTML = json.html;
					initVideoRating();
				}
			}
		});
	} else {
		alert('You must be logged in to rate this video.');
		return;
	}
}

function init() {
	loadPlayer();
	initVideoRating();
}

function loadVideoInfo() {
	return video;
}


function getFlashMovie(movieName) {
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
}
 
function initVideoRating() {
	var flvVars = {
			currRating: video.currentRating,
			numRatings: video.numRatings,
			currVideoId: video.Id,
			enableRating: video.canRate
	        };
	var flvParams = {
	        allowscriptaccess: "always",
	        wmode: "transparent"};
	var attrib = { id: 'videoRater' };
	var vidObject = swfobject.embedSWF('/public/shared/swf/videoRating.swf', "videoRater", "80", "25", "8", "/public/shared/swf/expressInstall.swf", flvVars, flvParams, attrib);
}

function checkVideoVotes() {
	var url = '/ajax/checkVideoVotes.html';
	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);
			$('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;
		}
	});	
}

function doClear(searchBox, default_string) {
	if (searchBox.value == default_string) { searchBox.value = ''; }       
	}

function noClear(searchBox, default_string) {
	if (searchBox.value == '') { searchBox.value = default_string; }       
	}

function displayEmailFriend(videoId) {
	/*
	var url = '/ajax/displayEmailFriendVideo.html';
	var pars = 'video_id='+ videoId;
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete: function(obj,json) {
			var json = obj.responseText.evalJSON(true);
	*/
			Effect.BlindDown('emailFriendBox', { duration : 0.4 });
			//eval("$('addToPlaylist').onclick = function () {};");
			eval("$('emailFriendVideo').onclick = function () {};");
			
			//$('emailFriendBoxMessage').innerHTML = json.content;
	/*		
		}
	});
	*/
	
	
}

function emailFriendVideo() {
	var isValid = emailFriendValid.validate();
	if (!isValid) {
		return false;
	} else {
		var url = '/ajax/emailFriendVideo.html';
		var pars = Form.serialize($('friendVideo'));
		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);
					
					
				}
				$('fullName').value = "";
				$('friendName').value = "";
				$('friendEmail').value = "";
				$('emailComment').value = "";
				emailFriendValid.reset();
			}			
		});
	}
}



function closeEmailFriend() {
	Effect.BlindUp('emailFriendBox', { duration : 0.4});
	eval("$('emailFriendVideo').onclick = function () { displayEmailFriend('"+ video.Id +"'); };");
	//eval("$('addToPlaylist').onclick = function () { addToPlaylist('"+ video.Id +"'); };");
}
