// Fire up Cufon (Font Replacement)
// This must be outside the $(document).ready(function()); in order to fire at the right time for IE

Cufon.replace('h2:not(h2[id=teachingactivities])');

// Survey Popup
function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=650,height=400,left = 710,top = 400');");
}

$(document).ready(function() {

	// Digital Collections Header
	$('#site').bind('change', function() {
		var url = $('#site option:selected').val();
		window.location.href = url;
    });

	// Search the Collection text on Blur & Focus
	$('div#header input.dmSearchField').bind('focus', function() {
		if ($('div#header input.dmSearchField').val() == "Search the Collection") {
			$(this).val("");
		}
   	});

	$('div#header input.dmSearchField').bind('blur', function() {
		if ($('div#header input.dmSearchField').val() == "") {
			$(this).val("Search the Collection");
		}
   	});

	// Looks for links with images in them, and adds the dmImgLink class
	// to remove border-bottom
	$('a:has(img)').addClass('dmImgLink');

	// Enable AutoComplete on All Pages
	$(".autofill").autocomplete(
		"/boomtown/dm.php/search/suggest?nicks[]=title&format=newline",
		{
			delay:10,
			minChars:2,
			matchSubset:1,
			matchContains:1,
			maxItemsToShow: 10,
			cacheLength:10
		}
	);

	/* Enable Date Picking if we're on the advanced search page */
	var beginDateWidth = $('#dmBeginDateSelect').width();

	if (beginDateWidth > 0) {

		// Get the initial values
		var y = parseInt($('#y1').val());
		var m = parseInt($('#m1').val()-1);
		var d = parseInt($('#d1').val());
		var y2 = parseInt($('#y2').val());
		var m2 = parseInt($('#m2').val()-1);
		var d2 = parseInt($('#d2').val());

		// Append the Date Picker Links to their respective locations
		$('<a href=\"noJS.php\" id=\"date-pick1\" class=\"dp-link\">Select Date</a>').appendTo('#dmBeginDateSelect');
		$('<a href=\"noJS.php\" id=\"date-pick2\" class=\"dp-link\">Select Date</a>').appendTo('#dmEndDateSelect');

		// Create the Date Pickers
		var dmBeginYear = 1900;

		$('#dmBeginDateSelect a')
			.datePicker(
				// associate the link with a date picker
				{
					createButton:false,
					day: 1,
					month: 0,
					year: 1900,
					startDate: '1900-01-01',
					endDate: '1925-12-31'
				}
			);

		$('#dmEndDateSelect a')
			.datePicker(
				// associate the link with a date picker
				{
					createButton:false,
					day: 1,
					month: 0,
					year: 1900,
					startDate: '1900-01-02', // This should be one day later than the begin_date
					endDate: '1925-12-31'
				}
			).dpSetSelected('1925-12-31');

		// Attach the First Date Picker to the Select Fields
		$('#dmBeginDateSelect a')
			.bind(
				'click',
				function()
				{
					$(this).dpDisplay();
					return false;
				}
			).bind(
				'dateSelected',
				function(e, selectedDate, $td, state)
				{
					beginCalUpdate(selectedDate);
				}
			);

		function beginCalUpdate(selectedDate)
		{

			// Removes old Notices
			$('.dmDateNotification').remove();

			// Gets the new Values
			var selectedDate = new Date(selectedDate);
			var dSelected = selectedDate.getDate();
			var mSelected = selectedDate.getMonth();
			var ySelected = selectedDate.getFullYear();

			y2 = parseInt($('#y2').val());
			m2 = parseInt($('#m2').val()-1);
			d2 = parseInt($('#d2').val());

			// Applies the new date to the Begin_date selects
			($('#d1')[0]).selectedIndex = dSelected - 1;
			($('#m1')[0]).selectedIndex = mSelected;
			($('#y1')[0]).selectedIndex = ySelected - dmBeginYear;

			// Grabs the new Vals
			y = parseInt($('#y1').val());
			m = parseInt($('#m1').val()-1);
			d = parseInt($('#d1').val());

			var newBeginEndDate = new Date(
				ySelected,
				mSelected,
				(dSelected+1)
		   	);

			// Set End Date for the Beginning Calendar
			$('#dmEndDateSelect a').dpSetStartDate(newBeginEndDate.asString());

		}

		// Attach the Second Date Picker to its respective select fields
		$('#dmEndDateSelect a')
			.bind(
				'click',
				function()
				{
					$(this).dpDisplay();
					return false;
				}
			).bind(
				'dateSelected',
				function(e, selectedDate, $td, state)
					{
						endCalUpdate(selectedDate);
					}
			);

		function endCalUpdate(selectedDate)
		{
			// Removes old Notices
			$('.dmDateNotification').remove();

			// Gets the new Values
			selectedDate2 = new Date(selectedDate);
			var d2Selected = selectedDate2.getDate();
			var m2Selected = selectedDate2.getMonth();
			var y2Selected = selectedDate2.getFullYear();

			// Applies the new date to the End_date selects
			($('#d2')[0]).selectedIndex = d2Selected - 1;
			($('#m2')[0]).selectedIndex = m2Selected;
			($('#y2')[0]).selectedIndex = y2Selected - dmBeginYear;

			// Grabs the new Vals
			y2 = parseInt($('#y2').val());
			m2 = parseInt($('#m2').val()-1);
			d2 = parseInt($('#d2').val());

			var newBeginEndDate = new Date(
				y2Selected,
				m2Selected,
				(d2Selected-1)
		   	);

			// Set End Date for the Beginning Calendar
			$('#dmBeginDateSelect a').dpSetEndDate(newBeginEndDate.asString());
		}

		// listen for when the beginning selects are changed and update everything
		$('#d1, #m1, #y1')
			.bind(
				'change',
				function()
				{
					// Removes old notices
					$('.dmDateNotification').remove();

					// Grabs the Dates from Each set of selects
					var updatedY = parseInt($('#y1').val());
					var updatedM = parseInt($('#m1').val()-1);
					var updatedD = parseInt($('#d1').val());

					y2 = parseInt($('#y2').val());
					m2 = parseInt($('#m2').val()-1);
					d2 = parseInt($('#d2').val());

					// If the beginning date is after the ending date
					if (updatedY >= y2 && updatedM >= m2 && updatedD >= d2) {
						beginSelectsUpdate();
					}
					else if (updatedY >= y2 && updatedM > m2) {
						beginSelectsUpdate();
					}
					else if (updatedY > y2) {
						beginSelectsUpdate();
					}

					// If beginning date < ending date
					else {
						beginSelectsUpdateCancel();
					}

					// Throw an error and reset selects to their old value saying the From date cannot be later than the To date (confused yet?)
					function beginSelectsUpdate() {

						// Updates the Begin Selects
						($('#d1')[0]).selectedIndex = d - 1;
						($('#m1')[0]).selectedIndex = m;
						($('#y1')[0]).selectedIndex = y - dmBeginYear;

						// Throws the error
						var afterNotificationDate = new Date(
								parseInt($('#y2').val()),
								parseInt($('#m2').val()-1),
								parseInt($('#d2').val())
					 );
					$('<span class="dmDateNotification">Please Select a date before ' + afterNotificationDate.asString() + '</span>').appendTo('#dmBeginDateSelect');

					} // end beginSelectsUpdate

					function beginSelectsUpdateCancel() {
						// Updates Y, M, and D variables for future use
						y = updatedY;
						m = updatedM;
						d = updatedD;

						// Parses the new Begin Date
						var newDate1 = new Date(
								y,
								m,
								d
							);
						$('#dmBeginDateSelect a').dpSetSelected(newDate1.asString());

						// Parses a new Start Date and applies it to the ending calendar
						var startDay = d + 1;
						var selectEndDate = new Date(
									y,
									m,
									startDay
								);
						$('#dmEndDateSelect a').dpSetStartDate(selectEndDate.asString());
					} // end beginSelectsUpdateCancel

				} // end bind function
			); // end $('#d1, #m1, #y1')


		// listen for when the ending selects are changed and update everything
		$('#d2, #m2, #y2')
			.bind(
				'change',
				function()
				{
					// Removes old error notifications
					$('.dmDateNotification').remove();

					// Grabs the Dates from Each set of selects
					y = parseInt($('#y1').val());
					m = parseInt($('#m1').val()-1);
					d = parseInt($('#d1').val());

					updatedY2 = parseInt($('#y2').val());
					updatedM2 = parseInt($('#m2').val()-1);
					updatedD2 = parseInt($('#d2').val());

					// If the beginning date is after the ending date
					if (y >= updatedY2 && m >= m2 && d >= updatedD2) {
						endSelectsUpdate();
					}
					else if (y >= updatedY2 && m > updatedM2) {
						endSelectsUpdate();
					}
					else if (y > updatedY2) {
						endSelectsUpdate();
					}

					// If beginning date < ending date
					else {
						endSelectsUpdateCancel();
					}

					// Throw an error and reset selects to their old value saying the before date cannot be earlier than the from date
					function endSelectsUpdate() {

						// Updates the Begin Selects
						($('#d2')[0]).selectedIndex = d2 - 1;
						($('#m2')[0]).selectedIndex = m2;
						($('#y2')[0]).selectedIndex = y2 - dmBeginYear;

						// Throws the error
						var beforeNotificationDate = new Date(
								parseInt($('#y1').val()),
								parseInt($('#m1').val()-1),
								parseInt($('#d1').val())
					 );
					$('<span class="dmDateNotification">Please Select a date after ' + beforeNotificationDate.asString() + '</span>').appendTo('#dmEndDateSelect');

					} // end beginSelectsUpdate

					function endSelectsUpdateCancel() {
						// Updates Y, M, and D variables for future use
						y2 = updatedY2;
						m2 = updatedM2;
						d2 = updatedD2;

						// Parses the new Begin Date
						var newDate1 = new Date(
								y2,
								m2,
								d2
							);
						$('#dmEndDateSelect a').dpSetSelected(newDate1.asString());

						// Parses a new Start Date and applies it to the ending calendar
						var endDay = d2 - 1;
						var selectBeginEndDate = new Date(
									y2,
									m2,
									endDay
								);
						$('#dmBeginDateSelect a').dpSetEndDate(selectBeginEndDate.asString());
					} // end beginSelectsUpdateCancel


				} // end bind function
			); // end $('#d2, #m2, #y2')
	}

});
