/*
Class responsible for cars selection
*/

function Selection () {
    this.list = "";
    this.limit = 10;
    this.limitationError = "Only 10 cars can be selected.";
    this.truncationError = "Only {0} cars left to be selected from {1}.";
}

Selection.prototype.initialize = function(limit, limitationError, truncationError) {
    this.limit = limit;
    this.limitationError = limitationError;
    this.truncationError = truncationError;
};

Selection.prototype.isInSelection = function(id) {
    var selection = this.getSelectionList();
    var arrayLength = selection.length;
    for(var i = 0; i < arrayLength; i++) {
        if(id == selection[i]) {
            return true;
        }
    }
    return false;
};

Selection.prototype.isEmpty = function() {
    return this.list == "";
};

Selection.prototype.getSelectionList = function() {
    if(this.limit != 0) {
        return this.list.split(",", this.limit);
    }
    else {
        return this.list.split(",");
    }
};

Selection.prototype.getSelectionCount = function() {
    if(this.list == "") {
        return 0;
    }
    var selectionArray;
    if(this.limit != 0) {
        selectionArray = this.list.split(",", this.limit);
    }
    else {
        selectionArray = this.list.split(",");
    }
    return  selectionArray.length;
};

Selection.prototype.getPosition = function(id) {
    var selection = this.getSelectionList();
    var arrayLength = selection.length;
    for(var i = 0; i < arrayLength; i++) {
        if(id == selection[i]) {
            return i;
        }
    }
    return null;
};

Selection.prototype.insertIntoBeginning = function(id) {
    if(this.list != "") {
        var isSelected = this.isInSelection(id);
        if(isSelected) {
            this.deleteFromSelection(id);
        }
        else {
            if(this.getSelectionCount() == this.limit) {
                this.deleteFromSelectionByIndex(this.limit - 1);
            }
        }
        this.list = id + (this.list != "" ? ("," + this.list) : "");
    }
    else {
        this.list = id + "";
    }
    return true;
};

Selection.prototype.addToSelection = function(id, silent) {
    if(this.list != "") {
        var isSelected = this.isInSelection(id);
        if((this.getSelectionCount() < this.limit || this.limit == 0)
                && !isSelected) {
            this.list += "," + id;
        }
        else {
            if(!isSelected && !silent) {
                alert(this.limitationError);
            }
            return false;
        }
    }
    else {
        this.list = id + "";
    }
    return true;
};

Selection.prototype.addArrayToSelection = function(array, silent) {
    if(! silent && this.getSelectionCount() == this.limit && this.limit != 0) {
        alert(this.limitationError);
        return new Array();
    }

    var newItemsCount = 0;
    var itemsAdded = new Array();
    var oldCount = this.getSelectionCount();
    var arrayLength = array.length;
    for(var i = 0; i < arrayLength; i++) {
        var isSelected = this.isInSelection(array[i]);
        if( ! isSelected) {
            newItemsCount++;
        }
        if((this.getSelectionCount() < this.limit || this.limit == 0) && !isSelected) {
            if(this.list == "") {
                this.list = "" + array[i];
            }
            else {
                this.list += "," + array[i];
            }
            itemsAdded[itemsAdded.length] = array[i];
        }
    }
    if(! silent && (oldCount + newItemsCount > this.limit) && this.limit != 0) {
        alert(this.truncationError
                .replace("{0}", oldCount)
                .replace("{1}", itemsAdded.length));
    }
    return itemsAdded;
};

Selection.prototype.deleteFromSelection = function(id) {
    var selectionArray = this.getSelectionList();
    var arrayLength = selectionArray.length;
    var result = "";
    for(var i = 0; i < arrayLength; i++) {
        if(id != selectionArray[i]) {
            if(result != "") {
                result+= ",";
            }
            result += selectionArray[i];
        }
    }
    this.list = result;
};

Selection.prototype.deleteFromSelectionByIndex = function(index) {
    if(index < this.getSelectionCount() && index >=0 ) {
        var selectionArray = this.getSelectionList();
        selectionArray.splice(index, 1);
        this.list = selectionArray.join(",");
    }
};

Selection.prototype.resetSelection = function() {
    this.list = "";
};

Selection.prototype.storeInCookies = function(prefix, expirationDate) {
    if(expirationDate) {
        $.cookie(prefix + "_selection", this.list, {expires: expirationDate, path: "/"});
    }
    else {
        $.cookie(prefix + "_selection", this.list, {path: "/"});
    }
};

Selection.prototype.restoreFromCookies = function(prefix) {
    this.list = $.cookie(prefix + "_selection");
    if(this.list == null) {
        this.list = "";
    }
};

/*
Switching elements in selection list
@id
    value of element, which needs to be switched
@switchDistance
    offset of element, which should be switched with

    Sample: switchItems(3, -2). Method searches for element, which equals "3".
    For example, its position is "i". Then:
    switching elements with "i" and "i-2" positions
*/
Selection.prototype.switchSelectionItems = function(id, switchDistance) {
    var selectionArray = this.getSelectionList();
    var arrayLength = selectionArray.length;
    for(var i = 0; i < arrayLength; i++) {
        if(id == selectionArray[i]) {
            var j = i + switchDistance;
            var tmp = selectionArray[j];
            selectionArray[j] = id;
            selectionArray[i] = tmp;
            break;
        }
    }
    this.list = selectionArray.join(",");
};

