// Fonctions permettant la gestion de la taille de la page
// version : 1.1
// Date: 20/10/03

/********************************************************************
*                   Gestion des évènements browser                  *
********************************************************************/
var previousNavBasPosition = -1;
var sizeNavHaute = -1;

var tailleNavHaut =101;
var tailleNavBas =16;
var memoTaillePage=-1;
var currentPage

// creation de l'objet page                                                                                                                                                   
currentPage = new resizablePage();

/********************************************************************
*                   Gestion de la position de la nav basse          *
********************************************************************/
function initializePage()
{
	lsizebas = 16;
	if (clientIs.ns && !clientIs.ns6)
	{
		window.captureEvents(Event.RESIZE );
		window.onresize = reloadDoc;
	}
	else
	{
		window.onresize = handleResize;
	}
	currentPage.updatePage();
	currentPage.updatePage();
}
/********************************************/
// fonction de gestion du resize sous ie
function handleResize()
{
	if(clientIs.mac && clientIs.ie)
	{
		//setTimeout("currentPage.updatePage()",100);
	}
	//setTimeout("handleResize",100);
	currentPage.updatePage();
}

/********************************************/
// fonction de rechargement de la page en cours
function reloadDoc()
{
	location.reload();
}


/********************************************************************
*                   Gestion de la taille de la fenêtre              *
********************************************************************/

// tailleNavHautBas = somme des tailles de la nav haute et basse
/*var tailleNavHautBas = 141;  // taile de la nav haute + taille de la nav basse
var tailleNavGaucheDroite = 381;
*/
/********************************************/
// fonction permettante de gérer le corps du tableau principal


function addSizeControl(controlName,urlShim,controlSizex,percentx,controlSizey,percenty,refName)
{
	// controlName : nom de l'image qui va être crée
	// controlSize : taille totale des éléments fixes dans l'axe ( "h" ou "v" )
	// controlType : c'est l'axe, "h" ou "v" 
	// urlShim : l'url de l'image invisible
	// percent : pourcentage de la partie variable 
	var strCale;
	controlSize = new point();
	controlSize.x = controlSizex;
	controlSize.y = controlSizey;
	
	percent = new point();
	percent.x = percentx;
	percent.y = percenty;
	
	currentPage.addControlItem(controlName,controlSize,percent,refName)
	currentPage.urlShim = urlShim;
	
	currentControlItem = currentPage.getControlObject(controlName);
	strCale = currentControlItem.getHTMLString();
//alert("strCale = " + strCale);	
	return strCale;
	
}
function ErrorOnControlObject(controlName)
{	// debug("erreur sur "+controlName);
return -1
}
function setPositionReference(controlName,referenceName,xoffset,yoffset)
{
	var tmpSize = 0;
	if(clientIs.ns && !clientIs.ns6 && !clientIs.ns7)
	{
		
		currentControlItem = currentPage.getControlObject(controlName);
		if(currentControlItem==-1) return ErrorOnControlObject(controlName);
		/*if(currentControlItem.type=="h")
			tmpSize = getElementPosition(referenceName).x+xoffset;
		if(currentControlItem.type=="v")
			tmpSize = getElementPosition(referenceName).y+yoffset;
		*/
		currentControlItem.referencePoint.setRef(referenceName);
		currentControlItem.referencePoint.x = xoffset;
		currentControlItem.referencePoint.y = yoffset;
		tmpSize = new point();
		var position = currentControlItem.referencePoint.getPosition()
		tmpSize.x = position.x + currentControlItem.size.x;
		tmpSize.y = position.y + currentControlItem.size.y;
		//tmpSize = getElementPosition(referenceName).x+xoffset;
		updateControl(controlName,tmpSize);
	}
	else
	{
		currentPage.setPositionReference(controlName,referenceName,xoffset,yoffset)
	}
}

function updateControl(controlName,controlSize)
{
	//// debug("updating "+controlName+" "+controlSize);
	currentPage.updateControl(controlName,controlSize)
}

