/**
 *
 * @param string sId
 * @return void
 */
XPage = function(sId){
	this._id = sId;
}//end function

/**
 *
 * @return void
 */
XPage.prototype.init = function (){

	var aH3 = this.findAllTrigger();
	var i;
	var regItemId = /(.+)_([^_]+)/;
	var aMatches;
	
	for(i in aH3){
		aMatches = regItemId.exec(aH3[i].id);
		
		if(aMatches.length == 3){
			aH3[i]._childId = aMatches[1];
			aH3[i]._type = aMatches[2];
			
			if(XPageHelper.enabledDefaultHandling){
				aH3[i].onmousedown = XPageHelper.onmousedownHandler;
			}//end if
			
		
		}//end if
		
	}//end for
	
}//end function

/**
 *
 * @return array
 */
XPage.prototype.findAllTrigger = function (){

	var oXPage = window.document.getElementById(this._id );
	var aH3 = oXPage.getElementsByTagName('h3');
	var i;
	var regTrigger = /trigger/;
	var aResult = new Array();
	
	for(i in aH3){
		
		if(regTrigger.test(aH3[i].className)){
			aResult[aResult.length] = aH3[i];
		}//end if
		
	}//end for
	
	return aResult;
	
}//end function

/**
 *
 * @return void
 */
XPage.prototype.closeAll = function (){

	var aH3 = this.findAllTrigger();
	var i;
	for(i in aH3){
		XPageHelper.closeChild(aH3[i].id);
	}//end for
}//end function

/**
 *
 * @return void
 */
XPage.prototype.openAll = function (){

	var aH3 = this.findAllTrigger();
	var i;
	for(i in aH3){
		XPageHelper.openChild(aH3[i].id);
	}//end for
}//end function

XPageHelper = new Object();
XPageHelper.enabledDefaultHandling = true;
/**
 *
 * @return void
 */
XPageHelper.onmousedownHandler = function(e){

	var regOpen = /_open/;
	var oChild = window.document.getElementById(this._childId );

	if(regOpen.test(oChild.className)){
		oChild.className = oChild.className.replace(/_open/, '');
		this.className = this.className.replace(/_open/, '_close');
	}//end if
	else{
		oChild.className = oChild.className.replace(new RegExp(this._type), this._type + '_open');
		this.className = this.className.replace(/_close/, '_open');
	}//end else

};//end function

/**
 *
 * @param string sId
 * @return void
 */
XPageHelper.openChild = function(sId){

	var regOpen = /_open/;
	var oItem =  window.document.getElementById(sId);
	
	if(oItem){
		var oChild = window.document.getElementById(oItem._childId);

		if(!regOpen.test(oChild.className)){
			oChild.className = oChild.className.replace(new RegExp(this._type), oItem._type + '_open');
			oItem.className = oItem.className.replace(/_close/, '_open');
		}//end else
	}//end if
	
};//end function

/**
 *
 * @param string sId
 * @return void
 */
XPageHelper.closeChild = function(sId){

	var regOpen = /_open/;
	var oItem =  window.document.getElementById(sId);
	if(oItem){
		var oChild = window.document.getElementById(oItem._childId);

		if(regOpen.test(oChild.className)){
			oChild.className = oChild.className.replace(/_open/, '');
			oItem.className = oItem.className.replace(/_open/, '_close');
		}//end if
	}//end if
};//end function

/**
 *
 * @param string sUrl
 * @return void
 */
XPageHelper.delPageItem = function(sUrl){
	if(confirm("Voulez-vous supprimer cet élément")){
		window.location.href = sUrl;
	}//end if
};//end function

var __XPageOpenToolBar = null;

/**
 *
 * @param string sId
 * @return void
 */
XPageHelper.switchToolbar = function(sId){

	oObject = window.document.getElementById(sId + '_toolbar');

	if(oObject){
	
		if(__XPageOpenToolBar != null){
			window.document.getElementById(__XPageOpenToolBar + '_toolbar').style.display = 'none';
			window.document.getElementById(__XPageOpenToolBar + '_trigger').className = 'xContentEditableButton trigger';
		}//end if
		
		if(sId != __XPageOpenToolBar){
			__XPageOpenToolBar = sId;
			oObject.style.display = 'block';
			window.document.getElementById(__XPageOpenToolBar + '_trigger').className = 'xContentEditableButton_open trigger';		
		}//end if
		else{
			__XPageOpenToolBar = null;
		}//end else

	}//end if
	
};//end function