//--- GLOBAL VARIABLES  -------//
var Class = {
	create: function() {
		return function() {
			this.initialize.apply(this, arguments);
		}
	}
}
if (typeof(ShB) == "undefined")
	ShB = { };
	
var contextRoot = null;
// focusedObject attribute is used to control the last object focused
var focusedObject = null;


//--- CHECK NAVIGATOR  --------//
function Browser() {
	var b=navigator.appName;
	if (b.indexOf('Netscape')!=-1) this.b="ns";
	else if ((b=="Opera") || (navigator.userAgent.indexOf("Opera")>0)) this.b = "opera";
	else if (b=="Microsoft Internet Explorer") this.b="ie";
	
	this.version=navigator.appVersion;
	this.v=parseInt(this.version);
	this.ns=(this.b=="ns" && this.v>=4);
	this.ie5=(this.version.indexOf('MSIE 5')>0);
	this.opera= (this.b=="opera")
	
	var index = navigator.userAgent.indexOf("MSIE");
	this.ie=(parseFloat(navigator.userAgent.substring(index + 5))>5 && this.opera==false);
	
}
is=new Browser();
var ns=is.ns;
var ie = is.ie;
var ie5 = is.ie5;
var opera= is.opera;

//--- END CHECK NAVIGATOR  -------//

//---  BASIC DIALOG FUNCTIONS   -------//
if (typeof(ShB.Dialog) == "undefined")
	ShB.Dialog = Class.create();
	
ShB.Dialog.z_index=1000;
//current Dialog
ShB.Dialog.currentDialogName = null;
//used in ie to resize the iframe object when the size of the dialog change
ShB.Dialog.resizeInterval = null;
//used to save the mouse position when the dialog is loading it
ShB.Dialog.posY=0;
ShB.Dialog.posX=0;

if (typeof(ShB.Dialog.dialogs)=="undefined")
	ShB.Dialog.dialogs=new Array(); 
		
ShB.Dialog.getDialog = function(value)
{
	if (typeof(value)=="string")
	{
		for (i=0; i<this.dialogs.length; i++)
		{
			if (this.dialogs[i].name==value)
				return ShB.Dialog.dialogs[i];
		}
	}
	else
	{
		while(value!=null)
		{
			if (value.id!=null && value.id.lastIndexOf("_dialog")==value.id.length-7)
				return ShB.Dialog.getDialog(value.id.substring(0,value.id.lastIndexOf("_dialog")));
			value= value.parentNode;
		}
	
	}
	return null;
}

ShB.Dialog.showTooltip = function(obj, name, forward, delay)
{	
	var dialog= this.getDialog(name);
	this.currentDialogName = null;
	if (ShB.Dialog.dialogToShow != obj && dialog!=null){
		ShB.Dialog.hide(name);
		ShB.Dialog.dialogToShow= obj;
	}
	ShB.Dialog.dialogToShowTimeout = setTimeout("ShB.Dialog.show(\""+name+"\",\""+forward+"\");", delay);
}

//showDialog
ShB.Dialog.show = function(name, forward)
{	
	this.currentDialogName = name;
	
	this.posX=coorX 
	this.posY=coorY

	var obj= this.getDialog(name);
	
	if (obj!=null)
		obj.doDialog();
	else if (forward!=null){	
		eval(forward); 
	}
}


ShB.Dialog.hideByScopeId = function(scopeId){
	for (var dialogIndex=this.dialogs.length-1; dialogIndex>=0; dialogIndex--)
	{
		if (this.dialogs.length>dialogIndex)
		{
			for (var scopeIndex=0; scopeIndex<this.dialogs[dialogIndex].scopeId.length; scopeIndex++)
			{
				if (this.dialogs[dialogIndex].scopeId[scopeIndex]==scopeId)
				{
					this.hide(this.dialogs[dialogIndex].name, false, true);
					break;
				}
			}
		}
	}	
}


ShB.Dialog.hideById = function(id){
	var c = document.getElementById(id);
	ShB.Dialog.hideByComponent(c);	
}
//hide all dialogs that are inside of a component
ShB.Dialog.hideByComponent = function(c){

	if (c!=null)
	{
		//TODO check for wicket debugger
		if (c.id!=null && c.id=="wicketAjaxDebugWindow")
			return;
	
		if (c.id!=null && c.id.length>0)
		{
			for(var p=this.dialogs.length-1; p>=0; p--)
			{
				if (this.dialogs.length>p && this.dialogs[p].parentId==c.id){
					var dialog = this.dialogs[p];
					this.hideByComponent(dialog.divObj)
					this.hide(dialog.name, false);
				}
			}
		}	
		
		for (var i=0; i<c.childNodes.length; i++)
		{
			var child = c.childNodes[i];
			if (child.nodeType=="3")
				continue;

			this.hideByComponent(child)			
		}
	}	
}

// DRAG FUNCTIONS //

//If is dragable
var drag=false
//Dialog o Cell object selected
var dragObj=null
//Mouse cor X
var coorX=0;
//Mouse cor Y
var coorY=0;

// dragStart
function iDS()
{
	drag=true;
}

// dragMove
function iDM(e)
{
   e= (e) ? e: ((window.event) ? event:null);

   if (ns)
   {
   	 coorX=e.clientX+window.scrollX; 
   	 coorY=e.clientY + window.scrollY;
   }
   if ((ie || ie5 ) && document.body != null)
    {
   	 coorX=e.clientX + document.documentElement.scrollLeft+ document.body.scrollLeft
   	 coorY=e.clientY + document.documentElement.scrollTop+ document.body.scrollTop
   }
	
	if (drag==true && dragObj!=null)
	{
		//when we create a new drag object, we have to implement the move function to
		//add the functionality to do when the object is dragging
		dragObj.move()

		if (ie || ie5) {
		    e.cancelBubble = true;
		    e.returnValue = false;
		}
		if (ns)
		{
		    e.preventDefault();
		    e.stopPropagation();
		}

	}
	return true;
}

// dragStop
function iDSo()
{
	drag=false
	if (dragObj!=null && dragObj.stopDrag!=null)
		dragObj.stopDrag();
	dragObj=null
}

// mouse events
if (ie || ie5) {document.onmousemove = iDM;document.onmouseup= iDSo;}
if (ns){document.addEventListener("mousemove", iDM, true);
	document.addEventListener("mouseup", iDSo, true)}


