/*Fires when you hover / unhover menu item*/
function m_submenu_show(el){
	if(!el)
		return;
	var uls = el.getElementsByTagName('UL');
	if(uls && uls[0])
		uls[0].style.display = 'block';
}
function m_submenu_hide(el){
	if(!el)
		return;
	var uls = el.getElementsByTagName('UL');
	if(uls && uls[0])
		uls[0].style.display = 'none';
}
function menu_hover(el){
	el.className = 'topmenu active';
}
function menu_unhover(el){
	el.className = 'topmenu';
}
function submenu_hover(li){
	if(li.nextSibling && li.nextSibling.tagName == 'LI')
		li.nextSibling.getElementsByTagName('A')[0].style.borderTop = '1px solid #fff';
	var ul = li.getElementsByTagName('UL');
	if(ul[0])
		ul[0].style.display = 'block';
}
function submenu_unhover(li){
	if(li.nextSibling && li.nextSibling.tagName == 'LI')
		li.nextSibling.getElementsByTagName('A')[0].style.borderTop = '1px solid #091a68';
	var ul = li.getElementsByTagName('UL');
	if(ul[0])
		ul[0].style.display = 'none';
}
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);};}

//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('topmenu') != -1){
			lis[i].onmouseover = get_hf1(lis[i]);
			lis[i].onmouseout = get_hf2(lis[i]);
		}
		if(lis[i].className.indexOf('submenu') != -1){
			lis[i].onmouseover = get_hf3(lis[i]);
			lis[i].onmouseout = get_hf4(lis[i]);
		}
	}
}
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;
}