Product = Class.create();

Product.prototype.product_id = 0;
Product.prototype.picture_id = 0;
Product.prototype.product_pics = {};
Product.prototype.container_pic = null;
Product.prototype.container_thumb = null;
Product.prototype.container_info = null;
Product.prototype.lastColor = 0; /* OMARQUE couleur */
Product.prototype.lastSize = 0; /* OMARQUE Taille */

Product.prototype.initialize = function(productId, container_info, container_pic_id, container_thumb_id, lastPicto)
{
	this.container_info = $(container_info);
	this.container_pic = $(container_pic_id);
	this.container_thumb = $(container_thumb_id);
	//this.changeProduct(productId);
	this.onColorSelect(lastPicto,productId); //on selectionne le premier pico (variante affichée)
	this.init_pagin();
}

Product.prototype.init_pagin = function()
{
	var parameters = {};
	parameters.productId = this.product_id;
	//parameters.categoryId = menu_mgr.lastSubCategory;
	new Ajax.Request("frontoffice/ajax/category/getNextAndPreviousGroup.php",{
				method : "post",
			 	evalJSON : "force",
				parameters : parameters,
				onSuccess : this.onSuccessGetNextAndPreviousGroup.bind(this)
			});
}

Product.prototype.onSuccessGetNextAndPreviousGroup = function(xhr)
{
	$("productPrevious").stopObserving("click");
	$("productNext").stopObserving("click");
	
	if(xhr.responseJSON === null || xhr.responseJSON.next === null || xhr.responseJSON.next == 0)
		$("productNext").hide();
	else
	{
//		$("productNext").observe("click",front_mgr.update.bind(front_mgr,"frontoffice/ajax/product/showGroup.php",{
//															"groupId" : xhr.responseJSON.next
//															})); AJAX
		locationNext = "index.php?page=product&groupId=" + xhr.responseJSON.next;	
		$("productNext").observe('click', function(event){
				front_mgr.navigateTo(locationNext)
			 });
		$("productNext").show(); 
	}
	
	if(xhr.responseJSON === null || xhr.responseJSON.previous === null || xhr.responseJSON.previous == 0)
		$("productPrevious").hide();
	else
	{
//		$("productPrevious").observe("click",front_mgr.update.bind(front_mgr,"frontoffice/ajax/product/showGroup.php",{
//															"groupId" : xhr.responseJSON.previous
//															}));
		locationPrevious = "index.php?page=product&groupId=" + xhr.responseJSON.previous;
		$("productPrevious").observe('click', function(event){
			front_mgr.navigateTo(locationPrevious)
		 });
		$("productPrevious").show();
	}
}

Product.prototype._onClickPicture = function(element, pictureId)
{
	if(pictureId != this.picture_id){
		var parameters = {};
		parameters.pictureId = pictureId;
		
		popupPictureZoom.options.parameters.pictureId = pictureId;
		parameters.size = "medium";

		this.picture_id = pictureId;
		
		new Ajax.Updater(this.container_pic,
						 "frontoffice/ajax/product/getPicture.php",{
						 	method : "post",
						 	evalJSON : "force",
							parameters : parameters
					 });
	}
}

Product.prototype.onColorSelect = function(color_id, product_id)
{
	if(Object.isUndefined(product_id))
		product_id = this.product_id;
	if(color_id != this.lastColor){
	last_color_id = this.lastColor;
		if(last_color_id != 0){
			$(last_color_id).removeClassName("pictoSelected");
		}
		this.lastColor = color_id;
		$(color_id).addClassName("pictoSelected");
		this.changeProduct(product_id);
	}
}

Product.prototype.onSizeSelect = function(size_id, product_id)
{
	//if(Object.isUndefined(product_id))
	this.product_id = product_id;
	if(size_id != this.lastSize){
	last_size_id = this.lastSize;
		if(last_size_id != 0){
			$(last_size_id).removeClassName("pictoSelected");
		}
		this.lastSize = size_id;
		$(size_id).addClassName("pictoSelected");
	}
	this.changeProduct(product_id,false);
}

