/*
	Class: reservation
		Init ClientSide Reservation mechanism
		
	
		
	Parameters:
		
	Exemple:
	
	About:
		reservation.js v.1.0 for mootools v1.2.5 09 / 2011
		
		by Floor Sàrl (http://www.floor.ch) MIT-style license
		
		modified by sb 09.2011
*/
var Reservation = new Class({
	Implements: [Events, Options],					   
	
	options : {
		TargetIncluded	: '.included',
		TargetOptions	: '.options',
		TargetPrice		: '.total>div>span',
		TargetQty		: '.qty',
		
		separator		: "'"
	},
	
	/*
		initialize
		
		contructor
	*/
    initialize: function(el,options) {
		this.setOptions(options);
		
		this.Element = el;
		
		this._initReservationOptions();
		
		this._calculatePrice();
    },
	
	/*
		_calculatePrice
		Private
		
		calculate price of the reservation
	*/
	_calculatePrice : function()	{
		var qty = 1;
		if (this.Element.getElement(this.options.TargetQty))
			qty = this.Element.getElement(this.options.TargetQty).value.toInt();
		if (!qty) qty = 1;
		
		var total = 0;
		var price = this._getElementPrice(this.Element.get('class'));
		total += price;
		this.Element.getElement(this.options.TargetIncluded).getElements('li').each(function(el){
			var price2 = this._getElementPrice(el.get('class'));
			total += price2;
		},this);
		total = total*qty;
		this.Element.getElement(this.options.TargetPrice).set('html',this._formatPrice(total));
	},
	
	/*
		_getElementPrice
		Private
		
		get element price out of a string (price['12.00'])
	*/
	_getElementPrice : function(str)	{
		var r;
		if (r = str.match(/price\[\'(\d+\.\d{2})\']/))	{
			return r[1].toInt();
		}
		return 0;
	},
	
	/*
		_initReservationOptions
		Private
		
		init Reservation boxes to add/remove an option
	*/
	_initReservationOptions : function()	{
		this.Element.getElement(this.options.TargetQty).addEvent(
			'click',function(e)	{
				this._calculatePrice();
			}.bind(this));
		this.Element.getElement(this.options.TargetOptions).getElements('li').each(function(el){
			el.addEvents({
				'click'	: function(e)	{
					e.stop();
					this._addRemoveOption(el,'add');
				}.bind(this)
			});
		},this);
		this.Element.getElement(this.options.TargetIncluded).getElements('li').each(function(el){
			el.addEvents({
				'click'	: function(e)	{
					e.stop();
				}.bind(this)
			});
		},this);
	},
	
	/*
		_addRemoveOption
		Private
		
		add/remove an option
	*/
	_addRemoveOption : function(el,type)	{
		var target = this.options.TargetIncluded;
		if (type != 'add') {
			target = this.options.TargetOptions;
			type = 'add';
		}
		else
			type = 'inc';
		var newEl = el.clone();
		newEl.addEvent('click',function(e)	{
			e.stop();
			this._addRemoveOption(newEl,type);
		}.bind(this));
		newEl.inject(this.Element.getElement(target));
		el.destroy();
		this._calculatePrice();
	},
	
	/*
		_formatPrice
		Private
		
		Add thousand separator and cent
	*/
	_formatPrice : function(price) {
		if (!price && price != '0') return;
		price = price.round(2)  
		
		var newformat = "" + price;	
		var newprice = price;
		
		// manage thousand separator
		var sRegExp = new RegExp('(-?[0-9]+)([0-9]{3})');
		while(sRegExp.test(newformat)) {
			newformat = newformat.replace(sRegExp, "$1"+this.options.separator+"$2");
		}
		
		// manage decimal 
		if (price.toFloat() == price.toInt()) {		
			return newformat + ".00";
		} else if ((price*10).toFloat() == (price*10).toInt()) {	
			return newformat + "0";
		} else {
			return newformat;
		}
	}
});



