/**
* functions to treat expanding tree-menu
*/
var menuStatuses = new Array();

function switchList(id) {
	var ico = document.images["order" + id];
	var sub = document.getElementById("subList" + id);
	
	if(menuStatuses.length > 0) {
		var isNew = true;
		for(var i=0; i < menuStatuses.length; i++) {
			var item = menuStatuses[i];
			if(item[0] == id) {
				isNew = false;
				if(item[1]) {
					switchItem(ico, sub, false);
				} else {
					switchItem(ico, sub, true);
				}
				item[1] = !item[1];
			}
		}
		if(isNew) {
			addItem(id);
			switchItem(ico, sub, true);
		}
	} else {
		switchItem(ico, sub, true);
		addItem(id);
	}
}

function switchItem(ico, sub, status) {
	ico.src = status ? (prefix + "/img/list_expanded.gif") : (prefix + "/img/list_collapsed.gif");
	sub.style.display = status ? "block" : "none";
}

function addItem(id) {
	menuStatuses.push(new Array(id, true));
}

/**
* functions to manage input fields
*/
function startInput(input, initValue) {
	if(input != null) {
		input.className = "";
		if(input.value == initValue) input.value = "";
	}
}

function finishInput(input, initValue) {
	if(input != null) {
		if((input.value == initValue) || (input.value == "")) {
			input.className = "empty";
			input.value = initValue;
		}
	}
}

/**
* opens new window with given params:
* - url - url to be displayed
* - title - title of opened window
* - w - window width in pixels
* - h - height
*/
var attrs = "scrollbars=yes,resizable=yes,status=no,toolbar=no,location=no,directories=no,menubar=no";

function popup(url, title, w, h) {
	attrsPlus = attrs;
	var left = 0;
	var top = 0;
	var resX = screenX();
	var resY = screenY();

	if(!isNaN(w)) {
		attrsPlus += ",width=" + w;
		if(!isNaN(resX)) left = Math.round((resX - w)/2);
	}
	attrsPlus += ",left=" + left;
	if(!isNaN(h)) {
		attrsPlus += ",height=" + h;
		if(!isNaN(resY)) top = Math.round((resY - h)/2);
	}
	attrsPlus += ",top=" + top;
	pop = window.open(url, "_blank", attrsPlus);
//	pop.document.title = title;
	pop.focus();
}

/**
* defines user screen width in pixels
*/
function screenX() {
	var result = 0;	// default screen resolution

	if(window.screen) {
		result = window.screen.width;
	}

	return(result);
}

/**
* defines user screen height in pixels
*/
function screenY() {
	var result = 0;	// default screen resolution

	if(window.screen) {
		result = window.screen.height;
	}

	return(result);
}

/**
* listener for login input
*/
function logListener(e) {
	if((window.event)&&(window.event.keyCode == 13)) {
		document.loginform.password.focus();
	} else if((e != null)&&(e.which == 13)) {
		document.loginform.password.focus();
	}
}

/**
* listener for password	input
*/
function passListener(e) {
	if((window.event)&&(window.event.keyCode == 13)) {
		document.loginform.submit();
	} else if((e != null)&&(e.which == 13)) {
		document.loginform.submit();
	}
}

/**
* register forms management
*/
function show(id) {
	if(document.getElementById) {
		x = document.getElementById(id);
		x.style.display = "block";
	}
}

function hide(id) {
	if(document.getElementById) {
		x = document.getElementById(id);
		x.style.display = "none";
		document.getElementById("head").value = "";
		document.getElementById("directphone").value = "";
	}
}

/**
* listener for search input
*/
function searchListener(e) {
	if((window.event)&&(window.event.keyCode == 13)) {
		document.searchform.submit();
	} else if((e != null)&&(e.which == 13)) {
		document.searchform.submit();
	}
}


var menuWaitTimer = {};
var menuOpenedCount = 0;

