﻿/* Geography Popup */
var GeographyPopup = new function () {

    // get geography popup content webservice
    this.ShowPopup = new WebService();
    this.ShowPopup.Go = function (sGeographyLevelName, iGeographyID) {

        InfoPopup.SetupPopup('geographypopup');

        aParams = new Array(['GeographyLevelName', sGeographyLevelName], ['GeographyID', iGeographyID]);
        this.RunWebService('/webservices/support.asmx', 'http://intuitivesystems', 'GetGeographyPopupContent', aParams, this, false);
    }

    this.ShowPopup.Done = function (oXML) {
        var sHTML = this.GetTagValue(oXML, 'GetGeographyPopupContentResult');

        InfoPopup.SetContent(sHTML);
    }


    // map
    this.Map = new function () {

        var me = this;
        this.GoogleMap;


        // event handlers
        this.OnClick = function (oMarker) {
            f.HidePopup();

            switch (this.GetMarkerType(oMarker)) {
                case 'regionid':
                    GeographyPopup.ShowPopup.Go('region', me.GetIDFromMarker(oMarker));
                    break;
                case 'resortid':
                    GeographyPopup.ShowPopup.Go('resort', me.GetIDFromMarker(oMarker));
                    break;
                case 'propertyid':
                    PropertyPopup.ShowPopup.Go(me.GetIDFromMarker(oMarker));
                    break;
            }
        }

        this.OnMouseOver = function (oMarker) {
            f.ShowPopup(me.GoogleMap.GetMarkerScreenPosition(oMarker), 'mapHover', '',
                'divInfoPopupMapHover_' + me.GetIDFromMarker(oMarker), true, -192, 28);
        }

        this.OnMouseOut = function (oMarker) {
            f.HidePopup();
        }


        // support functions
        this.GetIDFromMarker = function (oMarker) {
            return oMarker.MarkerID.split('_')[1];
        }
        this.GetMarkerType = function (oMarker) {
            return oMarker.MarkerID.split('_')[0];
        }
    }

}


/* Property Popup */
var PropertyPopup = new function () {

    // get property popup content webservice
    this.ShowPopup = new WebService();
    this.ShowPopup.Go = function (iPropertyID, ShowFreeNightsIcon, ShowEBDIcon, ShowSpecialOfferIcon, TestFreeKids) { // the Show<Icon name> and TestFreeKids arguments are optional

        if (TestFreeKids == undefined) {
            TestFreeKids = false;
        }

        InfoPopup.SetupPopup('propertypopup');

        aParams = new Array(['PropertyID', iPropertyID], ['ShowFreeNightsIcon', SafeBoolean(ShowFreeNightsIcon)], ['ShowEBDIcon', SafeBoolean(ShowEBDIcon)], ['ShowSpecialOfferIcon', SafeBoolean(ShowSpecialOfferIcon)], ['TestFreeKids', SafeBoolean(TestFreeKids)]);
        this.RunWebService('/webservices/support.asmx', 'http://intuitivesystems', 'GetPropertyPopupContent', aParams, this, false);
    }

    this.ShowPopup.Done = function (oXML) {
        var sHTML = this.GetTagValue(oXML, 'GetPropertyPopupContentResult');

        InfoPopup.SetContent(sHTML);
    }


    // map
    this.Map = new function () {

        var me = this;
        this.GoogleMap;


        // event handlers
        this.OnClick = function (oMarker) {
            f.HidePopup();
            PropertyPopup.ShowPopup.Go(me.GetIDFromMarker(oMarker));
        }

        this.OnMouseOver = function (oMarker) {
            f.ShowPopup(me.GoogleMap.GetMarkerScreenPosition(oMarker), 'mapHover', '',
                'divInfoPopupMapHover_' + me.GetIDFromMarker(oMarker), true, -192, 28);
        }

        this.OnMouseOut = function (oMarker) {
            f.HidePopup();
        }


        // support functions
        this.GetIDFromMarker = function (oMarker) {
            return oMarker.MarkerID.split('_')[1];
        }
    }

}



