//--- Gestion des menus

var W3C = document.getElementById? true : false; // IE5+, Netscape 6+, Opera 5+, Konqueror 2.1+, Mozilla and various Mozilla/Gecko-based browsers.
var NN4 = document.layers? true : false; //Netscape Navigator 4.x.
var IE4 = document.all? true : false; // IE version 4 (and above).

//---public

/**
 * Cache une div à partir de son id
 */
function hide(divId){
	if( document.getElementById(divId) ){
		document.getElementById(divId).style.display = "none";
	}
}

var CallFrequency=70;	//---50 appels par seconde
var slideDuration=500; 	//---durée du mouvement
/**
 * Déplier un menu
 */
function unfold(divId,from_top,from_left,from_height,from_width,to_top,to_left,to_height,to_width){
	if (IE4) {	//Special IE : pas de slide
		show(divId,	to_top,to_left,to_height,to_width );
		document.getElementById(divId).style.display = "block";
		return;
	}
	
	if( document.getElementById(divId) ){
		//Affichage du calque
		document.getElementById(divId).style.display = "block";
		slide(divId,from_top,from_left,from_height,from_width,to_top,to_left,to_height,to_width,slideDuration,"unfold");
	}
}

/**
 * Plier un menu
 */
function fold(divId,from_top,from_left,from_height,from_width,to_top,to_left,to_height,to_width){
	if (IE4) {	//Special IE : pas de slide
		document.getElementById(divId).style.display = "none";
		return;
	}	
	if( document.getElementById(divId) ){
		//Si le calque est déjà noté comme invisible alors c'est pas la peine de le plier
		if( document.getElementById(divId).style.display == 'none' ){	
			return;		
		}
		slide(divId,from_top,from_left,from_height,from_width,to_top,to_left,to_height,to_width,slideDuration,"fold");
	}
}

//---private

var T = new Array(); 

//---interface

function slide(divId,from_top,from_left,from_height,from_width,to_top,to_left,to_height,to_width,duration,slideType){
	//display
	T[divId] = new Array();
	T[divId]["start"] = getVector( from_top, from_left, from_height, from_width );
	show( divId, from_top, from_left, from_height, from_width );
	//slide
	T[divId]["stop"] = getVector( to_top, to_left, to_height, to_width );
	T[divId]["nbCall"] = Math.round( duration * CallFrequency / 1000 );
	T[divId]["period"] = Math.round( 1000 / CallFrequency );
	T[divId]["cursor"] = 0;
	runrun( divId , slideType);
}
//Définition du vector
function getVector( top, left, height, width ){
	V = new Array();
	V["top"] = top;
	V["left"] = left;
	V["height"] = height;
	V["width"] = width;
	return V;
}
//Définition des données du menu
function show( divId, top, left, height, width ){
	document.getElementById(divId).style.width = width;
	document.getElementById(divId).style.height = height;

	document.getElementById(divId).style.top = top;
	document.getElementById(divId).style.left = left;
}
//Mouvement 
function runrun( divId , slideType){
	var t, top, left, height, width;

	t = linear( T[divId]["cursor"] / T[divId]["nbCall"] );
	
	top = ( 1 - t ) * T[divId]["start"]["top"] + t * T[divId]["stop"]["top"]; 
	left = ( 1 - t ) * T[divId]["start"]["left"] + t * T[divId]["stop"]["left"]; 
	height = ( 1 - t ) * T[divId]["start"]["height"] + t * T[divId]["stop"]["height"]; 
	width = ( 1 - t ) * T[divId]["start"]["width"] + t * T[divId]["stop"]["width"]; 
	
	T[divId]["cursor"] = T[divId]["cursor"] + 1;
	show( divId, top, left, height, width );
	if( T[divId]["cursor"] <= T[divId]["nbCall"] ){ 
		setTimeout( 'runrun("'+divId+'","'+slideType+'")', T[divId]["period"] ); 
	}else{
		if( slideType == "fold" ){
			//afin que le menu soit vraiment invisible
			document.getElementById(divId).style.display = "none";
		}
	}
}

//Type de mouvement
function linear( t ){
	return Math.sin( Math.PI * t / 2);	
}