﻿/*  ObjectList.js
*   Description:
*           Javascript class to maintain a list of "selectable" objects.
*   By:     Paul Court
*   Date:   26/10/2006
*   Notes:
*           Objects stored in these lists *MUST* have a unique id property.
*/


/* --- The constructor --- */
function ObjectList(){
    this.items = [];
    this.selectedIds = [];
}

/* --- Prototype class methods --- */

// Name:    ObjectList.Contains
// Desc:    Does this list contain the specified item 
//          (using "id" to check.)?
// Params:  Id to check the list for.
// Returns: Boolean
ObjectList.prototype.Contains = function(itemId){
    if(this.FindIndex(itemId) != -1){ return true; }
    else { return false; }
}

// Name:    ObjectList.Add
// Desc:    Adds a new object to the list after checking
//          that id has an "id" property and is not already
//          in the list.
// Params:  New object to add.
// Returns: Boolean
ObjectList.prototype.Add = function(newObject){
    var result = false;
    if(typeof newObject == "object"){
        if(("id" in newObject) && (!this.Contains(newObject.id))){
            this.items.push(newObject);
            result = true;
        }
    }
    return result;
}

// Name:    ObjectList.FindIndex
// Desc:    Finds the array index of a specified item.
// Params:  Id to search for.
// Returns: Integer (-1 if not found).
ObjectList.prototype.FindIndex = function(itemId){
    var result = -1;
    for(var index = 0; index < this.items.length; index++){
        if(this.items[index].id == itemId){
            result = index;
        }
    }
    return result;
}

// Name:    ObjectList.Remove
// Desc:    Remove an item from the list. (using "id" to check);
// Params:  Id of the object to remove from the list.
ObjectList.prototype.Remove = function(itemId) {
    var itemIndex = this.FindIndex(itemId);
    if(itemIndex != -1){
        this.items.splice(itemIndex, 1);
    }
}

// Name:    ObjectList.Toggle
// Desc:    Toggle an item between selected states.
// Params:  Id of the item to toggle.
ObjectList.prototype.Toggle = function(itemId){
    if(this.Contains(itemId)){
        if(this.IsSelected(itemId)){
            // Item is selected, remove it.
            for(var i=0; i < this.selectedIds.length; i++){
                if(this.selectedIds[i] == itemId){
                    this.selectedIds.splice(i, 1);
                }
            }
        } else {
            // Item is not selected, add it.
            this.selectedIds.push(itemId);
        }
    }
    top.redrawGMap = true;
}

// Name:    ObjectList.GetSelectedItems
// Desc:    Returns an array of all the object that are currently
//          selected.
// Params:  none.
// Returns: Array of Objects.
ObjectList.prototype.GetSelectedItems = function(){
    var selectedItems = [];
    for(var i=0; i < this.selectedIds.length; i++){
        var itemIndex = this.FindIndex(this.selectedIds[i]);
        if(itemIndex != -1){
            selectedItems.push(this.items[itemIndex]);
        }
    }
    return selectedItems;
}

// Name:    ObjectList.IsSelected
// Desc:    Returns true if the specified item is selected.
// Params:  Item ID to search for.
// Returns: Boolean.
ObjectList.prototype.IsSelected = function(itemId){
    for(var i=0; i < this.selectedIds.length; i++){
        if(this.selectedIds[i] == itemId){ return true; }
    }
    return false;
}

// Name:    ObjectList.ClearSelection
// Desc:    Clears the currently selected items list.
// Params:  
// Returns: 
ObjectList.prototype.ClearSelection = function(){
    this.selectedIds = [];
}

// Name:    ObjectList.InvertSelection
// Desc:    Reverses the selected items.
// Params:
// Returns:
ObjectList.prototype.InvertSelection = function(){
    var newSelection = [];
    for(var i = 0; i < this.items.length; i++){
        if(!this.IsSelected(this.items[i].id)){
            newSelection.push(this.items[i].id);
        }
    }
    this.selectedIds = newSelection;
}

// Name:    ObjectList.SelectAll
// Desc:    Select all the items in this list.
// Params:
// Returns:
ObjectList.prototype.SelectAll = function(){
    this.ClearSelection();
    this.InvertSelection();
}
