//	NÉCESSITE utils.lib.js POUR LA FONCTION getHTMLElementPosition & getPageScroll & Add_Event

//	GÉRER LES TIMEINTERVAL POUR LE RETRAIT DU BASKET

function MiniBasket(div_id,div_tab_id,div_basket_container_id,container_height_adjuster,display) {
	var MiniBasket = this;
	var that = this;
	this.box = $('#'+div_id);
	this.timer_id_1;
	this.timer_id_2;
	
	this.has_been_displayed = false;
	
	this.Rapport = 1.0 / 20.0; // On divise par 20
	this.Mini = 2 * this.Rapport;
	
	this.HTML = new Object()
	this.HTML.div						= document.getElementById(div_id);
	this.HTML.tab						= document.getElementById(div_tab_id);
	this.HTML.container					= document.getElementById(div_basket_container_id);
	this.HTML.basket_height_adjuster		= document.getElementById(container_height_adjuster);
	
	if(this.HTML.div) {
		this.HTML.div.style.position = "absolute"; // IMPERATIF
		//-- Recup position de depart
		var pos = getHTMLElementPosition(this.HTML.div);
		this.posX = pos.left;
		this.posY = pos.top;
		this.iniX = this.posX;
		this.iniY = this.posY;
		this.finX = 0;
		this.finY = 0;
		
		this.width	= parseInt(this.HTML.div.offsetWidth);
		this.height	= parseInt(this.HTML.div.offsetHeight);
		
		this.HTML.tab.className = '';
		this.HTML.tab.onclick = function() { MiniBasket.showContainer(); };

		if(!display) {
			this.has_been_displayed = false;
			addClassName(this.HTML.container,'hidden');
			this.HTML.tab.className = 'open';
			this.HTML.tab.onclick = function() { MiniBasket.showContainer(); };
		}
		else  {
			this.has_been_displayed = true;
			removeClassName(this.HTML.container,'hidden');
			this.HTML.tab.className = 'close';
			this.HTML.tab.onclick = function() { MiniBasket.hideContainer(); };
			if((this.HTML.container.offsetHeight - 30) < parseInt(this.HTML.tab.offsetHeight)) {
				this.HTML.basket_height_adjuster.style.height = parseInt(this.HTML.tab.offsetHeight) - parseInt(this.HTML.container.offsetHeight) + 30 + 'px';
			}
		}
		
		//this.timer_id_2 = setInterval(function() { MiniBasket.checkScroll(); },100);
		
		// NEW JQUERY
		$(window).scroll(function() {
			clearTimeout(that.timer_id_1);
			that.timer_id_1 = setTimeout(function(){ MiniBasket.reposition(); }, 200);
		});
		
		$(window).scroll();
	}
	
	this.reposition = function() {
		var Scroll = getPageScroll();
		this.finY = Scroll.top + this.iniY;
		var distance = Math.floor(Math.abs(Scroll.top-this.box.position().top));
		var vitesse = Math.min(1500,distance*2);
		this.box.animate({
			top: this.finY+'px'
		}, vitesse );
	}
	
	this.hideContainer = function() {
		this.HTML.container.style.display = 'none';
		this.HTML.tab.className = '';
		this.HTML.tab.onclick = function() { MiniBasket.showContainer(); };
	}
	
	this.showContainer = function() {
		this.HTML.container.style.display = '';
		
		if(!this.has_been_displayed) {
			this.has_been_displayed = true;
			removeClassName(this.HTML.container,'hidden');
			if((this.HTML.container.offsetHeight - 30) < parseInt(this.HTML.tab.offsetHeight)) {
				this.HTML.basket_height_adjuster.style.height = parseInt(this.HTML.tab.offsetHeight) - parseInt(this.HTML.container.offsetHeight) + 30 + 'px';
			}
		}
		
		this.HTML.tab.className = 'close';
		this.HTML.tab.onclick = function() { MiniBasket.hideContainer(); };
	}
	
	//this.checkScroll = function() {
	//	var Scroll = getPageScroll();
	//	this.finX = Scroll.left + this.iniX;
	//	this.finY = Scroll.top + this.iniY;
	//	if((this.posY != this.finY) || ( this.posX != this.finX)) {
	//		clearInterval(this.timer_id_1);
	//		this.timer_id_1 = setInterval(function() { MiniBasket.replaceDiv(MiniBasket.finX,MiniBasket.finY); }, 10);
	//	}
	//	return (true);
	//}
	//
	//this.replaceDiv = function(x_,y_) {
	//	//-- Calcul Delta deplacement
	//	var Delta_X = (x_ - this.posX) * this.Rapport;
	//	var Delta_Y = (y_ - this.posY) * this.Rapport;
	//	//-- Test si fin deplacement
	//	if(((Delta_Y < this.Mini) && ( Delta_Y > - this.Mini)) && (( Delta_X < this.Mini) && ( Delta_X > - this.Mini))) {
	//		clearInterval(this.timer_id_1);
	//		this.deplaceDiv(x_,y_);
	//	}
	//	else {
	//		this.deplaceDiv(this.posX + Delta_X,this.posY + Delta_Y);
	//	}
	//}
	//
	//this.deplaceDiv = function(x_,y_) {
	//	if(arguments[0] != null){
	//		this.posX = x_;
	//		this.HTML.div.style.left = parseInt(x_) + "px";
	//	}
	//	if(arguments[1] != null){
	//		this.posY = y_;
	//		this.HTML.div.style.top = parseInt(y_) + "px";
	//	}
	//}
}
