/*
  Container classes
  (c) 2007 Shopzeus.com
*/

if (typeof Array.prototype.indexOf != "function") {
    Array.prototype.indexOf = function(avalue) {
        for (var i = 0; i<this.length ; i++ ){
            if (this[i] === avalue) {
                return i;
            }
        }
        return -1;
    }
}


function Dict() {
    this.keys = new Array();
    this.values = new Array();
}

Dict.prototype.hasKey = function(akey) {
    return (this.keys.indexOf(akey)>=0);
}
Dict.prototype.hasValue = function(avalue) {
    return (this.values.indexOf(avalue)>=0);
}


Dict.prototype.getValue = function(akey) {
    var idx = this.keys.indexOf(akey);
    if (idx>=0) {
        return this.values[idx];
    }   else {
        return undefined;
    }
}


Dict.prototype.setValue = function(akey,avalue) 
{
    var idx = this.keys.indexOf(akey);
    if (idx>=0) {
        this.values[idx] = avalue;
    } else {
        this.keys.push(akey);
        this.values.push(avalue);
    }
}

Dict.prototype.remove = function(akey) 
{
    var idx = this.keys.indexOf(akey);
    if (idx>=0) {
        this.keys.splice(idx,1);
        this.values.splice(idx,1);
    }
}

// Extra method: find key by value. We can use it as a reverse dictionary instantly ;-)
Dict.prototype.findKeyOf = function(avalue) 
{
    var idx = this.values.indexOf(avalue);
    if (idx>=0) {
        return this.keys[idx];
    }   else {
        return undefined;
    }
}


Dict.prototype.ToString = function ()
{
    var res = "Dict(";
    for (var i=0;i<this.keys.length;i++) {
        res += "'" + this.keys[i].toString() + "':'" + this.values[i].toString();
        if (i<this.keys.length-1) {
            res += ",";
        }
    }
    res += ")";
    return res;
}


