//	| ########################################################################## |
//	|-------------- powered by Djingle -------- 2006 --------------------------- |
//	| ########################################################################## |

/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//	---| class LAYER
//	---| author: Régis CAZANAVE (Djingle)
//	---| date: 2006-08-21
//	---| version: V1.0
//	---| (works bad on NS4)
/////////////////////////////////////////////////////////////////////////////////////

//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);
	
	if (this.layer) {
		//alert(this.layer.id);
		this.layerstyle = this.layer.style;
	} else {
		return false;//not found
	}
	
	//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.Open = function (display_style) {
		this.layerstyle.display = (display_style)?display_style:"block";
	};

	this.Close = function () {
		this.layerstyle.display = "none";
	};
	
	this.OpenClose = function () {
		if (this.layerstyle.display == "block") {
			this.Close();
			return false;
		} else {
			this.Open();
			return true;
		}
	};
	
	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.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;
		}		
  	};

}

/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//	---| class MsgBox
//	---| author: Régis CAZANAVE (Djingle)
//	---| date: 2010-01-10
//	---| version: V2.0
///////////////////////////////////////////////////////////////////////////////////// 

function MsgBox(mbObjectName, mbId, mbTitle, mbText, reminder, skinBase, boxStyle) {
	
	//init
	this.msgBoxDivName=mbId;
	
	this.msgBoxObjectName=mbObjectName;
	this.containerLayer = new LAYER(mbId+'Container');
	this.skinBase=skinBase;
	
	this.title=mbTitle;
	this.message=mbText;
	
	this.okButtonLabel='Ok';
	
	this.displayReminder=false;this.remindMe=false;
	if (reminder) {
		this.displayReminder=true;
	}
	this.fullScreenMask = new LAYER('fSMask');
	
	if (boxStyle)
		this.boxStyle=boxStyle;
	else
		this.boxStyle='darkBox';
		
	this.clickOk = function() {
	
		if (this.displayReminder && document.getElementById(this.msgBoxDivName+'ReminderCB').checked) {
			this.remindMe=true;
		}
		if (this.onOk()) {
			this.closeBox();
		}
	};
	
	this.onOk = function() {
		return true;
	};
	
	this.closeBox = function() {
		//close layer
		this.containerLayer.WriteInto('');
		this.containerLayer.Hide();
		
		//close dark mask
		this.fullScreenMask.Hide();
	};
	
	this.pop = function() {
		var tr=this._buildMsgBox();
		
		if (this.fullScreenMask) this.fullScreenMask.Show();
	
		this.containerLayer.WriteInto(tr);
		this.containerLayer.Show();
	};
	
	this._buildMsgBox = function() {
	
		var tr='<div id="'+this.msgBoxDivName+'" class="'+this.boxStyle+'">';
		tr+='<table width=100% cellpadding=3 cellspacing=0 border=0><tr><td class="'+this.boxStyle+'Title">'+this.title+'</td>';
		tr+='<td align=right class="'+this.boxStyle+'Title"><a href="javascript:'+this.msgBoxObjectName+'.closeBox();" class="'+this.boxStyle+'SmallLnk">Close <img src="'+this.skinBase+'images/buttons/close_line_black.gif" border=0 align=absmiddle /></a></td></tr>';
		tr+='<tr><td colspan=2 class="'+this.boxStyle+'Text">'+this.message+'</td></tr>';
		
		if (this.displayReminder) {
			tr+='<tr><td colspan=2 class="'+this.boxStyle+'Small"><input type=checkbox id="'+this.msgBoxDivName+'ReminderCB" value=1> Do not show this message box again</td></tr>';
		}
		tr+='<tr><td colspan=2 align=center><input type=button onclick="'+this.msgBoxObjectName+'.clickOk();" value="'+this.okButtonLabel+'" class="'+this.boxStyle+'Button"></td></tr>';
		tr+='</table></div>';
		if (this.addData) {
			for (var d in this.addData) {
				tr+='<input type=hidden name="'+d+'" id="'+d+'" value="'+this.addData[d]+'">';
			}
		}
		//alert(tr);
		return tr;
	};	
}

