//	| ########################################################################## |
//	|-------------- powered by Djingle -------- 2006 --------------------------- |
//	| ########################################################################## |

/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//	---| class LAYER
//	---| author: Régis CAZANAVE (Djingle)
//	---| date: 2006-08-21
//	---| version: V1.0
//	---| (works bad on NS4)

/////////////////////////////////////////////////////////////////////// main functions 
//internal functions -------------------------------------
function _LayerNS4(obj, layer_id)
{
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++)
	{
		if (x[i].id == layer_id)
		 	foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getObjNN4(x[i],layer_id);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}

//LAYER constructor ------------------------------------------
function LAYER(layer_id, content)
{
	this.initial_X=0;
	this.initial_Y=0;
	this.initial_W=0;
	this.initial_H=0;
	this.browser=0;
	
	//misc ---------
	this._getBrowser = function() {
		if (document.getElementById) //IE or FF
		{
			if (document.all) //IE only
				this.browser = "ie";
			else
				this.browser = "ff";
		}
		else if (document.layers) //NS
			this.browser = "ns";
	
		else return false;
		
		return this.browser;
	};
	
	
	//init -------------------------
	this._getBrowser();
	
	//if (this.browser=="ff")
	//else if (this.browser=="ie") //IE only
	// Now, same code:
	this.layer = document.getElementById(layer_id);
	//alert(this.layer.id);
	this.layerstyle = this.layer.style;

	//basic functions ------------------------------------
	this.WriteInto = function (layer_content, add) {
			if (add) this.layer.innerHTML=this.layer.innerHTML+layer_content;
			else this.layer.innerHTML=layer_content;

			/*
			else if (document.layers)
			{
				//todo: permettre d'ajouter aussi
				lyer = document.layers[layer_id];
				lyer.document.open();
				lyer.document.write('<P CLASS="'+layer_id+'">'+layer_content+'</P>');
				lyer.document.close();
			}
			*/
			return true;
	};

	if (content) {
		this.WriteInto(content);
	}
	
	this.GetContent = function () {
	
		return this.layer.innerHTML;
	}
	
	//make the layer visible and invisible
	this.Show = function () { 
		this.layerstyle.visibility = "visible";
		this.layerstyle.display = "block";
	};

	this.Hide = function () {
		this.layerstyle.visibility = "hidden";
		this.layerstyle.display = "none";
	};
	
	this.ShowHide = function () {
		if (this.layerstyle.visibility == "visible")
			this.Hide();
		else 
			this.Show();
	};
	
	this.Move = function (new_x, new_y, duration) {
		//todo: gérer duration
		//-1 means no change
		if (new_x!=-1) this.layerstyle.left = _AddPxToDimension(new_x);
		if (new_y!=-1) this.layerstyle.top = _AddPxToDimension(new_y);
	};
	
	this.CenterInPage = function () {
		//calculate positions
		//alert(screen.availWidth+ " "+ _RemovePxFromDimension(this.layerstyle.height));
		var lpos=(screen.availWidth - _RemovePxFromDimension(this.layerstyle.width))/2;
		var tpos=(screen.availHeight - _RemovePxFromDimension(this.layerstyle.height))/2;
		//alert(lpos);
		//var lpos=650;
		//var tpos=100;

		this.Move(lpos,tpos);
	};
	
	this.ResizeTo = function (new_w, new_h) {
		//if (!this.layer) return;
		//-1 means no change

		if (new_w!=-1) var new_w = _AddPxToDimension(new_w);
		if (new_h!=-1) var new_h = _AddPxToDimension(new_h);

		//voir a repartir de la lib allemande (meme nom de fct)
		if (document.layers)
		{
			this.layer.resizeTo(new_w, new_h);
		}
		else
		{
			if (new_w!=-1) this.layerstyle.width = new_w;
			if (new_h!=-1) this.layerstyle.height = new_h;
		}
	};
	
	this.ResizeAndMove = function (new_w, new_h, new_x, new_y) {
		this.Move(new_x, new_y);
		this.ResizeTo(new_w, new_h);
	};
	
	this.SetOpacity = function (opacity) {

		if (document.getElementById)
		{
			this.layerstyle.opacity=opacity/100;
		}
		if (document.all)
		{
			this.layerstyle.filter = 'alpha(opacity='+opacity+')';
		}
	};
	
	this._DispLoadingMsg = function(msg) {

		var to_return="<img src='"+skin_base+"loading.gif' align=absmiddle>";
		if (msg) to_return=to_return+" "+msg;
		else to_return=to_return+" Loading...";

		this.WriteInto(to_return);
	};	

	this.findPosXOLD = function() {
		var o=this.layer;
		if (this.browser=="ff")
		{
			var fixBrowserQuirks = true;
			
			if (o==null) {
				return null;
			}
			
			var left = 0;
			var top = 0;
			var width = 0;
			var height = 0;
			var parentNode = null;
			var offsetParent = null;
			
			offsetParent = o.offsetParent;
			var originalObject = o;
			var el = o; // "el" will be nodes as we walk up, "o" will be saved for offsetParent references
			while (el.parentNode!=null) {
				el = el.parentNode;
				if (el.offsetParent==null) {
				}
				else {
					var considerScroll = true;
					/*
					In Opera, if parentNode of the first object is scrollable, then offsetLeft/offsetTop already 
					take its scroll position into account. If elements further up the chain are scrollable, their 
					scroll offsets still need to be added in. And for some reason, TR nodes have a scrolltop value
					which must be ignored.
					*/
					if (fixBrowserQuirks && window.opera) {
						if (el==originalObject.parentNode || el.nodeName=="TR") {
							considerScroll = false;
						}
					}
					if (considerScroll) {
						if (el.scrollTop && el.scrollTop>0) {
							top -= el.scrollTop;
						}
						if (el.scrollLeft && el.scrollLeft>0) {
							left -= el.scrollLeft;
						}
					}
				}
				// If this node is also the offsetParent, add on the offsets and reset to the new offsetParent
				if (el == offsetParent) {
					left += o.offsetLeft;
					if (el.clientLeft && el.nodeName!="TABLE") { 
						left += el.clientLeft;
					}
					top += o.offsetTop;
					if (el.clientTop && el.nodeName!="TABLE") {
						top += el.clientTop;
					}
					o = el;
					if (o.offsetParent==null) {
						if (o.offsetLeft) {
							left += o.offsetLeft;
						}
						if (o.offsetTop) {
							top += o.offsetTop;
						}
					}
					offsetParent = o.offsetParent;
				}
			}
		
			if (originalObject.offsetWidth) {
				width = originalObject.offsetWidth;
			}
			if (originalObject.offsetHeight) {
				height = originalObject.offsetHeight;
			}
			alert('left='+left+' / top='+top+' / width='+width +' / height='+height);
			
			return left;
			//return {'left':left, 'top':top, 'width':width, 'height':height};
		}
		else if (this.browser=="ie") //IE only
		{
			return o.getBoundingClientRect().left;
		}
	};
	
	
	this.findPosX = function() {
		var obj=this.layer;

		if (this.browser=="ff")
		{
			var curleft = 0;
			if(obj.offsetParent)
				while(1) 
				{
				  curleft += obj.offsetLeft;
				  if(!obj.offsetParent)
					break;
				  obj = obj.offsetParent;
				}
			else if(obj.x)
				curleft += obj.x;
			return curleft;
		}
		else if (this.browser=="ie") //IE only
		{
			return obj.getBoundingClientRect().left;
		}
	};
	
	
	this.findPosY = function() {
		var obj=this.layer;
		
		if (this.browser=="ff")
		{
			var curtop = 0;
			if(obj.offsetParent)
				while(1)
				{
				  curtop += obj.offsetTop;
				  if(!obj.offsetParent)
					break;
				  obj = obj.offsetParent;
				}
			else if(obj.y)
				curtop += obj.y;
			return curtop;
		}
		else if (this.browser=="ie") //IE only
		{
			return obj.getBoundingClientRect().top;
		}		
  	};

}


