/**
DivSelect - Substitute for basic form select
v 1.1 / 28032008/JH, JJ-Net Group Oy (juha-heikki.lehtonen@jj-net.fi)

divselect.pickoptionhandler[selectid] (selectid being $id given to PHP's DivSelect::output)
set to any function to be called when option is picked (not if same option is selected continuously),
first attribute is value of picked option, second selectid

28th March/JH: fixed problem with menu height, when opening 2nd times, offsetHeight returned set height instead of full (added style.height="auto" before offsetHeight)

*/

function DivSelectControl(selectid) {
	this.hidelisttimer = null;
	this.current = null;
	this.pickoptionhandler = new Array();
	this.pickoptionhandler_post = new Array(); // after all selection related is done, these functions are executed
	this.getlistelement_i = 0;
	this.listops = 8;
	this.getlistelement = function(selectid) {
		return document.getElementById(selectid+"_list");
	}
	this.getvalueelement = function(selectid) {
		return document.getElementById(selectid);
	}
	this.gettitleelement = function(selectid) {
		return document.getElementById(selectid+"_title");
	}
	this.getlowestli = function(elem) {
		var lis = elem.getElementsByTagName("li");
		if(lis.length==0) {
			return elem;
		}
		return this.getlowestli(lis[0]);
	}
	this.getfirstoption = function(selectid) {
		var listelement = this.getlistelement(selectid);
		var lis = listelement.getElementsByTagName("li");
		return lis[0];
	}
	this.getoptioncount = function(selectid) {
		var listelement = this.getlistelement(selectid);
		var lis = listelement.getElementsByTagName("li");
		return lis.length;
	}
	this.togglelist = function (selectid) {
		var titleelement = this.gettitleelement(selectid);
		if(titleelement.getAttribute("_DISABLED")==1) {
			return;
		}
		var listelement = this.getlistelement(selectid);
	      	if(listelement.style.display!="none") {
	      		this.hidelist(listelement);
	      		return true;
	      	}
	      	this.hidelist();
		var buttonc = objcoords(titleelement);
		listelement.style.display = "block";
		listelement.style.height = "auto"; // must be set, so we get correct _full_ height with objcoords!
		var menuc = objcoords(listelement);
		var titleelement_x = buttonc.x;
		var titleelement_y = (buttonc.y+buttonc.h-1);
      		var bh = window.innerHeight ? window.innerHeight : document.body.scrollHeight;
      		/*if(titleelement_y+menuc.h>bh && (buttonc.y-menuc.h)>0) {
			titleelement_y = buttonc.y-menuc.h;
		}*/
		var firstop = this.getfirstoption(selectid);
		var firstop = this.getlowestli(firstop);
		var opheight = firstop.offsetHeight;
		var totalops = this.getoptioncount(selectid);
		if(titleelement_y+menuc.h>bh) {
			var listh;
			// count height according to options
			if(firstop && opheight>0) {
				var ops = Math.floor((bh-titleelement_y-4)/opheight);
				if(ops>totalops) {
					ops = totalops;
				}
				listh = ops*opheight;
			}
			else {
				listh = (bh-titleelement_y-4)*10;
			}
		}
		else if(totalops>this.listops) {
			var listh = opheight*this.listops;
		}
		if(!isNaN(listh)) {
			listelement.style.height = listh+"px";
		}
		listelement.style.left = titleelement_x+"px";
		listelement.style.top = titleelement_y+"px";
		listelement.style.zIndex = 999;
		var listw = (buttonc.w-2);
		listelement.style.width = listw+"px";
		addevent(listelement,"mousemove",divselect.hidelisttimer_clear);
		addevent(listelement,"mouseout",divselect.hidelisttimer_set);
		addevent(titleelement,"mousemove",divselect.hidelisttimer_clear);
		addevent(titleelement,"mouseout",divselect.hidelisttimer_set);
		this.current = listelement;
	}
	this.hidelisttimer_clear = function () {
		if(window.divselect_hidelisttimer) {
			clearTimeout(divselect_hidelisttimer);
		}
		return true;
	}
	this.hidelist = function (listelement) {
		divselect.hidelisttimer_clear();
		f_listelement = listelement || this.current;
		if(!f_listelement) {
			return false;
		}
		f_listelement.style.display = "none";
		return true;
	}
	this.hidelisttimer_set = function () {
		divselect.hidelisttimer_clear();
		divselect_hidelisttimer = setTimeout("divselect.hidelist()",1000);
	}
	this.pickoption = function(selectid,which,val,optdisabled) {
		// no need to change value of value already is selected
		var valueelement = this.getvalueelement(selectid);
		if(valueelement.value==val) {
			this.hidelist();
			return null;
		}
		if(optdisabled) {
			this.hidelist();
			return false;
		}
		// check first whether pickoption is to be confirmed with external function
		if(typeof(this.pickoptionhandler[selectid])=="function") {
			var func = this.pickoptionhandler[selectid];
			if(!func(val,selectid)) {
				this.hidelist();
				return false;
			}
		}
		var listelement = this.getlistelement(selectid);
		var titleelement = this.gettitleelement(selectid);
		var lis = listelement.getElementsByTagName("li");
		for(var i=0;i<lis.length;i++) {
			if(lis[i].firstChild && lis[i].firstChild.className=="divselect_selectedoption") {
				lis[i].firstChild.className = "";
			}
		}
		var valueelement = this.getvalueelement(selectid);
		valueelement.value = val;
		if(typeof(this.pickoptionhandler_post[selectid])=="function") {
			var func = this.pickoptionhandler_post[selectid];
			func(val,selectid);
		}
		titleelement.innerHTML = which.innerHTML;
		which.className = "divselect_selectedoption";
		this.hidelist();
	}
	this.select = function(selectid,optvalue) {
		var listelement = this.getlistelement(selectid);
		var lis = listelement.getElementsByTagName("li");
		for(var i=0;i<lis.length;i++) {
			if(lis[i].getAttribute("_OPTVALUE")==optvalue) {
				if(lis[i].getAttribute("_OPTDISABLED")=='1') {
					return false;
				}
				this.pickoption(selectid,lis[i].firstChild,optvalue,false);
				return true;
			}
		}
	}
	this.selected = function(selectid) {
		var valueelement = this.getvalueelement(selectid);
		return valueelement.value;
	}
	this.disable = function(selectid) {
		var titleelement = this.gettitleelement(selectid);
		titleelement.setAttribute("_DISABLED","1");
		titleelement.className = "divselect_title divselect_title_disabled";
		return true;
	}
	this.enable = function(selectid) {
		var titleelement = this.gettitleelement(selectid);
		titleelement.setAttribute("_DISABLED","0");
		titleelement.className = "divselect_title";
		return true;
	}
	this.enabled = function(selectid) {
		var titleelement = this.gettitleelement(selectid);
		var d = titleelement.getAttribute("_DISABLED");
		if(d=="1") {
			return false;
		}
		return true;
	}
}

divselect = new DivSelectControl();


function addevent(elem,ev,handler) {
	if(!elem) {
		return false;
	}
	if(elem.attachEvent) {
		return elem.attachEvent("on"+ev,handler);
	}
	else {
		return elem.addEventListener(ev,handler,false);
	}
}

function objcoords(which) {
	if(!which) {
		return false;
	}
	var fleft = 0;
	var ftop  = 0;
	var fwidth = which.offsetWidth;
	var fheight = which.offsetHeight;

	while (which.offsetParent){
		fleft += which.offsetLeft;
		ftop  += which.offsetTop;
		which     = which.offsetParent;
	}

	fleft += which.offsetLeft;
	ftop  += which.offsetTop;

	return { x : fleft, y : ftop, w : fwidth, h : fheight };
}
