var Ajax;
if (Ajax && (Ajax != null)) {
	Ajax.Responders.register({
		onCreate: function() {
			if ($('spinner') && Ajax.activeRequestCount > 0)
				Effect.Appear('spinner', {duration:0.5,queue:'end'});
		},
		onComplete: function() {
			if ($('spinner') && Ajax.activeRequestCount == 0)
				Effect.Fade('spinner', {duration:0.5,queue:'end'});
		}
	});
}

if (document.images) {
	pic1 = new Image(5, 5);
	pic1.src = "/images/tab-left-hover.png";

	pic2 = new Image(5, 5);
	pic2.src = "/images/tab-right-hover.png";

	pic3 = new Image(5, 5);
	pic3.src = "/images/tab-left-active.png";

	pic4 = new Image(5, 5);
	pic4.src = "/images/tab-right-active.png";
}

function confirmAndRedirect(prompt, url) {
	var confirmed = confirm(prompt);

	if (confirmed) {
		window.location = url;
	}
}

function markCheckboxes(query, mark) {
	query.find("input[type=checkbox]").each(function() {
		$(this).attr("checked", mark);
	})
}

function deselectAll(theForm) {
	for (i = 0; i < theForm.elements.length; i++) {
		if (theForm.elements[i].type == "checkbox") {
			theForm.elements[i].checked = false;
		}
	}
}

function selectAll(theForm) {
	for (i = 0; i < theForm.elements.length; i++) {
		if (theForm.elements[i].type == "checkbox") {
			theForm.elements[i].checked = true;
		}
	}
}

function selectUninvited(theForm) {
	deselectAll(theForm);
	for (i = 0; i < theForm.elements.length; i++) {
		if (theForm.elements[i].type == "checkbox" && theForm.elements[i].className == "uninvited") {
			theForm.elements[i].checked = true;
		}
	}
}

function selectRsvpByResponse(theForm, response) {
	deselectAll(theForm);
	for (i = 0; i < theForm.elements.length; i++) {
		if (theForm.elements[i].type == "checkbox" && theForm.elements[i].className == ('rsvp_' + response)) {
			theForm.elements[i].checked = true;
		}
	}
}

function logToConsole(data) {
	$("#console").text(data);
}

function logHTMLToConsole(data) {
	$("#console").html(data);
}

function setSuccessMessage(message) {
	if ($("#successMessage").size() > 0) {
		$("#successMessage").html(message);
	}
	else {
		$("#messages").append("<div id='successMessage'>" + message + "</div>");
	}
}

function setFailMessage(message) {
	if ($("#failMessage").size() > 0) {
		$("#failMessage").html(message);
	}
	else {
		$("#messages").append("<div id='failMessage'>" + message + "</div>");
	}
}

function localizeDates() {
	$("span.utcDate").each(function() {
		var outputFormat = $(this).attr("title");

		if (outputFormat) {
			var utcDate = Date.parseDate($(this).text(), Date.patterns.ISO8601Long);

			var offsetMillis = utcDate.getTimezoneOffset() * -60 * 1000;

			var localDate = new Date();
			localDate.setTime(utcDate.getTime() + offsetMillis);
			$(this).text(localDate.dateFormat(outputFormat));
			$(this).removeAttr("title");
		}
	});
}

function addDatePicker(changeMonthYear) {
	$(document).ready(function() {
		if (changeMonthYear) {
			$("input[type='text'].datePicker").datepicker({yearRange: '1900:' + (new Date().getYear() + 1901),
				changeMonth: true,
				changeYear: true
			});
		}
		else {
			$("input[type='text'].datePicker").datepicker({yearRange: '1900:' + (new Date().getYear() + 1901)});
		}
	});
}

/*
 * Image preview script
 * powered by jQuery (http://www.jquery.com)
 *
 * written by Alen Grakalic (http://cssglobe.com)
 *
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */

this.imagePreview = function(){
	/* CONFIG */

		xOffset = 10;
		yOffset = 30;

		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result

	/* END CONFIG */
	$("a.preview").hover(function(e){
		this.t = this.title;
		this.title = "";
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
    },
	function(){
		this.title = this.t;
		$("#preview").remove();
    });
	$("a.preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
};

/*
 * Tooltip script
 * powered by jQuery (http://www.jquery.com)
 *
 * written by Alen Grakalic (http://cssglobe.com)
 *
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */
/*this.tooltip = function() {
	*//* CONFIG *//*
		xOffset = 10;
		yOffset = 20;
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
	*//* END CONFIG *//*
	$(".tooltip").hover(function(e) {
		this.t = this.title;
		this.title = "";
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");

		var ttWidth = $("#tooltip").width();

		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
    },
	function(){
		this.title = this.t;
		$("#tooltip").remove();
    });
	$(".tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
};*/

/**
 * Insert content at caret position (converted to jquery function)
 * @link
 http://alexking.org/blog/2003/06/02/inserting-at-the-cursor-using-javascript
 */
$.fn.insertAtCaret = function (myValue) {
	return this.each(function() {
		//IE support
		if (document.selection) {
			this.focus();
			sel = document.selection.createRange();
			sel.text = myValue;
			this.focus();
		}
			//MOZILLA/NETSCAPE support
		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();
		}
	});

};


function addSlideHeader() {
	$(".slideHeader").click(function() {
		// do the google analytics event tracking
		if (pageTracker != null) {
			if ($(this).hasClass("slideHeaderExpanded")) {
				pageTracker._trackEvent('Progressive Disclosure', 'Hide', $(this).attr("title"));
			}
			else {
				pageTracker._trackEvent('Progressive Disclosure', 'Show', $(this).attr("title"));
			}
		}

		var slhdr = $(this);

		// do the toggling
		$(this).toggleClass("slideHeaderExpanded");
		slhdr.trigger("slideHeaderStart", $(this).next().height());
		$(this).next().slideToggle("fast", function() {
			slhdr.trigger("slideHeaderFinish");
		});


	})
	$(".slideHeader").each(function() {
		if (!$(this).hasClass("slideHeaderExpanded")) {
			$(this).next().css("display", "none");
		}
	})
}

$(document).ready(function() {
	localizeDates();
	imagePreview();
	$(".tooltip").tooltip({showURL: false});
	addSlideHeader();
});