﻿/* Search box highlighting / lightbox effect */
var HighlightSearch = new function () {

	this.EscapeEvent;

	this.OldMonthYearValue = null;
	this.OldDayValue = null;
	this.OldMealBasisID = null;

	this.iPaddingValue = 2; // change padding value here. (in px)


	this.SearchHighlight = function (sButtonText) {


		// create the overlay.
		e.CreateOverlay();


		// set up the div to hold the search tool.
		var oSearchHolder = document.createElement('div');
		oSearchHolder.setAttribute('id', 'divSearchHolder');
		document.body.appendChild(oSearchHolder);

		var oSearchPosition = e.GetPosition('divSearchTop');
		oSearchHolder.style.top = (oSearchPosition.Top - this.iPaddingValue) + 'px';
		oSearchHolder.style.left = (oSearchPosition.Left - this.iPaddingValue - 1) + 'px';
		oSearchHolder.style.padding = this.iPaddingValue + 'px';


		// get the bottom margin of the search tool BEFORE we move it out of the sidebar. (note: assumes values are in px)
		if (f.GetObject('divSearch').currentStyle != undefined) { // IE.
			var iBottomSearchMargin = parseInt(
		        parseInt(f.GetObject('divSearch').currentStyle.marginBottom) > parseInt(f.GetObject('divSearchMain').currentStyle.marginBottom) ?
		            f.GetObject('divSearch').currentStyle.marginBottom : f.GetObject('divSearchMain').currentStyle.marginBottom
		        );
		} else if (window.getComputedStyle != undefined) { // Other browsers.
			var iBottomSearchMargin = parseInt(
		        parseInt(window.getComputedStyle(f.GetObject('divSearch'), null).marginBottom) > parseInt(window.getComputedStyle(f.GetObject('divSearchMain'), null).marginBottom) ?
		            window.getComputedStyle(f.GetObject('divSearch'), null).marginBottom : window.getComputedStyle(f.GetObject('divSearchMain'), null).marginBottom
		        );
		} else { // Fallback.
			var iBottomSearchMargin = 0;
		}


		// also get the next sibling in the sidebar so we can insert the spacer in the right place.
		var oNextSidebarSibling = f.GetObject('divSearch').nextSibling;


		// bung the tool in the holder div.
		oSearchHolder.appendChild(f.GetObject('divSearchTop'));
		oSearchHolder.appendChild(f.GetObject('divSearch'));


		// now we need to create a spacer div to stop the rest of the sidebar from moving up.
		var oSidebar = f.GetObject('divSidebar');

		var oSidebarSpacer = document.createElement('div');
		oSidebarSpacer.setAttribute('id', 'divSidebarSpacer');
		oSidebar.insertBefore(oSidebarSpacer, oNextSidebarSibling);

		oSidebarSpacer.style.height = e.GetPosition(oSearchHolder).Height + 'px';
		oSidebarSpacer.style.width = '100%';
		oSidebarSpacer.style.marginBottom = (iBottomSearchMargin - this.iPaddingValue * 2) + 'px';


		// add button to cancel.
		var oCancelText = document.createTextNode(sButtonText);
		var oCancelLink = document.createElement('a');
		oCancelLink.setAttribute('id', 'aCancelSearchHighlight');
		oCancelLink.appendChild(oCancelText);

		f.GetObject('divSearchMain').insertBefore(oCancelLink, f.GetObject('btnSearch').nextSibling.nextSibling);

		f.AttachEvent(oCancelLink, 'click', function () { HighlightSearch.Cancel(); });
		f.AttachEvent(f.GetObject('btnSearch'), 'click', function () { HighlightSearch.Cancel(true); }); // also get rid of the highlight when the search button is clicked.


		// add an event for the ESC keypress.
		HighlightSearch.EscapeEvent = f.AttachEvent(document, 'keypress',
            function (oEvent) {
            	if (f.GetKeyCodeFromEvent(oEvent) == 27) {
            		HighlightSearch.Cancel();
            	}
            });


		// make sure we can actually see the search tool.
		e.ScrollIntoView('divSearchTop', this.iPaddingValue);

	}


	this.Cancel = function (bDontRestoreOldValues) {

		f.DetachEvent(HighlightSearch.EscapeEvent);

		f.GetObject('divSearchMain').removeChild(f.GetObject('aCancelSearchHighlight'));

		var oSidebar = f.GetObject('divSidebar');
		oSidebar.removeChild(f.GetObject('divSidebarSpacer'));
		oSidebar.insertBefore(f.GetObject('divSearch'), oSidebar.firstChild);
		oSidebar.insertBefore(f.GetObject('divSearchTop'), oSidebar.firstChild);

		document.body.removeChild(f.GetObject('divSearchHolder'));
		document.body.removeChild(f.GetObject('divOverlay'));


		// get back old values if they are stored.
		if (!bDontRestoreOldValues) {

			if (this.OldMonthYearValue != null) {
				f.SetValue('calDepartureDate_MonthYear', this.OldMonthYearValue);
				this.OldMonthYearValue = null;

				CalendarDateChange(f.GetObject('calDepartureDate_MonthYear'), 1); // recalculate calendar.
			}
			if (this.OldDayValue != null) {
				f.SetValue('calDepartureDate_Day', this.OldDayValue);
				this.OldDayValue = null;

				CalendarDateChange(f.GetObject('calDepartureDate_Day'), 1); // recalculate calendar.
			}
			if (this.OldMealBasisID != null) {
				f.SetValue('sddMealBasisID', this.OldMealBasisID);
				this.OldMealBasisID = null;
			}

		} else {

			this.OldMonthYearValue = null;
			this.OldDayValue = null;
			this.OldMealBasisID = null;

		}

	}


	this.FocusOnDateAndMealBasis = function (iYear, iMonthNumber, iMealBasisID, sButtonText) {

		// store old values for date and mealbasis.
		this.OldMonthYearValue = f.GetValue('calDepartureDate_MonthYear');
		this.OldDayValue = f.GetValue('calDepartureDate_Day');
		this.OldMealBasisID = f.GetValue('sddMealBasisID');
		f.SetValue('calDepartureDate_MonthYear', iMonthNumber + '_' + iYear);
		f.SetValue('sddMealBasisID', iMealBasisID);

		// make sure the we recalculate the day field and calendar.
		CalendarDateChange(f.GetObject('calDepartureDate_MonthYear'), 1);

		this.SearchHighlight(sButtonText);

	}


	this.FocusOnDate = function (iYear, iMonthNumber, sButtonText) {

		// store old values for date and mealbasis.
		this.OldMonthYearValue = f.GetValue('calDepartureDate_MonthYear');
		this.OldDayValue = f.GetValue('calDepartureDate_Day');
		f.SetValue('calDepartureDate_MonthYear', iMonthNumber + '_' + iYear);

		// make sure the we recalculate the day field and calendar.
		CalendarDateChange(f.GetObject('calDepartureDate_MonthYear'), 1);

		this.SearchHighlight(sButtonText);

	}

	// fix for the search not moving when resizing the window
	this.WindowResize = function () {

		// only need to do it if the search is highlighted
		if (f.GetObject('divSidebarSpacer') != undefined) {

			this.iPaddingValue = 2;

			var oSidebarPosition = e.GetPosition(f.GetObject('divSidebarSpacer'));

			// need to set top and left position of the search holder to those of the spacer in the sidebar
			document.getElementById('divSearchHolder').style.top = (oSidebarPosition.Top - this.iPaddingValue) + 'px';
			document.getElementById('divSearchHolder').style.left = (oSidebarPosition.Left - this.iPaddingValue - 1) + 'px';

		}

	}

}
