/** Intellisense Includes **/
///<includes src="jquery.class.js" />
///<includes src="dropdownlistextensions.js" />

var ResortCount = 0;
var locLev1 = "...";
var locLev2 = "......";
var arr_resortObjects = new Array();
var arr_locationObjects = new Array();
//var arr_locationLabelObjects = new Array();

///<summary>Create ResortObject class</summary>
var ResortObject = jQuery.Class.create({
    initialize: function(id, name, brandid, location0, location1, location2) {
        this._id = id;
        this._name = name;
        this._brandid = brandid;
        this._location0 = location0;
        this._location1 = location1;
        this._location2 = location2;
    },
    // Methods
    ID: function() { return this._id; },
    Name: function() { return this._name; },
    BrandID: function() { return this._brandid; },
    Location0: function() { return this._location0; },
    Location1: function() { return this._location1; },
    Location2: function() { return this._location2; }
});

///<summary>Create LocationObject class</summary>
var LocationObject = jQuery.Class.create({
    initialize: function(id, name, parent, root) {
        this._id = id;
        this._name = name;
        this._parent = parent;
        this._root = root;
    },
    // Methods
    ID: function() { return this._id; },
    Name: function() { return this._name; },
    Parent: function() { return this._parent; },
    Root: function() { return this._root; },
    Nameshort: function() {
        return this._name.replace(locLev1, '').replace(locLev1, '');
    }
});


///<summary>Gets the level of a location</summary>
function getLevel(locationStr) {
    if (locationStr.substring(0, locLev2.length) == locLev2) {
        level = 2; //sublocation
    }
    else if (locationStr.substring(0, locLev1.length) == locLev1) {
        level = 1; //location
    }
    else {
        level = 0; //country/state
    }
    return level;
}

///<summary>internal funcation to convert booking_array into objects</summary>
function internal_buildArrays() {
    //location labels
    //var locationLabels = eval('arr_locationlabels');

    //location variables
    var locations = eval('arr_locations_0');
    var LocationID = -1;
    var curLoc;
    //resort variables
    var resorts = eval('arr_resorts');
    var parentLocFound = false;

    arr_resortObjects = new Array(resorts.length);
    arr_locationObjects = new Array();
    //arr_locationLabelObjects = new Array();

    //iterators
    var r;
    var i;
    var j;
    var nextResort;
    var nextLocation0;
    var nextLocation1;
    var nextLocation2;

    for (li = 0; li < locations.length; li++) { //add location options
        curLoc = locations[li].split("||");
        if (getLevel(curLoc[1]) == 0) {
            parentLocFound = true;
            nextLocation0 = new LocationObject(curLoc[0], curLoc[1], -1, curLoc[0]);
            arr_locationObjects.push(nextLocation0);
        }
        if (parentLocFound) {
            if (getLevel(curLoc[1]) == 1) {
                nextLocation1 = new LocationObject(curLoc[0], curLoc[1], nextLocation0.ID(), nextLocation0.ID());
                arr_locationObjects.push(nextLocation1);
            }
            else if (getLevel(curLoc[1]) == 2) {
                nextLocation2 = new LocationObject(curLoc[0], curLoc[1], nextLocation1.ID(), nextLocation0.ID());
                arr_locationObjects.push(nextLocation2);

                //add resorts under this location
                for (i = 0; i < resorts.length; i++) {
                    r = resorts[i].split("||");
                    if (r[3] == curLoc[0]) {
                        nextResort = new ResortObject(r[0], r[1], r[2], nextLocation0.ID(), nextLocation1.ID(), nextLocation2.ID());
                        arr_resortObjects[i] = nextResort;
                    }
                }
            }
        }
    }
}

///<summary>Determines if resorts are in a location</summary>
///<returns>true if resorts are found<returns>
function hasResortsInLocation(locationid, brandid) {
    for (ihr = 0; ihr < arr_resortObjects.length; ihr++) {
        var item = arr_resortObjects[ihr];
        if (item != null && (item.Location0() == locationid || item.Location1() == locationid || item.Location2() == locationid) &&
            (brandid == item.BrandID() || brandid == 0)) {
            return true;
        }
    }
    return false;
}

///<summary>Populates top level location dropdownlist</summary>
function PopulateLocations() {
    internal_buildArrays();

    ClearOptions(getLocation()); 

    AddToOptionList(getLocation(), '-1', '');

    for (i = 0; i < arr_locationObjects.length; i++) {
        var itemLoc = arr_locationObjects[i];
        if (hasResortsInLocation(itemLoc.ID(), getBrandID())) {

            if (itemLoc.ID() == itemLoc.Root()) { //don't allow this to be selected
                var objOption = document.createElement("optgroup");
                objOption.label = itemLoc.Name();
                getLocation().appendChild(objOption);
            }
            else {
                AddToOptionList(getLocation(), itemLoc.ID(), itemLoc.Name());
            }
        }
    }
}

///<summary>Updates the resort dropdownlist based on the current selections</summary>
function UpdateResorts(pBrandID) {
    //controls
    var ddlLocation = getLocation();
    var ddlResorts = getResorts();
    //location variables
    var optGroup1 = null;
    var objOption = null;
    var LocationID = -1;

    if (ddlLocation != null && ddlLocation.selectedIndex > -1) { LocationID = ddlLocation[ddlLocation.selectedIndex].value; }

    var BrandID = pBrandID;
    var resortCount = 0;

    ClearOptions(ddlResorts);

    AddToOptionList(ddlResorts, -1, 'Select a hotel (optional)');

    if (LocationID > -1) {
        for (li = 0; li < arr_locationObjects.length; li++) {
            itemLoc = arr_locationObjects[li];
            if (itemLoc.ID() == LocationID || itemLoc.Parent() == LocationID || itemLoc.Root() == LocationID || LocationID == -1) {
                resortCount = 0;
                optGroup1 = document.createElement('optgroup');
                optGroup1.label = itemLoc.Nameshort();
                for (ihr = 0; ihr < arr_resortObjects.length; ihr++) {
                    ritem = arr_resortObjects[ihr];
                    if (ritem != null && ritem.Location2() == itemLoc.ID() && (ritem.BrandID() == pBrandID || pBrandID == 0)) {
                        resortCount++;
                        objOption = document.createElement("option");
                        objOption.value = ritem.ID();
                        objOption.innerHTML = ritem.Name();
                        optGroup1.appendChild(objOption);
                    }
                }
                if (resortCount > 0) {
                    ddlResorts.appendChild(optGroup1);
                 }
            }
        }
    }
    else {
        for (ihr = 0; ihr < arr_resortObjects.length; ihr++) {
            var item = arr_resortObjects[ihr];
            if (item != null && (item.BrandID() == pBrandID || pBrandID == 0)) {
                AddToOptionList(ddlResorts, item.ID(), item.Name()); 
            }
        }
    }
}

///<summary>Updates resorts dropdownlist by location</summary>
function UpdateResortsByLocation() {
    UpdateResortsByBrand();
}

///<summary>Updates resorts dropdownlist by brand</summary>
function UpdateResortsByBrand() {
    var BrandID = getBrandID();
    UpdateResorts(BrandID);
    getResorts().selectedIndex = 0;
}

