﻿var currContestEntryId;
var videoPlayerReady = false;
var videoWaitingFile = "";

$(document).ready(VideoContest_Initialize);

function VideoContest_Initialize() {
	$("[scriptid='btnLaunchVideo']").bind("click", VideoContest_ShowVideo);
	$("#btnVideoContestVote").bind("click", VideoContest_PopupVoteClick);
	//document.getElementById("video_modal").style.display = "none";

	//var so = new SWFObject("/resources/flash/flvplayer.swf?skin=/resources/flash/SkinUnderPlayStopSeekFullVol.swf", "videoplayerObject", "500", "400", "9.0.115", "#ffffff");
	//so.addParam("wmode", "transparent");
	//so.addParam("allowscriptaccess", "true");
	//so.useExpressInstall('/flash/expressinstall.swf');
	//so.write("videoplayer");

	$f("videoplayer", "/resources/flash/flowplayer.commercial-3.1.5.swf", {
		key: '#$1ec27118e4d44b1f8a4', 
		plugins: {

			controls: {
				// display properties 
				bottom: 0
						, height: 24
						, backgroundColor: '#2d3e46'
						, backgroundGradient: 'low'

				// controlbar-specific configuration 
						, fontColor: '#ffffff'
						, timeFontColor: '#333333'
						, autoHide: 'true'

				// which buttons are visible and which are not? 
						, stop: true
						, mute: true
						, fullscreen: false
						, play: true
						, volume: true
						, time: true
						, playlist: false
						, scrubber: true

			}

		}
	});
}

function videoTrigger(videoFile) {
	//$f("videoplayer").play(videoFile);
	if ($f("videoplayer").isLoaded()) {
		$f("videoplayer").unload();
		videoTrigger(videoFile);
	}
	else {
		$f("videoplayer").load(function() {
			$f("videoplayer").play(videoFile);
		});
	}
}

function VideoContest_ShowVideo(e) {
	e.preventDefault();
	e.stopPropagation();

	VideoDisplayModal('400', $(this).find(".VideoThumb"));
	videoTrigger($(this).find(".VideoThumb").attr("vfile"));
	currContestEntryId = $(this).find(".VideoThumb").attr("eid");
	if ($(this).find(".VideoThumb").attr("voted") == "true") {
		$("#imgVideoContestVote").attr("src", "/resources/images/btn_voted.png")
	}
	else {
		$("#imgVideoContestVote").attr("src", "/resources/images/btn_vote.png")
	}
}

function VideoContest_PopupVoteClick(e) {
	e.preventDefault();
	e.stopPropagation();

	if ($(".VideoThumb[eid=" + currContestEntryId + "]").attr("voted") == "true") return;

	var data = "{contestEntryId:" + currContestEntryId
            + "}";

	$.ajax({
		type: "POST",
		contentType: "application/json; charset=utf-8",
		url: "/App_Resources/services/ContestService.asmx/RecordVote",
		data: data,
		dataType: "json",
		success: VideoContest_VoteSuccess,
		error: VideoContest_VoteError
	});
}

function VideoContest_VoteSuccess(responseJSON) {
	//alert(responseJSON);
	//VideoHideModal();
	$(".VideoThumb[eid=" + currContestEntryId + "]").attr("voted", "true");
	$("#imgVideoContestVote").attr("src", "/resources/images/btn_voted.png")
}

function VideoContest_VoteError(responseJSON, error) {
	//error code
	//alert("error - " + responseJSON.responseText);
	//VideoHideModal();
}