$(document).ready(function(){
	$(".embed")
		.children("label[for$=_embed], input[name$=_embed]")
			.click(function(){
				$(this).next("input[name$=_embed]").andSelf().select();
			})
			.end()
		.children("form")
			.children(".button")
				.hide()
				.end()
			.children("select")
				.change(function(){
					var selVideoEmbed = $(this).val()
					var embed = $(this).parent().prev("input");
					var embed_code = embed.val();
	
					embed_code = embed_code.replace(/width="\w+"/g, "width=\"" + selVideoEmbed.split("x")[0] + "\"");
					embed_code = embed_code.replace(/height="\w+"/g, "height=\"" + selVideoEmbed.split("x")[1] + "\"");
					
					embed.val(embed_code);
				});
	
	$(".rating")
		.css("cursor", "pointer")
		.children("form")
			.hide()
			.end()
		.children(".stars")
			.hover(function(){
				$(this)
					.children("span.star")
						.removeClass("star")
						.addClass("star_was");
						
				$(this)
					.children("span")
						.hover(function(){
							var index = $(this).prevAll("span").andSelf().length;
							 
							$(this)
								.prevAll("span")
								.andSelf()
								.addClass("star_new")
								.text("*");
								
							$("#frmVideoRating select option")
								.eq(index)
								.attr("selected", true);
						},function(){
							$(this)
								.removeClass("star_new")
								.text(".");
						});
			},function(){
				$(this)
					.children("span")
						.removeClass("star_new")
						.text(".")
						.end()
					.children("span.star_was")
						.removeClass("star_was")
						.addClass("star")
						.text("*");
						
				$("#frmVideoRating select option")
					.eq(0)
					.attr("selected", true);
			})
			.click(function(){
				var message = $(this).parent().children(".message");
				if (!message.hasClass("good")) {
					$(this).parent().children(".message").remove();
					message = $("<span class=\"message\">Rating...</span>").appendTo($(this).parent());
					
					var compath = $("#frmVideoRating input[name=hidComPath]", "#videolibrary").val();
					
					var url = compath + "/ajax.cfm?method=ajaxVideoRating&returnFormat=plain";
					
					$.post(url,
						$("#frmVideoRating").serialize(),
						function(data){
							if (data.substr(0,4) === "true") {
								message.addClass("good").text("Thank you!");
							} else {
								message.addClass("bad").text("There was a problem.");
							}
					});
				}
			});
});
