function resize_video()
{
	var height = $(window).height();
	var width = $(window).width();

	$("#video_container").width(width+"px");
	
	if(!$('body').hasClass("ie6"))
		$("#video_container").height(height+"px");
	
	var center_vertical = false;
	
	// portrait
	if( (width / height) > 16 / 9 )
	{
		var video_height = (width * 9 / 16);
		var video_width = width;
		
		var video_margin_left = 0;
		
		if(center_vertical)
			var video_margin_top = (height - video_height) / 2;
		else
			var video_margin_top = 0;
			
	}
	// landscape
	else
	{
		var video_width = (height * 16 / 9);
		var video_height = height;
		
		var video_margin_top = 0;
		var video_margin_left = (width - video_width) / 2;
	}
	
	if($("#video_container").hasClass("video") && $("#video_container video").length)
	{
		var element = $("#video_container video");
	}
	else if($("#video_container").hasClass("video") && $("#video_container .flash").length)
	{
		var element = $("#video_container .flash");
	}
	else
	{
		var element = $("#video_container img");
	}
	
	
	element.css("margin-left", Math.round(video_margin_left)+"px");

	if(center_vertical)
		element.css("margin-top", Math.round(video_margin_top)+"px");

	element.css("height", Math.round(video_height)+"px");
	element.css("width", Math.round(video_width)+"px");
	
}


function show_video()
{
	$('#flash_replacement').show();
	$('#poster').hide();
}

function play_video()
{
	if($("#video_container").hasClass("video"))
	{
		var class_name = ($("#video_container").get(0).className).split(" ");
		var video_name = class_name[0];
		var loop = (typeof(class_name[2]) != "undefined" && class_name[2] == "loop") ? true : false;

		var video = false;

		var folder = "/sites/all/themes/framework/media/";
		var file = folder+video_name;

		if("HTMLVideoElement" in window)
		{
			var myVideo = document.createElement("video");
			var support_mp4 = myVideo.canPlayType('video/mp4');
			var support_ogv = myVideo.canPlayType('video/ogg');

			var html = '<video id="video" poster="'+file+'.jpg">';

			// check if browser supports mp4 video
			if(support_mp4== "maybe" || support_mp4 == "probably")
			{
				video = '<source type="video/mp4" src="'+file+'.mp4" />';
			}
			// check if browser supports ogv video
			else if(support_ogv== "maybe" || support_ogv == "probably")
			{
				video = '<source type="video/ogg" src="'+file+'.ogv" />';
			}

			html += video+'</video>';
		}

		// if we have video support in browser
		if(video)
		{
			// add video to dom
			$("#video_container").html(html);

			// play video when we have enough data
			/*$("#video").bind('canplaythrough', function()
			{
				this.play();
			});*/

			setTimeout(function()
			{
				$("#video").get(0).play();
			}, 1000);
			
			// loop video
			$("#video").bind('ended', function()
			{
				if(loop)
				{
					this.play();
				}
			});
		}
		else
		{
			var html = '<div class="flash"><div id="flash_replacement">'+$("#video_container").html()+'</div></div>'
			$("#video_container").html(html);

			var flashvars = {
				imageURL: file+".jpg",
				videoURL: file+".mp4",
				loop: loop
			}

			var params = {
				bgcolor: "#000000",
				wmode: 'transparent'
			}


			swfobject.embedSWF(folder+"video.swf", "flash_replacement", "100%", "100%", "9.2.0", false, flashvars, params);
		}
	}
}

$(document).ready(function()
{
	play_video();
	resize_video();
		
	var timeOut = null;
	var delay = false;
	
	window.onresize = function()
	{
		if(delay == false)
		{
			resize_video()
		}
		else
		{
			if(timeOut != null) 
				clearTimeout(timeOut);

			timeOut = setTimeout(resize_video, delay);
		}
	}
});