// ******************
// Функция формирования HTML-кода меню и вывода на страницу
// ******************
function drawJSMenu(containerId, hash, path) {
	// ----------- Константы ------------
	var commonClassName = "common";
	var parentClassName = "parent";
	var levelClassPrefix = "level_";
	var menuWaitInterval = 15000; // ms
	// ----------- Переменные ------------
	var container = document.getElementById(containerId);
	var html = _class = mouseOverOut = id = "";
	var i;
	var idArray = []
	// ----------- Функции ------------
	// Рекурсивная функция для прохождения по многоуровневому 
	// хэшу и формированию HTML-кода меню
	var cicleFunc = function(code, hash, level, parentId) {
		if (typeof level != "undefined" && level != 1) code += "<span id='b" + parentId + "' style='display:none;'>\n";
		for (i in hash) {
			var randId = parseInt(Math.random() * 1e10).toString() + parseInt(Math.random() * 1e10);
			_class = 
				((typeof hash[i][1] != "undefined") ? parentClassName : commonClassName) + 
				" " + levelClassPrefix + level;
			if (typeof hash[i][1] != "undefined") {
				id = " id='a" + randId + "'";
				idArray.push(randId);
			} else id = 0;
			code += "<a" + (id ? id : "") + " class='" + _class + "' href='" + path + i + "'>" + hash[i][0] + "</a>\n";
			if (typeof hash[i][1] != "undefined") code += cicleFunc(html, hash[i][1], level+1, randId);
		}
		if (typeof level != "undefined" && level != 1) code += "</span>\n";
		return code;
	}
	// ----------- Добавление HTML-кода меню на страницу ------------
	html = cicleFunc(html, hash, 1);
	container.innerHTML = html;
	// ----------- Добавление обработчиков событий ------------
	for (i = 0; i < idArray.length; i++) {
		document.getElementById("a" + idArray[i]).onmouseover = function() {
			menuOpenedCount++;
			var absId = this.id.substring(1, this.id.length);
			document.getElementById("b" + absId).style.display = "";
		}
		document.getElementById("a" + idArray[i]).onmouseout = function() {
			menuOpenedCount--;
			var absId = this.id.substring(1, this.id.length);
			if (typeof menuWaitTimer[absId] == "undefined" || menuWaitTimer[absId] == null) {
				menuWaitTimer[absId] = setInterval("hideJSMenu('" + absId + "')", menuWaitInterval);
			}
		}
		document.getElementById("b" + idArray[i]).onmouseover = function() {
			menuOpenedCount++;
			var absId = this.id.substring(1, this.id.length);
			if (typeof menuWaitTimer[absId] != "undefined") {
				clearInterval(menuWaitTimer[absId]);
				menuWaitTimer[absId] = null;
			}
		}
		document.getElementById("b" + idArray[i]).onmouseout = function() {
			menuOpenedCount--;
			var absId = this.id.substring(1, this.id.length);
			if (typeof menuWaitTimer[absId] == "undefined" || menuWaitTimer[absId] == null) {
				menuWaitTimer[absId] = setInterval("hideJSMenu('" + absId + "')", menuWaitInterval);
			}
		}
	}
}

// ******************
// Функция для убирания выпадушек, запускается по таймауту
// ******************
function hideJSMenu(id) {
	if (menuOpenedCount <= 0) {
		menuOpenedCount = 0;
		if (typeof menuWaitTimer[id] != "undefined") {
			clearInterval(menuWaitTimer[id]);
			menuWaitTimer[id] = null;
		}
		if (document.getElementById("b" + id)) {
			document.getElementById("b" + id).style.display = "none";
		}
	}
}
var menuArray = {
	'services' : ["Услуги VisaSPb", {
		'services' : ["Визы"],
		'insurance' : ["Мед. страхование"],
		'greenkarta' : ["Гринкарты"]
	}]
}
var menuArray2 = {
	'consuls/1' : ["Информация", {
		'zagran' : ["Загранпаспорта"],
		'consuls/1' : ["Консульства Спб"],
		'consuls/2' : ["Посольства Москвы"],
		'diplomats' : ["Представительства РФ"],
		'faq' : ["FAQ"]
	}]
}

$(document).ready(function(){
	
	$(".accordion2 p").hide();
	//$("#idcoment").hide();

	$(".accordion2 h3").click(function(){
		//alert ("my id"+ this.id+"coment");
		var idcoment=+this.id+"coment";
		//$("#"+idcoment).hide();
		//$(this).next(".accordion2 p").slideToggle("slow").siblings(".accordion2 p:visible").slideUp("slow");
		$("#"+idcoment).slideToggle("slow").siblings("#"+idcoment+":visible").slideUp("slow");
		$(this).toggleClass("active");
		$(this).siblings("h3").removeClass("active");
	});
});
$(document).ready(function(){
    $('.navlinks ul li').hover(
        function() {
            $(this).addClass("active");
            $(this).find('ul').stop(true, true); //    
            $(this).find('ul').slideDown();
        },
        function() {
            $(this).removeClass("active");
            $(this).find('ul').slideUp('fast');
        }
    );
});
