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);
				}
			}
		}
	},
	setElement : function(eltStyle,_left,_top,_width,_height)
	{
		eltStyle.position="absolute";
		eltStyle.top=_top+"px";
		eltStyle.left=_left+"px";
		eltStyle.height=_height+"px";
		eltStyle.width=_width+"px";
	},

	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 document.body.clientWidth;
		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;
	}
}