//---  DIALOG FUNCTIONS   -------//

ShB.Dialog.prototype = {

	initialize: function(name, dialogParentName, scopeId, config, modal, cache, visible, path) {
		this.name= name
		this.config=config;
		this.modal= modal;
		this.dialogParentName=dialogParentName;
		this.cache = cache;
		this.scopeId = scopeId;	
		this.minimize= false;
				
		if (ShB.Dialog.getDialog(name)!=null)
			ShB.Dialog.hide(name)			
		
		ShB.Dialog.dialogs[ShB.Dialog.dialogs.length]=this;	
		
		this.frmObj= document.createElement("iframe");
		this.frmObj.setAttribute("id",this.name+"_iframe");
		this.frmObj.style.position="absolute";
		this.frmObj.style.top=0;
		this.frmObj.style.left=0;
		var url = (path!=null)?path:"";
		this.frmObj.src= url+"resources/com.isencia.sherpa.web.styles.basic.script.ScriptDummy/blank.html";
		this.frmObj.setAttribute("frameBorder","0")
		this.frmObj.style.display="none"
		this.frmObj.setAttribute("allowtransparency","true");
		this.frmObj.style.filter="chroma(color=#FFFFFF)";

		this.divId = name+"_dialog";
		this.divObj= document.getElementById(this.divId);
		this.parentId = ShB.Dialog.findParentId(this);	
		
		this.c = ShB.Dialog.findContainer(this.divObj);
		this.c.appendChild(this.divObj)
		
		if (ie)
			this.c.appendChild(this.frmObj)
		
		if (visible)
		{
			ShB.Dialog.currentDialogName = name;	
			this.doDialog();			
		}
				
	},
		
	doDialog : function(){
	
		if (ShB.Dialog.currentDialogName==null || 
			ShB.Dialog.currentDialogName!=this.name)
			return;
			
		var els = getFormElements(this.divId)
		for (var i=0; i<els.length; i++)
		{
			els[i].scope = this.divObj;
		}
		if(window.checkNodes!=null)
			checkNodes(this.divObj)
		
		this.divObj.style.visibility = "hidden";
				
		//positioning dialog	
		//this.divObj.style.left = this.frmObj.style.left = this.getLeft()+"px";
		//this.divObj.style.top = this.frmObj.style.top = this.getTop()+"px";

		if (this.getLeft() + this.divObj.clientWidth > YAHOO.util.Dom.getViewportWidth() + document.body.scrollLeft)
			this.divObj.style.left = this.frmObj.style.left = (YAHOO.util.Dom.getViewportWidth() + document.body.scrollLeft - this.divObj.clientWidth) + "px";
		else	
			this.divObj.style.left = this.frmObj.style.left = this.getLeft()+"px";

		if (this.getTop() + this.divObj.clientHeight > YAHOO.util.Dom.getViewportHeight() + document.body.scrollTop)
			this.divObj.style.top = this.frmObj.style.top = (YAHOO.util.Dom.getViewportHeight() + document.body.scrollTop - this.divObj.clientHeight) + "px";
		else	
			this.divObj.style.top = this.frmObj.style.top = this.getTop()+"px";
		
		if (document.getElementById(this.name+"_pos")!=null && 
			document.getElementById(this.name+"_pos").value.length==0){
			document.getElementById(this.name+"_pos").value= parseInt(this.getLeft())+";"+parseInt(this.getTop());
		}
		
		if (ie)
			ShB.Dialog._resizeIframe()
				
		this.divObj.style.visibility = "visible";
		this.frmObj.style.display= "block";
		
  		this.scope = ShB.Scope.getScope();
		if (this.modal)
			ShB.Scope.showMask(this.name)
			//ShB.Scope.newScope(this.divObj.id,null, true)
			
		this.upLevel();
		
		this.divObj.tmpObj=this
		this.divObj.onmouseup= function(){
			ShB.Dialog.currentDialogName = this.tmpObj.name;
			if(ie && this.tmpObj.minimize==false)
				ShB.Dialog._resizeIframe();
		 	
			this.tmpObj.upLevel();
		};	
	},
	
	getLeft : function(){	
		var w=this.divObj.offsetWidth;
		
		var buttonPos = YAHOO.util.Dom.getX(this.config.id);
		var buttonWidth = (document.getElementById(this.config.id)!=null)?document.getElementById(this.config.id).offsetWidth:0;
		var relativePos = this.config.relLeft;
		var pos = this.config.left;
		//left of page
		var value = pos;		
		//center of page
		if (relativePos=="CENTER_OF_PAGE")
			value= (ie || ie5)?(YAHOO.util.Dom.getViewportWidth()+document.documentElement.scrollLeft+document.body.scrollLeft-w)/2:
					(YAHOO.util.Dom.getViewportWidth()+window.scrollX-w)/2;
		//right of page
		else if(relativePos=="RIGHT_OF_PAGE")
			value= (((ie || ie5)?(YAHOO.util.Dom.getViewportWidth()+document.documentElement.scrollLeft+document.body.scrollLeft-w):
					(YAHOO.util.Dom.getViewportWidth()+window.scrollX-w)))+pos;
		//left of button
		else if(relativePos=="LEFT_OF_BUTTON"){
			if (pos>=0)
				value = value+buttonPos;
			else
				value = buttonPos+value- w;	
		}
		//center of button
		else if (relativePos=="CENTER_OF_BUTTON")
			value = buttonPos+ (buttonWidth/2);
		//right of button
		else if (relativePos=="RIGHT_OF_BUTTON")
				value = buttonPos+ buttonWidth+ pos;
		else if (relativePos=="MOUSE")
			value = ShB.Dialog.posX+5;
					
		if (value<0)
			value = 0;
			
		if ((value+w)>screen.width && w<screen.width && screen.width-w-20>0)
			value=screen.width-w-20	
			
		return value;
	},
	
	getTop : function(){	
		var h=this.divObj.offsetHeight;
	
		var buttonPos = YAHOO.util.Dom.getY(this.config.id);
		var buttonHeight = (document.getElementById(this.config.id)!=null)?document.getElementById(this.config.id).offsetHeight:0;
		var relativePos = this.config.relTop;
		var pos = this.config.top;
		var value = 0;
		
		//top of page
		var value = pos;		
		//middle of page
		if (relativePos=="MIDDLE_OF_PAGE")
			value= (ie || ie5)?(YAHOO.util.Dom.getViewportHeight()+document.documentElement.scrollTop+document.body.scrollTop-h)/2:
					(YAHOO.util.Dom.getViewportHeight()+window.scrollY-h)/2;
		//bottom of page
		else if(relativePos=="BOTTOM_OF_PAGE")
			value= (((ie || ie5)?(YAHOO.util.Dom.getViewportHeight()+document.documentElement.scrollTop+document.body.scrollTop-h):
					(YAHOO.util.Dom.getViewportHeight()+window.scrollY-h)))+pos;
		//top of button
		else if(relativePos=="TOP_OF_BUTTON"){
			if (pos>=0)
				value = buttonPos+pos;
			else
				value = buttonPos+pos- h;
		}
		//middle of button
		else if (relativePos=="MIDDLE_OF_BUTTON")
			value = buttonPos+ (buttonHeight/2);
		//bottom of button
		else if (relativePos=="BOTTOM_OF_BUTTON"){
			if (pos>=0)
				value = buttonPos+ buttonHeight+ pos;
			else
				value = buttonPos+ buttonHeight+ pos - h
		}
		else if (relativePos=="MOUSE")
			value = ShB.Dialog.posY+5;
			
		if(value<0)
			value=0;
			
		return value;
	},
	
	upLevel : function(){
		this.frmObj.style.zIndex= ShB.Dialog.z_index++
		this.divObj.style.zIndex= ShB.Dialog.z_index++		
	}	
}

