﻿if (typeof IgluSearch == 'undefined')
{
    throw 'IgluSearch not found';
}

IgluSearch.Control =
{
    /*Name of this Search Control*/
    name: 'Iglu Ski',

    /*The HTML we will be rendering out*/
    html: '<div id="igluSearchControl" style="width: 185px;">' +
                '<div id="iglu_ski_country">' +
                    '<span class=\"standardWhiteText\" style="display: block;">Find Holidays In: </span>' +
                    '<select onchange="IgluSearch.Control.createResorts(this.value);" id="iglu_ski_cboCountry"></select>' +
                '</div>' +
                '<div id="iglu_ski_resort">' +
                    '<select id="iglu_ski_cboResort"></select>' +
                '</div>' +
                '<div id="iglu_ski_depport">' +
                    '<span class=\"standardWhiteText\" style="display: block;">Leaving From:</span>' +
                    '<select id="iglu_ski_cboDeparturePort"></select>' +
                '</div>' +
                '<div id="iglu_ski_date">' +
                    '<span class=\"standardWhiteText\" style="display: block;">On Date(s):</span>' +
                    '<select id="iglu_ski_cboDepartureDate"></select>' +
                '</div>' +
                '<div id="iglu_ski_proptype">' +
                    '<span class=\"standardWhiteText\" style="display: block;">Accommodation Type:</span>' +
                    '<select id="iglu_ski_cboPropertyType"></select>' +
                '</div>' +
                '<div align="right">' +
                    '<div style="padding-top: 10px;"> ' +
                        '<div style="width: 60px;" onclick="IgluSearch.Search();" class="genericSilverButtonContainer"><div class="leftSection"></div><div class="contentSection">Search</div><div class="rightSection"></div></div> ' +
                    '</div> ' +
                '</div>' +
            '</div>',

    /*These are the options that are definable by the client browser*/
    options:
    {
        showCountry: true,
        showResort: true,
        showDate: true,
        showDeparturePort: true,
        showPropertyType: true
    },

    /*The Function for the Country Combo Box*/
    createCountries: function() {
        //Check to make sure we have the countries...
        if (typeof IgluSearch.Data.Countries == 'undefined')
            throw 'Could not find Countries';

        //Get access to the Country Dropdown List
        var cboCountry = IgluSearch.get('iglu_ski_cboCountry');
        cboCountry.disabled = true;

        //Loop through all the Countries
        for (var i = 0; i < IgluSearch.Data.Countries.length; i++) {
            var option = document.createElement('option');
            option.setAttribute('value', IgluSearch.Data.Countries[i][0]); //Value
            option.innerHTML = IgluSearch.Data.Countries[i][1]; //Text

            //Append the child
            cboCountry.appendChild(option);
        }

        //Re-Enable the Combo Box
        cboCountry.disabled = false;

    },

    /*The Function for the Resort Combo Box*/
    createResorts: function(countrycode) {
        //Deal with the CountryCode, we may need to restrict the Resorts dependant on the selected country
        var resortsToUse;

        if (typeof countrycode != 'undefined' && countrycode != 'ALL') {
            resortsToUse = new Array();
            var totalResorts = 0;

            //Loop through all the Resorts, getting the ones that match
            for (var i = 0; i < IgluSearch.Data.Resorts.length; i++)
                if (IgluSearch.Data.Resorts[i][1] == countrycode) {
                resortsToUse[totalResorts] = IgluSearch.Data.Resorts[i];
                totalResorts++;
            }
        }
        else
            resortsToUse = IgluSearch.Data.Resorts;

        //Get access to the Resort Dropdown, and clear it of it's items
        var cboResort = IgluSearch.get('iglu_ski_cboResort');
        IgluSearch.clearSelect(cboResort);

        //Disable the control, i.e. Grey it out
        cboResort.disabled = true;

        var anyResort = document.createElement('option');
        anyResort.innerHTML = 'Any Resort';
        anyResort.value = '-1';
        cboResort.appendChild(anyResort);

        //Loop through all the items we got back from the webservice
        for (var i = 0; i < resortsToUse.length; i++) {
            var option = document.createElement('option');
            option.setAttribute('value', resortsToUse[i][0]); //Value
            option.innerHTML = resortsToUse[i][2]; //Text

            cboResort.appendChild(option);
        }

        cboResort.disabled = false;
    },

    /*The XmlHttpRequest Function for the Departure Combo Box*/
    createDeparturePoints: function() {
        var cboDeparturePort = IgluSearch.get('iglu_ski_cboDeparturePort');
        cboDeparturePort.disabled = true;

        //Loop through all the items we got back from the webservice
        for (var i = 0; i < IgluSearch.Data.DeparturePorts.length; i++) {
            var option = document.createElement('option');
            option.setAttribute('value', IgluSearch.Data.DeparturePorts[i][0]); //Value
            option.innerHTML = IgluSearch.Data.DeparturePorts[i][1]; //Text

            cboDeparturePort.appendChild(option);
        }

        cboDeparturePort.disabled = false;
    },

    /*The XmlHttpRequest Function for the Property Combo Box*/
    createPropertyTypes: function() {
        var cboPropertyType = IgluSearch.get('iglu_ski_cboPropertyType');
        cboPropertyType.disabled = true;

        //Loop through all the items we got back from the webservice
        for (var i = 0; i < IgluSearch.Data.PropertyTypes.length; i++) {
            var option = document.createElement('option');
            option.setAttribute('value', IgluSearch.Data.PropertyTypes[i][0]); //Value
            option.innerHTML = IgluSearch.Data.PropertyTypes[i][1]; //Text

            cboPropertyType.appendChild(option);
        }

        cboPropertyType.disabled = false;
    },

    /*The Function for the Departure Dates Combo Box*/
    GetDepartureDates: function() {
        var cboDepartureDate = IgluSearch.get('iglu_ski_cboDepartureDate');
        IgluSearch.clearSelect(cboDepartureDate);
        cboDepartureDate.disabled = true;

        var buildDateString = function(d, dd, m, y) {
            return '' + IgluSearch.getDay(d) + ', ' + dd.toString() + '-' + IgluSearch.getMonth(m) + '-' + y;
        };

        //Seperatrow
        var anySeparator = document.createElement('option'); anySeparator.innerHTML = '--------------------';

        //Any Date
        var anyDate = document.createElement('option'); anyDate.value = '-1'; anyDate.innerHTML = 'Any Date';
        cboDepartureDate.appendChild(anyDate);
        cboDepartureDate.appendChild(anySeparator);

        //Any Day Next Week
        var anyDateNextWeek = document.createElement('option'); anyDateNextWeek.value = 'NextWeek'; anyDateNextWeek.innerHTML = 'Any Day next week';
        cboDepartureDate.appendChild(anyDateNextWeek);

        //Any Day Next 2 Week
        var anyDateNext2Weeks = document.createElement('option'); anyDateNext2Weeks.value = 'NextTwoWeeks'; anyDateNext2Weeks.innerHTML = 'Any Day in 2 weeks';
        cboDepartureDate.appendChild(anyDateNext2Weeks);

        //January
        var january = document.createElement('option'); january.value = 'January'; january.innerHTML = 'Any Day in January';
        cboDepartureDate.appendChild(january);

        //February
        var february = document.createElement('option'); february.value = 'February'; february.innerHTML = 'Any Day in February';
        cboDepartureDate.appendChild(february);

        //March
        var march = document.createElement('option'); march.value = 'March'; march.innerHTML = 'Any Day in March';
        cboDepartureDate.appendChild(march);

        //April
        var april = document.createElement('option'); april.value = 'April'; april.innerHTML = 'Any Day in April';
        cboDepartureDate.appendChild(april);

        //December
        var december = document.createElement('option'); december.value = 'December'; december.innerHTML = 'Any Day in December';
        cboDepartureDate.appendChild(december);
        var monthSeparator = document.createElement('option'); monthSeparator.innerHTML = '--------------------';
        cboDepartureDate.appendChild(monthSeparator);

        var minDate = IgluSearch.Data.ThisSeasonStart;
        var maxDate = IgluSearch.Data.ThisSeasonEnd;
        var curDate = minDate;

        while (curDate.getFullYear() < maxDate.getFullYear() || curDate.getMonth() < maxDate.getMonth() || curDate.getDate() < maxDate.getDate()) {
            //Create the Date element
            var date = document.createElement('option');

            //Set the Text and Value
            date.innerHTML = buildDateString(curDate.getDay(), curDate.getDate(), curDate.getMonth(), curDate.getFullYear());
            date.value = '' + (curDate.getDate() < 10 ? ('0' + curDate.getDate()) : curDate.getDate()) + '-' + ((curDate.getMonth() + 1) < 10 ? ('0' + (curDate.getMonth() + 1)) : (curDate.getMonth() + 1)) + '-' + curDate.getFullYear();

            //Set the new Option for the Select
            cboDepartureDate.appendChild(date);

            //Increment a day
            IgluSearch.DateAdd(curDate, 'd', 1);
        }

        cboDepartureDate.disabled = false;
    },

    /*Build up the URL for the Search*/
    Search: function() {
        var completeUrl = IgluSearch.Control.LinkThroughUrl;

        //Get the Selected Country
        if (IgluSearch.Control.options.showCountry) {
            var cboCountry = IgluSearch.get('iglu_ski_cboCountry');

            if (cboCountry.value != 'ALL')
                completeUrl += '&CountryCode=' + cboCountry.value;
        }

        //Get the Selected Resort
        if (IgluSearch.Control.options.showResort) {
            var cboResort = IgluSearch.get('iglu_ski_cboResort');

            if (cboResort.value != '-1')
                completeUrl += '&ResortId=' + cboResort.value;
        }

        //Get the selected Date
        if (IgluSearch.Control.options.showDate) {
            var cboDate = IgluSearch.get('iglu_ski_cboDepartureDate');

            if (cboDate.value != '-1')
                completeUrl += '&Date=' + encodeURIComponent(cboDate.value);
        }

        //Get the selected Departure Port
        if (IgluSearch.Control.options.showDeparturePort) {
            var cboDeparturePort = IgluSearch.get('iglu_ski_cboDeparturePort');

            if (cboDeparturePort.value != 'ALL')
                completeUrl += '&DepPort=' + cboDeparturePort.value;
        }

        //Get the selected Property Type
        if (IgluSearch.Control.options.showPropertyType) {
            var cboPropertyType = IgluSearch.get('iglu_ski_cboPropertyType');

            if (cboPropertyType.value != 'ALL')
                completeUrl += '&PropType=' + cboPropertyType.value;
        }

        //Return the Complete Url
        return completeUrl;
    }
};

