var TreeMenu = Class.create({
	
	initialize : function(menuId, selectedId, restrictedIds) {	
		this.initTranslate();
		this.menuId = menuId;
		this.selectedId = selectedId;
		this.restrictedIds = restrictedIds;
		this.menu = $('treeMenu_'+menuId);
		//this.selectedNode = $('menuItem_'+nodeId);
		this.hideAll();
		this.setSelectedStyle();
		if (this.selectedId != 0) {
			this.foldOut();
		} else {
			this.showTopLevel();
		}
		this.setRestrictedItems();
	},
	
	initTranslate : function() {
		var languageTable = {};
		languageTable['da'] = {};
		languageTable['da']['The page '] = 'Siden ';
		languageTable['da'][' requires authentication.'] = ' kræver godkendelse.';
		languageTable['da']['Username'] = 'Brugernavn';
		languageTable['da']['Password'] = 'Password';
		languageTable['da']["Send"] = 'Send'; 
		languageTable['da']["Cancel"] = 'Annuller';				
		this.translate = new Translate(languageTable);
	},
	
	hideAll: function() {
		this.menu.childElements().each(function(e) {
			e.hide();
		});
	},
	
	setSelectedStyle : function() {		
		if (this.selectedId != 0) {
			$('menuItem_' + this.selectedId).setStyle({				
				fontWeight : 'bold'
			});
		}
	},
	
	getNestLevel : function(node) {
    	for (var i = 1; i<=4; i++) {
			if (node.hasClassName('menuItemNestLevel_'+i)) {
				return i;	
			}
		}
        return false;
    },
	
	findParent: function(node) {
		var nestLevel = this.getNestLevel(node);
		if (nestLevel == 1) return false;
		while(this.getNestLevel(node)>= nestLevel) {
			node = node.previous();
		}
		return node;
	},
	
	
	foldOut: function() {					
		var selectedNode = $('menuItem_' + this.selectedId);
    	var nestLevel = this.getNestLevel(selectedNode);
    	var showNodes = new Array();
    	showNodes.push(selectedNode);
    	otherNode = selectedNode;
    	var parent = this.findParent(otherNode);
    	while (parent) {
			showNodes.push(parent);
			otherNode = parent;
			parent = this.findParent(otherNode);
		}
		//alert(showNodes[0].id + ' '+ showNodes[1].id);
		showNodes.each(function(node,index) {
			this.showSiblings(node);
		}.bind(this));
		this.showChildren(selectedNode);	
    },
    
    showTopLevel: function() {
		$$('#treeMenu_'+this.menuId + ' .menuItemNestLevel_1').each(function(e) {
			e.show();
		});
	},
    
    showSiblings : function(node) {
		var nestLevel = this.getNestLevel(node);
		if (nestLevel == 1) {
			this.showTopLevel();	
			return;
		}
		
		node.show();
		
		// Op
		while (this.getNestLevel(node.previous())>= nestLevel) {
			if (this.getNestLevel(node.previous()) == nestLevel) {
				node.previous().show();
			}
			node = node.previous();
		}
		
		// Ned
		while (this.getNestLevel(node.next())>= nestLevel) {
			if (this.getNestLevel(node.next()) == nestLevel) {
				node.next().show();
			}
			node = node.next();
		}
	},
	
	showChildren : function(node) {
		var nestLevel = this.getNestLevel(node);
		while (this.getNestLevel(node.next())> nestLevel) {
			if (this.getNestLevel(node.next()) == nestLevel + 1) {
				node.next().show();
			}
			node = node.next();
		}
	},
	
	setRestrictedItems : function() {
		this.restrictedIds.each(function(id) {
			$('menuItem_' + id).down('a').observe('click', this.restrictedClick.bind(this));
		}.bind(this));
	},
	
	restrictedClick : function(event) {
		event.stop();
		var a = event.element();
		if (!a.tagName == 'a') { a = a.up('a'); }		
		this.targetHref = a.href;
		this.targetTitle = a.firstChild.nodeValue;
		this.returnPath = document.location.href;
		this.showAccessDialog();
		a.blur();
	},
	
	showAccessDialog : function() {
		$('curtain').setStyle({
			background: 'black',
			opacity: 0.3,
			top:0,
			left:0,
			width: document.viewport.getWidth() + 'px',
			height: document.viewport.getHeight() + 'px',
			display:'block'
		});
		
		$('modalDialog').update('<div id="accessContainer"><form id="accessForm" name="accessForm" method="post" action="' + this.targetHref + '">' + this.translate._('The page ') + this.targetTitle + this.translate._(' requires authentication.') + '<br /><br />' + this.translate._('Username') + '<br /><input type="text" name="accessUsername" id="accessUsername" /><br /><br />'+ this.translate._('Password')+  '<br /><input type="password" name="accessPassword" id"accessPassword" /><br /><br /><input type="submit" name="accessSend" id="accessSend" value="' + this.translate._('Send')+'" /> <input type="button" name="accessCancel" id="accessCancel" value="' + this.translate._('Cancel')+'" /><input type="hidden" name="returnPath" id="returnPath" value="'+this.returnPath+'" /></form></div>');
		$('accessContainer').setStyle({
			position:'absolute',
			width: '200px',
			height: '200px',
			left: Math.round((document.viewport.getWidth()-220)/2) + 'px',
			top: Math.round((document.viewport.getHeight()-240)/2) + 'px',
			background: 'white',
			border:'4px double black',
			textAlign: 'left',
			padding:'20px',
			fontSize: '12px'
		});
		$('accessCancel').observe('click', this.cancelAuthentication.bind(this));
		$('accessUsername').focus();
	},
	
	cancelAuthentication : function() {
		$('curtain').setStyle({
			display: 'none'
		});
		$('modalDialog').update('');
	}

});