/*
//todo
this.MoveBy = function (new_x, new_y, duration) {
			  
			  this.xpos += x
			  obj.left = obj.xpos
			  obj.ypos += y
			  obj.top = obj.ypos
				
	};

DDObj.prototype.setBgColor = function(d_x)
{
	if(dd.n4 && this.div) this.div.bgColor = d_x;
	else if(this.css) this.css.background = d_x;
	this.bgColor = d_x;
};

DDObj.prototype.resizeTo = function(d_w, d_h, d_o)
{
	
};
DDObj.prototype.resizeBy = function(d_dw, d_dh)
{
	this.resizeTo(this.w+dd.Int(d_dw), this.h+dd.Int(d_dh));
};

*/

/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

//	---| class MSGBOX
//	---| author: Régis CAZANAVE (Djingle)
//	---| date: 2006-08-21
//	---| version: V1.0
//	---| (works bad on NS4)

/////////////////////////////////////////////////////////////////////// main functions 
function MSGBOX(msgbox_id, mb_title, mb_text, mb_actions_array)
{
	//init
	this.inheritFrom = LAYER;
	this.inheritFrom(msgbox_id);
	
	var tmp="<div id='"+ msgbox_id +"_drager' style='width:100%;'><table width=100% border=0 height=15 cellspacing=0 cellpadding=2 bgcolor=#CCCCCC><tr><td class=bg_cell_1 height=15><div id='"+ msgbox_id +"_title' style='width:100%;'>"+mb_title+"</div></td><td width=20 align=center class=bg_cell_1><a href='javascript:"+msgbox_id+".Hide();'>[X]</a></td></tr></table></div><table width=100% border=0 height=95% cellspacing=0 cellpadding=5 bgcolor=#CCCCCC><tr><td valign=top><div id='"+msgbox_id+"_text'>"+mb_text+"</div></td></tr><tr><td valign=top><div id='"+msgbox_id+"_actions' align=center></td></tr></table>";
	
	//alert(tmp);
	
	/*
	var master=eval("dd.elements."+ msgbox_id +"_drager");
	var son =eval("dd.elements."+msgbox_id);
	master.addChild(son); 
	*/
	
	this.WriteInto(tmp);
	this.CenterInPage();

	//specific MSGBOX methods
	this.SetText = function (text) {
			var fake=new LAYER(msgbox_id+"_text", text);
	};
	
	this.SetTitle = function (the_title) {
			var fake=new LAYER(msgbox_id+"_title", the_title);
	};
	
	
	this.SetActions = function (actions_array) {
	
			tmp="";
			for (a=1;a<=actions_array.length;a++) 
			{
				if (actions_array[a])
				{
					tmp=tmp+"<a href='#' onclick='"+actions_array[a]["action"]+";'>[ "+actions_array[a]["label"]+" ]</a>&nbsp;&nbsp;";
				}
			
			}
			var fake=new LAYER(msgbox_id+"_actions", tmp);
	};
	
	//actions init
	if (mb_actions_array) this.SetActions(mb_actions_array);

}

