//$Id: tricker.js,v 1.1 2010-07-05 13:04:05 cg Exp $
var  Ticker =  new Class( {
	_min:false,
	_max:false,

	_draggerLeft:false,
	_draggerRight:false,
	_bar:false,
	_zonePrice:false,
	_stepPrice : false,
	_offset:0,
	_offsetRight:0,
	_stepSize:0,
	_onFinish: function(){},

	/**
	 * construct
	 * options:
	 * min: min price, default 0, must be number
	 * max: Max price must be number
	 * Step: Step between Min and max,  must be number
	 * priceHtml: html code to write prices. Tag '<price>' automatically replace by current price
	 * draggerLeft: left Drag
	 * draggerRight: right drag
	 * bar : the bar element
	 * zonePrice: where prices are write
	 */
	initialize : function( params ){

	 params = $extend({min:0,max:false,step:9,precisionMultiplier:1,onFinish:false,offsetRight:0,offset:0,priceHtml:"<li><price>&euro;</li>",draggerLeft:false,draggerRight:false,bar:false,zonePrice:false},params);

	if("number" != typeof params.min  )
		throw new TickersException("Min must be a number");

	if("number" != typeof params.max  )
		throw new TickersException("Max must be a number");

	if("number" != typeof params.step  )
		throw new TickersException("Step must be a number");



	if("number" != typeof params.precisionMultiplier  )
		throw new TickersException("precisionMultiplier must be a number");

	if("number" != typeof params.offset )
		throw new TickersException("offset must be a number");
	if("number" != typeof params.offsetRight )
		throw new TickersException("offsetRight must be a number");

	if("function" != typeof params.onFinish  )
		throw new TickersException("onFinish must be a function");

	this._onFinish = params.onFinish;

	if(params.min >= params.max)
		throw new TickersException("Min  must be < max");

	this._offsetRight = params.offsetRight;

	this._max = params.max;
	this._min = params.min;

	this._draggerLeft = $(params.draggerLeft);
	if(!this._draggerLeft)
		throw new TickersException("DraggerLeft not in dom");


	this._zonePrice = $(params.zonePrice);
	if(!this._zonePrice){
		this._zonePrice = new Element("div");
	}
		//throw new TickersException("zonePrice not in dom");

	this._draggerRight = $(params.draggerRight);
	if(!this._draggerRight)
		throw new TickersException("DraggerRight not in dom");

	this._bar = $(params.bar);

	if(!this._bar)
		throw new TickersException("Bar not in dom");

	var max = ( params.step* params.precisionMultiplier);
	var size = this._bar.getWidth();
	this._stepSize  =( size / max);
	currentPrice = this._min;
	this._stepPrice =new  Array(max);
	var i;
	var prices = "";

	var ecart = ((this._max - this._min)/( params.step));
	++ params.step;
	var round = (ecart < params.step  )

	var lang = 'en-en';
	// TRY TO GET CURRENT LANGUAGE
	var htmlTag = $$('html');

	if(htmlTag){
		htmlTag = htmlTag[0];
		lang = htmlTag.lang;

	//MooTools.lang.setLanguage(lang);
	}
	for(i = 0;i<params.step;++i){
		var price = round?currentPrice.round(2):currentPrice.round();

		//FIXME : TROUVE MIEU
		if(lang.toLowerCase() != 'en-en')
		price = price.toString().replace('\.',',');

		prices += params.priceHtml.replace("<price>",price);
		currentPrice+=ecart;
	}

	this._zonePrice.set('html',prices);
	var smallEcart = (ecart /  params.precisionMultiplier);

	currentPrice =  this._min;
	++max;
	for(i = 0;i<max;++i){

		currentPrice = currentPrice;
		this._stepPrice[i] =round?currentPrice.round(2):currentPrice.round() ;
		currentPrice+=smallEcart;
	}



	this._draggerLeft.slide = new Drag(	this._draggerLeft ,
	 {

		limit:{x:[0,size],y:[0,0]},
		onDrag:this._setSlideLimit.bind(this),
		onComplete:this._updateBorne.bind(this),
		preventDefault :true,
		snap: 0

	 }
	);



	this._draggerLeft.setStyle('left',0);
	this._draggerLeft.slide.limit ={x:[0,size],y:[0,0]};
	this._draggerRight.slide = new Drag(this._draggerRight,
	 {

		limit:{x:[0,size-(this._draggerRight.getWidth())],y:[0,0]},
		onDrag:this._setSlideLimit.bind(this),
		onComplete:this._updateBorne.bind(this),
		preventDefault :true,
		snap: 0

	 }
	);
	this._draggerRight.slide.limit ={x:[0,size-(this._draggerRight.getWidth())],y:[0,0]};
	this._draggerRight.setPosition({x:size-this._draggerRight.getWidth()});
	this._draggerLeft.slide.value.now['x'] = 0;
	this._draggerRight.slide.value.now['x'] = size-this._draggerRight.getWidth();

},//initialize




_updateBorne : function(){
	if(	window.releaseCapture )
		window.releaseCapture();
	this._moveBar();
	var	sL = ((this._draggerLeft.slide.value.now['x']+this._offset)/this._stepSize).round();
	var sR = ((this._draggerRight.slide.value.now['x']+this._offsetRight)/this._stepSize).round();

	if(sR > this._stepPrice.length-1)
		--sR;

	var min = this._stepPrice[sL];
	var max = this._stepPrice[ sR];
	this._onFinish(min,max);

},//_updateBorne

_moveBar: function(){

	var leftL = this._draggerLeft.getStyle('left').toInt();;
	var leftR = this._draggerRight.getStyle('left').toInt();

	if(isNaN(leftL))
		leftL =0;

	if(isNaN(leftR))
		leftR =this._draggerRight.getPossition().x;

	var rW = this._draggerRight.getWidth();
	var lW = this._draggerLeft.getWidth();
	var width = (leftR-leftL) + (rW/4) + (rW/2);
	var left = leftL + (lW/2);

	this._bar.setStyles({
		"width": width+"px",
		"left":left.round()+"px"

	})

},

_setSlideLimit : function(elm,d){

		var pos = (this._draggerLeft == elm) ? 1 : 0;
		var drag = (pos == 1) ? this._draggerLeft : this._draggerRight;
		var otherDrag = (pos == 1) ? this._draggerRight : this._draggerLeft;
		var offset = (pos == 1) ? this._offset :  this._offsetRight ;
		var left = otherDrag.getStyle('left').toInt();
		if(isNaN(left))
			left = 0;

		drag.slide.limit.x[pos] = left+((otherDrag.getWidth())/2-offset);
		this._moveBar();
},//setSlideLimit

_findPosOfTic: function(tic){
var index = 0;
	var maxLength = this._stepPrice.length -1;
	try{
	this._stepPrice.each(function(elm,pos){

		if(tic == elm){
			index = pos;
			throw $break
		}

		if(pos == maxLength ){
			index = pos;
			return;
		}

		var nextElm = this._stepPrice[pos+1];

		if(tic > elm && (tic < nextElm)){
			var posMin = tic -elm;
			var posMax = nextElm -tic;

			if(posMin < posMax)
				index = pos;
			else
				index = pos +1
				throw $break
		}
			if(tic < elm)
			index = pos;

	}.bind(this))
	}catch($break){
		//break
	}
	return index;
},//_findPosOfTic

setLeftTic :function(pos){
	this._setTic(	this._draggerLeft,pos);

},//setLeftPos

setRightTic :function(pos){
	this._setTic(	this._draggerRight,pos);

},//setLeftPos

_setTic :function(elm,pos){

	var offset = (elm ==this._draggerRight) ? -this._offsetRight : this._offset;
	pos = this._findPosOfTic(pos);

	var	sL = (pos*this._stepSize) + offset;

if(sL > elm.slide.limit.x[1])
	sL = elm.slide.limit.x[1];
	elm.setPosition({x:sL});
	elm.slide.value.now['x'] = sL;
	this._moveBar();

}//setLeftPos


})//tickers

  function TickersException(message) {
		this.message = message;
		this.nom = "TickersException";

	}//TickersException

