/*
	Class: product.js
		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
		
		last modified by Denis Schneiter 18.09.2007
*/
var Product = new Class({
	Implements: [Events, Options],		
	
	options : {
		targetProduct : '.product',		// Product target (the product container)
		targetPrice : '.price',			// Product price target 
		targetVariation : '.prdvars',		// Product variations target (all)
		separator : '\'',					// The char who separe 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._getPrdInfos(el);
			this._getVariation(el);
			this._getBasket(el);
		}, this);
	},
	/*
	Function: _getPrdInfos
		Internal method
		
		get the product ID and the product categoryID
		get the product price.
		the product price must be found into a title (ex: <span title="1200">
		
		Parameters:
			prd - html element
	*/
	_getPrdInfos : function(prd) {
		// Get product ID
		this.prdID = prd.id.replace(/^item/, '');
		
		// Get the product price 
		this.els = prd.getElements(this.options.targetPrice );
		this.els.each(function(el){				
			this.price = el.title;
			this.newprice = this.price;
			this.elprice = 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) {;
		this.els = prd.getElements('.add');
		this.els.each(function(el){
			$(el).addEvent('click', function(e){
				this.basket.add(this.prdID,this.newprice);
			}.bind(this));
							
		}, this);
	},
	/*
	Function: _addVariation
		Internal method
		
		Change the product price according to the variation price
		
		Parameters:
			variationPrice - supplement price 
	*/
	_addVariation : function(prd){
		
		var variationPrice = 0;
		
		var removeSeparator = new RegExp(this.options.separator);
		
		this.newprice = this.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) { 
				variationPrice = variationPrice + el.alt.toInt();			 	
			}	
		}, this);
		
		this.newprice = this.newprice + variationPrice;

		if (this.options.formatPrice == '1') { this._formatPrice(); }
		
		this.elprice.innerHTML = this.newprice;  //inject new price
	},
	/*
	Function: _formatPrice
		Internal method
		
		Add thousand separator and cent
		
	*/
	_formatPrice : function() {
		
		var newformat = "" + this.newprice;	
		
		var sRegExp = new RegExp('(-?[0-9]+)([0-9]{3})');
		while(sRegExp.test(newformat)) {
			newformat = newformat.replace(sRegExp, "$1"+this.options.separator+"$2");
		}
		
		this.newprice = newformat + ".00";				
	}
	
});

window.addEvent('domready',function() {
	var product = new Product();
});
