var currentPage;
var menustr;
var currentMenu;
var currentSelection;

var hashListener = {
	ie:		/MSIE/.test(navigator.userAgent),
	ieSupportBack:	true,
	hash:	document.location.hash,
	check:	function () {
		var h = document.location.hash;
		if (h != this.hash) {
			this.hash = h;
			this.onHashChanged();
			document.getElementById('loader').style.display="block";
		}
	},
	init:	function () {

		// for IE we need the iframe state trick
		if (this.ie && this.ieSupportBack) {
			var frame = document.createElement("iframe");
			frame.id = "state-frame";
			frame.style.display = "none";
			document.body.appendChild(frame);
			this.writeFrame("");
		}

		var self = this;

		// IE
		if ("onpropertychange" in document && "attachEvent" in document) {
			document.attachEvent("onpropertychange", function () {
				if (event.propertyName == "location") {
					self.check();
				}
			});
		}
		// poll for changes of the hash
		window.setInterval(function () { self.check() }, 50);
	},
	setHash: function (s) {
		// Mozilla always adds an entry to the history
		s = document.location.hash;
		if (this.ie && this.ieSupportBack) {
			this.writeFrame(s);
		}
//		document.location.hash = s;
	},
	getHash: function () {
		return document.location.hash;
	},
	writeFrame:	function (s) {
		//The line below is used so the IE can have the front button event but not the back
		s = document.location.hash;
		
		var f = document.getElementById("state-frame");
		var d = f.contentDocument || f.contentWindow.document;
		d.open();
		d.write("<script>window._hash = '" + s + "'; window.onload = parent.hashListener.syncHash;<\/script>");
		d.close();
	},
	syncHash:	function () {
		var s = this._hash;
		if (s != document.location.hash) {
			document.location.hash = s;
		}
	},
	onHashChanged:	function () {}
	
};

function popup(mylink, windowname){
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
	href=mylink;
	else
	href=mylink.href;
	window.open(href, windowname, 'width=320,height=150,scrollbars=no,border=no');
	return false;
}


function loadPage(state){

	var stateparts = new Array();
	stateparts = state.split('!');
	var page = stateparts[0];

	var cat = stateparts[1];
	var id = stateparts[2];

	$('undermenu').load('pages/loadpage.php?p='+stateparts[0]+'&c='+stateparts[1]+'&id='+stateparts[2]);
	
};

function setCurrentPage()
{
	window.currentPage = "#1";
}

function getCurrentPage()
{
    return window.currentPage;
}

function init(){
	goforit();
}

function initializeMyMenu(){
	
	var menus = new Array();
	
	menus = getMenu().split(';');
	var i=0;
	var text = '';
	var a = '';
	for (i=0; i<menus.length; i++){
		text = text + ' ' + menus[i];
	}
		$(menus[0]+'-td').addEvent('mouseenter',function(){showSubMenu(menus[0])});
		$(menus[1]+'-td').addEvent('mouseenter',function(){showSubMenu(menus[1])});
		$(menus[2]+'-td').addEvent('mouseenter',function(){showSubMenu(menus[2])});
		$(menus[3]+'-td').addEvent('mouseenter',function(){showSubMenu(menus[3])});
		$(menus[4]+'-td').addEvent('mouseenter',function(){showSubMenu(menus[4])});

		$(menus[0]+'-td').addClass('menutable-td');
		$(menus[1]+'-td').addClass('menutable-td');
		$(menus[2]+'-td').addClass('menutable-td');
		$(menus[3]+'-td').addClass('menutable-td');
		$(menus[4]+'-td').addClass('menutable-td');
		
		$(menus[0]+'-td').addEvent('mouseleave',function(){hideSubMenu(menus[0])});
		$(menus[1]+'-td').addEvent('mouseleave',function(){hideSubMenu(menus[1])});
		$(menus[2]+'-td').addEvent('mouseleave',function(){hideSubMenu(menus[2])});
		$(menus[3]+'-td').addEvent('mouseleave',function(){hideSubMenu(menus[3])});
		$(menus[4]+'-td').addEvent('mouseleave',function(){hideSubMenu(menus[4])});

		$('menu_area').addEvent('mouseleave',function(){$('menu_container').set('html', '')});
			//$('blacklist-td').set('styles',{'padding-bottom':'2px'});
		//$('blacklist-td').addClass('menutable-td');
		$('blacklist-td').addEvent('mouseenter',function(){
			//$('blacklist-td').addClass('menutable-sel');
			$('menu_container').set('html', '');
		});
		$('blacklist-td').addEvent('mouseleave',function(){$('blacklist-td').removeClass('menutable-sel')});

}


function showSubMenu(menuitem){

	//show the appropriate submenu
	$('menu_container').set('html', $(menuitem+'sub').get('html'));
	//$(menuitem+'-td').addClass('menutable-sel');

	//calculate the correct position and adjust the submenu margins to place the submenu under its parent menu
	var menuitemcoor = $(menuitem).getCoordinates();
	var submenutablecoor = $(menuitem+'subtable').getCoordinates();
	var rightgap = 2*(submenutablecoor.left - menuitemcoor.left) + 60;
	var leftgap = 2*(menuitemcoor.left - submenutablecoor.left) - 60;
	if(submenutablecoor.left > menuitemcoor.left || submenutablecoor.right < menuitemcoor.right){
		if (rightgap>0){
			$(menuitem + 'submargin-right').set('styles',{'width':rightgap+'px','background-color':'white'});
			//$(menuitem + 'submargin-right').addClass('menutestclass');
		}
		if (leftgap>0){
			$(menuitem + 'submargin-left').set('styles',{'width':leftgap+'px','background-color':'white'});
		}
	}

}
function hideSubMenu(menuitem){
	$(menuitem+'-td').removeClass('menutable-sel');
}
function setMenu(str){
	window.menustr = str;
}
function getMenu(){
	return window.menustr;
}

window.onload = init;

window.addEvent('domready', function() {
	initializeMyMenu();

	hashListener.init();
//	hashListener.setHash('1');
	hashListener.onHashChanged = function () {
		var hash = hashListener.getHash();
		
		if (hash == ''){
			hash = '#1';
		}
				// for example: if hash is "#2!2!34" the below substring is "2!2!34" --> without #
		state = hash.substr(1);
		nocache = Math.random();

		loadPage(state+'&nocache = '+nocache);
		
		window.scroll(0,0);

	};
	hashListener.onHashChanged();
	
});