function mbHT(dialogName) {
	
	ShB.Dialog.currentDialogName=null;
	var obj= ShB.Dialog.getDialog(dialogName);	
	clearTimeout(ShB.Dialog.dialogToShowTimeout);
	if (obj!=null && obj.dialogToShowTimeout!=null) {
		clearTimeout(obj.dialogToShowTimeout);
		obj.dialogToShowTimeout=null;		
	}
	if (obj==null || obj.dialogToHideTimeout==null) {
		if(obj!=null && obj.divObj.style.visibility!="hidden")
			obj.dialogToHideTimeout=setTimeout("ShB.Dialog.hide('"+dialogName+"');", 250);
	}
}

function mbTC(dialogName) {	
		
	var obj= ShB.Dialog.getDialog(dialogName);	
	if (obj!=null && obj.dialogToHideTimeout!=null) {
		clearTimeout(obj.dialogToHideTimeout);
		obj.dialogToHideTimeout=null;	
		obj.upLevel();
	}	
}
x=0;
ShB.Dialog.findContainer= function(obj){
	while(obj.parentNode!=null){
		
		if(obj.nodeName=="FORM")
			return(obj);
			
		obj = obj.parentNode;
	}
	return document.body;
}

ShB.Dialog._resizeIframe = function()
{
	if (ShB.Dialog.currentDialogName==null)
		return;	
	
	var dialog = this.getDialog(ShB.Dialog.currentDialogName);
	var w= dialog.divObj.offsetWidth;
	var h= dialog.divObj.offsetHeight;
	dialog.frmObj.style.height=(h)+"px";
	dialog.frmObj.style.width=(w)+"px";
	
}
ShB.Dialog.hideLoadingDialog = function()
{
	for (var i=0; i<this.dialogs.length; i++){
		if (this.dialogs[i].loadingDialog!=null)
			this.hide(this.dialogs[i].name)
	}	
}

