// TODO Remove me.
if(!console) var console = {};
if(!console.log) console.log = function(a){};

$(function() {
	// Add placeholders to mailing list subscription form and search box
	$("#jikjji-jikjji").placeholder("placeholder");	
	var q = $("#search_query");
	q.placeholder("placeholder");
	$("form.filtering").submit(function() {
		if(q.val()==q.attr("title")) {
			q.val('');
			return true;
		}
	});
});

$(function() {
	var menu_buttons = $("#navigation li a");
	menu_buttons.click(function() {
		menu_buttons.removeClass("selected");
		$(this).addClass("selected");
	});
	var acct_buttons = $("#account_navigation li a");
	acct_buttons.click(function() {
		acct_buttons.removeClass("selected");
		$(this).addClass("selected");
	});
});

$(function() {
	
	var is_producer = $("#id_is_producer");
	var billing_information = $("#billing_information");
	var fields = $("input,select",billing_information);
	if(!is_producer.attr("checked")) {
		billing_information.hide();
	}
	is_producer.change(function() {
		if(is_producer.attr("checked")) {
			billing_information.queue([]).show();
		} else {
			billing_information.queue([]).hide();
		}
	});
});

$(function() {
	// Javascript detection for support form
	$("body.support.dialog form #id_has_js").val("true");
});


// TODO make all upload code a jQuery extension

var clr = null;
var clr2 = null;
var bar = null;
var bar2 = null;
var status1 = null;
var status2 = null;
var su_form;
var tu_form;
var file;
var file2;
var removelink;
var removelink2;
var outerbar;
var outerbar2;

/*var ajax_uplader = function(form) {
	this.is_uploading = false;
	this.form = form;
};*/


function upload_error(kind) {
	if(kind == "screenplay") {
		$("#id_document").val("");
		su_form[0].reset();
		outerbar.hide();
		removelink.hide();
		bar.css('width','0%');
		clr = false;
		file.show();
		alert("There was an error while uploading your screenplay. Are you sure it was a PDF?");
	} else {
		$("#id_treatment").val("");
		tu_form[0].reset();
		outerbar2.hide();
		removelink2.hide();
		bar2.css('width','0%');
		clr2 = false;
		file2.show();
		alert("There was an error while uploading your 5 page treatment. Are you sure it was a PDF with 5 pages or less?");
	}
}

function complete_upload(id, kind) {
	if(kind == "screenplay") {
		$("#id_document").val(id);
		removelink.show();
		outerbar.show();
		file.hide();
		bar.hide();
		$(".progress_bar", outerbar).addClass('complete');
		$(".filename", outerbar).html("Uploaded file");
		
		removelink.unbind('click').click(function() {
			$("#id_document").val("");
			su_form[0].reset();
		 	outerbar.hide();
			removelink.hide();
			bar.css("width", "0%");
			file.show();
			return false;
		});
		clr = false;
	} else {
		$("#id_treatment").val(id);
		removelink2.show();
		bar2.hide();
		outerbar2.show();
		file2.hide();
		bar.hide();
		$(".progress_bar", outerbar2).addClass('complete');
		$(".filename", outerbar2).html("Uploaded file");
		
		removelink2.unbind('click').click(function() {
			$("#id_treatment").val("");
			tu_form[0].reset();
		 	outerbar2.hide();
			removelink2.hide();
			bar2.css("width", "0%");
			file2.show();
			return false;
		});
		clr2 = false;
	}
};

function check_upload(key) {
	if(clr) {
		$.ajax({
			type: "GET", cache: false, dataType: "json",
			url: "/document/progress/"+ key + "/",
			success: function(data) {
				console.log(data);
				if(data) {
					var width = Math.round(data.progress * 100) + "%";
					bar.css("width", width);
				}
			},
			complete: function() {
				setTimeout(function() {check_upload(key);}, 1500);
			}
		});
	}
};

function check_upload2(key) {
	if(clr2) {
		$.ajax({
			type: "GET", cache: false, dataType: "json",
			url: "/document/progress/"+ key + "/",
			success: function(data) {
				console.log(data);
				if(data) {
					var width = Math.round(data.progress * 100) + "%";
					bar2.css("width", width);
				}
			},
			complete: function() {
				setTimeout(function() {check_upload2(key);}, 1500);
			}
		});
	}
};

function make_progress_bar() {
	return $('<div class="upload_progress"><div class="bar"></div><div class="status">Uploading... <a href="#" class="cancel">Cancel</a></div></div>');
}

$(function() {
	
	// Fade out notifications
	$('#notifications .success, #notifications .error').click(function() {
		$(this).fadeOut(100);
	}).addClass('clickable');
	
	if($("body.dashboard")) {
		// We're on the dashboard page
		
		su_form = $("form#screenplay_upload");
		file = $("input[type=file]", su_form);
		removelink = $('a.remove',su_form);
		outerbar = $('span.document',su_form);
		bar = $(".value", outerbar);
		
		file.change(function() { su_form.submit(); });
		if(su_form.length > 0) {
			su_form.submit(function() {
				file.hide();
				outerbar.show();
				bar.show().css('width','0%');
				removelink.show();
				bar.css('width','0%');
				$(".filename", outerbar).html("Uploading file...");
				$(".progress_bar", outerbar).removeClass('complete');
				
				removelink.unbind('click');
				removelink.click(function() {
					$("#"+su_form.attr('target')).attr('src','about:blank');
					outerbar.hide();
					removelink.hide();
					bar.css('width','0%');
					file.show();
					clr = false;
					su_form[0].reset();
					return false;
				});
				
				clr = true;
				setTimeout(function() {
					var action = su_form.attr("action").split("/");
					var key = action[action.length-2];
					check_upload(key);
				}, 1000);
				return true;
			});
		}


		tu_form = $("form#treatment_upload");
		file2 = $("input[type=file]", tu_form);
		removelink2 = $('a.remove',tu_form);
		outerbar2 = $('span.document',tu_form);
		bar2 = $(".value", outerbar2);
		file2.change(function() { tu_form.submit(); });
		if(tu_form.length > 0) {
			tu_form.submit(function() {
				file2.hide();
				outerbar2.show();
				bar2.show().css('width','0%');;
				removelink2.show();
				$(".filename", outerbar2).html("Uploading file...");
				$(".progress_bar", outerbar2).removeClass('complete');
				
				removelink2.unbind('click');
				removelink2.click(function() {
					$("#"+tu_form.attr('target')).attr('src','about:blank');
					outerbar2.hide();
					removelink2.hide();
					bar2.css('width','0%');
					file2.show();
					clr2 = false;
					tu_form[0].reset();
					return false;
				});
				
				clr2 = true;
				setTimeout(function() {
					var action = tu_form.attr("action").split("/");
					var key = action[action.length-2];
					check_upload2(key);
				}, 1000);
				return true;
			});
		}


		var sa_form = $("#screenplay_add");
		if(sa_form.length > 0) {
			sa_form.submit(function() {
				if($("#id_document").val() == "") {
					alert("You need to upload a screenplay!");
					return false;
				}
				return true;
			});
		}
	}
	
	var doc = $("#id_document");
	if(doc.val() != "") {
		complete_upload(doc.val(),'screenplay');
	}
	
	var treat = $("#id_treatment");
	if(treat.val() != "") {
		complete_upload(treat.val(),'treatment');
	}	
	
	var gtags = $("#id_genre_tags");
	if(gtags.length > 0) {
		gtags.fccomplete();
	}
	
	$("#id_starring_actor_type option[value='']").text(''); // TODO Fix this hack on the server-side
});

$(function() {        
 	$(".hasfocus").focus();
});