var VideoEmbed =  {
	
	controlbar_height: 24,
	current_select : {
		aspect: {w: 16, h: 9},
		type: "html",
		width: 640
	},

	ShowEmbedWindow : function()
	{
		$f("player").hide();
		//$("#embed_window").show();
		$("#embed_window").dialog({
			bgiframe: false,
			resizable: false,
			width: 500,
			height: 300,
			modal: true,			
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			close: function(event, ui) {
				$f("player").show();
			},
			buttons: {
				"Закрыть": function() {
					$(this).dialog("close");
				}
			}
		});
		$("#embed_window").dialog("open");
	},
	
	CalcHeight : function(width)
	{
		width = parseInt(width);
		var height = width * this.current_select.aspect.h / this.current_select.aspect.w;
		height = this.controlbar_height + height;
		this.current_select.width = width;
		return height;
	},
	GetHeight : function(width) 
	{
		$("#embed_height").html(this.CalcHeight(width));
		this.GenerateCode();
	},

	//бредовый бред
	ChooseAspect : function(sender)
	{
		var prev_aspect_h = this.current_select.aspect.h;
		
		$(sender).addClass("embed_selected");
		$("#embed_aspect_" + this.current_select.aspect.h).removeClass("embed_selected");

		$("#embed_sizes_" + this.current_select.aspect.h).hide();				
		this.current_select.aspect.w = $(sender).attr("w");
		this.current_select.aspect.h = $(sender).attr("h");
		$("#embed_sizes_" + this.current_select.aspect.h).show();

		var size = $("#embed_sizes_" + this.current_select.aspect.h + " span:first-child").get();
		this.ChooseSize(size, prev_aspect_h);
	},
	ChooseSize : function(sender, prev_aspect_h)
	{
		$(sender).addClass("embed_selected");
		if (!prev_aspect_h) prev_aspect_h = this.current_select.aspect.h;
		$("#embed_sizes_" + prev_aspect_h + " span[w='"+ this.current_select.width +"']").removeClass("embed_selected");
		this.current_select.width = $(sender).attr("w");
		this.GenerateCode();
	},
	GenerateCode : function()
	{
		var height = this.CalcHeight(this.current_select.width);
		var code = '';
		if (this.current_select.type == "html")
		{
			code += '<object width="'+ this.current_select.width +'" height="'+ height +'" data="http://video.citysakh.ru/player.php" type="application/x-shockwave-flash">';
			code += '<param name="movie" value="http://video.citysakh.ru/player.php">';
			code += '<param name="allowfullscreen" value="true"><param name="flashvars" value="config=http://video.citysakh.ru/playerconfig.php?videoid='+ video_id +'">';
			code += '</object>';
		}
		$("#embed_code").val(code);
	}
}
