/*
 * Item.js
 * by Jarkko Tuomala
 *
 * Copyright (c) Jarkko Tuomala, 2005.
 *
 */

function Item(id, shortname, longname, desc, age, price, discount, category, image, dimension, stock, itemdate) {
    if ( arguments.length > 0 ) {
        this.init(arguments);
    }
}

Item.prototype.init = function(args) {
    this._id = (args[0]) ? args[0] : null;
    this._shortname = (args[1]) ? args[1] : null;
    this._longname = (args[2]) ? args[2] : null;
    this._desc = (args[3]) ? args[3] : null;
    this._age = (args[4]) ? args[4] : null;
    this._price = (args[5]) ? args[5] : null;
    this._discount = (args[6]) ? args[6] : null;
    this._category = (args[7]) ? args[7] : null;
    this._image = (args[8]) ? args[8] : null;
    this._dimension = (args[9]) ? args[9] : null;
    this._stock = (args[10]) ? args[10] : null;
	this._itemdate = (args[11]) ? args[11] : null;
    this._basket = 0;
};

Item.prototype.getId = function() {
    return this._id;
};

Item.prototype.getShortname = function() {
    return this._shortname;
};

Item.prototype.getLongname = function() {
    return this._longname;
};

Item.prototype.getDescription = function() {
    return this._desc;
};

Item.prototype.getAge = function() {
    return this._age;
};

Item.prototype.getPrice = function() {
    return this._price;
};

Item.prototype.getDiscount = function() {
    return this._discount;
};

Item.prototype.setDiscount = function(iDiscount) {
   this._discount = iDiscount;
};

Item.prototype.getCategory = function() {
    return this._category;
};

Item.prototype.getImage = function() {
    return this._image;
};

Item.prototype.getDimension = function() {
    return this._dimension;
};

Item.prototype.getStock = function() {
    return this._stock;
};

Item.prototype.getItemDate = function() {
    return this._itemdate;
};

Item.prototype.getBasket = function() {
    return this._basket;
};

Item.prototype.setBasket = function(iAmount) {
    this._basket = iAmount;
};

Item.prototype.getItemPrice = function() {
	var price = this.getPrice();
	var sPrice = String(price);
	var discount = parseFloat(this.getDiscount());
	if (discount > 0) {
		sPrice = String(Math.round(price*(1-discount)*100)/100);
	}
	return parseFloat(sPrice);
};

