var titles = new Array();
var startDates = new Array();
var endDates = new Array();
var locations = new Array();
var buildings = new Array();
var rooms = new Array();
var descriptions = new Array();
var tips = Array(); // Store tooltips that have been created.
var currentTipID = null;

$.extend($.datepicker,{_checkOffset:function(inst,offset,isFixed){return offset}});

$(document).ready(function()
{
	// alert("Hello world");
	var sdContainer = $("#start-date");
	var edContainer = $("#end-date");
	var sdIsHidden = true;
	var edIsHidden = true;

	sdContainer.datepicker
	({
		changeMonth: true,
		changeYear: true,
		beforeShow: function(input, inst)
		{
			sdIsHidden = false;
			// sdContainer.get(0).scrollIntoView(true);
		},
		onClose: function(dateText, inst)
		{
			sdIsHidden = true;
		}
	});

	edContainer.datepicker
	({
		changeMonth: true,
		changeYear: true,
		beforeShow: function(input, inst)
		{
			edIsHidden = false;
			// edContainer.get(0).scrollIntoView(true);
		},
		onClose: function(dateText, inst)
		{
			edIsHidden = true;
		}
	});

	$("#start-date-icon").bind("click", function()
	{
		datePickerClick("start-date");
	});

	$("#end-date-icon").bind("click", function()
	{
		datePickerClick("end-date");
	});

	function datePickerClick(containerName)
	{
		if(containerName == "start-date")
		{
			// alter("Performing action on
			container = sdContainer;
			hidden = sdIsHidden;
		}
		else
		{
			container = edContainer;
			hidden = edIsHidden;
		}

		if(hidden)
		{
			// container.datepicker("show");
			if(containerName == "start-date")
			{
				sdContainer.datepicker("show");
				sdIsHidden = false;
			}
			else
			{
				edContainer.datepicker("show");
				edIsHidden = false;
			}
		}
		else
		{
			// container.datepicker("hide");
			if(containerName == "start-date")
			{
				sdContainer.datepicker("hide");
				sdIsHidden = true;
			}
			else
			{
				edContainer.datepicker("hide");
				edIsHidden = true;
			}
		}
	}

	$(".event-title").bind("mouseenter", function(e)
	{
		tipIndex = jQuery.inArray(this.id, tips);

		if(currentTipID != this.id) // if(tipIndex == -1)
		{
			// alert("Adding");
			var eventElement = $(this);
			var headerElement = $("#nav");
			
			var position = eventElement.position();
			var width = eventElement.width();
			var height = eventElement.height();

			// alert(this.id);

			$(".popup-container").fadeOut(400);
			$(".popup-container").remove(); // Remove any existing tooltips before showing the new one.

			var tipHTML = "<div class='popup-container' style='position: absolute; z-index: 99999; display: none; top: " + (Math.round(position.top) - height) + "px; left: " + (Math.round(position.left)) + "px;'>";
			tipHTML += "<div class='popup'>";
			tipHTML += "<div class='title'><h3>" + titles[this.id] + "</h3></div>";
			tipHTML += "<div class='content'><p>";
			tipHTML += "<strong>" + startDates[this.id] + " to " + endDates[this.id] + "</strong><br />" + locations[this.id];

			if(buildings[this.id] != "")
			{
				tipHTML += "&mdash;" + buildings[this.id];
				if(rooms[this.id] != "")
					tipHTML += ", " + rooms[this.id];
			}

			tipHTML += "</p>";

			tipHTML += "<p>" + descriptions[this.id] + "</p>";
			tipHTML += "</div>"; // Description container div.
			tipHTML += "</div>"; // Div containing styles.
			tipHTML += "</div>";// Overlay div.
			var newElement = $(tipHTML);

			newElement.insertAfter(this);
			// newElement.appendTo("body");
			newElement.fadeIn(600);
			
			// alert("Event top: " + Math.round(position.top) + ", Header element - top: " + headerElement.position().top + ", height: " + headerElement.height() + ", New element - height: " + newElement.height());
			
			var newTop = Math.round(position.top) - Math.round(newElement.height()) - 10; // Subtracted about 10 pixels to give the popup just a little more space.
			var imgArrow = $("<img src='/images/tip-down.png' class='tip-bottom' />");
			
			if(Math.round(position.top) - newElement.height() <= (headerElement.position().top + headerElement.height()))
			{
				newTop = Math.round(position.top) + Math.round(eventElement.height());
				imgArrow = $("<img src='/images/tip-up.png' class='tip-top' />");
				newElement.prepend(imgArrow);
			}
			else
				newElement.children(".popup").append(imgArrow);
			
			// imgArrow = $(imgArrow);
			
			newElement.css("top", newTop); 

			tips.push(this.id);
			currentTipID = this.id;
		}
	});

	$(".event-title").bind("mouseleave", function(e)
	{
		tipIndex = jQuery.inArray(this.id, tips);

		$(".popup-container").fadeOut(400);
		$(".popup-container").remove(); // Remove any existing tooltips before showing the new one.

		if(tipIndex != -1)
		{
			// alert("Removing");
			tips.splice(tipIndex, 1);
			currentTipID = null;
		}
	});

	// Confirmation popups for event deletion.
	$(".event-delete-link").bind("click", function(e)
	{
		return window.confirm("Are you sure you want to remove the selected event?");
	});
});

// Doing this here because TinyMCE doesn't seem to like to play well with other JS framewords document ready functions.
window.onload = function()
{
	// Tiny MCE stuff.
	if($(".mceEditor").length > 0)
	{
		tinyMCE.init(
		{
			mode: "specific_textareas",
			plugins: "paste",
			editor_selector: "mceEditor",
			theme: "advanced",
			entity_encoding: "raw",
			theme_advanced_buttons1 : "bold,italic,underline,|,bullist,numlist,|,link,unlink,image,|,removeformat,code,pastetext",
			theme_advanced_buttons2 : null,
			theme_advanced_buttons3 : null,
			paste_convert_middot_lists: false,
			paste_remove_styles: true,
			paste_remove_spans: true,
			force_p_newlines: true,
			remove_linebreaks: false
		});
	}
};