function resizablePage(lSize)
{
	//propriétés
	this.controlItemList = new Array();
	this.nbControls = 0;
	this.layerSize = lSize;
	this.layerName = "navBas";
	this.urlShim = "";

	this.sizeModifier = new browserModifier();
	//méthodes
	
	//on ajoute une image pour controler la taille de la page
	this.addControlItem = function (imageName,sizeToSubstract,percent,refName){
	
		tempPoint = new point();
		tempPoint.x=1;
		tempPoint.y=1;
	
		var newControlItem = new HW_SIZECONTROL(this,imageName,sizeToSubstract,refName,(percent ? percent : tempPoint));
		//if(percent) newControlItem['percentage']=percent;
		
		this.controlItemList[this.nbControls] = newControlItem;
		this.nbControls++ ;
	
	}

	this.updatePage = function(){
		
		this.updateControls();
	}
	this.setPositionReference = function(controlName,referenceName,xoffset,yoffset){
	// debug("setting position ref");
		// newRef = new reference(referenceName,xoffset,yoffset)
		newRef = new point(referenceName);
		//newRef.name = referenceName;
		newRef.x = xoffset;
		newRef.y = yoffset;
		//newRef.setRef(referenceName);
	
		var strControl = "";
		currentControlItem = this.getControlObject(controlName)
		if(currentControlItem==-1) return ErrorOnControlObject(controlName);
		currentControlItem.referencePoint = newRef;
		
	};
	this.updateControls = function(){
		var imgx,i;
		for(i=0;i<this.nbControls;i++)
		{
			//currentControlItem = this.controlItemList[i];
			//this.updateControl(currentControlItem.refName);
			this.controlItemList[i].update();
		}
	};

	this.getControlObject = function(controlName){
		for(i=0;i<this.nbControls;i++)
			if(this.controlItemList[i].refName==controlName) return this.controlItemList[i];
		return -1;
	};
	
	this.getControlSize = function(controlName){
		var tmpSize = new point();
		var offset = new point();
		currentControlItem = this.getControlObject(controlName);
		if(currentControlItem==-1) return ErrorOnControlObject(controlName);
		if(!currentControlItem) return -1;
		//// debug(currentControlItem.referencePoint.getRef());
		if(currentControlItem.referencePoint.getRef()==null  )
		//if(currentControlItem.referencePoint==null || (clientIs.ns && !clientIs.ns6 && !clientIs.ns7) )
		{
			
			tmpSize = currentControlItem.getSize();
			// debug("size "+controlName+" "+	tmpSize.x+" "+	tmpSize.y+" "+(currentControlItem.referencePoint==null));
		}
		else
		{
			// positionImageReference + offsetImageReference

			var position = currentControlItem.referencePoint.getPosition();
			// debug("pos ="+controlName+" "+position.x+", "+currentControlItem.size.x+", "+position.y+", "+currentControlItem.size.y);
			/*
			tmpSize.x = position.x + ((clientIs.ns && !clientIs.ns6 && !clientIs.ns7) ? currentControlItem.size.x :0);
			tmpSize.y = position.y + ((clientIs.ns && !clientIs.ns6 && !clientIs.ns7) ? currentControlItem.size.y :0);
			*/
			tmpSize.x = position.x + (currentControlItem.size.x );
			tmpSize.y = position.y + (currentControlItem.size.y );
			//tmpSize.x = position.x +  currentControlItem.size.x ;
			//tmpSize.y = position.y + currentControlItem.size.y ;
		}
		//// debug("Coord  size "+tmpSize.x+" "+tmpSize.y);

		tmpPoint = new point();
			
		//tmpPoint.x = parseInt(parseInt(this.findWidth()-tmpSize.x)*currentControlItem.percentage.x)/100;
		//tmpPoint.y = parseInt(parseInt(this.findHeight()-tmpSize.y)*currentControlItem.percentage.y)/100;

		tmpPoint.x = Math.max(1,parseInt(parseInt(this.findWidth()-tmpSize.x)*currentControlItem.percentage.x));
		tmpPoint.y = Math.max(1,parseInt(parseInt(this.findHeight()-tmpSize.y)*currentControlItem.percentage.y));
		// debug("getting control size ("+controlName+") "+this.findWidth()+" "+tmpSize.x+" "+tmpPoint.x);
		//return parseInt(parseInt(this.findHeight()-tmpSize)*currentControlItem.percentage)/100;
		return  tmpPoint;
	};
	/*
	this.getHTMLString = function(controlName){
		// renvoie le tag HTML pour le document.write()
		var tmpSize;
		var strControl = "";
	
		currentControlItem = this.getControlObject(controlName);
		if(currentControlItem==-1) return ErrorOnControlObject(controlName);
		
		tmpSize = this.getControlSize(controlName);
		
		urlShim = currentPage.urlShim
		//if(currentControlItem.type=="h")
			//strControl = '<img src="'+urlShim+'" width="'+tmpSize +'" height="1" id="'+controlName+'" name="'+controlName+'">';
		//if(currentControlItem.type=="v")
			//strControl = '<img src="'+urlShim+'" width="1" height="'+tmpSize +'" id="'+controlName+'" name="'+controlName+'">';
			strControl = '<img src="'+urlShim+'" width="'+tmpSize.x +'" height="'+tmpSize.y +'" id="'+controlName+'" name="'+controlName+'">';
		return strControl;
	}
	*/
	this.findHeight = function(){
		var x;
		var y;
		y = (clientIs.ns && (!clientIs.ns6 || (clientIs.ns6 && clientIs.mac))) ? window.innerHeight : document.body.clientHeight ;
		//	if (clientIs.ns&&!clientIs.ns6 && !clientIs.mac) y-=15
		//	if(clientIs.mac) y-=1;
		if(clientIs.ns6 && !y) y=window.innerHeight;
		return y-this.sizeModifier.windowSizeMax.y-1;
		return y;
		// Enlever le commentaire sur la ligne qui suit pour imposer un max et un min à la taille de la page
		return Math.max(450,Math.min(y,618))-1;
	};
	this.findWidth = function(){
		var x;
		var y;
		x = (clientIs.ns && (!clientIs.ns6 || (clientIs.ns6 && clientIs.mac))) ? window.innerWidth : document.body.clientWidth ;
		if(clientIs.ns6 && !x) x=window.innerWidth;
		// Enlever le commentaire sur la ligne qui suit pour imposer un max et un min à la taille de la page
		//	return Math.max(780,Math.min(x-this.sizeModifier.windowSizeMax.x,1004-this.sizeModifier.windowSizeMax.x))

		return Math.max(780-this.sizeModifier.windowSizeMin.x,Math.min(x-this.sizeModifier.windowSizeMax.x,1004-this.sizeModifier.windowSizeMax.x))
	//	return Math.max(780,Math.min(x,1004))-1;;	
	}

}

