if (typeof RML == "undefined") RML = new Object();
if (typeof RML.create == "undefined") RML.create = new Object();

RML.init_create = function ()
{
	$("#design-our-poster").bind("click", RML.create.open_poster);
	$("#be-a-pundit").bind("click", RML.create.open_pundit);
	
	$("#submit-a-url").bind("click", function () {
		$("#vcc-form1").css("display", "none");
		$("#vcc-form5").css("display", "block");
		
		$("#thefile").removeClass("required");
		$("#the-url").addClass("required");
		
		$("#message-done").css("display", "none");
		
		return false;
	});
	
	$("#submit-a-file").bind("click", function () {
		$("#vcc-form1").css("display", "block");
		$("#vcc-form5").css("display", "none");
		
		$("#thefile").addClass("required");
		$("#the-url").removeClass("required");
		
		$("#message-done").css("display", "none");
		return false;
	});
	
	$("#the-url-submit").bind("click", function () {
		var url = RML.params.template_directory + "/json.check-url.php";
		var postdata = "the-url=" + $("#the-url").val();
		
		$("#message").html("Please wait.").css("display", "block");
		
		$.post(url, postdata, function (data) {
			var info = eval("(" + data + ")");
			
			$("#url").attr("value", info.url);
			$("#type").attr("value", info.content_type);
			
			if (info.http_code != 200) {
				$("#message").html("Bad URL.").css("display", "block");
			} else {
				$("#vcc-form5").css("display", "none");
				$("#vcc-form1").css("display", "none");
				$("#vcc-form2").css("display", "block");
				$("#vcc-form3").css("display", "block");
				$("#vcc-form4").css("display", "block");
				$("#message").html("").css("display", "none");
				
				if (/^image\//.test(info.content_type)) {
					$("#attachment-note").css("display", "none");
					$("#attachment-label").css("display", "none");
					$("#attachment").css("display", "none");
				} else if (/^text\/html/.test(info.content_type) && /^http\:\/\/www\.youtube\.com\/watch\?v\=/.test(info.url)) {
					$("#attachment-note").css("display", "none");
					$("#attachment-label").css("display", "none");
					$("#attachment").css("display", "none");
				}
			}

			return false;
		});
		
		return false;
	});
	
	$("#thefile").bind("change", function () {
		var filename = $(this).val();
		var extension = filename.slice(-3).toLowerCase();
		
		switch (extension) {
			case "jpg": case "gif": case "png":
				$("#attachment-note").css("display", "none");
				$("#attachment-label").css("display", "none");
				$("#attachment").css("display", "none");
				// fallthrough
			case "pdf":
			case "mp3":
				$("#attachment-note").css("display", "none");
				$("#attachment-label").css("display", "none");
				$("#attachment").css("display", "none");
				$("#vcc-form5").css("display", "none");
	
				$("#vcc-form1").css("display", "none");
				$("#vcc-form2").css("display", "block");
				$("#vcc-form3").css("display", "block");
				$("#vcc-form4").css("display", "block");
				$("#message").html("").css("display", "none");
				break;			
			default:
				$("#thefile").val("");
				$("#message").html("Unsupported file type.").css("display", "block");
				break;
		}
	});
	
	$("#original").bind("change", function () {
		var val = $(this).fieldValue()[0];
		
		if (val == undefined) {
			$("#submit-button").css("display", "none");
		} else {
			$("#submit-button").css("display", "block");
		}
	});
	
	$("#vcc-form").attr("action", RML.params.template_directory + "/json.create.php");
	RML.create.options.url = RML.params.template_directory + "/json.create.php";
	
	$("#vcc-form").validate({
		submitHandler: function (form) {
			$(form).ajaxSubmit(RML.create.options); 
			
			$("#vcc-form").css("display", "none");
			$("#message").html("Submitting").css("display", "block");
	
			return false;
		}
	});

	$("#vcc-form").resetForm();

	$("#thefile").val("");
	
	
	if ($.getURLParam("poster") == null && $.getURLParam("pundit") == null) {
		$("#contest").attr("checked", "");
	} else {
		$("#contest").attr("checked", "");
	}
	
	if ($.getURLParam("poster") == null && $.getURLParam("pundit") == null) {
		$("#vcc-form1").css("display", "block");
	} else {
		$("#attachment-note").css("display", "none");
		$("#attachment-label").css("display", "none");
		$("#attachment").css("display", "none");
		$("#vcc-form5").css("display", "none");

		$("#vcc-form1").css("display", "none");
		$("#vcc-form2").css("display", "block");
		$("#vcc-form3").css("display", "block");
		$("#vcc-form4").css("display", "block");
		$("#message").html("").css("display", "none");
	}
	
	if ($.getURLParam("poster") != null || $.getURLParam("pundit") != null) {
		$.scrollTo("#vcc-form");
	} else {
		var poster = $.cookie('rml_open_poster');		
		var pundit = $.cookie('rml_open_pundit');		
		
		if (poster != null && poster == "true") {
			$("#design-our-poster").trigger("click");
		} else if (pundit != null && pundit == "true") {
			$("#be-a-pundit").trigger("click");
		}
	}
}

RML.create.beforeSubmit = function (formData, jqForm, options) {
	console.log("RML.create.beforeSubmit()", formData, jqForm, options);
}

RML.create.success = function (responseText, statusText) {
	console.log("RML.create.success()", responseText, statusText);
	$("#vcc-form1").css("display", "block");
	$("#vcc-form2").css("display", "none");
	$("#vcc-form3").css("display", "none");
	$("#vcc-form4").css("display", "none");
	$("#vcc-form").css("display", "block");
	$("#thefile").val("");
	$("#vcc-form").clearForm();
	$("#contest").attr("checked", "");
	$("#message").html("").css("display", "none");
	$("#message-done").css("display", "block");
}

RML.create.options = { 
	target:        "#nothing",
	beforeSubmit:  RML.create.before,
	success:       RML.create.success,
	type:      "post",
	dataType:  "json",
	resetForm: true
}; 

RML.create.open_poster = function ()
{
	console.log("RML.create.open_poster()");
	
	$.cookie('rml_open_poster', 'false', { path: '/', domain: RML.params.domain });		
	RML.create.open_activity("poster/poster_activity.swf");
	
	return false;
}

RML.create.open_pundit = function ()
{
	console.log("RML.create.open_pundit()");

	$.cookie('rml_open_pundit', 'false', { path: '/', domain: RML.params.domain });		
	RML.create.open_activity("pundit/pundit_activity.swf");
	
	return false;
}

RML.create.open_activity = function (filename)
{
	console.log("RML.create.open_activity()", filename);

	var $activity = $("body").createAppend("div", { "id": "activity" });
	var $activity_swf = $activity.createAppend("div", { "id": "activity-swf" });

	$activity.css("height", $(document).height());
	$activity.bind("click", function() {
//		RML.create.close_activity();
		return false;
	});

	if (swfobject.hasFlashPlayerVersion("6.0.65")) {
		var expressInstall = RML.params.template_directory + "/_swf/expressInstall.swf";
		var flashvars = { };
		var params = { menu: "false", scale: "noscale" };
		var attributes =  { };
		swfobject.embedSWF(RML.params.template_directory + "/swf/" + filename, "activity-swf", "980", "640", "9", expressInstall, flashvars, params, attributes);
	}
	
	$.scrollTo("#wrap", { duration: 250 });
}
	

RML.create.close_activity = function ()
{
	console.log("RML.create.close_activity()");
	
	$("#activity").remove();
	
	return false;
}
