var lsFrame;

LanguageSelector.prototype.WIN_IE50 = (navigator.userAgent.toLowerCase().indexOf("msie 5.0") > 0); 

function LanguageSelector () {
	this.clData = null;
	this.curC = null;
	this.curL = null;
	this.countrySelector = document.getElementById('countries');
	this.languageList = document.getElementById('languageList');
	this.normal = document.getElementById('normal');
	this.external = document.getElementById('external');
	this.externalimg = document.getElementById('externalimg');
	this.externallinkimg = document.getElementById('externallinkimg');
	this.externallink = document.getElementById('externallink');
	this.ls = document.getElementById('ls_content');
	this.ls.obj = this;
	this.country = document.getElementById('lscountry');
	this.lang = document.getElementById('lslanguages');
	this.countryBut = document.getElementById('countryBut');
	if (this.countryBut) this.countryBut.onclick = createContextFunction(this, "openCountry");
	this.langBut = document.getElementById('langBut');
	this.langBut.onclick = createContextFunction(this, "openLang");
	this.closing = null;
	this.ls.onmouseout = createContextFunction(this, "close");
	this.ls.onmouseover = function () {if (this.obj.closing) clearTimeout(this.obj.closing)};
	this.canBeClosed = true;
	
	this.countrySelector.ls = this;
	this.countrySelector.onfocus = function () {this.ls.canBeClosed = false;}
	this.countrySelector.onblur = function () {this.ls.canBeClosed = true;}
	this.countrySelector.onchange = createContextFunction(this, "updateLanguages", "updateLanguages");
}
LanguageSelector.prototype.openCountry = function () {
	if (this.WIN_IE50) document.location.href = 'splashpage.html';
	if (!this.clData) this.getData();
	this.country.style.visibility = 'visible';
	if (this.WIN_IE50) {
		this.country.style.display = 'block';
		this.lang.style.marginLeft = 'auto';
	}
	if (this.initialOption) {
		this.initialOption.selected = true;
		this.updateLanguages();
	}
	this.open();
}
LanguageSelector.prototype.openLang = function () {
	if (!this.clData) this.getData();
	this.country.style.visibility = 'hidden';
	if (this.WIN_IE50) {
		this.country.style.display = 'none';
		this.lang.style.marginLeft = '100px';
	}
	if (this.initialOption) {
		this.initialOption.selected = true;
		this.updateLanguages();
	}
	this.open();
}
LanguageSelector.prototype.open = function () {
	document.getElementById('ls_top').style.display = 'none';
	document.getElementById('ls_content').style.display = 'block';
	document.getElementById('ls_content').style.position = 'relative';
}
LanguageSelector.prototype.close = function (e) {
	if (e) var dest = e.relatedTarget; else var dest = window.event.toElement;
	if (!isChild(this, dest)) {
		if (this.canBeClosed) this.closing = setTimeout(createContextFunction(this, "closeIt"), 500);
	}
	return true;
}
LanguageSelector.prototype.closeIt = function () {
	document.getElementById('ls_top').style.display = 'block';
	document.getElementById('ls_content').style.display = 'none';
	clearInterval(this.closing);
}
LanguageSelector.prototype.updateLanguages = function () {
	var c, l;
	var newLi;
	this.clearLanguages();
	c = this.countrySelector.options[this.countrySelector.selectedIndex].value;
	if (!c) return;
	var entries = this.clData.getHTMLById(c).getElementsByTagName('li');
	for (var i = 0; i < entries.length; i++) {
		newLi = document.createElement('li');
		if (entries[i].innerHTML.indexOf('http:') != -1) {
			this.normal.style.display = 'none';
			this.externalimg.src = entries[i].className;
			this.externallinkimg.href = entries[i].innerHTML;
			this.externallink.href = entries[i].innerHTML;
			this.external.style.display = 'block';
		} else {
			this.external.style.display = 'none';
			this.normal.style.display = 'block';
			if (entries[i].id == 'selected')
				newLi.innerHTML = '<a href="javascript:changeLanguage(\'' + this.curC + '_' + this.curL + '\', \'' + c + '_' + entries[i].className + '\');" class="current">' + entries[i].innerHTML + '</a>'
			else 
				newLi.innerHTML = '<a href="javascript:changeLanguage(\'' + this.curC + '_' + this.curL + '\', \'' + c + '_' + entries[i].className + '\');">' + entries[i].innerHTML + '</a>';
			this.languageList.appendChild(newLi);
		}
	}
	this.countrySelector.blur();
}
LanguageSelector.prototype.clearLanguages = function () {
	var list;
	list = this.languageList;
	while (list.hasChildNodes()) {
		list.removeChild(list.firstChild);
	}
}
LanguageSelector.prototype.fill = function () {
	this.clData = lsFrame;
	var entries = this.clData.getHTMLByTag('li')
	for (var i = 0; i < entries.length; i++) {
		if (entries[i].className == 'selected') this.curC = entries[i].id;
	}
	var l = this.clData.getHTMLById('selected');
	this.curL = l.className;
	this.fillSelectbox();
	this.updateLanguages();
}
LanguageSelector.prototype.fillSelectbox = function () {
	var current;
	var newOption;
	var txt, value;
	var entries = this.clData.getHTMLByTag('li');
	for (var i = 0; i < entries.length; i++) {
		if (entries[i].parentNode.parentNode.id == 'langdata') {
			txt = entries[i].innerHTML.substr(0, entries[i].innerHTML.toUpperCase().indexOf('<UL>'));
			value = entries[i].id;
			newOption = document.createElement('option');
			newOption.innerHTML = txt;
			newOption.value = value;
			this.countrySelector.appendChild(newOption);
			if (entries[i].className == 'selected') {
				newOption.selected = true;
				this.initialOption = newOption;
			}
		}
	}
}
LanguageSelector.prototype.getData = function () {
	lsFrame = new RPCFrame(window);
	lsFrame.setLocation('languages.html', createContextFunction(this, "fill"));
}
function changeLanguage (cur, fut) {
	if (document.frmLs.remember.checked == true) {
		setCookie('countryLanguage', fut, 365);
	}
	document.location.href = document.location.href.replace('/' + cur + '/', '/' + fut + '/');
}
