try{
//This file defines a Layer-object which is used to show layers in an easy way.


//This is the constructor of the Layer-object.
function Layer(msg) {
	 if (!msg) {
		  /*		 var div=document.createElement("div");
		 div.style.border="0px solid black";
		 //div.style.backgroundColor="white";
		 return new Layer(div);*/
		  
		  return this;
	 }
	if(msg && msg.tagName && msg.tagName.toLowerCase()=="div") {
		 this.layer=msg; 
		 if (!this.layer.className){
       		this.layer.style.border="1px solid black";
				this.layer.style.backgroundColor="white";
		 }

	}
	else {
		 var div=document.createElement("div");
		 div.appendChild(document.createTextNode(msg));
		 return new Layer(div);
	}
	if (this.layer.getAttribute("didFetch")!=1) {
		 //is it the first time it is requested?
		 //this.layer.style.border="1px solid black";
		 this.layer.setAttribute("didFetch",1);
		 Layer.prototype.all.push(this);
		 Layer.prototype.isOpen.push(0);
		 this.masterIndex=Layer.prototype.all.length-1;	
		 this.layer.setAttribute("masterIndex",this.masterIndex);
		 this.assignEvents();
		 if (!this.layer.parentNode) {
			  document.body.appendChild(this.layer);
		 }
		 return this;
	}
	else {
		 //use the previous one.
		 return this.all[this.layer.getAttribute("masterIndex")];
		 
	}
}

Layer.prototype.all=new Array();
Layer.prototype.selects=null;
Layer.prototype.isOpen=new Array();

//Shows a layer-instance with coordinates relative to the element which fires the event.
Layer.prototype.setWidthFromEl=function(el){
    if (getBrowserType()==1){
           
    }    
}

Layer.prototype.alert=function(myevent, srcEl, relativeWidth, relativeHeight, timelap, closeAllOther, dx, dy) {
	 
	 if (!relativeWidth) {
		  relativeWidth=1;
	 }
	 if(!relativeHeight) {
		  relativeHeight=1;
	 }
	 if (getBrowserType()==1) {
		  var x=myevent.clientX+document.body.scrollLeft-myevent.offsetX-myevent.srcElement.offsetWidth+myevent.srcElement.offsetWidth*relativeWidth+(dx?dx:0);
		  var y=myevent.clientY+document.body.scrollTop-myevent.offsetY+myevent.srcElement.offsetHeight*relativeHeight+(dy?dy:0);
	 }
	 else {
		  var x=myevent.clientX+document.body.scrollLeft-myevent.offsetX+(dx?dx:0);
		  var y=myevent.clientY+document.body.scrollTop-myevent.offsetY+(dy?dy:0);
	 }
	 this.showAbsolute(x,y,closeAllOther);

	 if (srcEl){
	    this.assignMouseCounterEvent(srcEl);
	    this.assignMouseOutEvent(srcEl);
	 }
	 this.closeDelayed(timelap);

}


//Shows the layer on screen given by the absolute coordinates x and y.
Layer.prototype.showAbsolute=function(x,y,closeAllOther) {
	 if (closeAllOther) {
		  var n=this.all.length;
		  for (var i=0;i<n;i++) {
				if (i!=this.masterIndex){
					 this.all[i].close();
				}
		  }
	 }
   this.layer.style.position="absolute";
	this.layer.style.zIndex=Layer.zIndex;
	Layer.zIndex++;
	if (this.width) {
		 this.layer.style.width=this.width;
	}
	this.layer.style.left=x;
	this.layer.style.top=y;
	this.open();
	return this;
}
//Returns all select elements in the document (cached at second call).
Layer.prototype.getSelects=function()
{
	if(this.selects==null){
		this.selects=document.getElementsByTagName("select");
	}
	return this.selects;
}
//Defines how the layer is shown on screen. Should be defined together with Layer.close
Layer.prototype.open=function() {
	 
     var myselects=this.getSelects();
	 	//uncomment this for hidden selects!	
    	//for (var i=0;i<myselects.length;i++) {
		//myselects[i].style.visibility="hidden";
    	// }
     this.opened=true;
     this.isOpen[this.masterIndex]=1;
	  //if (this.layer.style.display=="none" || (!this.layer.style.display)) {
		  this.layer.style.display="inline";
	  //}
	 var outerThis=this;
	 return this;
}
//Returns true if at least one layer is open.
Layer.prototype.isOneOpen=function()
{
	for (var i=0;i<this.isOpen.length;i++)
	{
		if(this.isOpen[i]==1) return true;
	}
	return false;
}
//Defines how the layer is removed from screen. Should be defined together with Layer.open.
Layer.prototype.close=function() {
	 this.opened=false;
     this.isOpen[this.masterIndex]=0;
     if(!this.isOneOpen()) {
		var myselects=this.getSelects();
		for (var i=0;i<myselects.length;i++) {
			myselects[i].style.visibility="visible";
		}
	 }
	 this.layer.style.display="none";
	 /*if (this.layer.parentNode) {
		  this.layer.parentNode.removeChild(this.layer);
		  }*/
	 return this;
	 
}
//Used to assure that each showing of a layer has a z-index higher than all previous. This number is increased everytime a layer is shown.
Layer.zIndex=10;
Layer.prototype.assignEvents=function() {
	var outerThis=this; //to avoid shadowing in inner functions and to avoid confusion with 
						//meaning of this in inner functions.
	outerThis.layer.onclick=function() {
		outerThis.close();
	}	
	/*outerThis.layer.onmouseout=function() {
		outerThis.closeDelayed();
	} */
	this.assignMouseCounterEvent();
	this.assignMouseOutEvent();
	
	return this;
}

Layer.prototype.assignMouseOutEvent=function(el){
    if (!el){ 
        el=this.layer;
    }
    var outerThis = this;
    
    function myOnMouseOut() {
		outerThis.closeDelayed();
    }        
    
    addEvent(el, 'onmouseout', myOnMouseOut);    
}

Layer.prototype.assignMouseCounterEvent=function(el){
    if (!el){ 
        el=this.layer;
    }
    var outerThis = this;
    
    function myOnMouseOver() {
    	if (! outerThis.mouseOverCounter) {
    		outerThis.mouseOverCounter=1;
    	}
    	else  {
    		outerThis.mouseOverCounter++;		
    	}
    }        
    
    addEvent(el, 'onmouseover', myOnMouseOver);
}

Layer.prototype.timelap=500; //static value of timeinterval for closure of layer.

//closes the layer if the mouseOverCounter matches the one passed as argument.
Layer.prototype.closeConditioned=function(mouseOverCounter_old) {
	if (this.mouseOverCounter==mouseOverCounter_old) {//this means that mouse has not passed over the layer.
		this.close();
	}
	return this;
}
//Closes the layer after a small delay if the mouse 
//is not over the layer or has been over the
//layer in the waiting time.
//Note that the function is overloaded. 
//If it is called with an argument (an integer)
//this is taken to be the master index of the layer-instance.
//If a timelap less than 0 is given, then the layer is never removed.
Layer.prototype.closeDelayed=function(timelap) {
	 var mytimelap=(timelap?timelap:this.timelap);
	 this.timelap=mytimelap;
	 if (mytimelap>=0) {
		  var command=("Layer.prototype.all["+this.masterIndex+"].closeConditioned("+this.mouseOverCounter+");");
		  window.setTimeout(command,mytimelap);	
	 }
	 return this;
}
Layer.prototype.setWidth=function(width) {
	 this.width=width;
	 return this;
}

//Alerts a message in a fancy layer (to avoid the alert-box ok-clicking).
//Can be called with one, two or three arguments. The first must be a message.
//The calling semantics is as follows:
//niceAlert(msg, event?, timelap?, uniqueid?) : shows an alert at mouse position lasting timelap milliseconds.
//If event is null, it is shown at (50,50).
function niceAlert(msg, myevent, timelap, uniqueId) {
	 // try {
	 var layer;
	 if (uniqueId) {
		  if (! niceAlert.layers[uniqueId]) {
				//Create and store in static hash table...
				niceAlert.layers[uniqueId]=new Layer(msg);
		  }
		  layer=niceAlert.layers[uniqueId];
	 }
	 else {
		  layer=new Layer(msg);
	 }
	 layer.setWidth(200);
	 if (myevent) {
		  layer.alert_mousePosition(myevent,null,null,timelap);
	 }
	 else if(timelap) {
		  layer.alert_absolute(50,50, timelap);
	 }
	 else {
		  layer.alert_absolute(50,50,2000);
	 }
	 //catch(e) {
	 //		  handleError(e,"Der skete en uventet fejl i niceAlert()");
	 //	 }
}
niceAlert.layers=new Object();
}
catch(e){
    alert(e.message+e.lineNumber);    
}