//hideDialog
ShB.Dialog.hide = function(name, cache, modal)
{	
	if (name==this.currentDialogName)	
		this.currentDialogName = null;

	var obj= this.getDialog(name);
	if (obj!=null)
	{		
		var newModal = (modal!=null)?modal:obj.modal;
		var newCache = (cache!=null)?cache:obj.cache;
		if (document.getElementById(obj.divObj.id)!=null)
		{
			if(newModal && document.getElementById(obj.divObj.id).style.visibility!="hidden"){
				ShB.Scope.hideMask(obj.name)
				//ShB.Scope.previousScope();
				//ShB.Scope.activateForm(ShB.Scope.getScope());
			}	
			if (!newCache)
	   		{
	   			if (document.getElementById(obj.divId)!=null)
				{
		   			//var anim = new YAHOO.util.Anim(obj.divId, { opacity:{to:0}},0.6, YAHOO.util.Easing.easeBoth);
		   			//anim.onComplete.subscribe(function(){
		   				
		   		//		var obj= ShB.Dialog.getDialog(this.dialogName);
		   		//		if (obj==null)
		   		//			return;
		   				var divObj = document.getElementById(obj.divId);
		   				divObj.parentNode.removeChild(divObj)
		   				var frmObj = document.getElementById(obj.frmObj.id);
		   				
		   				if (ie && frmObj)frmObj.parentNode.removeChild(frmObj);
		   				
		   					
		   			//});
	   				//anim.dialogName = name;
		   				this.remove(obj.name);
				}
			}
			else{
				obj.divObj.style.visibility = "hidden";
				if (ie)
					obj.frmObj.style.display = "none";
			
			}
			for (var dialogIndex=this.dialogs.length-1; dialogIndex>=0; dialogIndex--)
			{
				if (this.dialogs.length>dialogIndex && this.dialogs[dialogIndex].dialogParentName==obj.name)
					this.hide(ShB.Dialog.dialogs[dialogIndex].name, cache, modal)
			}
		}
		else if (!newCache){
		   	this.remove(obj.name);	
			for (var dialogIndex=this.dialogs.length-1; dialogIndex>=0; dialogIndex--)
			{
				if (this.dialogs.length>dialogIndex && this.dialogs[dialogIndex].dialogParentName==obj.name)
					this.remove(ShB.Dialog.dialogs[dialogIndex].name)
			}
		   	
		}
		//anim.animate()
	}		
}

ShB.Dialog.remove = function(name){
	for (var i=0; i<this.dialogs.length; i++)
	{
		if (this.dialogs[i].name==name)
			this.dialogs.splice(i,1)
	}
}

//find id of first parent of a dialog that has defined the id
ShB.Dialog.findParentId = function(dialog){

	var parent = dialog.divObj.parentNode;	
	while(parent.parentNode!=null)
	{
		if (parent.id!=null)
			return parent.id;
		
		parent = parent.parentNode;
	}
}

//Hide all dialogs
ShB.Dialog.hideAll = function()
{
	for (var i=0; i<ShB.Dialog.dialogs.length; i++)
	{
		this.hide(ShB.Dialog.dialogs[i].name)
	}
}

