var PapercutMenu = {
	Version: '2.5.0'
}





//This fixes a bug for flickering background images in internet explorer 6
try {
	document.execCommand('BackgroundImageCache', false, true);
}
catch(e) {
}
//This fixes a bug for flickering background images in internet explorer 6





var spacing = 5;
var visibles = new Array();
var isOverMenu = false;
var menuTimer = null;

function end(a) {
	if (a.length > 0) {
		return a[a.length-1];
	}
	else {
		return false;
	}
}

var activeLinks = new Object();
var menuClasses = new Array();
var visibleIds = new Object();

function removeHangingMenus() {
	
}

function urlClean(str) {
	return str.replace(/#/, 'p').replace(/&/, 'a').replace(/\?/, 'q').replace(/=/, 'e');
}
Event.observe(window, 'load', function() {
	var matchURL = urlClean(end(location.href.toString().split('/')));
	
	// get the class names of all menus on the page
	var menus = document.getElementsByClassName('dmenu');
	for (var i = 0; i < menus.length; i++) {
		
		// get all top level links
		var c = document.getElementsByClassName(menus[i].innerHTML + '-top-div');
		for (var j = 0; j < c.length; j++) {
			var links = c[j].getElementsByTagName('a');
			for (var k = 0; k < links.length; k++) {
				// the id is the relative/absolute URL
				if (links[k].id == matchURL) {
					links[k].parentNode.className = 'activelink';
					//links[k].parentNode.innerHTML = '<div class="activeBlock">&nbsp;</div>' + links[k].parentNode.innerHTML;
				}
			}
		}
		
		// get all sub level links
		var c = document.getElementsByClassName(menus[i].innerHTML + '-sub-div');
		// check if there is a parent item
		for (var j = 0; j < c.length; j++) {
			var subId = $(c[j].id.replace(/div/, 'li'));
			if (subId != null) {
				if (subId.parentNode.className == menus[i].innerHTML + '-sub') {
					//subId.childNodes[1].style.display = '';
				}
				subId.childNodes[0].style.display = '';
				var links = c[j].getElementsByTagName('a');
				for (var k = 0; k < links.length; k++) {
					// the id is the relative/absolute URL
					if (links[k].id == matchURL) {
						var l = links[k].parentNode.parentNode.parentNode;
						while ($(l.id.replace(/div/, 'li')) != null) {
							l = $(l.id.replace(/div/, 'li'));
							if (l.parentNode.parentNode.className.match(/top-div/)) {
								l.className = 'activelink';
								activeLinks[l.id] = true;
							}
							l = l.parentNode.parentNode;
						}
					}
				}
			}
		}
	}
	
	removeHangingMenus();
});

function showMenuPopup(numPopups, direction, alignment, item) {
	clearTimeout(menuTimer);
	//d('cleared ' + menuTimer);
	isOverMenu = true;
	
	var subMenu = $(item.parentNode.id.replace(/li/, 'div'));
	var parentMenu = $(item.parentNode.parentNode.id.replace(/ul/, 'div'));
	
	if (visibles.length && subMenu != visibles[visibles.length - 1][0] && parentMenu != visibles[visibles.length - 1][0]) {
		hidePopups(parentMenu);
	}
	if (subMenu != null && (numPopups == -1 || visibles.length < numPopups)) {
		visibles.push({0:subMenu, 1:item.parentNode});
		item.parentNode.className = 'activelink';
		var ofDiv = Position.positionedOffset(item.parentNode.parentNode.parentNode);
		var ofLink = Position.positionedOffset(item);
		switch (direction) {
			case 'u':
				subMenu.style.left = p[0] + 'px';
				subMenu.style.top = p[1] - subMenu.getHeight() - spacing + 'px';
				subMenu.style.display = '';
				//new Effect.Appear(subMenu, {queue:{position:'end', scope:subMenu.id}, duration:.5});
			break;
			
			case 'd':
				subMenu.style.left = ofLink[0] + 'px';
				subMenu.style.top = ofLink[1] + item.offsetHeight + spacing + 'px';
				subMenu.style.display = '';
				//new Effect.BlindDown(subMenu, {queue:{position:'end', scope:subMenu.id}, duration:.5});
			break;
			
			case 'r':
				subMenu.style.left = ofDiv[0] + item.offsetWidth + spacing + 'px';
				if (alignment == 'top') {
					subMenu.style.top = ofDiv[1] + ofLink[1] + 'px';
				}
				else {
					subMenu.style.top = ofDiv[1] + item.offsetHeight - subMenu.getHeight() + 'px';
				}
				subMenu.style.display = '';
				
				if(right_side(subMenu) >= screen_right()){
					subMenu.style.left = parseInt(subMenu.style.left) - 450 + "px";
				}
				//new Effect.Appear(subMenu, {queue:{position:'end', scope:subMenu.id}, duration:.5});
			break;
		}
	}
	else {
		
	}
}

function hidePopups(stop) {
	clearTimeout(menuTimer);
	//d('hiding with ' + stop + ' and ' + menuTimer);
	if (stop == null) {
		while (visibles.length && visibles[visibles.length-1][0] != stop) {
			if (!activeLinks[visibles[visibles.length-1][1].id]) {
				visibles[visibles.length-1][1].className = '';
			}
			visibles[visibles.length-1][0].style.display = 'none';
			//new Effect.Fade(visibles[visibles.length-1][0], {queue:{position:'end', scope:visibles[visibles.length-1][0].id}, duration:.5});
			visibles.pop();
		}
	}
	else {
		while (visibles.length && visibles[visibles.length-1][0] != stop) {
			if (!activeLinks[visibles[visibles.length-1][1].id]) {
				visibles[visibles.length-1][1].className = '';
			}
			visibles[visibles.length-1][0].style.display = 'none';
			visibles.pop();
		}
	}
		
}

function setOut() {
	isOverMenu = false;
	clearTimeout(menuTimer);
	menuTimer = setTimeout('hidePopups()', 2000);
}