/*Fires when you hover / unhover menu item*/
function m_submenu_show(el){
	if(!el)
		return;
	var uls = el.getElementsByTagName('UL');
	if(uls){
		for(var i = 0; i < uls.length; i++){
			if(uls[i].className.indexOf('submenu') != -1){
				uls[i].style.display = 'block';
				return;
			}
		}
	}
}
function m_submenu_hide(el){
	if(!el)
		return;
	var uls = el.getElementsByTagName('UL');
	if(uls){
		for(var i = 0; i < uls.length; i++){
			if(uls[i].className.indexOf('submenu') != -1){
				uls[i].style.display = 'none';
				return;
			}
		}
	}
}
/*Fires when you hover / unhover flyout submenu link*/
function submenu_hover(el){
	if(el) el.style.backgroundColor = '#e5e8ef';
}
function submenu_unhover(el){
	if(el) el.style.backgroundColor = '#fff';
}
/*Swaps menu images on hover (white -> green -> white). 
Not sure if this is needed, cause it looks quite ugly*/
function menu_hover(el){
	var clsname = el.id;
	el = get_first_child(el);
	if(!el)
		return;
	var l = clsname.length + 3;
	var pos;
	if(el && (pos = el.className.indexOf(clsname + '_na')) != -1){
		el.className = el.className.substring(0, pos) + ' ' + clsname + '_a ' + el.className.substring(pos + l);
	}
}
function menu_unhover(el){
	var clsname = el.id;
	el = get_first_child(el);
	if(!el)
		return;
	var l = clsname.length + 2;
	var pos;
	if(el && (pos = el.className.indexOf(clsname + '_a')) != -1){
		el.className = el.className.substring(0, pos) + ' ' + clsname + '_na ' + el.className.substring(pos + l);
	}
}
function get_first_child(el){
	if(!el)
		return;
	el = el.firstChild;
	while(el && el.nodeType == 3)
		el = el.nextSibling;
	return el;
}
/*Helper functions*/
function get_hf1(el){ return function(){menu_hover(el);m_submenu_show(el);};}
function get_hf2(el){ return function(){menu_unhover(el);m_submenu_hide(el);};}
function get_hf3(el){ return function(){submenu_hover(el);};}
function get_hf4(el){ return function(){submenu_unhover(el);};}
/*Use these 2 functions for active menu item - it won't blink then*/
function get_hf5(el){ return function(){m_submenu_show(el);};}
function get_hf6(el){ return function(){m_submenu_hide(el);};}
//Adds menu events listeners
function set_menu_behaviour(){
	var c = document.getElementById('menu');
	if(!c)
		return;
	var lis = c.getElementsByTagName('li');
	var l = lis.length, i = 0;
	for(; i < l; i++){
		if(lis[i].className.indexOf('menu_item') != -1){
			lis[i].onmouseover = get_hf1(lis[i]);
			lis[i].onmouseout = get_hf2(lis[i]);
		}
		if(lis[i].className.indexOf('submenu_item') != -1){
			lis[i].firstChild.onmouseover = get_hf3(lis[i]);
			lis[i].firstChild.onmouseout = get_hf4(lis[i]);
		}
	}
}
function set_active_menu_item(id){
	var el = document.getElementById(id);
	if(!el)
		return;
	el.onmouseover = get_hf5(el);
	el.onmouseout = get_hf6(el);
	if(el = get_first_child(el))
		el.className = el.className.replace(id + '_na', id + '_a');
}

function popupi_img(img_url, width, height){
	var pos_x = (screen.width - width) / 2;
	var pos_y = (screen.height - height) / 2;
	window.open('/view.php?f=' + img_url, '', 'width=' + width + ', height=' + height + ', scrollbars=0, menubar=0, status=1, toolbar=0, left=' + pos_x + ', screenX=' + pos_x + ', top=' + pos_y + ', screenY=' + pos_y);
	return false;
}
function popupi_page(url, width, height){
	var pos_x = (screen.width - width) / 2;
	var pos_y = (screen.height - height) / 2;
	window.open(url, '', 'width=' + width + ', height=' + height + ', scrollbars=1, menubar=0, status=1, toolbar=0, left=' + pos_x + ', screenX=' + pos_x + ', top=' + pos_y + ', screenY=' + pos_y);
	return false;
}

function equal_height(ids){
	var l = ids.length;
	var max_height = 0, i = 0, h = 0;
	var el, t;
	for(; i < l; i++){
		el = document.getElementById(ids[i]);
		ids[i] = el;
		if(!el)
			continue;
		if(el.offsetHeight && el.offsetHeight > max_height)
			max_height = el.offsetHeight;
	}
	for(i = 0; i < l; i++)
		if(ids[i]){
			h = max_height;
 			if(t = getStyle(ids[i], "paddingTop")) h -= parseInt(t);
			if(t = getStyle(ids[i], "paddingBottom")) h -= parseInt(t);
 			if(t = getStyle(ids[i], "borderTopWidth") && parseInt(t)) h -= parseInt(t);
			if(t = getStyle(ids[i], "borderBottomWidth") && parseInt(t)) h -= parseInt(t);
			ids[i].style.height = h + 'px';
		}
}
function getStyle(el, prop){
	if(el.currentStyle)
		return el.currentStyle[prop];
	else if(window.getComputedStyle)
		return document.defaultView.getComputedStyle(el, null)[prop];
	return false;
}
