//global variables that can be used by all the functions on this page.
var selects;
var inputs;
var radios = new Array();
var checkboxes = new Array();
var hovers = new Array();
var buttons = new Array();
var selectText = "Bitte wählen";

//this function runs when the page is loaded so put all your other onload stuff in here too.
function init() {
	//check if styles are enabled and only then start replacing elements
	if(document.getElementById('stylesheetTest')) {
		if(findPosX(document.getElementById('stylesheetTest')) == -999) {
			replaceSelects();
		}
	}
	replaceBanner();
}



function replaceSelects() {
	//get all the select fields on the page
    selects = document.getElementsByTagName('select');
	
	//cycle trough the select fields
    for(var i=0; i < selects.length; i++) {
		
		//create and build div structure
		var selectArea = document.createElement('div');
		var left = document.createElement('div');
		var right = document.createElement('div');
		var center = document.createElement('div');
		var centertext = document.createElement('div');
		var button = document.createElement('a');
		var optgroup = document.createElement('strong');
		var text = document.createTextNode(selectText);
		if (selects[i].options[0].text) text = document.createTextNode(selects[i].options[0].text);
		
		center.id = "mySelectText"+i;
		button.href="javascript:showOptions("+i+")";
		selectArea.className = "selectArea";
		left.className = "sleft";
		right.className = "sright";
		center.className = "scenter";
		centertext.className = "scentertext";
		right.appendChild(button);
		center.appendChild(centertext);
		centertext.appendChild(text);
		selectArea.appendChild(left);
		selectArea.appendChild(right);
		selectArea.appendChild(center);
		
		//hide the select field
        selects[i].style.display='none'; 
		
		//insert select div
		selects[i].parentNode.insertBefore(selectArea, selects[i]);
		
		//build & place options div
		var optionsDiv = document.createElement('div');
		optionsDiv.className = "optionsDivInvisible";
		optionsDiv.id = "optionsDiv"+i;
		optionsDiv.style.left = findPosX(selectArea) + 'px';
		optionsDiv.style.top = findPosY(selectArea) + 21 + 'px';
		
		if (document.getElementById("redirect")) optionsDiv.style.top = findPosY(selectArea) - 305 + 'px';
		if (document.getElementById("branchen")) optionsDiv.style.top = findPosY(selectArea) - 305 + 'px';
		
		
		//get select's options and add to options div		
		var optlink=1;
		for(var j=1; j < selects[i].childNodes.length; j++) {
		
		//get a optiongroup
				if(selects[i].childNodes[j].tagName == "OPTGROUP") {
					var optionHolder = document.createElement('p');
					var optionGrouper = document.createElement('strong');
					var optionTxt = document.createTextNode(selects[i].childNodes[j].label);
					optionGrouper.appendChild(optionTxt);
					optionHolder.appendChild(optionGrouper);
					optionsDiv.appendChild(optionHolder);	

					
		//get childs of optiongroup
					for(var x=0; x < selects[i].childNodes[j].childNodes.length; x++) {
					    
						var optionHolder = document.createElement('p');
						var optionLink = document.createElement('a');
						var optionTxt = document.createTextNode(selects[i].childNodes[j].childNodes[x].text);
						optionLink.href = "javascript:showOptions("+i+"); selectMe('"+selects[i].id+"',"+optlink+","+i+");";
						optionLink.appendChild(optionTxt);
						optionHolder.appendChild(optionLink);
						optionsDiv.appendChild(optionHolder);
						optlink++;					
					}		
							
		//get select's options
				} else {	
		
					var optionHolder = document.createElement('p');
					var optionLink = document.createElement('a');
					var optionTxt = document.createTextNode(selects[i].childNodes[j].text);
					optionLink.href = "javascript:showOptions("+i+"); selectMe('"+selects[i].id+"',"+j+","+i+");";
					optionLink.appendChild(optionTxt);
					optionHolder.appendChild(optionLink);
					optionsDiv.appendChild(optionHolder);
				}
		}
		
		//insert options div
		document.getElementsByTagName("body")[0].appendChild(optionsDiv);
	}
}

function showOptions(g) {
		elem = document.getElementById("optionsDiv"+g);
		if(elem.className=="optionsDivInvisible") {elem.className = "optionsDivVisible";}
		else if(elem.className=="optionsDivVisible") {elem.className = "optionsDivInvisible";}
}

function selectMe(selectFieldId,linkNo,selectNo) {
	//feed selected option to the actual select field
	if (selectFieldId=='leistungen') {
		linkNo-=1;
	}
	selectField = document.getElementById(selectFieldId);
	for(var k = 0; k < selectField.options.length; k++) {
		if(k==linkNo) {
			selectField.options[k].selected = "selected";
		}
		else {
			selectField.options[k].selected = "";
		}
	}
	//show selected option
	textVar = document.getElementById("mySelectText"+selectNo);
	var newText = document.createTextNode(selectField.options[linkNo].text);
	textVar.replaceChild(newText, textVar.childNodes[0]);
	if(selectField.name=='branchen')  {
	  	document.branchenform.submit();
	} else {
		document.optionform.submit(); 
	}
}

function findPosY(obj) {
	var posTop = 0;
	while (obj.offsetParent) {
		posTop += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return posTop;
}
function findPosX(obj) {
	var posLeft = 0;
	while (obj.offsetParent) {
		posLeft += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return posLeft;
}


var current="0", s=5000, currentteaser="";

function replaceBanner()
{
	
	if (document.getElementById("teaser1")) {
	
	currentteaser = document.getElementById("teaser" + current);
	if(currentteaser) currentteaser.style.visibility="hidden";
	
	if (current == 3) current=0;
	current++;
	
	currentteaser = document.getElementById("teaser" + current);
	if(currentteaser) currentteaser.style.visibility="visible";
	
	
	window.setTimeout("replaceBanner()", s);
	}

}


window.onload = init;