/*
Class for Car objects
*/

function CarItem(id, manufacturerName, modelName, price, year, mileage, photoId) {
	this.id = id;
    this.manufacturerName = manufacturerName;
    this.modelName = modelName;
    this.priceString = price;
    this.yearString = year;
    this.mileageString = mileage;
    this.photoId = photoId;
    return this;
}

/*
Class for storing and operating cars
*/
function CarsList () {
    this.list = null;
    this.defaultLimit = 30;
    this.limit = null;
}

CarsList.prototype.initialize = function(limit) {
    if(limit != undefined && limit != null) {
        this.limit = limit;
    }
    else {
        this.limit = this.defaultLimit;
    }
    this.list = new Array();
};

CarsList.prototype.isInList = function(id) {
    for(var i = 0; i < this.limit; i++) {
        if(id == this.list[i].id) {
            return i;
        }
    }
    return null;
};

CarsList.prototype.isEmpty = function() {
    return this.list.length == 0;
};

CarsList.prototype.addCar = function(car) {
    this.list.pop();
    var index = this.isInList(car.id);
    if(index != null) {
        this.list.splice(index, 1);        
    }
    this.list.splice(0, 0, car);
};

CarsList.prototype.reset = function() {
    this.list = new Array();
};

CarsList.prototype.restoreFromJSON = function(JSONData, limit) {
    this.list = JSON.parse(JSONData);
    this.limit = limit;
};

/*
CarsList.prototype.storeInCookies = function(prefix, expirationDate) {
    if(expirationDate) {
        $.cookie(prefix + "_carsList", JSON.stringify(this.list), {expires: expirationDate, path: "/"});
    }
    else {
        $.cookie(prefix + "_carsList", JSON.stringify(this.list), {path: "/"});
    }
};
*/

/*
CarsList.prototype.restoreFromCookies = function(prefix, limit) {
    this.list = JSON.parse($.cookie(prefix + "_carsList"));
    if(this.list == null) {
        this.list = new Array(limit);
    }
    this.limit = limit;
    this.count = 0;
    for(var i = 0; i < this.limit; i++) {
        if(this.list[i] == undefined) {
            break;
        }
        this.count ++;
    }
};
*/

