﻿
var Dictionary = new Class.create();
Dictionary.prototype = {

    /**
    * Constructor
    * @type Dictionary
    */
    initialize: function() 
    {
        this.keys = new Array();
        this.values = new Array();
        this.iteratorIndex = 0;
    },
    
    /**
    * Add a key value pair to the dictionary
    * @param {object} key: key to store
    * @param {object} value: value to store
    * @return object with key and value properties of the item added
    * @type object
    */
    add: function(key, value){
        this.keys[this.keys.length] = key;
        this.values[this.values.length] = value;
    
        return this.objectAtIndex(this.keys.length - 1);
    },

    /**
    * Check to see if dictionary contains key
    * @param {object} key: key to check for
    * @return true if contains key, false if doesn't
    * @type bool
    */
    contains: function(key){
        if(this.indexOf(key)> -1)
        { return true; }
        else
        { return false; }
    },
    
    /**
    * Check to see if dictionary contains value
    * @param {object} value: value to check for
    * @return true if contains value, false if doesn't
    * @type bool
    */
    containsValue: function(value){
        if(this.indexOfValue(value)> -1)
        { return true; }
        else
        { return false; }
    },
    
    /**
    * Gets the index of the key
    * @param {object} key: key to check for
    * @return index if contains key, -1 if doesn't
    * @type int
    */
    indexOf: function(key){
        for(var index = 0; index < this.keys.length; index++)
        {
            if(this.keys[index] == key)
            {
                return index;
            }
        }
        return -1;
    },
    
    /**
    * Gets the index of the value
    * @param {object} value: value to check for
    * @return index if contains value, -1 if doesn't
    * @type int
    */
    indexOfValue: function(value){
        for(var index = 0; index < this.values.length; index++)
        {
            if(this.values[index] == value)
            {
                return index;
            }
        }
        return -1;
    },
    
    /**
    * Gets the value from the key
    * @param {object} key: key to search from
    * @return value from the key
    * @type object
    */
    getValue: function(key){
        var index = this.indexOf(key);
        if(index > -1)
        {
            return this.values[index];
        }
        else
        {
            alert(key + ' was not found.');
        }
    },
    
    objectAtIndex: function(index){
        if(index >= this.keys.length || index < 0)
        { return null; }
        
        var retval = function(){}
        retval.prototype.key = this.keys[index];
        retval.prototype.value = this.values[index];
        return retval;
    },
    
    /**
    * Removes the key value pair from the dictionary
    * @param {object} key: key to remove
    * @return object with key and value properties of the item removed
    * @type object
    */
    remove: function(key){
        alert('remove not yet implemented.');
    },
    
    /**
    * Sets the value at the given key
    * @param {object} key: key
    * @param {object} value: value to store
    * @return object with key and value properties of the item updated
    * @type object
    */
    setValue: function(key, value){
        var index = this.indexOf(key);
        if(index > -1)
        {
            this.values[index] = value;
        }
        else
        {
            this.add(key,value);
        }
        return this.objectAtIndex(index);
    },
    
    
//**********Iterators********************
    /**
    * Moves the iterator and returns the next key value pair
    * @return object with key and value properties of the next item
    * @type object
    */
    next: function(){
        this.iteratorIndex++;
        return current();
    },
    
    /**
    * Returns the current key value pair
    * @return object with key and value properties of the current item
    * @type object
    */
    current: function(){
        return this.objectAtIndex(this.iteratorIndex);
    },
    
    /**
    * Returns the next key value pair
    * @return object with key and value properties of the next item
    * @type object
    */
    peek: function(){
        return this.objectAtIndex(this.iteratorIndex + 1);
    },
    
    /**
    * Returns the previous key value pair
    * @return object with key and value properties of the previous item
    * @type object
    */
    peekBack: function(){
        return this.objectAtIndex(this.iteratorIndex - 1);
    },
    
    /**
    * Moves the iterator back and returns the previous key value pair
    * @return object with key and value properties of the previous item
    * @type object
    */
    prev: function(){
        this.iteratorIndex--;
        return current();
    },
    
    /**
    * Moves the iterator back and returns the previous key value pair
    * @return object with key and value properties of the previous item
    * @type object
    */
    previous: function(){
        return prev();
    },
    
    /**
    * Moves the iterator back to the beginning
    */
    reset: function(){
        this.iteratorIndex = 0;
    }
//*******End Iterators********************

};