/*
	Class: FormCheck
		Performs different action to the product.
		
	Usage:
		For instant, use to manage product variation
		
	Parameters:
		targetProduct - Product target (the product container)
		targetPrice - Product price target 
		targetVariation - Product variations target (all)
		separator - The char who separe thousand 
		formatPrice - Choose if the price must be formated // 0 : no format price (ex:1200) / 1 : format (ex:1'200.00)
		
	Exemple:
	
	About:
		product.js v.1.0 for mootools v1.1 05 / 2007
		
		by Floor SA (http://www.floor.ch) MIT-style license
		
		modified by Denis Schneiter 18.09.2007
		modified by sb 10.05.2010
*/
var Product = new Class({
	Implements: [Events, Options],		
	
	options : {
		targetProduct : '.product',		// Product target (the product container)
		targetPrice : '.priceinfo',			// Product price target
		targetPriceVar : '.priceinfo .price',			// Product price target for Variation 
		targetVariation : '.prdvars',		// Product variations target (all)
		separator : '\'',					// The char who separate thousand 
		formatPrice : '1'					// 0 : no format price (ex:1200) / 1 : format (ex:1'200.00)
	},

    /*
	Constructor: initialize
		Constructor
	
		Get the product price and add Event into some product variations (whit the variation price)
	*/
	
	initialize : function(options) {
		this.setOptions(options)
	
		this.basket = new Basket({
			'container' : 'basketviewer',
			'lang' : 'fr'
		});
	
		$(document.body).getElements(this.options.targetProduct).each(function(el){
			this._getVariation(el);
			this._getBasket(el);
		}, this);
	},
	/*
	Function: _getVariation
		Internal method
		
		get all variations and his price supplement for the product.
		add Event to change variations
		
		Parameters:
			prd - html element
	*/
	_getVariation : function(prd) {
		this.els = $(document.body).getElements('.' + this.options.targetVariation, prd);
		this.els.each(function(el){
			el.addEvent('click', function(e){
				this._addVariation(prd);
			}.bind(this))	
		}, this);
	},
	/*
	Function: _getBasket
		Internal method
		
		get all variations and his price supplement for the product.
		add Event to change variations
		
		Parameters:
			prd - html element
	*/
	_getBasket : function(prd) {
		if (!prd.getElement(this.options.targetPrice ))
			return false;
		// get productID
		var prdID = prd.id.replace(/^item|^attach/, '');
		// Get the product price 
		var price = prd.getElement(this.options.targetPrice ).get('html');
		
		this.els = prd.getElements('.add');
		this.els.each(function(el){
			$(el).addEvent('click', function(e){
				this.basket.add(prdID,price);
			}.bind(this));
							
		}, this);
	},
	/*
	Function: _addVariation
		Internal method
		
		Change the product price according to the variation price
		
		Parameters:
			variationPrice - supplement price 
	*/
	_addVariation : function(prd){
		// Get the product price 
		var price = prd.getElement(this.options.targetPriceVar ).get('title');
		
		var variationPrice = 0;
		
		var removeSeparator = new RegExp(this.options.separator);
		
		var newprice = price.replace(removeSeparator, '').toInt(); // clear thousand separator and change string to int
		
		this.els = $(document.body).getElements('.' + this.options.targetVariation, prd);
		this.els.each(function(el){
			if(el.checked == true && el.alt) { 
				el.alt = el.alt.replace(removeSeparator, '').toInt();
				variationPrice = variationPrice.toInt() + el.alt.toInt();			 	
			}	
		}, this);
		//console.log(this.newprice,variationPrice);
		newprice = newprice + variationPrice;
		//console.log(this.newprice);
		if (this.options.formatPrice == '1') { newprice = this._formatPrice(newprice); }
		
		prd.getElement(this.options.targetPriceVar ).innerHTML = newprice;  //inject new price
	},
	/*
	Function: _formatPrice
		Internal method
		
		Add thousand separator and cent
		
	*/
	_formatPrice : function(price) {
		
		var newformat = "" + price;	
		
		var sRegExp = new RegExp('(-?[0-9]+)([0-9]{3})');
		while(sRegExp.test(newformat)) {
			newformat = newformat.replace(sRegExp, "$1"+this.options.separator+"$2");
		}
		
		return newformat + ".00";				
	}
	
});

window.addEvent('domready',function() {
	var product = new Product();
	$$('.order .priceinfo').each(function(el){
		var price = '';
		if (el.getElement('.price'))
			price = el.getElement('.price').get('html');
		else
			price = el.get('html');
		if (price == '0.00') {
			el.set('html','prix sur demande')
			el.set('style','width:90%');
			el.getPrevious().destroy();
		}
	});
});
