var Menu = Class.create();
Menu.prototype = {
	initialize : function () {
		this.firstlevel = Array();
		this.secondlevel = Array();
		this.thirdlevel = Array();
		this.root = 'http://bedrijfsverzekeraar.slagbijalmelo.nl';
		this.requestUrl = '';
	},
	
	setRoot : function(url){
		this.requestUrl = url;
		dhtmlHistory.initialize();
		dhtmlHistory.addListener(this.historyChange);
		var curLocation = new String(document.location);
		if(dhtmlHistory.isFirstLoad()){
			if(curLocation.indexOf('#') > -1){
				document.location = curLocation.replace('#', '');
			}
		}
	},
	replaceLinks : function (rootElement){
		var elements = $(rootElement).getElementsByTagName('a');
		for(var i=0; i<elements.length;i++){ 
			var curElement = elements[i];
			oldHref = curElement.href;
			newHref = oldHref.replace(this.requestUrl,'');
			curElement.href = this.requestUrl +"#" +newHref ;	
		}
		
	},
	historyChange : function (newLocation){
		Menu.setPage(newLocation);
		Menu.expandMenu();
	},
	addFirstLevel : function (name) {
		this.firstlevel.push(name);
	},
	addSecondLevel : function (name) {
		this.secondlevel.push(name);
	},
	addThirdLevel : function (name){
		this.thirdlevel.push(name);
	},
	hideFirstlevel :function (ignoreElement){
		for(var i=0 ; i<=this.firstlevel.length-1; i++){
			if($(this.firstlevel[i] +'sub').id != ignoreElement.id){
				new Effect.BlindUp(this.firstlevel[i] +'sub' ,{duration:0.2});
				elm = this.firstlevel[i] +'button' ;
				$(elm).style.fontWeight ='normal';	
		
			}
		}
	},
	hideSecondlevel :function (ignoreElement){
		for(var i=0 ; i<=this.secondlevel.length-1; i++){
			if($(this.secondlevel[i] +'sub').id != ignoreElement.id){
				new Effect.BlindUp(this.secondlevel[i] +'sub',{duration:0.2});
				elm = this.secondlevel[i] +'button' ;
				$(elm).style.fontWeight ='normal';
			
				
			}
		}
	},
	hideThirdlevel : function(ignoreElement){
		for(var i=0; i<=this.thirdlevel.length-1; i++){
			if($(this.thirdlevel[i] +'button').id != ignoreElement.id){
				$(this.thirdlevel[i] +'button').style.textDecoration = 'none';
				
			}
		}
	},
	
	setPage : function(page){
		var newPage = page.replace('#','');	
		
		new Ajax.Updater('MainText',Menu.requestUrl +page);
	},
	updateContent : function (element){
		page = element.href.replace('#','');
		if(page == 'http://www.debedrijfsverzekeraar.nl/contact/routebeschrijving'){
			document.location = page;
		}
		new Ajax.Updater('MainText',page,{evalScript:true});
	},
	expandMenu : function (){
		var curLocation = new String(document.location);
		var hash = curLocation.substr(curLocation.indexOf('#'));
		var path = hash.split('/');		
		var firstLevelElm = path[1] +'sub';
		var secondLevelElm = path[2] +'sub';
		var thirdLevelElm = path[3] +'button';
		if($(firstLevelElm)){
			if(!Element.visible($(firstLevelElm))){
				new Effect.BlindDown($(firstLevelElm), {duration:0.2});		
				$(path[1] +'button').style.fontWeight = 'bolder';
			}
			Menu.hideFirstlevel($(firstLevelElm));
		}
		if($(secondLevelElm)){
			if(!Element.visible($(secondLevelElm))){
				new Effect.BlindDown(secondLevelElm, {duration:0.2});
				$(path[2] +'button').style.fontWeight = 'bolder';
			}
			Menu.hideSecondlevel($(secondLevelElm));
		} 
		
		if($(thirdLevelElm)){
			Menu.hideThirdlevel(thirdLevelElm);
			$(thirdLevelElm).style.textDecoration = 'underline';
		}
	}
}
