﻿var selects;
var selectText = "...";

function changeLang(langId) {
	var need = String(location).indexOf("id=")+3;
	var need2 = String(location).indexOf("id=")+4;

	var pid = String(location).charAt(need);
	var pid2 = String(location).charAt(need2);

	final_pid = pid + pid2;

//	if(langId == 1 || langId == 2) {
//		langId += 1;
//	} else if(langId == 3) {
//		langId -= 2;
//	}

	if (isNaN(pid)) {
  	window.location.href = './index.php?id=4&L='+langId;
  } else {
  	window.location.href = './index.php?id='+final_pid+'&L='+langId;
  }

}
function changeLang2(id) {
	var need = String(location).indexOf("L=")+2;
	var lang = String(location).charAt(need);

	if(id == 0) {
		var id = 4;
	} else if(id == 1) {
		var id = 7;
	} else if(id == 2) {
		var id = 17;
	} else if(id == 3) {
		var id = 19;
	} else if(id == 4) {
		var id = 15;
	} else if(id == 5) {
		var id = 18;
	}

	if (isNaN(lang)) {
  	 window.location.href = './index.php?id='+id+'&L=1';
  } else {
  	window.location.href = './index.php?id='+id+'&L='+lang;
  }
}

function init() { replaceSelects(); }

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++) {
		if (selects[i].className != 'donotusejs') {
		//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 button = document.createElement('a');
		//var text = document.createTextNode(selectText);
		if (selects[i].id) {
			var text = document.createTextNode(selects[i].id);
		}else{
			selects[i].id = i;
			var text = document.createTextNode(selectText);
		}
		if (selects[i].style.width) {
			selectArea.style.width = selects[i].style.width;
		}

		center.id = "mySelectText"+i;
		button.href="javascript:showOptions("+i+")";
		selectArea.className = "selectArea";
		left.className = "left";
		right.className = "right";
		center.className = "center";
		right.appendChild(button);
		center.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');
		if (selects[i].style.width) {
			x = selects[i].style.width;
			//x.length = x.length - 2;
			x = x.substr(0, x.length-2)
			x = x - 29;
			optionsDiv.style.width = x+"px";
			center.style.width = x+"px";
		}
		optionsDiv.className = "optionsDivInvisible";
		optionsDiv.id = "optionsDiv"+i;
		optionsDiv.style.left = findPosX(selectArea) + 'px';
		optionsDiv.style.top = findPosY(selectArea) + 19 + 'px';
		//get select's options and add to options div
		for(var j=0; j < selects[i].options.length; j++) {
			var lang_key = selects[i].options[j].value;
			var optionHolder = document.createElement('p');
			var optionLink = document.createElement('a');
			var optionTxt = document.createTextNode(selects[i].options[j].text);
			if (selects[i].className == 'redirect') {
				optionLink.href = "javascript:showOptions("+i+"); selectMe('"+selects[i].id+"',"+j+","+i+"); changeLang2("+lang_key+")";
			} else {
				optionLink.href = "javascript:showOptions("+i+"); selectMe('"+selects[i].id+"',"+j+","+i+"); changeLang("+lang_key+")";
			}
			optionLink.appendChild(optionTxt);
			optionHolder.appendChild(optionLink);
			optionsDiv.appendChild(optionHolder);
		}

		//insert options div
		document.getElementsByTagName("body")[0].appendChild(optionsDiv);

		//check if preselected
		for(var j=0; j < selects[i].options.length; j++) {
			if (selects[i].options[j].selected) {
				selectMe(selects[i].id,j,i);
			}
			//alert(selects[i].options[j].text);
		}
	}
}
}

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
	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]);
}

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;
}

window.onload = init;