
$(document).ready(function() {
	if (is_mobile())
		return;
	attach_behavior();	

	$("textarea").focus();
	$("#url_register").attr("checked", false);
	
	// jQuery UI
	
	$("button").button();

	// Hide infoboxes if empty
	
	if (!$(".error").html())
		$(".error").hide();
	if (!$(".notice").html())
		$(".notice").hide();
	
	// Recall form contents in case of error
	
	if (is_error()) {
		urlp_fill();
		if ($("#url_register").attr("checked")) {
			$("#url_register").change().change();
		}
	}
	
	// Clipboard handling
	
	if ($("#url_copy").length) {
		ZeroClipboard.setMoviePath("ui/js/zeroclipboard.swf");
		
		var clip = new ZeroClipboard.Client();
		var text = "";
		var copymsg;
		var lcount = 0;
		
		$(".url_copy_link").each(function() {
			text += $(this).attr("href") + "\r\n";
			lcount++;
		});
		clip.setText(text);
		
		clip.setHandCursor(true);
		clip.glue("url_copy");
		clip.addEventListener("onComplete", function(client, text) {
			lcount > 1 ?
				msg("Your links have been copied to the clipboard!") :
				msg("Your link <a href='" + text + "'>" + text + "</a> has been copied to the clipboard!");
		});		
	}	
});

function attach_behavior() {

	// Main page behavior

	$("#menu_forgot").click(function() {
		$("#dialog_forgot").modal({
			maxWidth : 800
		});
	});

	$("#auth_login").click(function() {		
		$("#form_auth").submit();
	});

	$("#url_entry textarea").keypress(function(e) {		
		if (e.keyCode == '13') {
			e.preventDefault();
			$("#url_button").click();
		}
	});
	
	$("#url_button")
	.click(function() {
		if ($("textarea").val())
			$("#form_url").submit();
	})
	.hover(
		function() {			
			$("#url_entry").css("background-color", "#83FF64"); },
		function() {			
			$("#url_entry").css("background-color", "#78EA5C"); }
	);

	$("#url_register").change(function() {
		if (this.checked)
			$("#url_email").show();
		else
			$("#url_email").hide();
	});
	
	$("#url_email")
	.focus(function() {
		if (this.value == "Enter your e-mail address") this.value = ""; })
	.blur(function() {
		if (this.value == "") this.value = "Enter your e-mail address"; });
	
	// Link management behavior
	
	$("#mylinks tbody td").click(function() {		
		var tr = $(this).parent();	
		var td = tr.find(".mylinks-url").filter(".editable");
		var alias = tr.find("td a").get(0).innerHTML;
		var div = td.find("div");
		var ie = div.length; // is IE?	
		
		if (ie)
			td.html(div.html());
			
		td
			.removeClass("editable")
			.wrapInner(
				"<textarea name='url_" + alias + "' rows='5' " +
				"style='width:" + (ie ? "600px" : "100%") + ";'></textarea>"
			)
			.css("overflow", "visible");
	});
	
	$("#mylinks tbody a").click(function(e) {
		e.stopPropagation();
	});
	
	$("#manage_submit").click(function() {
		var has_err	= false;	
		$("textarea").each(function() {		
			if (!this.value.match(/^(https?|ftps?):\/\/.+/)) {
				$(this)
					.css("background-color", "#f88")					
					.focus(function() {
						$(this).css("background-color", "white")
					});
				has_err = true;
			}						
		});
		
		if (has_err) {
			err("Please fix the indicated fields - URLs must be properly formed.")
		}
		else {		
			$("#manage_form").submit();
		}		
	});	
}

function err(msg) {	
	$(".error")		
		.html(msg)
		.fadeOut().fadeIn();
}

function msg(msg) {
	$(".notice")		
		.html(msg)
		.fadeOut().fadeIn();
}

function is_error() {
	
	// Return true if the error message is
	// non-empty.
	
	var errstr = $(".error").html() || "";	
	return (errstr.replace(/(^\s+|\s+$)/gi, "") ? true : false);
}

function is_mobile() {
	return ($(".m").filter(":visible").length ? true : false);
}

function urlp_get(tag) {
	return __urlp[tag];
}

function urlp_foreach(fn) {

	// Iterate over all URL parameters, executing a
	// function taking two arguments (key and value)
	// for each. Exit loop if function returns false.

	var url = __urlp;
	$.each(url, function(k, v) {
		if (fn.apply(null, [k, v]) == false) return false;
	});	
}

function urlp_fill() {
	
	// Fill in form parameters from __urlp,
	// except "action".
	
	urlp_foreach(function(k, v) {
		if (k == "action") return true;
		$("input[name=" + k + "],textarea[name=" + k + "]").val(v);
		$("input[name=" + k + "]").filter("[type=checkbox]").attr("checked", true);
	});	
}


