var rowHeight = 0;

$(document).ready(function() {
	// Update the clocks
	setInterval(update_clocks, 1000);
	
	// Set the hover effect for the search button
	$("#SearchButton").bind("mouseover", function() { this.src = this.src.replace('.gif', '_hover.gif'); }).bind("mouseout", function() { this.src = this.src.replace('_hover.gif', '.gif'); });
	
	// Set the hover effect and click action for the main menu items
	$("#sidebar #menu ul li.lvl1").css("cursor", "pointer").bind("mouseover mousemove", function() {
		if ($(this).attr("id") == "active_menuitem") return;
		$(this).css("background-position", '-386px 0px');
	}).bind("mouseout", function() {
		if ($(this).attr("id") == "active_menuitem") return;
		$(this).css("background-position", '0px 0px');
	}).bind("click", function(e) {
		if (!e) e = window.event;
		if (e.target.nodeName == "DIV" || e.target.nodeName == "LI") {
			window.location = $(e.target).children("a:first").attr("href");
		}
	});
	
	// Set the hover effect and click action for the submenu items
	$("#sidebar #menu ul li.lvl2").css("cursor", "pointer").bind("mouseover mousemove", function() {
		if ($(this).attr("id") == "active_menuitem") return;
		$(this).css("background-position", '-386px 0px');
	}).bind("mouseout", function() {
		if ($(this).attr("id") == "active_menuitem") return;
		$(this).css("background-position", '0px 0px');
	}).bind("click", function(e) {
		if (!e) e = window.event;
		if (e.target.nodeName == "DIV" || e.target.nodeName == "LI") {
			window.location = $(e.target).children("a:first").attr("href");
		}
	});
	
	// Set the hover effect and click action for the third-level submenu items
	$("#sidebar #menu ul li.lvl3").css("cursor", "pointer").bind("mouseover mousemove", function() {
		if ($(this).attr("id") == "active_menuitem") return;
		$(this).css("background-position", '-386px 0px');
	}).bind("mouseout", function() {
		if ($(this).attr("id") == "active_menuitem") return;
		$(this).css("background-position", '0px 0px');
	}).bind("click", function(e) {
		if (!e) e = window.event;
		if (e.target.nodeName == "DIV" || e.target.nodeName == "LI") {
			window.location = $(e.target).children("a:first").attr("href");
		} else if (e.target.nodeName == "SPAN") {
			window.location = $(e.target).parent().children("a:first").attr("href");
		}
	});
	
	// Hide the second and third-level submenu items
	$("#sidebar #menu ul li.lvl2").css("display", "none");
	$("#sidebar #menu ul li.lvl3").css("display", "none");
	
	// Set the click action to toggle the display for main menu items
	$("#sidebar #menu ul li.lvl1 span").bind("click", function() {
		$("#sidebar #menu ul li").each(function() {
			if ($(this).attr("id") == "active_menuitem") return;
			$(this).css("background-position", "0px 0px");
		});
		if (escape($(this).html()) == "%BB") { // %BB = &raquo;
			$("#sidebar #menu ul li.parent_" + this.id.replace("item_", "")).css("display", "");
			$(this).html("&laquo;");
		} else if (escape($(this).html()) == "%AB") { // %AB = &laquo;
			$("#sidebar #menu ul li.parent_" + this.id.replace("item_", "")).css("display", "none");
			$(this).html("&raquo;");
			
			$("#sidebar #menu ul li.parent_" + this.id.replace("item_", "") + " span").each(function(oLi) {
				$("#sidebar #menu ul li.parent_" + this.id.replace("item_", "")).css("display", "none");
				
				if (escape($(this).html()) == "%AB") { // %AB = &laquo;
					$(this).html("&raquo;");
				}
			});
		}
	});
	
	// Set the click action to toggle the display for submenu items
	$("#sidebar #menu ul li.lvl2 span").bind("click", function() {
		$("#sidebar #menu ul li").each(function() {
			if ($(this).attr("id") == "active_menuitem") return;
			$(this).css("background-position", "0px 0px");
		});
		if (escape($(this).html()) == "%BB") { // %BB = &raquo;
			$("#sidebar #menu ul li.parent_" + this.id.replace("item_", "")).css("display", "");
			$(this).html("&laquo;");
		} else if (escape($(this).html()) == "%AB") { // %AB = &laquo;
			$("#sidebar #menu ul li.parent_" + this.id.replace("item_", "")).css("display", "none");
			$(this).html("&raquo;");
		}
	});
	
	// Get the active menu item
	var oActive;
	if (oActive = $("#active_menuitem")) {
		if ($(oActive).hasClass('lvl1')) {
			$(oActive).css("background-position", "-386px 0px");
			$(oActive).children().children("span").trigger("click");
		} else if ($(oActive).hasClass('lvl2')) {
			var parent = $(oActive).attr('className').replace("lvl2 parent_", "");
			$("span#item_" + parent).trigger("click");//.parent().parent().css("background-position", "-386px 0px");
			$(oActive).css("background-position", "-386px 0px");
			$(oActive).children().children("span").trigger("click");
		} else if ($(oActive).hasClass('lvl3')) {
			var parent = $(oActive).attr('className').replace("lvl3 parent_", "");
			parent = $("span#item_" + parent).parent().parent().attr("className").replace("lvl2 parent_", "");
			$("span#item_" + parent).trigger("click");
			parent = $(oActive).attr('className').replace("lvl3 parent_", "");
			$("span#item_" + parent).trigger("click");//.parent().parent().css("background-position", "-386px 0px");
			$("span#item_" + $("span#item_" + parent).parent().parent().attr("className").replace("lvl2 parent_", ""));//.parent().parent().css("background-position", "-386px 0px");
			$(oActive).css("background-position", "-386px 0px");
		}
	}
	
	// Fix the message row heights
	$("table#messages tr").each(function() {
		rowHeight = $(this).height();
		
		$(this).children("td").children(".m1fill, .m2fill, .m3fill, .m4fill").each(function() {
			$(this).height(rowHeight);
		});
	});
	
	// Set the hover effect for the conversion and form submit buttons
	$("input.convert").bind("mouseover", function() { $(this).css("background-position", "0px -29px"); }).bind("mouseout", function() { $(this).css("background-position", "0px 0px"); });
	$("input.schedule").bind("mouseover", function() { $(this).css("background-position", "0px -29px"); }).bind("mouseout", function() { $(this).css("background-position", "0px 0px"); });

	// Set the keypress action for conversion fields	
	$("table.convertor input").bind("keyup", function(e) {
		if (!e) e = window.event;
		var keyCode = (e.which) ? e.which : e.keyCode;

		if (
		    (keyCode >= 48 && keyCode <= 57)  // 0-9
		 || (keyCode >= 96 && keyCode <= 105) // numlock 0-9
		 || keyCode == 190                    // .
		 || keyCode == 110                    // numlock .
		 || keyCode == 46                     // delete
		 || keyCode == 8)                     // backspace
		{
			lastmodified = $(this).attr("id");
			convert();
		}
	});
	
	// Set the hover and click effects for the booking table rows
	$("table#booking tbody tr").bind("mouseover", function() { $(this).children().each(function() { $(this).css("background-position", "left top"); $(this).css("color", "#ffffff"); $(this).css("cursor", "pointer"); }); $(this).children().children().css("color", "#ffffff"); }).bind("mouseout", function() { $(this).children().each(function() { $(this).css("background-position", "left 50px"); $(this).css("color", "#000000"); }); $(this).children().children().css("color", "#0033cc"); }).bind("click", function() { window.location.href = $(this).children().children().attr("href"); });
	
	// Set the clearing effect on the search field
	$("#SearchInput").bind("focus", function() {
		$(this).attr("value", "");
	});
	
	// Hide dependent forms
	var depForm = $("#dependentForm");
	if (depForm) {
		var rel = depForm.attr("rel");
		
		if ($("#" + rel + "N").attr("checked") == true)
		{
			depForm.css("display", "none");
		}
		
		$("#" + rel + "Y").bind("click change", function() {
			if ($(this).attr("checked") == true) {
				$("#dependentForm").css("display", "block");
			}
		});
		
		$("#" + rel + "N").bind("click change", function() {
			if ($(this).attr("checked") == true) {
				$("#dependentForm").css("display", "none");
			}
		});
	}
});

