//	| ########################################################################## |
//	|-------------- 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;
}

function _RemovePxFromDimension(dim)
{
	return dim.substring(0,dim.length-2);

}

function Int(d_x, d_y)
{
	return isNaN(d_y = parseInt(d_x))? 0 : d_y;
}

//LAYER constructor ------------------------------------------
function LAYER(layer_id, content)
{
	this.initial_X=0;
	this.initial_Y=0;
	this.initial_W=0;
	this.initial_H=0;
	
	if (document.getElementById)
	{
		this.layer = document.getElementById(layer_id);
		this.layerstyle = this.layer.style;
	}
	else if (document.all)
	{
		this.layer = document.all[name];
		this.layerstyle = this.layer.style;
	}
	else if (document.layers)
	{
		this.layer = _LayerNS4(document, layer_id);
		this.layerstyle = this.layer;
	}
	else return false;
	
	//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);
	}
	
	//make the layer visible and invisible
	this.Show = function () { 
			this.layerstyle.visibility = "visible";
	};

	this.Hide = function () {
			this.layerstyle.visibility = "hidden";
	};
	
	this.Move = function (new_x, new_y, duration) {
			//todo: gérer duration
			//-1 means no change
			if (new_x!=-1) this.layerstyle.left = new_x;
			if (new_y!=-1) this.layerstyle.top = 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 = Int(new_w)+"px";
			if (new_h!=-1) var new_h = Int(new_h)+"px";
			
			//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);
	};	

}


/*
//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) {
	
		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;
			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=500 src='"+this.base_managt_URL+"?"+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) {
			//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
}