/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//	---| class ConfirmBox
//	---| author: Régis CAZANAVE (Djingle)
//	---| date: 2010-01-10
//	---| version: V1.0
///////////////////////////////////////////////////////////////////////////////////// 

function ConfirmBox(mbObjectName, mbId, mbTitle, mbText, onOkFunction, reminder, skinBase, boxStyle) {
	this.inheritFrom = MsgBox;
	this.inheritFrom(mbObjectName, mbId, mbTitle, mbText, reminder, skinBase, boxStyle);
	
	this.okButtonLabel='Yes';
	this.denyButtonLabel='No';
	
	if (onOkFunction)
		this.onOk = onOkFunction;
	
	this.clickDeny = this.closeBox;
	
	this._buildMsgBox = function() {
	
		var tr='<div id="'+this.msgBoxDivName+'" class="'+this.boxStyle+'">';
		tr+='<table width=100% cellpadding=3 cellspacing=0 border=0><tr><td class="'+this.boxStyle+'Title">'+this.title+'</td>';
		tr+='<td align=right class="'+this.boxStyle+'Title"><a href="javascript:'+this.msgBoxObjectName+'.closeBox();" class="'+this.boxStyle+'SmallLnk">Close <img src="'+this.skinBase+'images/buttons/close_line_black.gif" border=0 align=absmiddle /></a></td></tr>';
		tr+='<tr><td colspan=2 class="'+this.boxStyle+'Text">'+this.message+'</td></tr>';
		
		if (this.displayReminder) {
			tr+='<tr><td colspan=2 class="'+this.boxStyle+'Small"><input type=checkbox id="'+this.msgBoxDivName+'ReminderCB" value=1> Do not show this message box again</td></tr>';
		}
		tr+='<tr><td colspan=2 align=center><input type=button onclick="'+this.msgBoxObjectName+'.clickOk();" value="'+this.okButtonLabel+'" class="'+this.boxStyle+'Button"> <input type=button onclick="'+this.msgBoxObjectName+'.clickDeny();" value="'+this.denyButtonLabel+'" class="'+this.boxStyle+'Button"></td></tr>';
		
		tr+='</table></div>';
		if (this.addData) {
			for (var d in this.addData) {
				tr+='<input type=hidden name="'+d+'" id="'+d+'" value="'+this.addData[d]+'">';
			}
		}
		return tr;
	};
}

/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//	---| class TABS
//	---| author: Régis CAZANAVE (Djingle)
//	---| date: 2006-08-28
//	---| version: V1.0
//	---| (works bad on NS4)
/////////////////////////////////////////////////////////////////////////////////////
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+"Contents";
	var the_content_layer = new LAYER(this.content_div_id);
	
	this.tabs_list=this.layer.getElementsByTagName("li");
	
	this.active_tab=-1;
	
	//specific TABS methods	
	this.callTab = function (tab_ix, tab_src, add_data) {
	
		if (!tab_ix) tab_ix="0";
		
		this.active_tab=tab_ix;
		
		if (call_mode=="ajax") {
			var the_response;var the_result;var the_result_a=new Array;

			//build data array
			var data = new Array;
			if (add_data)
				data=add_data;
			data["tab_ix"]=tab_ix;
			data["tab_src"]=tab_src;
			
			if (this.debug_mode) alert("envoi à la page distante avec data = todo=get_tab&tab_ix=" +tab_ix);
			
			the_response=CallAJAX("GET", this.base_managt_URL, data, false);

			if(the_response.readyState == 4) {
				the_result=the_response.responseText;
				if (this.debug_mode) alert("retour de cette page = "+the_result);
				the_result_a=the_result.split("|");
				
				//display
				if (the_result_a[0]=="ok") 
					the_content_layer.WriteInto(the_result_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
}
