Array.prototype.find = function (element)
{
	for (var keys in this)
	{
		if (this[keys] == element)
		{
			return keys;
			break;
		}
	}
	return -1;
};
function IsNumeric(input)
{
   return (input - 0) == input && input.length > 0;
}

var lastClicks = new Array();

function IDtoNumber(id){
    var ar = id.split("_");
    return ar[1];
}
function menuItemExists(id){
	return document.getElementById("menuLink_"+id)!= null;
}
function menuBarExists(id){
	return document.getElementById("menuBar_"+id)!= null;
}
function getFirstMenuItemID(){
	var id = -1;
	$('.level0:first').each(function(index, element){
		id = IDtoNumber(element.id);
	});
	var bar = document.getElementById("menuBar_"+id);
	var items = bar.getElementsByTagName('a');
	return IDtoNumber(items.item(0).id);
}
function getMenuItemURL(id){
	return document.getElementById("menuLink_"+id).href;
}
function getMenuItemName(id){
	var html = document.getElementById("menuLink_"+id).innerHTML;
	if (html.substr(0,1) == "<"){
		html = html.substr(4,html.length-1);
		html = html.substring(0,html.length-5);
	}
	return html;
}
function getParentMenuItemID(barID){
	if (document.getElementById("menuLink_"+barID) == null)
		return -1;
	else 
    	return IDtoNumber(document.getElementById("menuLink_"+barID).parentNode.parentNode.id);
}
function menuItemHasChildMenuBar(itemID){
	return document.getElementById("menuBar_"+itemID) != null;
}
function getMenuBar(id){
	return getParentMenuItemID(id);
}
function getParentMenuBar(id){
	return getParentMenuItemID(id);
}
function getAllParentMenuBars(id){
	var bars = [];
	var current = id;
	while (current != -1){
		bars.push(current);
		current = getParentMenuBar(current);
	} return bars;
}
function getChildMenuBarIDs(barID){
	var ids = new Array();
	var bar = document.getElementById("menuBar_"+barID);
	var items = bar.getElementsByTagName("a");
	
	for (var q = 0; items.length > q; q++)
	{
		var id= IDtoNumber(items.item(q).id);
		if (document.getElementById("menuBar_"+id)){
			ids.push(id);
		}
	}
	return ids;
}
function getMenuItemRelatives(itemID){ // return all parent menu bars, its own menu bar and if a child menu bar exists: also return the child.
	var ids = new Array();
	if (menuItemHasChildMenuBar(itemID))
		ids.push(itemID);
	
	current = getMenuBar(itemID);
	while (current != -1){
		ids.push(current);
		current = getParentMenuBar(current);
	}
	return ids; // lower menubar is first
}
function activateMenuBar(barID, itemID){
	var bar = document.getElementById("menuBar_"+barID);
	var items = bar.getElementsByTagName("a");
	for (var q =0; q < items.length; q++)
	{
		var id= items.item(q).id;
		
		var c = 'activeMenuBar_activeItem';
        if ($('#'+id).hasClass('activeMenuBar_inactiveItem'))
			c = 'activeMenuBar_inactiveItem';
        if ($('#'+id).hasClass('inactiveMenuBar_Item'))
			c = 'inactiveMenuBar_Item';
        if ($('#'+id).hasClass('hiddenMenuBar_Item')){
			c = 'hiddenMenuBar_Item';
			$("#menuBar_"+barID).css('display','block'); //unhide
		}
		
		$('#'+id).stop(true,true);
		document.getElementById(id).removeAttribute("style");
        if (id == "menuLink_"+itemID && !$('#'+id).hasClass('activeMenuBar_activeItem')) // NOT
			$('#'+id).switchClass(c,'activeMenuBar_activeItem',300);
		else if (id != "menuLink_"+itemID && !$('#'+id).hasClass('activeMenuBar_inactiveItem'))
			$('#'+id).switchClass(c,'activeMenuBar_inactiveItem',300);		
	}
}
function deactivateMenuBar(barID){
    var bar = document.getElementById("menuBar_"+barID);
    var items = bar.getElementsByTagName("a");
    for (var q =0; q < items.length; q++){
        var id= items.item(q).id;
		var c = 'activeMenuBar_activeItem';
        if ($('#'+id).hasClass('activeMenuBar_inactiveItem'))
			c = 'activeMenuBar_inactiveItem';
        if ($('#'+id).hasClass('hiddenMenuBar_Item')){
			c = 'hiddenMenuBar_Item';
			$("#menuBar_"+barID).css('display','block'); //unhide
		}
		
        if (!$('#'+id).hasClass('inactiveMenuBar_Item')){ // NOT
			$('#'+id).stop(true,true);
			document.getElementById(id).removeAttribute("style");
			$('#'+id).switchClass(c,'inactiveMenuBar_Item',300);
		}
    }
}
function hideMenuBar(barID){
    var bar = document.getElementById("menuBar_"+barID);
    var items = bar.getElementsByTagName("a");
    for (var q =0; q < items.length; q++){
        var id= items.item(q).id;
		var c = 'activeMenuBar_activeItem';
        if ($('#'+id).hasClass('activeMenuBar_inactiveItem'))
			c = 'activeMenuBar_inactiveItem';
        if ($('#'+id).hasClass('inactiveMenuBar_Item'))
			c = 'inactiveMenuBar_Item';
        if (!$('#'+id).hasClass('hiddenMenuBar_Item'))
		{
			document.getElementById(id).removeAttribute("style");
			$('#'+id).removeClass(c);
			$('#'+id).addClass('hiddenMenuBar_Item');
			$("#menuBar_"+barID).css('display','none'); //unhide
		}
    }
}
function isHiddenMenuItem(id){
	return $('#menuLink_'+id).hasClass('hiddenMenuBar_Item');
}
function openMenuBar(id){
	if (mouseClick(id) && !isHiddenMenuItem(id)){
		
		if (getParentMenuBar(id) > 0){
			openMenuBar(getParentMenuBar(id));
		}
			
		activateMenuBar(getMenuBar(id),id);
		deactivateMenuBar(id);
	}
	return false;
}
function getMenuItemURLSuffix(id){
	var ids = getMenuItemRelatives(id);
	var name = '/';
	var previous = id;
	for (var q= 0; q<ids.length; q++){
		barID = ids[q];
		if (barID != id){
			name = '/'+getMenuItemName(previous)+name;
			previous = barID;
		}
	}
	return name;
}
function getMenuItemHashURL(id){
	return (id + getMenuItemURLSuffix(id));
}
var currentPage = getHash();
var currentSubPage = getSubPage();
var slideshowIsStopped = false;
var newsIsStopped = false;
function menuItemPress(id,force){
	
	galleryIsStopped = true;
	archiveIsStopped = true;
	slideshowIsStopped = true;
	newsIsStopped = true;
	
	if (mouseClick(id) && (!isHiddenMenuItem(id) || force)){
		
		if (!force)
			setHashAddition('');
			
		var ids = getMenuItemRelatives(id);
		$('.menuBar').each(function(index, element){
			var barID = IDtoNumber(element.id);
			 if (ids.find(barID) == -1){
				hideMenuBar(barID);
			 }
		});
		
		var previous = id;
		for (var q= 0; q<ids.length; q++){
			barID = ids[q];
			if (barID == id){
				deactivateMenuBar(barID);
			} else {
				activateMenuBar(barID,previous);
				previous = barID;
			}
		}
		if (!menuItemHasChildMenuBar(id)){
			if (!isGenuineHash() || getHash() != id){
				setHash(getMenuItemHashURL(id));
			}
			currentPage = id;
			currentSubPage = getSubPage();
			//alert("URL: " + document.location.href + " ;HASH: " + document.location.hash+ " ;ID: " + id);
			setTitle(getMenuItemName(id));
			var url = getMenuItemURL(id)+'/ajax/'+ getHashAddition();
			
			$('#content').stop();
			$('#content').animate({opacity:0},300,function(){
				$('#content').load(url, function() {
					$('#content').stop();
					$('#content').animate({opacity:1},300);
				});
			});
		}
	}
	return false;
}
function getSubPage(){
	var addition = getHashAddition().split('/');
	if (addition.length > 1 && (addition[0] == "Gallery" || addition[0] == "NewsItem"))
		return addition[1];
	else
		return -1;
}
function setTitle(addition){
	document.title = "Lenny Oosterwijk - "+addition;
}
function mouseClick(id){
	var time = new Date().getTime();
	lastClicks.push([id,time]);
	for (var q = 0; q < lastClicks.length-1 ; q++){
		if (time - lastClicks[q][1] > 300){
			lastClicks.shift();
			q--;
		} else if (lastClicks[q][0] == id) {
			return false;
		}
	}
	return true;
}
function getHash(){
	var hash = (self.document.location.hash.substring(1)).split('/');
	if (menuItemExists(hash[0])){
		return hash[0];
	} else {
		return getFirstMenuItemID();
	}
}
function getProtectiveControlHash(){
	return getMenuItemHashURL(getHash()).replace(' ','_').replace(' ','_').replace(' ','_').replace(' ','_').replace(' ','_').replace(' ','_');
}
function isGenuineHash(){
	var p = getProtectiveControlHash();
	return (document.location.hash.split(p)).length > 1;
}
function getHashAddition(){
	var p = getProtectiveControlHash();
	return (document.location.hash.split(p))[1];
}
function setHashAddition(a){
	setHash(getProtectiveControlHash()+a);
}
function setHash(h){
	document.location.hash = '#'+(h.replace(/ /g,'_'));
	lasthash = getProtectiveControlHash();
}
function printStackTrace() {
  var callstack = [];
  var isCallstackPopulated = false;
  try {
    i.dont.exist+=0; //doesn't exist- that's the point
  } catch(e) {
    if (e.stack) { //Firefox
      var lines = e.stack.split('\n');
      for (var i=0, len=lines.length; i<len; i++) {
        if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
          callstack.push(lines[i]);
        }
      }
      //Remove call to printStackTrace()
      callstack.shift();
      isCallstackPopulated = true;
    }
    else if (window.opera && e.message) { //Opera
      var lines = e.message.split('\n');
      for (var i=0, len=lines.length; i<len; i++) {
        if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
          var entry = lines[i];
          //Append next line also since it has the file info
          if (lines[i+1]) {
            entry += ' at ' + lines[i+1];
            i++;
          }
          callstack.push(entry);
        }
      }
      //Remove call to printStackTrace()
      callstack.shift();
      isCallstackPopulated = true;
    }
  }
  if (!isCallstackPopulated) { //IE and Safari
    var currentFunction = arguments.callee.caller;
    while (currentFunction) {
      var fn = currentFunction.toString();
      var fname = fn.substring(fn.indexOf('function') + 8, fn.indexOf('')) || 'anonymous';
      callstack.push(fname);
      currentFunction = currentFunction.caller;
    }
  }
  output(callstack);
}

function output(arr) {
  //Optput however you want
  alert(arr.join('\n\n'));
}

