var OPEN=1;
var CLOSED=0;
var CLOSED_GIF="/humanconsultcms/PLSite/Default/graphics/common/dropClose.gif";
var OPEN_GIF="/humanconsultcms/PLSite/Default/graphics/common/dropOpen.gif";

function DropBox(dropBoxRef) {
	 //Need to save a reference to this to avoid shadowing in inner functions...
	 var thisRef=this;
	 //go...
	 this.dropBoxRef=dropBoxRef;
	 this.OPEN_IMG=document.createElement("IMG");
	 this.OPEN_IMG.src=OPEN_GIF;
	 this.CLOSED_IMG=document.createElement("IMG");
	 this.CLOSED_IMG.src=CLOSED_GIF;
	 //fetching title either by a h2-tag or by a title attribute...
	 this.title="";
	 if(this.dropBoxRef.getElementsByTagName("h2").length>0) {
		  this.title=this.dropBoxRef.getElementsByTagName("h2")[0].innerHTML;
		  this.dropBoxRef.removeChild(this.dropBoxRef.getElementsByTagName("h2")[0]);
	 }
	 else {
		  this.title=this.dropBoxRef.getAttribute("title");
	 }
	 if (dropBoxRef.getElementsByTagName("div").length==0) {
		  alert("You must use a nested div-element inside the dropbox-div in order to make it work.");
	 }
	 this.innerdiv=this.dropBoxRef.getElementsByTagName("div")[0];
	 //get both images for proper preloading...
	 this.buttonImg1=document.createElement("img");
	 this.buttonImg1.src=OPEN_GIF;
	 this.buttonImg1.style.border="";	 
	 this.buttonImg2=document.createElement("img");
	 this.buttonImg2.src=CLOSED_GIF;
	 this.buttonImg2.style.border="";
	 this.headLine=document.createElement("table");
	 this.headLine.className="openclose";
	 var outerThis = this;
	 this.headLine.onclick = function(e) {
	 	if(!e) {e=window.event;}
	 	outerThis.swapState();
	 	e.cancelBubble = true;
	 }
	 this.headLine.onkeypress = function(e) {
	 	if(!e) {e=window.event;}
	 	e.cancelBubble = true;
	 }
	 this.headLine.style.cursor="hand";
	 this.headLine.width="100%";
	 this.headLine.border=0;
	 this.headLine.insertRow(0);
	 this.headLine.rows[0].insertCell(0);
	 this.headLine.rows[0].insertCell(0);
	 this.headLine.rows[0].cells[0].width=10;
	 var anchor=document.createElement("a");
	 anchor.href="javascript:;";
	 anchor.appendChild(this.buttonImg1);
	 anchor.appendChild(this.buttonImg2);

	 this.headLine.rows[0].cells[0].appendChild(anchor);
	 this.headLine.rows[0].cells[1].appendChild(document.createTextNode(this.title));
	 this.dropBoxRef.insertBefore(this.headLine,this.dropBoxRef.firstChild);
	 //initially open or close...
		//support for persistent state.
	this.state=new StateObject(this.getUID());
	this.state.isOpen=(this.innerdiv.style.display=="none"?0:1);
	//now loading cookie-info...
	this.state.load();

	//go...
	this.setState();
}
//Opens and closes repeatedly.
DropBox.prototype.swapState=function() {
	if(this.state.isOpen) {
		this.closeBox();
	}
	else {
		this.openBox();
	}
}

//Sets the state of the dropbox depending on whether this.state.isOpen is true.
DropBox.prototype.setState=function() {
	 if (this.state.isOpen==0) {
		  this.closeBox();
	 }
	 else {
		  this.openBox();
	 }

}
DropBox.prototype.getUID=function() {
	if (this.dropBoxRef.getAttribute("id")) {
		 return this.dropBoxRef.getAttribute("id");
	}	
	else if(this.title) {
		 return escape(this.title);
	}
	else {
		 return "xxxxxxxxx";
	}
}

//Used to store local variables globally.
var GLOBAL_FOR_VARIOUS_PURPOSES;
DropBox.prototype.openBox=function() {
	 this.state.isOpen=1;
	 this.state.save();	 
	 this.buttonImg2.style.display="none";
	 this.buttonImg1.style.display="block";
	 this.innerdiv.style.display='block';
	 //alert(this.dropBoxRef.outerHTML);
	 return this;
	 
} 

DropBox.prototype.closeBox=function() {
	 this.state.isOpen=0;
	 this.state.save()
	 this.innerdiv.style.display="none";
	 this.buttonImg2.style.display="block";
	 this.buttonImg1.style.display="none";
}
var pageDropBoxes=new Object();
function detectDropboxes() {
	 var elements=document.getElementsByTagName("div");
	 var i; var n=elements.length;
	 for (i=0;i<n;i++) {
		  if (elements[i].getAttribute("dropbox")==1 && elements[i].getAttribute("didDetect")!=1) {
				var dropBox=new DropBox(elements[i]);
				if (elements[i].getAttribute("id")!="") {
					pageDropBoxes[elements[i].getAttribute("id")]=dropBox;
				}

				elements[i].setAttribute("didDetect",1);
		  }
	 }
}
