// javascript functions

if (window.attachEvent) {
	window.attachEvent("onload", load_functions);
	}
else {
	window.onload = load_functions;
	}



function load_functions() {
	load_languages();
	load_top_menu();
	load_header_img();
	load_submenu();
	}

function load_header_img() {
	var img = "url(/images/template/002.jpg)";
	var images = {
			"index"		: "/images/template/001.jpg",
			"articles"	: "/images/template/002.jpg",
			"services"	: "/images/template/002.jpg",
			"join"		: "/images/template/002.jpg",
			"lists"		: "/images/template/004.jpg",
			"about_us"	: "/images/template/008.jpg",
			"contact"	: "/images/template/009.jpg"
			};

	for(x in images) {
		if(x == page_name) {
			img = "url(" + images[x] + ")";
			}
		}
	document.getElementById("header_img").style.backgroundImage = img;
	};

function isObject(x) { 
	return (typeof x == "object");
	}

function load_languages() {
	html = '';
	if(isObject(languages)) {
		for(i in languages) {
			html += '<a href="/' + i + '/' + page_filename + '"><img style="margin: 2px; border: none;" title="' + languages[i]['name'] + '" src="' + img_url + 'template/language_' + i + '.gif" alt="' + languages[i]['name'] + '" width="20" height="13"></a>';
			/*html += '<a href="/' + i + '/index.php"><img style="margin: 2px; border: none;" title="' + languages[i]['name'] + '" src="' + img_url + 'template/language_' + i + '.gif" alt="' + languages[i]['name'] + '" width="20" height="13"></a>';*/
			}
		document.getElementById("languages_container").innerHTML = html;
		}
	}

function load_top_menu() {
	menu_html = '<table width="100%">';
	construct_menu("top");
	menu_html += '</table>';
	document.getElementById("menu_container").innerHTML = menu_html;
	}





function construct_menu(lvl) {
	if(isObject(menu_items[lvl])) {
		menu_html += '<ul ' + (lvl=='top' ? 'id="nav"' : '') + '>';
		for(i in menu_items[lvl]) {
			menu_html += '<li>';
			if(lvl != "top") {
				menu_html += '';
				}
			menu_html += '<a href="' + base_url + menu_items[lvl][i].name + '.php" onmouseover="sfHover();">' + menu_items[lvl][i].menu_link + '</a>';
			if(isObject(menu_items[i])) {
				construct_menu(i);
				}
			menu_html += '</li>';
			}
		menu_html += '</ul>';
		}
	}



sfHover = function() {
	try {
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
				}
			sfEls[i].onmouseout=function() {
				//this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
				this.className=this.className.replace(new RegExp("sfhover"), "");
				}
			}
		}
	catch(e) {
		}
	}

function load_send_mail_window() {
	var result = "";
	id = getURLVar("id");
	var html = '' ;
	html += '<iframe ' ;
	html += 'src="http://www.ajakirjanikud.ee/saada.php?id=' + id + '" ' ;
	html += 'frameborder="0" style="width: 550px; height: 550px; border: none;"></iframe>' ;	
	document.getElementById('send_mail_window').innerHTML = html ;
}

function send_mail() {
	var result = "";
	id = getURLVar("id");
	if(id) {
		setTimeout('sendMessageHttpRequest("http://www.ajakirjanikud.ee/saada.php", "id=" + id)', 2000);
		}
	else {
		document.getElementById("sending_mail_result").innerHTML = "Invalid or missing ID!";
		}
	}

function getURLVar(urlVarName) {
	var urlHalves = String(document.location).split("?");
	var urlVarValue = "";
	if(urlHalves[1]) {
		var urlVars = urlHalves[1].split("&");
		for(i =0; i <= (urlVars.length); i++) {
			if(urlVars[i]) {
				var urlVarPair = urlVars[i].split('=');
				if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
					urlVarValue = urlVarPair[1];
					}
				}
			}
		}
	return urlVarValue;   
	}

function sendMessageHttpRequest(queryUrl, queryContent) {
	q = false;
	if(window.XMLHttpRequest) {
		try {
			q = new XMLHttpRequest();
			}
		catch(e) {
			q = false;
			}
		}
	else if(window.ActiveXObject) {
		try {
			q = new ActiveXObject("Msxml2.XMLHTTP");
			}
		catch(e) {
			try {
				q = new ActiveXObject("Microsoft.XMLHTTP");
				}
			catch(e) {
				q = false;
				}
			}
		}

	if(q) {
		try {
			q.onreadystatechange = getMessageHttpResult;
    			q.open("POST", queryUrl, true);
    			q.setRequestHeader("Content-length", queryContent.length);
    			q.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    			q.setRequestHeader("Connection", "close");
      			q.send(queryContent);
			}
		catch(e) {
			alert(e);
			}
		}
	}

function getMessageHttpResult() {
	if(q) {
		if(q.readyState == 4) {
			proccessHttpResult(q.status, q.statusText, q.responseText);
			}
		}
	}

function proccessHttpResult(statusCode, statusText, responseText) {
	if(statusCode == 200) {
		document.getElementById("sending_mail_result").innerHTML = responseText;
		}
	};

function load_submenu() {
	if(page_name == 'index') {
		return false;
		}
	var html = '';
	var top_link = '';
	html += '<table style="width: 100%; height: 26px;" border="0" cellspacing="0" cellpadding="0">';
	html += '<tbody>';
	html += '<tr>';
	html += '<td> </td>';
	for(x in menu_items) {
		if(top_link == '' && x == page_name) {
			top_link = x;
			}
		if(top_link == '') {
			for(y in menu_items[x]) {
				if(top_link == '' && menu_items[x][y]['name'] == page_name) {
					top_link = x;
					}
				}
			}
		}
	if(top_link == 'top') {
		top_link = '';
		}
	if(top_link != '') {
		html += '<td style="width: 50px; background: transparent url(/images/template/menu_bg' + (page_name == menu_items['top'][top_link]['name'] ? '_selected' : '') + '.png) no-repeat 2px -8px; padding: 0px 30px 0px 35px; white-space: nowrap;">';
		html += '<a href="' + base_url + menu_items['top'][top_link]['filename'] + '" style="color: #333333; font-weight: bold; text-decoration: none;">' + menu_items['top'][top_link]['menu_link'] + '</a>';
		html += '</td>';
		for(x in menu_items[top_link]) {
			html += '<td style="width: 50px; background: transparent url(/images/template/menu_bg' + (page_name == menu_items[top_link][x]['name'] ? '_selected' : '') + '.png) no-repeat 2px -8px; padding: 0px 30px 0px 35px; white-space: nowrap;">';
			html += '<a href="' + base_url + menu_items[top_link][x]['filename'] + '" style="color: #333333; font-weight: bold; text-decoration: none;">' + menu_items[top_link][x]['menu_link'] + '</a>';
			html += '</td>';
			}
		}
	html += '</tr>';
	html += '</tbody>';
	html += '</table>';



	if(page_name != 'index') {
		document.getElementById("submenu_links").innerHTML = html;
		}
	};