/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

//	---| class TABS
//	---| author: Régis CAZANAVE (Djingle)
//	---| date: 2006-08-28
//	---| version: V1.0
//	---| (works bad on NS4)

/////////////////////////////////////////////////////////////////////// main functions 
function TABS(tabs_system_id, base_managt_URL, call_mode) {
	if (!call_mode) call_mode="ajax";
	
	//init
	this.inheritFrom = LAYER;
	this.inheritFrom(tabs_system_id);
	
	if (base_managt_URL) this.base_managt_URL=base_managt_URL;
	
	this.content_div_id=tabs_system_id+"_content";
	var the_content_layer = new LAYER(this.content_div_id);
	
	this.tabs_list=this.layer.getElementsByTagName("li");
	
	//specific TABS methods	
	this.CallTab = function (tab_ix, tab_src, add_data) {
	
		if (!tab_ix) tab_ix="0";
		
		if (call_mode=="ajax") {
			var the_response;
			var the_result;
			var the_error;
			var the_error_a=new Array;

			the_content_layer._DispLoadingMsg();	
			
			//build data array
			var data = new Array;
			if (add_data)
				data=add_data;
			data["todo"]="get_tab";
			data["tab_ix"]=tab_ix;
			data["tab_src"]=tab_src;
			
			if (debug_mode) alert("envoi à la page distante avec data = todo=get_tab&tab_ix=" +tab_ix);
			
			the_response=CallAJAX("POST", this.base_managt_URL, data, false);

			if(the_response.readyState == 4) {
				the_result=the_response.responseText;
				if (debug_mode) alert("retour de cette page = "+the_result);
				the_error_a=the_result.split("|");
				
				//display
				if (the_error_a[0]=="ok") 
					the_content_layer.WriteInto(the_error_a[1]);
			}
		} else if (call_mode=="iframe") {
			var td="<IFRAME width='100%' height='90%' src='"+this.base_managt_URL+"?tab_ix="+tab_ix+"&"+tab_src+"' frameborder=0 align='top' border=0 noresize></IFRAME>";
			the_content_layer.WriteInto(td);
		} else if (call_mode=="use_local_function") {
			eval(tab_src);
		}
		//change tab style
		this.UnSelectAllTabs();
		this.SelectTab(tab_ix);
	};
	
	this.SelectTab = function (tab_ix) {
		if (!tab_ix) tab_ix="0";
		//search in all li elements
		//this.tabs_list[tab_ix].className="selected";
		this.tabs_list[tab_ix].id="selected";
	};

	this.UnSelectAllTabs = function () {
		for (x=0;x<this.tabs_list.length;x++) {
			//this.tabs_list[x].className=tabs_system_id;
			this.tabs_list[x].id=tabs_system_id;
		}
	};
	
	//tabs init
}