﻿/* Tabs */
var GeographyTabs = new function () {

    /* Private Properties */
    var me = this;
    var oReadyState = 'idle';
    var oSetTabObjectOnContentLoaded;
    var oSetDivOnContentLoaded;

    this.FacebookButtonHide = function (oShowDiv) {
        if (oShowDiv.id == 'divGeographyOverview' || oShowDiv.id == 'divGeographyPricesAndOffers' || oShowDiv.id == 'divGeographyHotels') {
            var aFacebookbutton = f.GetElementsByClassName('iframe', 'FacebookButton', 'divGeographyTabContent');
            for (var i = 0; i < aFacebookbutton.length; i++) {
                f.Hide(aFacebookbutton[i]);
            }
        } else {
            var aFacebookbutton = f.GetElementsByClassName('iframe', 'FacebookButton', 'divGeographyTabContent');
            for (var i = 0; i < aFacebookbutton.length; i++) {
                f.Show(aFacebookbutton[i]);
            }
        }
    }

    /* Load Other Content */
    this.LoadOtherContent = function (sExcludeTab) {

        oReadyState = 'loading';

        FormFunctionsWithViewstate.Call('GetOtherPageContent', this.OtherContentLoaded, sExcludeTab);
    }
    this.OtherContentLoaded = function (sContent) {

        //if no viewstate, fallback to ordinary links
        if (sContent == 'Error: No Viewstate') {
            var aTabs = f.GetElementsByClassName('a', 'geographyTab', 'divGeographyTabs');

            for (var i = 0; i < aTabs.length; i++) {
                aTabs[i].onclick = function () { return true; };
            }

            return;
        }

        //else put the content in the holder and carry on
        var oOtherContentHolder = f.GetObject('divGeographyTabContentOtherTabs');

        f.Show(oOtherContentHolder); //we have to temporarily show this so that the scripts can run reliably
        f.SetHTML(oOtherContentHolder, sContent, true);
        f.Hide(oOtherContentHolder);

        var aTabsContent = f.GetElementsByClassName('div', 'geographyTabContent', 'divGeographyTabContent');
        for (var i = 1; i < aTabsContent.length; i++) {
            f.Hide(aTabsContent[i]);
        }

        oReadyState = 'loaded';

        if (oSetTabObjectOnContentLoaded != undefined && oSetDivOnContentLoaded != undefined) {
            GeographyTabs.Show(oSetTabObjectOnContentLoaded, oSetDivOnContentLoaded);
        }

    }


    /* Show */
    this.Show = function (oTabObject, oDiv) {


        //select the correct tab
        oTabObject = f.SafeObject(oTabObject);
        var aTabs = f.GetElementsByClassName('a', 'geographyTab', 'divGeographyTabs');

        for (var i = 0; i < aTabs.length; i++) {
            f.SetClassIf(aTabs[i], 'selected', aTabs[i] == oTabObject);
        }


        //show the correct div
        var oShowDiv = f.SafeObject(oDiv);
        var aDivs = f.GetElementsByClassName('div', 'geographyTabContent', 'divGeographyTabContent');

        if (oReadyState == 'loaded' || oShowDiv != null) {

            //show the div we want
            for (var i = 0; i < aDivs.length; i++) {
                f.ShowIf(aDivs[i], aDivs[i] == oShowDiv);
            }

            //hide the other content holder if this div is not inside it (because it simply refuses to have a height of zero)
            var oOtherTabContentHolder = f.GetObject('divGeographyTabContentOtherTabs');
            f.ShowIf(oOtherTabContentHolder, oShowDiv.parentNode == oOtherTabContentHolder);

            //hack to refresh and centre the map until google fixes it
            if (oShowDiv.id == 'divGeographyOverview' && f.GetObject('divGeographyMap')) {
                GeographyMap.Map.CentreAndZoom();
            }
            me.FacebookButtonHide(oShowDiv);
            //make sure we don't go changing the tabs as the content loads
            oSetTabObjectOnContentLoaded = undefined;
            oSetDivOnContentLoaded = undefined;

        } else {
            oPleaseWaitDiv = f.GetObject('divGeographyTabContentLoading');

            for (var i = 0; i < aDivs.length; i++) {
                f.ShowIf(aDivs[i], aDivs[i] == oPleaseWaitDiv);
            }

            //in the unlikey event that the content finishes loading while we're looping through the divs,
            //show it straight away, else set it to appear when it's loaded
            if (oReadyState == 'loaded') {
                this.Show(oTabObject, oDiv);
            } else {
                oSetTabObjectOnContentLoaded = oTabObject;
                oSetDivOnContentLoaded = oDiv;
            }
        }
    }

}


/* Map */
var GeographyMap = new function () {

    var me = this;
    this.Map;
    this.Redirects = new Array();

    this.OnClick = function (oMarker) {
        window.location = me.Redirects[oMarker.MarkerID.split('_')[1]];
    }
    this.OnMouseOver = function (oMarker) {
        f.ShowPopup(me.Map.GetMarkerScreenPosition(oMarker), 'geographyPopup', '', 'divGeographyMapHover_' + oMarker.MarkerID.split('_')[1], true, -188, 30);
    }
    this.OnMouseOut = function (oMarker) {
        f.HidePopup();
    }
}


/* Holiday Searches */
var GeographyHolidaySearches = new function() {

    this.PackageSearch = new WebService();
    this.PackageSearch.Go = function(sGeographyLevel, iGeographyID, nMinPricePerPerson, nMaxPricePerPerson,
        iProductAttributeID, iMealBasisID, iRating, sDepartureDateRangeStart, sDepartureDateRangeEnd,
        iDepartureAirportGroupID, iDepartureAirportID, iDuration, iAdults, iChildren) {

        FormHandler.CloseInfo();
        ShowWaitMessage('divGeographyHolidaySearchWaitMessage');

        var aParams = new Array(['GeographyLevel', sGeographyLevel], ['GeographyID', iGeographyID], ['MinPricePerPerson', nMinPricePerPerson],
            ['MaxPricePerPerson', nMaxPricePerPerson], ['ProductAttributeID', iProductAttributeID], ['MealBasisID', iMealBasisID],
            ['Rating', iRating], ['DepartureDateRangeStart', sDepartureDateRangeStart], ['DepartureDateRangeEnd', sDepartureDateRangeEnd],
            ['DepartureAirportGroupID', iDepartureAirportGroupID], ['DepartureAirportID', iDepartureAirportID], ['Duration', iDuration],
            ['Adults', iAdults], ['Children', iChildren]);
            
        this.RunWebService('/WebServices/Search.asmx', 'http://intuitivesystems', 'GeographyHolidaySearch', aParams, this, false);
    }
    this.PackageSearch.Done = function(oXML) {
        var sRedirectTo = this.GetTagValue(oXML, 'GeographyHolidaySearchResult');

        if (s.StartsWith(sRedirectTo, 'WARNING: ')) {
            e.ModalPopup.Close();
            FormHandler.ShowWarning(sRedirectTo.substring(9));
        } else {
            window.location = sRedirectTo;
        }
    }


}
