/**
 * BorderLayout.js version 1.0.0
 * Gestionnaire de mise en page automatique.
 * Réarrange des div en fonction de contraintes de mise en page.
 */
var LayoutComponent = Class.create();
LayoutComponent.prototype = {
	initialize : function(id,width,height,orientation)
	{
		this.id=id;
		this.width=width;
		this.height=height;
		this.orientation=orientation;
	}
}

var LayoutCoord = Class.create();
LayoutCoord.prototype = {
	initialize : function()
	{
		this.y_north=0;
		this.x_west=0;
		this.x_east=0;
		this.y_south=0;
	}
}


var BorderLayout = Class.create();
BorderLayout.prototype = {
	initialize : function(_typeLayout)
	{
		this.layoutManager = new Array();
		this.layoutContentIndex = new Array();
		this.typeLayout = _typeLayout;  //0:nromal, 1:inverse
		this.NORTH = 1;
		this.EAST = 2;
		this.WEST = 3;
		this.SOUTH = 4;
		this.CENTER = 5;
	},
	addLayoutComponent : function(id,width,height,orientation)
	{
		this.layoutContentIndex[id] = this.layoutManager.length;
		this.layoutManager[this.layoutManager.length] = new LayoutComponent(id,width,height,orientation);
	},
	removeLayoutComponent : function(id)
	{
		var index = this.layoutContentIndex[id];
		if (index!="undefined")
		{
			this.layoutManager.splice(index,1);
			this.layoutContentIndex[id]="undefined";
		}
	},
	doLayout : function()
	{
		var broswerWidth=this.getBrowserWidth();
		var broswerHeight=this.getBrowserHeight();
		var coord = new LayoutCoord();
		var index = 0;
		var elts = document.body.getElementsByTagName('div');
		/*Rï¿½cupï¿½re les dimensions minimums*/
		var minWidth = 0;
		var minHeight = 0;
		for (compindex = 0 ; compindex < elts.length ; compindex++) 
		{
			elt = elts[compindex];
			if (!Element.visible(elt))
				continue;
			var index = this.layoutContentIndex[elt.id];
			if (typeof(index)!="undefined")
			{
				var layout = this.layoutManager[index];
				if (layout.orientation == this.NORTH)
				{
					minHeight += layout.height;
				}
				if (layout.orientation == this.SOUTH)
				{
					minHeight += layout.height;
				}
				if (layout.orientation == this.WEST)
				{
					minWidth += layout.width;
				}
				if (layout.orientation == this.EAST)
				{
					minWidth += layout.width;
				}
			}
		}
		/*calcul des coordonnï¿½es nï¿½cessaires au placement des ï¿½lï¿½ments*/
		for (compindex = 0 ; compindex < elts.length ; compindex++) 
		{
			elt = elts[compindex];
			if (!Element.visible(elt))
				continue;
			var index = this.layoutContentIndex[elt.id];
			if (typeof(index)!="undefined")
			{
				var layout = this.layoutManager[index];
				if (layout.orientation == this.NORTH)
				{
					coord.y_north = layout.height;
				}
				if (layout.orientation == this.WEST)
				{
					coord.x_west = layout.width;
				}
				if (layout.orientation == this.SOUTH)
				{
					coord.y_south = Math.max(broswerHeight,minHeight)-layout.height;
				}
				if (layout.orientation == this.EAST)
				{
					coord.x_east = Math.max(broswerWidth,minWidth)-layout.width;
				}
			}
		}
		if (coord.x_east==0)
		{
			coord.x_east = Math.max(broswerWidth,minWidth);
		}
		if (coord.y_south==0)
		{
			coord.y_south = Math.max(broswerHeight,minHeight);
		}
		/*on place les ï¿½lï¿½ments*/
		for (compindex = 0 ; compindex < elts.length ; compindex++) 
		{
			elt = elts[compindex];
			if (!Element.visible(elt))
				continue;
			var index = this.layoutContentIndex[elt.id];
			if (typeof(index)!="undefined")
			{
				var layout = this.layoutManager[index];
				var eltStyle=elt.style;
				if (layout.orientation == this.NORTH)
				{
					if (this.typeLayout==0)
						this.setElement(eltStyle,0,0,Math.max(broswerWidth,minWidth),layout.height);
					else
						this.setElement(eltStyle,coord.x_west,0,coord.x_east-coord.x_west,layout.height);
				}
				if (layout.orientation == this.WEST)
				{
					if (this.typeLayout==0)
						this.setElement(eltStyle,0,coord.y_north,layout.width,coord.y_south-coord.y_north);
					else
						this.setElement(eltStyle,0,0,layout.width,Math.max(broswerHeight,minHeight));
				}
				if (layout.orientation == this.SOUTH)
				{
					if (this.typeLayout==0)
						this.setElement(eltStyle,0,coord.y_south,Math.max(broswerWidth,minWidth),layout.height);
					else
						this.setElement(eltStyle,coord.x_west,coord.y_south,coord.x_east-coord.x_west,layout.height);
				}
				if (layout.orientation == this.EAST)
				{
					if (this.typeLayout==0)
						this.setElement(eltStyle,Math.max(broswerWidth,minWidth)-layout.width,coord.y_north,layout.width,coord.y_south-coord.y_north);
					else
						this.setElement(eltStyle,Math.max(broswerWidth,minWidth)-layout.width,0,layout.width,Math.max(broswerHeight,minHeight));
				}
				if (layout.orientation == this.CENTER)
				{
					this.setElement(eltStyle,coord.x_west,coord.y_north,coord.x_east-coord.x_west,coord.y_south-coord.y_north,true,elt);
				}
			}
		}
	},
	setElement : function(eltStyle,_left,_top,_width,_height,isCenter,elt)
	{
            var padleft=0;
            var padright=0;
            var padtop=0;
            var padbottom=0;
            if (isCenter==true && elt!=null && Element.getStyle(elt,"paddingLeft")!=null)
            {
                padleft = Element.getStyle(elt,"paddingLeft").substring(0,Element.getStyle(elt,"paddingLeft").length-2);
                padright = Element.getStyle(elt,"paddingRight").substring(0,Element.getStyle(elt,"paddingRight").length-2);
                padtop = Element.getStyle(elt,"paddingTop").substring(0,Element.getStyle(elt,"paddingTop").length-2);
                padbottom = Element.getStyle(elt,"paddingBottom").substring(0,Element.getStyle(elt,"paddingBottom").length-2);
            }
            eltStyle.position="absolute";
            eltStyle.top=_top+"px";
            eltStyle.left=_left+"px";
            if(eval(_height-padtop-padbottom)>=0)
                eltStyle.height=eval(_height-padtop-padbottom)+"px";
            else
                eltStyle.height="0px";
            if(eval(_width-padleft-padright)>=0)
                eltStyle.width=eval(_width-padleft-padright)+"px";
            else
                eltStyle.width="0px";
	},

	getBrowserWidth : function()
	{
		if (window.innerWidth) 
			return window.innerWidth;
		else if (document.documentElement && document.documentElement.clientWidth!=0)
    			return document.documentElement.clientWidth;
		else if (document.body) 
			return Math.max(document.body.clientWidth,document.body.offsetWidth);
		return 0;
	},
	getBrowserHeight : function()
	{
		if (window.innerHeight) 
			return window.innerHeight;
		else if (document.documentElement && document.documentElement.clientHeight!=0)
    			return document.documentElement.clientHeight;
		else if (document.body) 
			return document.body.clientHeight;
		return 0;
	}
}