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 = Position.positionedOffset(Element.extend(li));
	
	//xy.h += 2;
	
	if (li.level == 0) {
		// show down
		li.submenu.style.left = xy[0] + 'px';
		li.submenu.style.top = xy[1] + li.getHeight()  + '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.positionedOffset($('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.prepare();
	Position.clone($('exclam'), $('popanim'))
	Element.show($('popanim'));
	
	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;
    if ($('popanim')) Element.hide($('popanim'));
    if (typeof menuDOMInit == "function") menuDOMInit();
    if (typeof menuInit == "function") menuInit();
    if (typeof showMessage == "function") 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;

function strip_tags(str, allowed_tags) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Luke Godfrey
    // +      input by: Pul
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +      input by: Alex
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Marc Palau
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Eric Nagel
    // +      input by: Bobby Drake
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: strip_tags('<p>Kevin</p> <br /><b>van</b> <i>Zonneveld</i>', '<i><b>');
    // *     returns 1: 'Kevin <b>van</b> <i>Zonneveld</i>'
    // *     example 2: strip_tags('<p>Kevin <img src="someimage.png" onmouseover="someFunction()">van <i>Zonneveld</i></p>', '<p>');
    // *     returns 2: '<p>Kevin van Zonneveld</p>'
    // *     example 3: strip_tags("<a href='http://kevin.vanzonneveld.net'>Kevin van Zonneveld</a>", "<a>");
    // *     returns 3: '<a href='http://kevin.vanzonneveld.net'>Kevin van Zonneveld</a>'
    // *     example 4: strip_tags('1 < 5 5 > 1');
    // *     returns 4: '1 < 5 5 > 1'
 
    var key = '', allowed = false;
    var matches = [];
    var allowed_array = [];
    var allowed_tag = '';
    var i = 0;
    var k = '';
    var html = '';
 
    var replacer = function(search, replace, str) {
        return str.split(search).join(replace);
    };
 
    // Build allowes tags associative array
    if (allowed_tags) {
        allowed_array = allowed_tags.match(/([a-zA-Z]+)/gi);
    }
 
    str += '';
 
    // Match tags
    matches = str.match(/(<\/?[\S][^>]*>)/gi);
 
    // Go through all HTML tags
    for (key in matches) {
        if (isNaN(key)) {
            // IE7 Hack
            continue;
        }
 
        // Save HTML tag
        html = matches[key].toString();
 
        // Is tag not in allowed list? Remove from str!
        allowed = false;
 
        // Go through all allowed tags
        for (k in allowed_array) {
            // Init
            allowed_tag = allowed_array[k];
            i = -1;
 
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
            if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}
 
            // Determine
            if (i == 0) {
                allowed = true;
                break;
            }
        }
 
        if (!allowed) {
            str = replacer(html, "", str); // Custom replace. No regexing
        }
    }
 
    return str;
}