// controleur de taille de bloc
function HW_SIZECONTROL(currentPage,ctrlName,ctrlSize,ctrlReference,ctrlPercentage)
{
	this.page = currentPage;
	this.type = "HW_SIZECONTROL";
	this.refName = ctrlName;				// string
	this.ref = new point(ctrlName);
	this.size = ctrlSize;				// point
	this.percentage = ctrlPercentage;	// point
	this.referencePoint = new point(ctrlReference);		// point
	
	
	// debug("creating "+ctrlName+" "+this.referencePoint);
	this.update = function(controlName,controlSize){
		var imgx,i;
		
		if(controlSize) this.size = controlSize;
	
		
		var tmpRef = this.ref.getRef();
		// debug("ref "+tmpRef);
		if(!tmpRef) return;
// debug("ref2 "+tmpRef);
		var tmpsize = this.page.getControlSize(this.refName);
		tmpRef.width = tmpsize.x;
		tmpRef.height = tmpsize.y;
	//if(this.refName=="zonePedago") window.status="size info:"+tmpRef+" "+tmpsize.x+" "+tmpsize.y;
	};
	this.getSize= function(){
		return this.size;
	}
	this.getRef= function(){
		return this.ref;
	}
	this.getHTMLString = function(){
		// renvoie le tag HTML pour le document.write()
		var tmpSize;
		var strControl = "";
		
		tmpSize = this.page.getControlSize(this.refName);
		
		urlShim = currentPage.urlShim;
		
		strControl = '<img src="'+urlShim+'" width="'+tmpSize.x +'" height="'+tmpSize.y +'" id="'+this.refName+'" name="'+this.refName+'">';
		return strControl;
	}
}

function browserModifier()
{
	this.windowSizeMax = new point();
	this.windowSizeMin = new point();
	this.layerModifier = new point();
	if(clientIs.mac)
	{
		if(clientIs.ie)
		{
			this.windowSizeMax.y = 2;
			this.windowSizeMax.x = 1;
			this.windowSizeMin.x = 0;
			this.layerModifier.y = 10;
		}
		else
		if(clientIs.ns6 || clientIs.ns7)
		{
			this.windowSizeMax.y = 2;
			this.windowSizeMax.x = 16;
			this.windowSizeMin.x = 4;
			this.layerModifier.y = 10;
		}
		else
		{
			this.windowSizeMax.y = 2;
			this.windowSizeMax.x = 16;
			this.windowSizeMin.x = 0;
			this.layerModifier.y = 10;
		}
	}
	else
	{
		if(clientIs.ie)
		{
			this.windowSizeMax.y = 2;
			this.windowSizeMax.x = 1;
			this.windowSizeMin.x = 0;
			this.layerModifier.y = 2;
		}
		else
		if(clientIs.ns6 || clientIs.ns7)
		{
			
			this.windowSizeMax.y = 2;
			this.windowSizeMax.x = 16;
			this.windowSizeMin.x = 4;
			this.layerModifier.y = 2;
		}
		else
		{
			
			this.windowSizeMax.y = 2;
			this.windowSizeMax.x = 16;
			this.windowSizeMin.x = 0;
			this.layerModifier.y = 2;
		}
		
	}
	
}
