EntAdmin = {}

EntAdmin.Help = function(params){
	var options = {
		page:false
	}
	Object.extend(options, params);
	url = '/help/admin/';
	if(options.page){
		url += options.page +'/';
	}
	var fullSearchWindow = window.open(url, 'HelpWindow', 'width=700,height=600,resizable=1,scrollbars=0,toolbar=0,location=0,directories=0,status=0,menubar=0');
	if(window.focus){
		fullSearchWindow.focus();
	}
}

var GUIDropDown = Class.create();
GUIDropDown.prototype = {
	initialize: function(options){
		this.options = {
			classname: 'div.GUIDropDown',
			childClassname: 'div.GUIDropDownBox'
		}
		Object.extend(this.options, options || {});
		
		this.lastActive = false;
		
		Event.observe(window, 'load', this.onLoad.bindAsEventListener(this));
	},
	
	onLoad: function(event){
		var elements = $$(this.options.classname);
		$A(elements).each(
			function(iter){
				Event.observe(iter, 'click', this.popup.bindAsEventListener(this));
			}.bind(this)
		);
		var children = $$(this.options.childClassname);
		$A(children).each(
			function(iterChild){
				iterChild.style.display = "none";
				$A(iterChild.getElementsBySelector("a")).each(
					function(link){
						Event.observe(link, 'click', this.goToLink.bindAsEventListener());
					}.bind(this)
				);
			}.bind(this)
		);
		
		Event.observe(document.body, 'click', this.hideLast.bindAsEventListener(this));
	},
	goToLink: function(event){
		Event.stop(event);
		try{
			var block = !confirm( Event.element(event).attributes['confirm'].value );
		}
		catch(e){
			var block = false;
		}
		if( !block )
			document.location = Event.element(event).href;
	},
	popup: function(event){
		Event.stop(event);
		var element = Event.element(event);
		if(element.className == 'GUIDropDown'){
			var sibling = element.getElementsBySelector(this.options.childClassname)[0];
		} else {
			var sibling = element.next();
		}
		if(this.lastActive && this.lastActive != sibling){
			this.hideLast();
		}
		var pos = this.getAbsolutePos(sibling.parentNode);
		var height = sibling.parentNode.getHeight();
		
		sibling.toggle();
		sibling.style.zIndex = 100;
		sibling.style.position = 'absolute';
		sibling.style.left = (pos.x) +'px';
		sibling.style.top = (pos.y+height) +'px';
		
		this.lastActive = sibling;
	},
	getAbsolutePos: function(el) {
		var SL = 0, ST = 0;
		var is_div = /^div$/i.test(el.tagName);
		if (is_div && el.scrollLeft)
			SL = el.scrollLeft;
		if (is_div && el.scrollTop)
			ST = el.scrollTop;
		var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
		if (el.offsetParent && el.offsetParent.style.position != 'relative') {
			var tmp = this.getAbsolutePos(el.offsetParent);
			r.x += tmp.x;
			r.y += tmp.y;
		}
		return r;
	},
	hideLast: function(){
		if(this.lastActive){
			this.lastActive.style.display = 'none';
			this.lastActive = false;
		}
	}
}

var GUIDropDownObj = new GUIDropDown();