ShB.Dialog.minimize = function(id)
{
//	if (document.getElementById(id+"-foot")!=null)
//		document.getElementById(id+"-foot").style.display="none"	
		
	var dialog = ShB.Dialog.getDialog(id);
	dialog.restoreWidth= document.getElementById(id+"_dialog").offsetWidth;
	dialog.restoreHeight= document.getElementById(id+"-body").offsetHeight;
	dialog.restoreLeft= parseInt(dialog.divObj.style.left);
	dialog.restoreTop= parseInt(dialog.divObj.style.top);
	
	document.getElementById(id+"-head").style.width = document.getElementById(id+"-head").offsetWidth;
	document.getElementById(id+"-body").style.display="none"
	
	if (ie)
		ShB.Dialog._resizeIframe()
	

}
ShB.Dialog.maximize = function(id)
{
	var dialog = ShB.Dialog.getDialog(id);
	//moveTo(dialog.divObj, 0, 0)
	
	dialog.restoreWidth= document.getElementById(id+"_dialog").offsetWidth;
	dialog.restoreHeight= document.getElementById(id+"-body").offsetHeight;
	//dialog.restoreLeft= getLeft(dialog.divObj);
	//dialog.restoreTop= getTop(dialog.divObj);
	
	//document.getElementById(id+"_dialog").style.width=(getViewportSize(false).x-20)+"px";
	document.getElementById(id+"-head").style.width="auto";
	document.getElementById(id+"-body-content").style.width= "100%";
	document.getElementById(id+"-body-content").style.height= "100%";
	
	document.getElementById(id+"-body").style.display="block";
//	if (document.getElementById(id+"-foot")!=null)
//		document.getElementById(id+"-foot").style.display="block";

	if (ie)
		ShB.Dialog._resizeIframe()

}

ShB.Dialog.restore = function(id)
{
	var dialog = ShB.Dialog.getDialog(id);
	//moveTo(dialog.divObj, dialog.restoreTop, dialog.restoreLeft)
	
	document.getElementById(id+"-head").style.width = "auto";
	document.getElementById(id+"_dialog").style.width=dialog.restoreWidth+"px";
	document.getElementById(id+"-body").style.width= dialog.restoreWidth+"px";
	document.getElementById(id+"-body").style.height= dialog.restoreHeight+"px";
		
	document.getElementById(id+"-body").style.display="block";
//	if (document.getElementById(id+"-foot")!=null)
//		document.getElementById(id+"-foot").style.display="block";

	if (ie)
		ShB.Dialog._resizeIframe()

}

ShB.Dialog.drag = function(name)
{
	dragObj= this.getDialog(name);
		
	dragObj.upLevel();
	dragObj.relPosX= coorX -parseInt(dragObj.divObj.style.left, 10);
	dragObj.relPosY= coorY -parseInt(dragObj.divObj.style.top, 10);
	
	dragObj.move = function()
	{
		if (coorX<0 || coorY<0)	return;
		
		if (ie || ie5 || ns)
		{
			this.divObj.style.left = this.frmObj.style.left = coorX -this.relPosX+"px";
			this.divObj.style.top = this.frmObj.style.top = coorY -this.relPosY+"px";
		}
		if (ie)
			ShB.Dialog._resizeIframe()
		
		if (document.getElementById(name+"_pos")!=null)
			document.getElementById(name+"_pos").value= parseInt(this.divObj.style.left)+";"+parseInt(this.divObj.style.top);
	}
	drag=true;
}

ShB.Dialog.resize = function(name){

	dragObj= this.getDialog(name);	

	dragObj.relPosX= coorX ;
	dragObj.relPosY= coorY ;
	    	
	dragObj.w = dragObj.divObj.offsetWidth;
	dragObj.h = document.getElementById(name+"-body").offsetHeight
	dragObj.xx=0;
	dragObj.move= function()
	{
    	var w = Number(this.w)+ (coorX -Number(this.relPosX))
    	var he = Number(this.h)+ (coorY -Number(this.relPosY))
    	    	
	   this.incr = (coorX==this.relPosX)?null:(coorX>Number(this.relPosX))
    	//if (coorX==Number(this.relPosX))
 	  
 		this.relPosX= coorX
		this.relPosY= coorY
		
		if (w<=50 || he<=0)
			return;
		
		this.w= w;
		this.h= he;
		
    	this.divObj.style.width =  w;
    	
     	if (document.getElementById(this.name+"-body")==null)
    		return;

    	document.getElementById(this.name+"-body-content").style.width= w+"px";
 	    			
    	if (this.divObj.offsetWidth<=document.getElementById(this.name+"-body-content").offsetWidth)
     		this.prevStyleWidth = w;
	  	else if (this.prevStyleWidth!=null)
    		document.getElementById(this.name+"-body-content").style.width= this.prevStyleWidth+"px";
    
 	    document.getElementById(this.name+"-body").style.height= (he)+"px";
		document.getElementById(this.name+"-body-content").style.height= "100%";	
  		
		if (ie)
			ShB.Dialog._resizeIframe()

	}
	drag=true;
}
