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

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

function NavigationBox(table,tab,container,height_adjuster) {
	var NavigationBox = this;
	var that = this;
	this.box = $('#'+table);
	//window.alert('NavBox');
	this.timer_id_1;
	this.timer_id_2;

	this.Rapport = 1.2 / 20.0; // On divise par 20
	this.Mini = 2 * this.Rapport;

	this.HTML = new Object()
	this.HTML.tbl				= document.getElementById(table);
	this.HTML.tab				= document.getElementById(tab);
	this.HTML.container			= document.getElementById(container);
	this.HTML.height_adjuster	= document.getElementById(height_adjuster);

	if(this.HTML.tbl) {
		this.HTML.tbl.style.position = "absolute"; // IMPERATIF
		//-- Recup position de depart
		var pos = getHTMLElementPosition(this.HTML.tbl);
		//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.tbl.offsetWidth);
		this.container_width	= parseInt(this.HTML.container.offsetWidth);
		this.height			= parseInt(this.HTML.tbl.offsetHeight);
		this.container_height	= parseInt(this.HTML.container.offsetHeight);

		this.HTML.tab.className = 'close';
		// OLD // this.HTML.tab.onclick = function() { NavigationBox.hideContainer(); };

		if((this.container_height - 20) < this.HTML.tab.offsetHeight) {
			this.HTML.height_adjuster.style.height = parseInt(this.HTML.tab.offsetHeight) - parseInt(this.container_height) + 20 + 'px';
		}

		//this.timer_id_2 = setInterval(function() { NavigationBox.checkScroll(); },1000);
		
		// NEW JQUERY
		$(window).scroll(function() {
			clearTimeout(that.timer_id_1);
			that.timer_id_1 = setTimeout(function(){ NavigationBox.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.tbl.rows[0].cells[1].style.display = 'none';
		this.HTML.tbl.rows[1].cells[1].style.display = 'none';
		this.HTML.tbl.rows[2].cells[1].style.display = 'none';
		this.HTML.tbl.rows[0].cells[2].style.display = 'none';
		this.HTML.tbl.rows[1].cells[2].style.display = 'none';
		this.HTML.tbl.rows[2].cells[2].style.display = 'none';
		this.HTML.tab.className = '';
		// OLD // this.HTML.tab.onclick = function() { NavigationBox.showContainer(); };
	}

	this.showContainer = function() {
		//this.HTML.container.style.display = '';
		this.HTML.tbl.rows[0].cells[1].style.display = '';
		this.HTML.tbl.rows[1].cells[1].style.display = '';
		this.HTML.tbl.rows[2].cells[1].style.display = '';
		this.HTML.tbl.rows[0].cells[2].style.display = '';
		this.HTML.tbl.rows[1].cells[2].style.display = '';
		this.HTML.tbl.rows[2].cells[2].style.display = '';
		this.HTML.tab.className = 'close';
		// OLD // this.HTML.tab.onclick = function() { NavigationBox.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)) {
	//	if(this.posY != this.finY) {
	//		clearInterval(this.timer_id_1);
	//		this.timer_id_1 = setInterval(function() { NavigationBox.replaceDiv(NavigationBox.finY); }, 100);
	//	}
	//	return (true);
	//}
	//
	//this.replaceDiv = function(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))) {
	//	if(Delta_Y < this.Mini &&  Delta_Y > - this.Mini) {
	//		clearInterval(this.timer_id_1);
	//		this.deplaceDiv(y_);
	//	}
	//	else {
	//		//this.deplaceDiv(this.posX + Delta_X,this.posY + Delta_Y);
	//		this.deplaceDiv(this.posY + Delta_Y);
	//	}
	//}
	//
	//this.deplaceDiv = function(y_) {
	//	/*
	//	if(arguments[0] != null){
	//		this.posX = x_;
	//		this.HTML.tbl.style.left = parseInt(x_) + "px";
	//	}
	//	if(arguments[1] != null){
	//	*/
	//	if(arguments[0] != null){
	//		this.posY = y_;
	//		this.HTML.tbl.style.top = parseInt(y_) + "px";
	//	}
	//}
}

