﻿var videomodal;
var videomodaloverlay;
var videomodaloverlayposition;
var videomodalcontent;

var videomodaltop;
var videomodalbottom;

var videomodalbg;

var videocurrbutton = null;

$(document).ready(function() {

	videomodal = $("#video_modal");
	videomodaloverlay = $("#video_modal_overlay");
	videomodalcontent = $("#video_modal_content");

	videomodaltop = $("#video_modal_top");
	videomodalbottom = $("#video_modal_bottom");

	videomodalbg = $("#video_screen");

	$("#btnVideoModalClose").bind("click", VideoHideModal);
});

function VideoDisplayModal(width, currButton) {
	if (videocurrbutton != null) {
		videomodal.css("display", "none");
		videomodalbg.css("display", "none");
		videomodaloverlay.animate({ top: videomodaloverlayposition.top, left: videomodaloverlayposition.left, width: 10, height: 10 }, 1, function() {
			videocurrbutton = null;
			VideoDisplayModal(width, currButton);
		});
		return;
	}

	videomodal.css("width", width.toString() + "px");
	videomodaltop.css("width", (width - 60).toString() + "px");

	videomodalbottom.css("width", (width - 44).toString() + "px");
	videomodalcontent.css("width", (width - 50).toString() + "px");

	var size = new Object();

	size.x = document.documentElement.clientWidth;

	size.y = (document.documentElement.clientHeight / 2) + document.documentElement.scrollTop;

	var mt = (size.y - (videomodal.height() / 2));

	videomodalbg.css("height", $(window).height() + "px");
	videomodalbg.css("display", "block");

	var position = currButton.position();

	var videoTop;
	var videoLeft;

	/*if (position.top > (document.documentElement.clientHeight / 2)) {
		videoTop = position.top + 146 - videomodal.height();
		videomodaloverlay.css("top", position.top + 146);
	}
	else {
		videoTop = position.top;
		videomodaloverlay.css("top", position.top);
	}

	if (position.left > (document.documentElement.clientWidth / 2)) {
		videoLeft = position.left + 200 - videomodal.width();
		videomodaloverlay.css("left", position.left + 200);
	}
	else {
		videoLeft = position.left;
		videomodaloverlay.css("left", position.left);
	}*/

	videoTop = (position.top + 73) - (videomodal.height() / 2);
	videomodaloverlay.css("top", position.top + 73);

	videoLeft = (position.left + 100) - (videomodal.width() / 2);
	videomodaloverlay.css("left", position.left + 100);

	if (videoLeft < 0) {
		videoLeft = position.left;
	}
	else if ((videoLeft + videomodal.width()) > document.documentElement.clientWidth) {
		videoLeft = position.left - ((videoLeft + videomodal.width()) - document.documentElement.clientWidth);
	}
	
	videomodaloverlay.css("display", "block");
	videomodaloverlayposition = videomodaloverlay.position();

	videomodaloverlay.animate({ top: videoTop, left: videoLeft, width: videomodal.width(), height: videomodal.height() }, 500, function() {
		videomodal.css({ top: videoTop, left: videoLeft, display: 'block' });
	});

	videocurrbutton = currButton;
}

function VideoHideModal() {
	var mt = (($(window).height() / 2) - (videomodal.height() / 2));

	videomodal.css("display", "none");
	videomodalbg.css("display", "none");

	videomodaloverlay.animate({ top: videomodaloverlayposition.top, left: videomodaloverlayposition.left, width: 10, height: 10 }, 500, function() {
		videomodaloverlay.css("display", "none");
	});
	videocurrbutton = null;

	$f("videoplayer").stop();
	$f("videoplayer").unload()
}