//Create our overlay object
var overlay = $("<div id='modal-overlay'></div>");

//Create our modal window
var modalWindow = $("<div id='modal-window'></div>");

function showVideo() {
	//Append the overlay to the document body
	$("body").append(overlay.click(function() { hideVideo(); }))
	//Add a loader to our page
	$("body").append("<div id='modal-load'></div>");
	overlay.css("opacity", 0.8);
	overlay.fadeIn(150);
	//Activate a listener 
	$(document).keydown(handleEscape);	
	
	//Load the image
/*	var img = new Image();
	$(img).load(function () {
		var imageWidth = img.width / 2 ;
		var imageHeight = img.height / 2;
		modalWindow.css({
			"margin-left": -imageWidth,
			"margin-top": -imageHeight
		});	
		$("#modal-load").remove();
		modalWindow.append(img);
		$("body").append(modalWindow);
		modalWindow.fadeIn(150);
	})
	.attr({ src: 'promoVideo.jpg' }).click(function() {
		modalHide();
	});*/


	var imageWidth = 640 / 2 ;
	var imageHeight = 478 / 2;
	modalWindow.css({
		"margin-left": -imageWidth,
		"margin-top": -imageHeight
	});	
	$("body").append(modalWindow);
	$("#modal-load").remove();
	modalWindow.fadeIn(150);
	$('#modal-window').html("<object style='z-index:50;' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0'  width='640' height='478'> 	  <param name='movie' value='TornadoPageVideo.swf'><param name='quality' value='high'><param name=wmode value='transparent'><param name='scale' value='noscale'> 	  <embed src='TornadoPageVideo.swf' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash'  width='640' height='478'  scale='noscale'  wmode='transparent'></embed></object><noscript><object style='z-index:50;' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0' width='640' height='478'><param name='movie' value='TornadoPageVideo.swf' /><param name='quality' value='high' /><embed src='TornadoPageVideo.swf' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='640' height='478'></embed></object></noscript>");

}

function hideVideo() {
	modalHide();
}

//Our function for hiding the modalbox
function modalHide() {
	$(document).unbind("keydown", handleEscape)
	var remove = function() { $(this).remove(); };
	overlay.fadeOut(remove);
	modalWindow
		.fadeOut(remove)
		.empty();
}

//Our function that listens for escape key.
function handleEscape(e) {
	if (e.keyCode == 27) {
		modalHide();
	}
}