Product.prototype.changeProduct = function(product_id, changePictures)
{
	if(Object.isUndefined(product_id))
		product_id = this.product_id;
	if(Object.isUndefined(changePictures))
		changePictures = true;
	var parameters = {};
	parameters.productId = product_id;
	parameters.ms = new Date();
	this.product_id = product_id;
	new Ajax.Request("frontoffice/ajax/product/getProductInfos.php",{
					 	method : "post",
					 	evalJSON : "force",
						parameters : parameters,
						onSuccess : this._onChangeProductSuccess.bind(this,product_id,changePictures),
						onFailure : this._onChangeProductFailure.bind(this,product_id,changePictures)
					 });
}

Product.prototype._onChangeProductSuccess = function(product_id, changePictures, xhr)
{
	var product_id = xhr.responseJSON.id;
	var product_name = xhr.responseJSON.name;
	var product_desc = xhr.responseJSON.desc;
	var product_amount = xhr.responseJSON.amount.toFixed(2) + " €";
	var product_amount_custom = null;
	if(xhr.responseJSON.amount_custom != 0)
		product_amount_custom = xhr.responseJSON.amount_custom.toFixed(2) + " €";
	
	if(xhr.responseJSON.pictures.length > 0 && changePictures)
	{
		this.container_thumb.update();
		this.picture_id = 0;
	}

	this.container_info.select("[class=\"productTitle\"]")[0].innerHTML = product_name;
	this.container_info.select("[class=\"productDescription\"]")[0].innerHTML = product_desc;
	this.container_info.select("[class=\"productPrice\"]")[0].innerHTML = product_amount;
	if(product_amount_custom !== null)
		this.container_info.select("[class=\"productOldPrice\"]")[0].innerHTML = product_amount_custom;

	if(changePictures)
		xhr.responseJSON.pictures.each(this._insertPictures.bind(this));

	var listPicto = this.container_info.select("[class=\"productPicto\"]","[class=\"productPicto pictoSelected\"]");// $$(".productPicto");
	listPicto.each(this._onUpdatePictos.bind(this,product_id,xhr));
	
	if(xhr.responseJSON.cross.length > 0)
		cross_mgr.changeCross(xhr.responseJSON.cross, xhr.responseJSON.amount);
}

Product.prototype._onUpdatePictos = function(product_id, xhr, item)
{	
	var picto_label = item.id.split("_")[0];
	var picto_id = window.parseInt(item.id.split("_")[1]);
	if(picto_label == "Tailles")
	{
		item.stopObserving("click");
		if(xhr.responseJSON.pictos[picto_id] == null)
		{
			var last_size_id = this.lastSize;
			if(last_size_id == item.id)
			{
				$(last_size_id).removeClassName("pictoSelected");
				this.lastSize = 0;
			}
			item.setOpacity(0.2);
		}else
		{
			item.setOpacity(1.0);
			item.observe("click",this.onSizeSelect.bind(this,item.id,xhr.responseJSON.pictos[picto_id]));	
		}
	}
}

Product.prototype._insertPictures = function(item)
{
	var picture_id = item.id;
	var picture_name = item.name;
	var picture_urlBig = item.urlBig;
	var picture_urlMedium = item.urlMedium;
	var picture_urlMediumSmall = item.urlMediumSmall;
	var picture_urlSmall = item.urlSmall;
	
	var pictureSmall = new Element("img",{"id" : "picture_"+picture_id,
									"src" : picture_urlSmall});
	this.container_thumb.insert(pictureSmall);
	
	this.product_pics[picture_id] = pictureSmall;
	this.product_pics[picture_id].observe("click",this._onClickPicture.bind(this,pictureSmall,picture_id));
	if(this.picture_id == 0)
	{
		this.product_pics[picture_id].fireNativeEvent("click");
	}
}

Product.prototype._onChangeProductFailure = function(product_id, xhr)
{
	alert(xhr.responseJSON.error);
}