// JavaScript Document

var Product = Class.create();
Product._name = "";
Product._qty = 0;
Product._price = 0.0;
Product._id = 0;

Product.prototype = {
	initialize: function(product_id, product_name, product_price, product_qty) {
		this._id = product_id;
		this._name = product_name;
		this._price = product_price;
		this._qty = parseInt(product_qty);
		this._active = 1;
	},
	getTotal: function () {
		return parseFloat(this._qty * this._price) * this._active;	
	},
	getQty: function () {
		return parseInt(this._qty * this._active);	
	}	
}

var ShoppingCart = Class.create();
ShoppingCart.prototype = {
	initialize: function(arr_id,arr_prods_names, arr_prods_prices) {
		this._products = new Array();
		this._cart = new Array();
		this._render_slots = new Array();
		this._position = 0;
		
		if(!arr_id) {return false}
		for(var i = 0; i < arr_prods_names.length; i++) {
			var prod = new Product(arr_id[i],arr_prods_names[i], arr_prods_prices[i],0);
			this._products.push(prod);
		}
	},
	addProduct: function(prod_id, prod_name, prod_price) {
		this._products.push(new Product(prod_id, prod_name, prod_price,0));
	},
	addToCart: function(prod_id, prod_qty) {
		//alert(prod_qty);
		// revisamos si ya hay algun producto con ese id
		var f = false;
		this._cart.each(
						function(p) {
							if(p._id == prod_id) {
								p._qty += parseInt(prod_qty);	
								if(p._qty > 6) {p._qty = 6};
								f = true;
							}
						})
		
		if(!f) {
			//alert("hola1");
			// obtenemos la muestra
			var ps = this.productById(prod_id)
			//alert("hola2");
			this._cart.push(new Product(prod_id, ps._name, ps._price, prod_qty))
			//alert("hola3");
			if(this._cart.length > 7) {this._position +=1;};
		}
		
		this.renderCart();
	},
	removeFromCart: function(cs) {
		this._cart.splice(this._render_slots[cs],1);
		this.renderCart();
		//this
	},
	productById: function(prod_id) {
		for(var i=0; i < this._products.length; i++) {
			p = this._products[i];
			if(p._id == prod_id){return p}
		};
		
	},
	cartById: function(prod_id) {
		for(var i=0; i < this._cart.length; i++) {
			p = this._cart[i];
			if(p._id == prod_id){return p}
		};
		
	},	
	getTotal: function () {
		var t = 0;
		
		this._cart.each(function(p) {t += p.getTotal()})
		return t;
	},
	getTotalQty: function () {
		var t = 0;
		this._cart.each(function(p) {t += parseInt(p._qty)})
		return t;
	},
	renderCart: function(){
		//alert("hola1");
		var d=new Date();
		//alert("hola3");
		//alert(d)
		//alert(d.getMonth);
		//if(d.getMonth()>6) {return false}
		//alert("hola2");
		//escondemos todos los slots del carrito
		for (var i=1; i <= 7; i++) {$('cart_slot_'+i).hide();}
		//alert("hola2");
		$('cart_products').style.width= ' 0px';
		
		
		if(this._cart.length != 0) {
			$('cart_guaranteed').hide();
			$('cart_always_free').show();
			$('cart_cart').show();
			$('cart_help_1').hide();
			$('cart_help_2').show();
		} else {
			$('cart_guaranteed').show();
			$('cart_always_free').hide();
			$('cart_cart').hide();
			$('cart_help_1').show();	
			$('cart_help_2').hide();
		}
		// iteramos los productos en el carrito
		// si hay mas de seis productos distintos en el carrito calculamos la posicion:
		if(this._cart.length > 7) {
			this._position = this._cart.length - this._position < 7 ? 0 : this._position;
		}
		
		var c=1; 					 
		for (var i = this._position; i < this._cart.length; i++) {
			var cs = $('cart_slot_'+c);
			var ci = $('cart_img_'+c);
			ci.src = "img/products/cart_"+this._cart[i]._id+".gif";
			
			this._render_slots[c] = i;
			
			var cn = $('cart_number_'+c);
			
			if(this._cart[i]._qty>1) {
				cn.style.backgroundImage = 'url("img/products/'+this._cart[i]._qty+'.gif")';
				cn.show();
			} else {
				cn.hide();
			}
			cs.show();
			c++;
			if(c>7) {break}
			//alert("img/products/cart_"+this._cart[i]._id+".gif");
		}
		
		$('cart_products').style.width= (c-1)*32 + 'px';
		$('cart_total_price').innerHTML = this.getTotal().toFixed(2);
	},
	toLeft:function() {
		var t = this._position-1 <0 ? 0 : this._position-1;
		if(t!=this._position) {
			this._position=t;
			this.renderCart();
		}
	},
	toRight:function() {
		var t = this._cart.length - (this._position+1) < 7 ? this._position : this._position+1;
		if(t!=this._position) {
			this._position=t;
			this.renderCart();
		}
	},
	clear:function() {
		this._cart.clear();
		this._position = 0;
		this.renderCart();
	},
	toJSON:function () {
		return this._cart.toJSON();
	}
}

var ShoppingCheck = Class.create();
ShoppingCheck.prototype = {
	initialize: function() {
		this._products = new Array();
	},
	newProduct: function(prod_id, prod_name, prod_price, prod_qty) {
		this._products.push(new Product(prod_id, prod_name, prod_price,prod_qty	));
	},
	addProduct: function (obj_prod) {
		this._products.push(obj_prod);
		//this._products.each(function(p) {alert("hola" + p._id)})
	},
	productById: function(prod_id) {
		for(var i=0; i < this._products.length; i++) {
			p = this._products[i];
			if(p._id == prod_id){return p}
		};
		
	},	
	getTotal: function () {
		var t = 0;
		
		this._products.each(function(p) {t += p.getTotal()})
		return t;
	},
	changeQty:function (pid, qty) {
		var p = this.productById(pid);
		p._qty = qty;
		renderTotal();
	},
	getQty:function(pid) {
		var p = this.productById(pid);
		return p._qty;
	},
	setActive: function(pid, active) {
		var p = this.productById(pid);
		p._active = active;
		renderTotal();
	},
	renderTotal: function () {
		
	},
	toJSON:function () {
		//return this._products.toJSON();
		var ret_p = new Array();
		this._products.each(function(p) {if (p._active) {ret_p.push(p)}});
		return ret_p.toJSON();
	}	
}