/* Base Class */
var InfoPopup = new function() {

	this.KeepPosition = false; // this is so we can update content only without having the popup jump around!
	this.TopPosition = 0;
	this.LeftPosition = 0;

	this.Map = null; // this is so we can link a map into the popup. (for the old style map)
	this.OnClose; // this allows the page to define a function to fix (re-setup) its map if it needs it after the popup has closed.
	
	
	// creates and/or sets up the popup to put content into
	this.SetupPopup = function(sClassName) {
	
		// make sure we've got our div
		var oPopup = f.GetObject('divInfoPopup');
		if (!oPopup) {
			oPopup = document.createElement('div');
			oPopup.className = 'modalpopup '.concat(sClassName);
			oPopup.setAttribute('id', 'divInfoPopup');
			oPopup.style.display = 'none';
			document.body.appendChild(oPopup);
			
		} else if (oPopup.style.display == 'none') {
			this.KeepPosition = false;
		} else {
			this.History.CreateMarker(); // add the previous state to the history before we alter anything
			
			this.KeepPosition = true;
			this.TopPosition = oPopup.style.top;
			this.LeftPosition = oPopup.style.left;
		}
		
		// set it up
		oPopup.className = 'modalpopup '.concat(sClassName);
		f.SetHTML(oPopup, '<div id="divInfoPopupRetrieveWait">' + Constants.GeographyPopupPleaseWait + '</div>');
		
		// show the popup if it's not already visible
		if (oPopup.style.display == 'none') {
			e.ModalPopup.Show('divInfoPopup');
			
			// override the escape event so the popup closes properly
			f.DetachEvent(e.ModalPopup.EscapeEvent);
			e.ModalPopup.EscapeEvent = f.AttachEvent(document, 'keypress',
				function(oEvent) {
				    if (f.GetKeyCodeFromEvent(oEvent) == 27) {
				        InfoPopup.Close();
				    }
				});
		}
		
		e.ModalPopup.UpdateScreenPosition(); // center the wait message on screen.
		
		return oPopup;
	
	}
	
	// sets the html
	this.SetContent = function(sHTML) {
		var oPopup = f.GetObject('divInfoPopup');
		f.SetHTML(oPopup, sHTML, true);
		
		if (this.KeepPosition) {
			oPopup.style.top = this.TopPosition;
			oPopup.style.left = this.LeftPosition;
			
			e.ModalPopup.CheckOverlayIsTallEnough();
		} else {
			e.ModalPopup.UpdateScreenPosition();
		}
		if (this.Map) GoogleMap.CentreAndZoom(15); // This MUST be called AFTER the modal popup is shown. (for the old style map)
		if (this.History.HasHistory()) f.Show('btnInfoPopupBack');
	}
	
	// close
	this.Close = function() {
		this.History.Clear();
		this.Map = null;
		e.ModalPopup.Close();
		
		if (typeof this.OnClose == "function") this.OnClose(); // This will allow us, amongst other things, to fix the map on the page we've come from if it needs it. (if it defines a function to do so)
	}
	
	
	
	/* History Storing (for back button) */
	this.History = new function() {

		this.aHistoryHTML = new Array();


		this.CreateMarker = function() {
			var oPopup = f.GetObject('divInfoPopup');
			if (oPopup) {
				var sHTML = oPopup.innerHTML;
				var sClassName = oPopup.className;
				
				this.aHistoryHTML.push([sHTML, sClassName]);
			}
		}
		
		this.GoBack = function() {
			var sLastPageDetails = this.aHistoryHTML.pop();
			var sHTML = sLastPageDetails[0];
			var sClassName = sLastPageDetails[1];
			
			var oPopup = f.GetObject('divInfoPopup');
			if (oPopup) {
				oPopup.className = sClassName;
				
				this.KeepPosition = true;
				InfoPopup.SetContent(sHTML);
			}
		}
		
		
		this.Clear = function() {
			this.aHistoryHTML = new Array();
		}
		
		
		this.HasHistory = function() { return this.aHistoryHTML.length > 0; }

	}

}