var ItemManager =
{
    _item: null,
	 _bundle: null,
	 
    Initialise: function()
    {
        if (this._item == null)
        {
         this._item = [];
        }
    },
	 
	 InitialiseBundle: function()
    {
        if (this._bundle == null)
        {
         this._bundle = [];
        }
    },

    Add: function(item)
    {
       this.Initialise();

		 this._item.push(item);
    },

	 CheckItemInBundle: function(item)
    {
		if (this._bundle) {
			var i;
			var bundleItem = null;
			for (i=0;i<this._bundle.length;i++) {
				bundleItem = this._bundle[i];
				if (bundleItem.getId() == item.getId()) {
					return true;
				}			
			}
		}
		return false;
    },
	 
	 getBundleString: function()
    {
		if (this._bundle) {
			var i;
			var bundleItem = null;
			var bundleArray = new Array(0);
			for (i=0;i<this._bundle.length;i++) {
				bundleItem = this._bundle[i];
				bundleArray.push(bundleItem.getId() + "_" + bundleItem.getBasket());
			}
			return (bundleArray.length > 0) ? bundleArray.join(";") : null;
    	} else {
			return null;
		}
	},
	
	 readBundleString: function(bundlestring)
    {
		var i;
		var bundleItem = null;
		var bundleArray = bundlestring.split(";");
		for (i=0;i<bundleArray.length;i++) {
			bundleItem = ItemManager.getItemById(bundleArray[i].substring(0,bundleArray[i].indexOf("_")));
			if (bundleItem) {
				ItemManager.AddToBundle(bundleItem);
				bundleItem.setBasket(parseInt(bundleArray[i].substring(bundleArray[i].indexOf("_")+1,bundleArray[i].length)));
			}
		}
    },
	 
	 fetchBundleInformation: function() 
	{
		if (this._bundle) {
			var i;
			var bundleItem = null;
			var bundleArray = new Array(0);
			for (i=0;i<this._bundle.length;i++) {
				bundleItem = this._bundle[i];
				bundleArray.push(bundleItem.getId()+" "+bundleItem.getShortname()+" "+bundleItem.getBasket()+" kpl "+bundleItem.getItemPrice()+" €");
				
			}
			return (bundleArray.length > 0) ? bundleArray.join(String.fromCharCode(10)) : null;
		} else {
			return null;
		}
	},
	 
	 getTotalInBundle: function()
    {
		if (this._bundle) {
			var i;
			var bundleItem = null;
			var total = 0;
			var price;
			var amount;
			for (i=0;i<this._bundle.length;i++) {
				bundleItem = this._bundle[i];
				price = parseFloat(bundleItem.getItemPrice());
				amount = parseInt(bundleItem.getBasket());
				total += (price * amount);
			}
			return Math.round(total*100)/100;
		}
	},
	 
	 AddToBundle: function(item)
    {
       this.InitialiseBundle();
		 if (!this.CheckItemInBundle(item)) {
			this._bundle.push(item);
		 }
		 item.setBasket(item.getBasket()+1);
    },
	 
	 RemoveFromBundle: function(item)
    {
		if (this._bundle) {
			var i;
			var bundleItem = null;
			var removed = null;
			for (i=0;i<this._bundle.length;i++) {
				bundleItem = this._bundle[i];
				if (bundleItem.getId() == item.getId()) {
					removed = this._bundle.remove(i);
				}			
			}
			if (removed != null) {
				item.setBasket(0);
			}
		}
    },
	 
	 getItemsInBundle: function()
    {
		return this._bundle;
    },
	 
    getItemById: function(id)
    {
		var i;
		var item = null;
		for (i=0;i<this._item.length;i++) {
			item = this._item[i];
			if (item.getId() == id) {
				break;
			}
		}
		return item;
    },
	 
	 getItemsByKey: function(key)
    {
		var i;
		var item = null;
		var items = new Array(0);
		for (i=0;i<this._item.length;i++) {
			item = this._item[i];
			if (item.getShortname().indexOf(key) != -1 || item.getLongname().indexOf(key) != -1 || item.getDescription().indexOf(key) != -1) {
				items.push(item);
			}
		}
		return items;
    },
	 
	 getItemByShortname: function(name)
    {
		var i;
		var item = null;
		for (i=0;i<this._item.length;i++) {
			item = this._item[i];
			if (item.getShortname() == name) {
				break;
			}
		}
		return item;
    },

	 getItemsByAge: function(age)
    {
		var i;
		var item = null;
		var items = new Array(0);
		for (i=0;i<this._item.length;i++) {
			item = this._item[i];
			if (item.getAge() == age) {
				items.push(item);
			}
		}
		return items;
    },
	 	 
	 getItemsByCategory: function(category)
    {
		var i;
		var item = null;
		var items = new Array(0);
		for (i=0;i<this._item.length;i++) {
			item = this._item[i];
			if (item.getCategory() == category) {
				items.push(item);
			}
		}
		return items;
    },

	 getDiscountedItems: function()
    {
		var i;
		var item = null;
		var items = new Array(0);
		for (i=0;i<this._item.length;i++) {
			item = this._item[i];
			if (parseFloat(item.getDiscount()) > 0) {
				items.push(item);
			}
		}
		return items;
    },
	
	 getItemOfDay: function()
    {
		var i;
		var days;
		var item = null;
		var items = new Array(0);
		var today = new Date();
		var itemdate = null;
		for (i=0;i<this._item.length;i++) {
			item = this._item[i];
			if (item.getItemDate() != null) {
				itemdate = new Date(item.getItemDate());
				days = Math.round((itemdate-today)/(1000*60*60*24));
				if (parseInt(days) == 0) {
					item.setDiscount('0.1');
					return item;
				}
			}
		}
		return null;
    },
	 	 
	 getAllItems: function()
    {
		return this._item;
    }
};