function clicked(ent) {
	if (!ent) var ent = window.event;
	var tg = (window.event) ? ent.srcElement : ent.target;
	if (tg.textObj != undefined) {
		window.location = tg.textObj.href;
	}
	
	ent.cancelBubble = true;
	if (ent.stopPropagation) 
		ent.stopPropagation();
}

function liMouseOver() {
	var li = this;
	
	// highlight
	AddClass(li, 'over');
	
	if (li.textObj != null) {		
		AddClass(li.textObj, 'over');
	}
	
	if (li.style.cursor) {
		li.style.cursor = "hand";
		li.style.cursor = "pointer";
	}
	
	// show submenu
	if (!li.submenu) {
		return;
	}
	
	// position submenu
	var xy = FindXYWH(li);
	
	if (li.level == 0) {
		// show down
		li.submenu.style.left = xy.x + 'px';
		li.submenu.style.top = xy.y + xy.h  + 'px'; 
	} else {
		var parnode = FindXYWH(li.parentNode);
		li.submenu.style.left = 176 + 'px';
		li.submenu.style.top = xy.y - parnode.y + 'px'; 
	}

	li.submenu.style.visibility = 'visible';
	li.submenu.style.display = 'block';
}

function liMouseOut() {
	var li = this;
	
	KillClass(li, 'over');
	
	if (li.textObj) {
		KillClass(li.textObj, 'over');
	}
	
	if (!li.submenu) {
		return;
	}
	
	li.submenu.style.visibility = 'hidden';
}

function initLiPanels(main, level) {
	var lis = main.getElementsByTagName('li');
	for (var i = 0 , len = lis.length; i < len; i++) {
		var li = lis[i];
		
		// do not use another lis
		if (li.parentNode != main) {
			continue;
		}
		
		if (HasClass(li, "expand")) {
			// submenu exists, initialize it
			var uls = li.getElementsByTagName('ul');
			li.submenu = uls[0];
			li.level = level;
			initLiPanels(uls[0] , level + 1);
		}
		
		// get children 'a' nodes to highlight
		var as = li.getElementsByTagName('a');
		if (as.length > 0 && as[0].parentNode == li) {
			if (level > 0 && HasClass(li, "expand")) {
				as[0].href = "#";
			} else {
				li.onclick = clicked;
			}
			li.textObj = as[0];
		} 
		
		li.onmouseover = liMouseOver;
		li.onmouseout = liMouseOut;
	}	
}

function menuInit() {
	if (!document.getElementById) {
		return;
	}
	
	var menu = document.getElementById('menu');

	if (!menu) {
		return;
	}
	
	initLiPanels(menu, 0);
}

function FindXY(obj){
	var x=0,y=0;
	while (obj) {
		if (obj.offsetLeft)
			x+=obj.offsetLeft - (obj.scrollLeft || 0);
		if (obj.offsetTop)
			y+=obj.offsetTop - (obj.scrollTop || 0);
		obj = obj.offsetParent;
	}
	return {x:x,y:y};
}

function FindXYWH(obj){
	if (!obj) return { x:0, y:0, w:0, h:0 };
	var objXY = FindXY(obj);
	return { x:objXY.x, y:objXY.y, w:obj.offsetWidth||0, h:obj.offsetHeight||0 };
}

function AttachEvent(obj,evt,fnc,useCapture){
	if (obj.addEventListener){
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	} else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
	else obj['on'+evt]=fnc;
	return true;
}

function HasClass(obj,cName){ return (!obj || !obj.className)?false:(new RegExp("\\b"+cName+"\\b")).test(obj.className) }
function AddClass(obj,cName){ if (!obj) return; if (obj.className==null) obj.className=''; return obj.className+=(obj.className.length>0?' ':'')+cName; }
function KillClass(obj,cName){ if (!obj) return; return obj.className=obj.className.replace(RegExp("^"+cName+"\\b\\s*|\\s*\\b"+cName+"\\b",'g'),''); }

////////////////////////////////////////////////////////////////////////

function openWindow(img, id) {
	window.open(img,id,'height=650,width=700,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no');
}

function imagePopup(src) {
	window.open(src, 'image', 'height=400,width=400,status=no,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=yes');
}


var showedAndNotHidden = true;

function hidePopup() {
	showedAndNotHidden = false;

	if (!document.getElementById) {
		return;
	}
	
	if (!Element.hasClassName($('popup'), 'visible')) {
		return;	
	}
	
	Position.clone($('popup'), $('popanim'))
	Element.show($('popanim'));
	
	Element.removeClassName($('popup'), 'visible');
	
	Position.prepare();
	pos = Position.cumulativeOffset($('exclam'));
	
	fromdim = $('popanim').getDimensions();
	todim = $('exclam').getDimensions();
	
	new Effect.Parallel(
    [ 
	  new Effect.Move('popanim', { x: pos[0], y: pos[1], mode: 'absolute'}), 
      new Effect.Scale('popanim', todim.height/fromdim.height*100, {scaleX: false}),
	  new Effect.Scale('popanim', todim.width/fromdim.width*100, {scaleY: false})
	],
    { duration: 0.5, 
      afterFinish: function(effect)
        { Element.hide($('popanim')); } 
    }
  	);
  }

	

function showPopup() {
	if (!document.getElementById) {
		return;
	}
	if (Element.hasClassName($('popup'), 'visible')) {
		return;	
	}
	
	Position.clone($('exclam'), $('popanim'))
	Element.show($('popanim'));
	
	Position.prepare();
	pos = Position.positionedOffset($('popup'));
	
	fromdim = $('popanim').getDimensions();
	todim = $('popup').getDimensions();
	
	new Effect.Parallel(
    [ 
	  new Effect.Move('popanim', { x: pos[0], y: pos[1], mode: 'absolute'}), 
      new Effect.Scale('popanim', todim.height/fromdim.height*100, {scaleX: false}),
	  new Effect.Scale('popanim', todim.width/fromdim.width*100, {scaleY: false})
	],
    { duration: 0.5, 
      afterFinish: function(effect)
        { Element.hide($('popanim'));
		  Element.addClassName($('popup'), 'visible'); } 
    }
  	);
}

////////////////////////////////////////////////////////////////////////

function pageInit() {
    if (arguments.callee.done) return;
    arguments.callee.done = true;
    
    menuDOMInit();
    menuInit();
    showMessage();
}


function inputEnter(element, val, leave) {
	if (leave == undefined) 
		leave = '';
	if (element.value == val) {
		element.value = leave;
	}
}

function inputLeave(element, val, leave) {
	if (leave == undefined) 
		leave = '';
	if (element.value == leave) {
		element.value = val;
	} else if (element.value == '') {
		element.value = val;
	}
}

if (typeof window.addEventListener != 'undefined') {
	document.addEventListener("DOMContentLoaded", pageInit, false);
} else if(typeof window.attachEvent != 'undefined') {
	pageInit();
}

window.onload = pageInit;

