/******************************************************************************
* ixCarPriceManagerComplement.js
******************************************************************************/


function ixCarPriceManagerComplement() {
	this._pivotFromReference = new Object();
}

ixCarPriceManagerComplement.prototype.getCurrentReferenceOid = function(manager) {
	var currentProduct = objGshpPriceManager.currentProduct;
	var currentReference = currentProduct.currentReference;
	return currentReference.oid;
}

ixCarPriceManagerComplement.prototype.onChangeDimension = function (select) {
	if (typeof (lookMode) == 'undefined') {
		var ref = objGshpPriceManagerComplement.getCurrentReferenceOid(objGshpPriceManager);
		var inputQuantity = YAHOO.util.Dom.getElementsByClassName('ixCarBasketQuantityInput', 'input');
		inputQuantity[0].setAttribute("id", "gshpBasketQuantityInputId_" + ref);

		var addBtn = YAHOO.util.Dom.getElementsByClassName('ixCarAddToBasket', 'div');
		if (objGshpPriceManagerComplement.getCurrentStock(objGshpPriceManager) < 1) {
			YAHOO.util.Dom.addClass(addBtn, 'ixCarHidden');
		}
		if (objGshpPriceManagerComplement.getCurrentStock(objGshpPriceManager) > 0) {
			YAHOO.util.Dom.removeClass(addBtn, 'ixCarHidden');
		} 
	}

}

ixCarPriceManagerComplement.prototype.getCurrentStock = function(manager) {
	var currentProduct = objGshpPriceManager.currentProduct;
	var currentReference = currentProduct.currentReference;
	return currentReference.stock;
}

ixCarPriceManagerComplement.prototype.onClickPackItem = function (manager, checkbox, product) {
	// div element css
	var div = document.getElementById('ixCarLookItem_' + product.oid);
	if (div) {
		if (checkbox.checked)
			div.className = "ixCarLookItemOn";
		else
			div.className = "ixCarLookItemOff";
	}

	// enable/disable dimension select
	var i = 0;
	for (i = 0; i <= 5; i++) {
		var select = document.getElementById('gshpReferenceDimensionSelect_' + product.oid + '_packItem_' + i);
		if (select)
			select.disabled = !checkbox.checked;
	}

	// add/remove product oid to "facoOidProductList" hidden input (list of product added to basket)
	var input = document.getElementById('ixCarOidProductList');
	var value = input.value;
	if (checkbox.checked) {
		// add (if not already here)
		if (value == "" || value.indexOf("" + product.oid) < 0) {
			input.value = input.value + ' ' + product.oid;
		}
	}
	else {
		// remove if exists
		var reg = new RegExp("( " + product.oid + ")", "g");
		if (value != "" && value.indexOf("" + product.oid) >= 0) {
			input.value = input.value.replace(reg, '');
		}
	}
}

ixCarPriceManagerComplement.prototype.associateReferences = function (map) {
	if (typeof(lookMode) == 'undefined') {
		this._pivotFromReference = map;
	}
	else {
		for (var key in map)
			this._pivotFromReference[key] = map[key];
	}
}

ixCarPriceManagerComplement.prototype.getPivotOid = function (referenceOid) {
	return this._pivotFromReference[referenceOid]
}

ixCarPriceManagerComplement.prototype.getCurrentReferenceOid = function (manager) {
	if (typeof(lookMode) == 'undefined') {
		var currentProduct = objGshpPriceManager.currentProduct;
		var currentReference = currentProduct.currentReference;
		if (currentReference != null)
			return currentReference.oid;
	}
}

var objGshpPriceManagerComplement = new ixCarPriceManagerComplement();

function ixCarMultipleAddToBasket() {

	var oid = objGshpPriceManager.currentPack.oid;
	var q = '1';
	var selectionnedProductList = 0;

	if (oid != null) {
		var packReferencesDescr = "";
		var productList = objGshpPriceManager.currentPack.productList;
		for (var ind = 0; ind < productList.length; ind++) {
			var product = productList[ind];
			if (product.isSelectedPackItem() == true) {
				selectionnedProductList++;
				var description = objGshpPriceManager._buildPackItemDescription(product, parseInt(q, 10));
				if (description != "") {
					if (packReferencesDescr != "") packReferencesDescr += "|";
					packReferencesDescr += description;
				}
			}
		}

		if (q.match(/^[1-9][0-9]*$/) && selectionnedProductList > 0) {
			gshp.basket.on('ready', function () {
				gshp.basket.addPack(oid, parseInt(q, 10), packReferencesDescr);
			});
		}
		else
			alert("Pas de produit sélectionné");
	}
	
}
