
WeGluDropDown = function() {

	var divList;
	var timout;
	var currentDropdown;
	
	this.timout = 0;
	this.divList = new Array();
	this.currentDropdown = undefined;
}

WeGluDropDown.prototype =
{

createDropDown:function (buttonDiv, dropDownDiv, align, events) {

	if(typeof(buttonDiv) == "string")
	  buttonDiv = document.getElementById(buttonDiv);
	if(typeof(dropDownDiv) == "string")
	  dropDownDiv = document.getElementById(dropDownDiv);
	if(typeof(align) != "string")
	  align = "";
	  

	align = align.toLowerCase();
	buttonDiv.alignRight = (align.indexOf("right") >= 0);
	buttonDiv.alignBottom = (align.indexOf("bottom") >= 0);
	buttonDiv.alignCenter = (align.indexOf("center") >= 0);
	  
	var newDiv = document.createElement("DIV");
	newDiv.id = "dropdown_"+ dropDownDiv.id;
	var ind = 1;
	while(document.getElementById(newDiv.id)) {
	  newDiv.id = "dropdown_"+ dropDownDiv.id +"_"+ ind;
	  ++ind;
	}
	newDiv.className = dropDownDiv.className;
	var ss = dropDownDiv.innerHTML;
	ss = ss.replace (/id="?(\w+)"?/gi, "id=\"dropdown_$1\"");

	newDiv.innerHTML = ss;
	newDiv.style.display = "block";
	newDiv.style.overflow = "hidden";
	document.body.appendChild(newDiv);
	buttonDiv.keepHeight = newDiv.offsetHeight;
	newDiv.style.height = "0px";
	newDiv.style.position = "absolute";
	newDiv.targetObj = this;
	newDiv.parentButton = buttonDiv;
	buttonDiv.hiddingBlocked = false;
	
	this.divList[this.divList.length] = newDiv;
	
	buttonDiv.dropdown = newDiv;
	buttonDiv.targetObj = this;
	buttonDiv.showing = false;
	buttonDiv.dropDownVisible = false;
	//buttonDiv.onclick = this.buttonClick;
	eval("DHTMLSuite.commonObj.addEvent(buttonDiv, \"click\", this.buttonClick)");
	
	buttonDiv.isMouseInside = false;
	//buttonDiv.onmouseover = newDiv.onmouseover = this.dropDownMouseOver;
	eval("DHTMLSuite.commonObj.addEvent(buttonDiv, \"mouseover\", this.dropDownMouseOver)");
	eval("DHTMLSuite.commonObj.addEvent(newDiv, \"mouseover\", this.dropDownMouseOver)");
	
	//buttonDiv.onmouseout = newDiv.onmouseout = this.dropDownMouseOut;
	eval("DHTMLSuite.commonObj.addEvent(buttonDiv, \"mouseout\", this.dropDownMouseOut)");
	eval("DHTMLSuite.commonObj.addEvent(newDiv, \"mouseout\", this.dropDownMouseOut)");
	
	if(events)
	  buttonDiv.events = events;
	else
	  buttonDiv.events = {};
	  
	return newDiv;
},

cleanAll:function() {
	for(var n = 0; n < this.divList.length; n++) {
	  document.body.removeChild(this.divList[n]);
	}
	this.divList.splice(0, this.divList.length);
	  
	if(this.timout) {
	  clearTimeout(this.timout);
	  this.timout = 0;
	}
},

// Cleans elements with id starting by "idmask"
cleanFromIdMask:function(idmask) {
	n = 0;
	while(n < this.divList.length) {
	  if(this.divList[n].parentButton.id.substr(0, idmask.length) == idmask) {
	    document.body.removeChild(this.divList[n]);
		this.divList.splice(n, 1);
	  }
	  else
	    ++n;
	}
	  
	if(this.timout) {
	  clearTimeout(this.timout);
	  this.timout = 0;
	}
},

dropDownMouseOver:function (e) {
	if(document.all) e = event;

	var obj = this;
	if(obj.parentButton)
	  obj = obj.parentButton;

	obj.isMouseInside = true;
	if(obj.events.onMouseOver)
	  obj.events.onMouseOver(obj);
},

dropDownMouseOut:function (e) {
	if(document.all) e = event;
	
	var mx, my;
	mx = e.clientX;
	my = e.clientY;
	
	var obj = this;
	  
	/*********************************************************/
	// Check if the mouse is really outside the object
	var ox = DHTMLSuite.commonObj.getLeftPos(obj);
	var oy = DHTMLSuite.commonObj.getTopPos(obj);
    if(self.pageYOffset) {
	  ox -= self.pageXOffset;
	  oy -= self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop){
	  // Explorer 6 Strict
      ox -= document.documentElement.scrollLeft;
	  oy -= document.documentElement.scrollTop;
	}
	else if (document.body){
      // all other Explorers
  	  ox -= document.body.scrollLeft;
	  oy -= document.body.scrollTop;
    }
	
	if(mx > ox+8  &&  mx < ox + obj.offsetWidth-8 &&  my > oy+4  &&  my < oy + obj.offsetHeight-8)
	  return;
	  
	/****************************************************/

	if(obj.parentButton) {
	  obj = obj.parentButton;
	}
	obj.isMouseInside = false;

	if(obj.hiddingBlocked)
	  return;
	
	if(obj.dropdown.style.height == "0px") {
	  if(obj.events.onMouseOut)
	    obj.events.onMouseOut(obj);
	}
	else {
	  window.dropDownRef = obj.targetObj;
	  obj.targetObj.timout = setTimeout("window.dropDownRef.exitDelay('"+ obj.id +"')", 500);
	}
},

// Blocks the dropdown hidding until the next mouseout event
blockMouseHidding:function(button, block) {
	if(typeof(button) == "string")
	  button = document.getElementById(button);
	button.hiddingBlocked = block;
	if(!block  &&  !button.isMouseInside) {
	  window.dropDownRef = button.targetObj;
	  button.targetObj.timout = setTimeout("window.dropDownRef.exitDelay('"+ button.id +"')", 500);
	}
},

exitDelay:function(id) {
	this.timout = 0;
	var obj = document.getElementById(id);
	if(obj  &&  !obj.isMouseInside) {
	  this.doClickButton(obj, false);
	  if(obj.events  &&  obj.events.onMouseOut)
	    obj.events.onMouseOut(obj);
	}
},

buttonClick:function () {

	this.targetObj.doClickButton(this, !this.dropDownVisible);
},

doClickButton:function (button, visible) {

	if(typeof(button) == "string")
	  button = document.getElementById(button);
	  
	//var ss = "";
	//for(var e in button)
	//  if(typeof(button[e]) != "function")
	//    ss +=  e + "= "+ button[e] + " / ";
    //alert(ss);

	if(button.showing  ||  visible == button.dropDownVisible)
	  return;
	button.showing = true;
	button.dropDownVisible = visible;

	if(visible) {
	  if(this.currentDropdown)
	    this.dropDownHideHard(this.currentDropdown);
	  button.dropdown.style.height = "auto";
	  button.keepHeight = button.dropdown.offsetHeight;
	  button.dropdown.style.height = "0px";
	  var x = DHTMLSuite.commonObj.getLeftPos(button);
	  var y = DHTMLSuite.commonObj.getTopPosScrolled(button);
	  
	  if(button.alignRight)
	    x += button.offsetWidth - button.dropdown.offsetWidth;
	  else if(button.alignCenter)
	    x += Math.floor((button.offsetWidth - button.dropdown.offsetWidth) / 2);
	  if(button.alignBottom)
	    y += button.offsetHeight;
	  button.dropdown.style.left = x + 'px';
	  button.dropdown.style.top = y + 'px';
	  window.dropDownRef = this;
	  this.currentDropdown = button;
	  this.timout = setTimeout("window.dropDownRef.dropDownShow('"+ button.id +"')", 5);
	}
	else {
	  window.dropDownRef = this;
	  this.timout = setTimeout("window.dropDownRef.dropDownHide('"+ button.id +"')", 5);
	}
},

dropDownShow:function (sobj) {
	this.timout = 0;
	var obj = document.getElementById(sobj);
	if(!obj)
	  return;
	  
	if(!obj.showing)
	  return;
	  
	var dd = Math.floor(obj.dropdown.offsetHeight / 2);
	if(dd < 8)  dd = 8;
	var h = obj.dropdown.offsetHeight + dd;
	if(h > obj.keepHeight)
	  h = obj.keepHeight;
	obj.dropdown.style.height = h + "px";
	if(h == obj.keepHeight) {
	  obj.showing = false;
	  if(obj.events.onDropDown)
	    obj.events.onDropDown(obj);
	}
	else {
	  window.dropDownRef = this;
	  this.timout = setTimeout("window.dropDownRef.dropDownShow('"+ sobj +"')", 5);
	}
},

dropDownHide:function (sobj) {	

	this.timout = 0;
	var obj;
	if(typeof(sobj) == "string")
	  obj = document.getElementById(sobj);
	else
	  obj = sobj;
	if(!obj)
	  return;

	if(!obj.showing)
	  return;
	var dd = Math.floor(obj.dropdown.offsetHeight / 3);
	if(dd < 8)  dd = 8;
	var h = obj.dropdown.offsetHeight - dd;
	if(h < 0)
	  h = 0;
	obj.dropdown.style.height = h + "px";
	if(h == 0) {
	  obj.showing = false;
	  this.currentDropdown = undefined;
	  if(obj.events.onDropDown)
	    obj.events.onDropDown(obj);
	}
	else {
	  window.dropDownRef = this;
	  this.timout = setTimeout("window.dropDownRef.dropDownHide('"+ sobj +"')", 5);
	}
},

// Hides the dropdown without animation
dropDownHideHard:function (sobj) {
	var obj;
	if(typeof(sobj) == "string")
	  obj = document.getElementById(sobj);
	else
	  obj = sobj;
	if(!obj)
	  return;
	obj.dropDownVisible = false;
	this.currentDropdown = undefined;
	obj.dropdown.style.height = "0px";
	obj.showing = false;
}


}
