var baseUrl = "/web/flash/flv/";

$(function() {
	$f("mainPlayer", {src: "web/flash/flowplayer-3.0.7.swf", wmode: "transparent"}, {
		screen:{height:265 + "px", width: 401+"px"},
		canvas: {
			backgroundGradient: 'none'
		},
		clip: {
			baseUrl: baseUrl,
			onStart: function(clip){
				var clipItem = $("#playlist > li > ul > li[url='" + clip.url + "']");
				$.ajax({
					url: "mmVideoData.do",
					cache: false,
					data: "id=" + clipItem.attr("videoId"),
					async: false,
					success: function(msg) {
						$("#info_holder").html(msg);
						addthis.button("#video_share_link", addthis_config);
					}
				 }).responseText;
				$("#video_title").text(clipItem.find("span.name").text());

				// Setup the rating system
				$(".star_rating").mouseover(function() {
					for (x=parseInt($(this).attr("rating")); x>0; x--) {
						$(".star_rating[rating='" + x + "']").attr("src", "web/img/icons/full_star.gif");
					}
				});
				$(".star_rating").mouseout(function() {
					$(".star_rating").attr("src", "web/img/icons/empty_star.gif");
				});
				$(".star_rating").click(function() {
					$(".star_rating").unbind();
					$(".star_rating").css("cursor", "inherit");
					
					$.ajax({
						url: "mmVideoRating.do",
						cache: false,
						data: "id=" + clipItem.attr("videoId") + "&rating=" + $(this).attr("rating"),
						async: false
					 });
				});
			}
		},
		plugins: {
			controls:{
				backgroundGradient: 'high',
				backgroundColor: '#696969',
				timeColor: '#FFFFFF',
				progressColor: '#293800',
				bufferColor: '#424242',
				sliderColor: '#696969',
				buttonColor: '#333333',
				buttonOverColor: '#696969',
				all: false,
				play: true,
				stop: true,
				scrubber: true,
				time: true,
				mute: true,
				volume: true,
				fullscreen: true
			}
		}
	});

	$("#playlist > li > .video_header").click(function() {
		$(this).toggleClass("open");
		$(this).siblings("ul").slideToggle("fast");
	});
	
	$("#playlist > li > ul > li").click(function() {
		var url = document.location.toString().replace(/#.*/, "");

		// Add the anchor to the location
		document.location = url + "#" + $(this).attr("videoid");
		
		$("#playlist > li > ul > li").removeClass("active");
		$(this).addClass("active");
		$f("mainPlayer").play($(this).attr("url"));
		
		if (track_ajax) {
			pageTracker._trackPageview("/mmVideo.do?id=" + $(this).attr("videoid"));
		}
	});
	
	var id = document.location.toString().replace(/.*(\?id=|#)/, "");
	
	if (id.match(/\d*/)) {
		$("#playlist > li > ul > li[videoId='" + id + "']").addClass("active").parent().slideToggle("fast").siblings(".video_header").toggleClass("open");
		$f("mainPlayer").play($("#playlist > li > ul > li[videoId='" + id + "']").attr("url"));
	}
});

var openEmailDialog = function() {
	$("#senders_name").val("");
	$("#recipient_email").val("");
	$("#refer_message").val("");
	$("#recipient_name").val("");
	
	dialog.setWidth(340);
	dialog.setButtons({
		Cancel: function() {
			dialog.close();
		},
		Send: function() {
			var clipItem = $("#playlist > li > ul > li.active");
			
			var proceed = true;
			var updateValidator = $("#referral_form").validate({ 
			    rules: { 
					senders_name: {
						required: true,
						words: true,
						maxlength: 25
					}, 
					recipient_name: {
						required: true, 
						words: true,
						maxlength: 25
					}, 
					recipient_email: {
						required: true,
						email: true,
						maxlength: 100
					}, 
					refer_message: {
						required: true,
						words: true,
						maxlength: 200
					}
				},
			    messages: { 
					senders_name: "Your Name is Required", 
					recipient_name: "Your Friend's Name is Required",
					recipient_email: "A Valid Email Address is Required", 
					refer_message: "A Brief Message is Required"
			    },
			    // the errorPlacement has to take the table layout into account 
			    errorPlacement: function(error, element) { 
			    	// Position the error label under the input element
			    	element.parent().append( error );
			    	//error.appendTo( errorHolder ); 
			    }
			});
			
			if (updateValidator.form()) {
				$.ajax({
					url: "mmVideoReferral.do",
					cache: false,
					data: "id=" + clipItem.attr("videoId") + 
							"&senderName=" + $("#senders_name").val() +
							"&recipientEmail=" + $("#recipient_email").val() +
							"&message=" + $("#refer_message").val() +
							"&recipientName=" + $("#recipient_name").val(),
					async: false
				 });
				dialog.close();
			}
		}
	});
	
	dialog.open();
};