jQuery.fn.extend({
	"insertAtCaret": function(myValue) {
		this.each(function(i) {
			if (document.selection) {
				this.focus();
				sel = document.selection.createRange();
				sel.text = myValue;
				this.focus();
			} else if (this.selectionStart || this.selectionStart == '0') {
				var startPos = this.selectionStart;
				var endPos = this.selectionEnd;
				var scrollTop = this.scrollTop;
				this.value = this.value.substring(0, startPos)+myValue+this.value.substring(endPos,this.value.length);
				this.focus();
				this.selectionStart = startPos + myValue.length;
				this.selectionEnd = startPos + myValue.length;
				this.scrollTop = scrollTop;
			} else {
				this.value += myValue;
				this.focus();
			}
		});
	}
});

addlink = function(name, path) {
	$("#markdown").insertAtCaret("[" + name + "](" + path + ")");
};

successalert = function(message) {
	$("body").prepend("<div id=\"successalert\">" + message + "</div>");
	$("#successalert").hide().fadeIn("fast").delay(2000).fadeOut("slow", function() {
		$(this).remove();
	});
};

$(document).ready(function() {
	$("body").ajaxError(function(e, xhr, options, er) {
		alert("Ajax call failed with code " + xhr.status + " (" + xhr.statusText + "): " + xhr.responseText);
	});
	$("#closepreviewbutton").click(function() {
		$("#preview, #closepreviewbutton, #currenthtml").hide();
		$("#markdown, #previewbutton, #submitmarkdownbutton, #cancelmarkdownbutton, #editinghelp").show();
	});
	$("#cancelmarkdownbutton").click(function() {
		$("#preview, #submitmarkdownbutton, #previewbutton, #cancelmarkdownbutton, #markdown, #editinghelp").hide();
		$("#currenthtml, #closepreviewbutton").show();
		$("#markdown").val($("#currentmarkdown").html());
	});
	$(".editcaptionbutton").click(function() {
		var container = $(this).parents("dd:first");
		container.find(".canceleditcaptionbutton, input.caption, .savecaptionbutton").show();
		container.find("span.caption, .editcaptionbutton").hide();
	});
	$(".canceleditcaptionbutton").click(function() {
		var container = $(this).parents("dd:first");
		container.find("span.caption, .editcaptionbutton").show();
		container.find(".canceleditcaptionbutton, input.caption, .savecaptionbutton").hide();
		container.find("input.caption").val(container.find("span.caption").is(".hint") ? "" : container.find("span.caption").html());
	});
	$(".savecaptionbutton").click(function() {
		var container = $(this).parents("dd:first");
		jQuery.post("/editcaption", {"caption": container.find("input.caption").val(), "photo": container.parents("li:first").find("img:first").attr("src")}, function(data, textstatus, xhr) {
			var container = $("img").filter(function() {
				return $(this).attr("src") == data.photo;
			}).parents("li:first");
			if (data.caption.length == 0)
				container.find("span.caption").addClass("hint").html("None");
			else
				container.find("span.caption").removeClass("hint").html(data.caption);
			container.find("span.caption, .editcaptionbutton").show();
			container.find(".canceleditcaptionbutton, input.caption, .savecaptionbutton").hide();
			successalert("Successfully edited");
		});
	});
	$(".deletephotobutton").click(function() {
		if (!confirm("Are you sure you want to delete this photo?"))
			return;
		var container = $(this).parents("dd:first");
		jQuery.post("/deletephoto", {"photo": container.parents("li:first").find("img:first").attr("src")}, function(data, textstatus, xhr) {
			var container = $("img").filter(function() {
				return $(this).attr("src") == data.photo;
			}).parents("li:first").remove();
			successalert("Successfully deleted");
		});
	});

	// IE-specific
	if (jQuery.browser.msie) {
		if (jQuery.browser.version < 9) {
			$(".twocol, .threecol, .fourcol").css("width", "320px").columnize({width: 150});
		}
	}
});