/*Set the Pre Render event for this Control, so we can show/hide what we need to.*/
IgluSearch.PreRender = function()
{
    /*Hide the Country if we need to...*/
    if (!IgluSearch.Control.options.showCountry)
        IgluSearch.get('iglu_ski_country').style.display = 'none';
    else
        IgluSearch.Control.createCountries();

    /*Hide the Resort if we need to...*/
    if (!IgluSearch.Control.options.showResort)
        IgluSearch.get('iglu_ski_resort').style.display = 'none';
    else
        IgluSearch.Control.createResorts('ALL');

    /*Hide the Date if we need to...*/
    if (!IgluSearch.Control.options.showDate)
        IgluSearch.get('iglu_ski_date').style.display = 'none';
    else
        IgluSearch.Control.GetDepartureDates();

    /*Hide the DeparturePort if we need to...*/
    if (!IgluSearch.Control.options.showDeparturePort)
        IgluSearch.get('iglu_ski_depport').style.display = 'none';
    else
        IgluSearch.Control.createDeparturePoints();

    /*Hide the PropertyType if we need to...*/
    if (!IgluSearch.Control.options.showPropertyType)
        IgluSearch.get('iglu_ski_proptype').style.display = 'none';
    else
        IgluSearch.Control.createPropertyTypes();
}