function update_clocks() {
	// Loop through all clocks on the page
	$(".clock").each(function() {
		// Get the properties: [12:34:56, 12] or [23:45:01, 24]
		var properties = this.id.split(" ");
		
		// Split the hours, minutes and seconds [12, 34, 56]
		var clock = properties[0].split(":");
		clock[0] = parseInt(clock[0], 10);
		clock[1] = parseInt(clock[1], 10);
		clock[2] = parseInt(clock[2], 10);
		
		// Add one second
		clock[2]++;
		
		// If we are now at the 60th second, reset the seconds and add a minute
		if (clock[2] == 60) {
			clock[2] = 0;
			clock[1]++;
		}
		
		// If we are now at the 60th minute, reset the minutes and add an hour
		if (clock[1] == 60) {
			clock[1] = 0;
			clock[0]++;
		}
		
		// If we are now at the 24th hour, reset the hours
		if (clock[0] == 24) {
			clock[0] = 0;
		}
		
		// Build the <span>'s new ID: "12:34:57 12"
		var id = "";
		if (clock[0] < 10) id = id + "0";
		id = id + clock[0] + ":";
		if (clock[1] < 10) id = id + "0";
		id = id + clock[1] + ":";
		if (clock[2] < 10) id = id + "0";
		id = id + clock[2] + " " + properties[1];
		
		// Create an empty string to hold the AM/PM notification, or nothing
		// in the case of a 24-hour clock
		clock[3] = "";
		
		// If this is a 12-hour clock, determine AM/PM notification
		if (properties[1] == "12")
		{
			// Default to PM, set to AM if it's before noon
			clock[3] = " PM";
			if (clock[0] < 12) clock[3] = " AM";
			
			// If the hour displays as 0, set it to 12
			if (clock[0] == 0) clock[0] = 12;
			
			// If we're past noon, subtract 12 hours (i.e. 14:00 becomes 2:00)
			if (clock[0] > 12) clock[0] -= 12;
		}
		
		// Build the time display string: "12:34 AM" or "0:34"
		var time = clock[0] + ":";
		if (clock[1] < 10) time = time + "0";
		time = time + clock[1] + clock[3];
		
		// Set the new ID for the next iteration
		this.id = id;
		
		// Set the clock display time as the <span>'s content
		this.innerHTML = time;
	});
}

