/*

Specificky select, nastylovany podle naseho, nahrazuje klasicky select, ktery schovava a zobrazuje misto nej rucne vytvoreny

*/

var CustomSelect = {
	init: function() {
		//inicializujeme pro vsechny selecty ktere maji ve tride custom
		var span = Array(), textnode, option, active;
		var selects = getElementsByClassName(null,"select","custom");
		for(a in selects) {
			option = selects[a].getElementsByTagName("option");
			
			//vytahneme si nazev aktivniho prvku
			active = option[0].childNodes[0].nodeValue;
			textnode = document.createTextNode(active);
			for(b = 0; b < option.length; b++) {
				if(option[b].selected == true) {
					textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
				}
			}
			
			//vytvorime nahradni span, ktery se zobrazi namisto selectu
			span[a] = document.createElement("span");
			span[a].className = "customselect";
			span[a].id = "select" + selects[a].name;
			span[a].appendChild(textnode);
			span[a].title = textnode.nodeValue;
			selects[a].parentNode.insertBefore(span[a], selects[a]);
			
			//span[a].onclick = CustomSelect.select;
			AddEvent(span[a],"click",CustomSelect.select,!true);
			AddClass(selects[a],'custom-ok');
		}
	},
	select: function(ev) {
		var obj = SrcFromEvent(ev);
		while(obj.parentNode && !parseClassesContain(obj.className,'customselect')) obj = obj.parentNode;
		
		var divs = getElementsByClassName(null,"div","customselectbox");
		var isMy = false;
		for(var i in divs){
			document.body.removeChild(divs[i]);
			if(divs.id == ('div'+obj.id)) isMy = true;
		}
		if(isMy) return;
		
		var div = document.createElement("div");
		div.className = 'customselectbox';
		div.style.position = 'absolute';
		div.style.left = findPosX(obj)+'px';
		div.style.top = findPosY(obj)+'px';
		div.id = 'div'+obj.id;
		
		var divIn1,divIn2,divIn3;
		divIn1 = document.createElement("div"); divIn1.className = 'in1'; div.appendChild(divIn1);
		divIn2 = document.createElement("div"); divIn2.className = 'in2'; divIn1.appendChild(divIn2);
		divIn3 = document.createElement("div"); divIn3.className = 'in3'; divIn2.appendChild(divIn3);
		
		//select = obj.nextSibling;		
		var option = obj.nextSibling.getElementsByTagName("option");
		for(b = 0; b < option.length; b++) {
			var item = document.createElement("span");
			item.className = 'item';
			if(option[b].selected == true) item.className += ' active';
			item.innerHTML = option[b].childNodes[0].nodeValue
			item.title = option[b].value;
			item.onclick = CustomSelect.change;
			divIn3.appendChild(item);
		}
		
		document.body.appendChild(div);
		AddEvent(document.body,"click",CustomSelect.cancel,!true);
		StopEvent(ev);
	},
	change: function(ev){
		var obj = SrcFromEvent(ev);
		if(!obj) obj = this;
		
		//var div = obj.parentNode;
		var div = obj;		
		while(div.parentNode && !parseClassesContain(div.className,'customselectbox')) div = div.parentNode;
		
		var id = div.id.substring(3);
		var span = getId(id);
		if(!span) return;
		
		var select = span.nextSibling;
		select.value = obj.title;
		option = select.getElementsByTagName("option");
		for(d = 0; d < option.length; d++) {
			if(option[d].selected == true) {
				span.childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
				span.title = option[d].childNodes[0].nodeValue;
			}
		}
		document.body.removeChild(div);
		select.onchange();
		StopEvent(ev);
	},
	cancel: function(){
		var div = getElementByClassName(document.body,'DIV','customselectbox');
		if(div) document.body.removeChild(div);
	}
}

//na onload aktualizujeme
AddEvent(window,'load',CustomSelect.init);

