/* Functions */
function processPopups() {
	var links = $$("a.popup");
	links.each(function(link){
		link.onclick = function() {
			var sWidth = ",width=600";
			var sHeight = '';
			var sModal = '';
			if(this.getAttribute('w')) 		sWidth = ',width='+ this.getAttribute('w');
			if(this.getAttribute('h')) 		sHeight = ',height='+ this.getAttribute('h');
			if(this.getAttribute('modal')) 	sModal = ',modal=1,dependant=1,chrome=0,alwaysRaised=1';
			//alert(sWidth+' '+sHeight);
			window.open(this.href,'','toolbar=0,resizable=1,menubar=0,scrollbars=1'+sWidth+sHeight+sModal);
			return false;
		}//onclick	
	});
	var links = $$("a.modal");
	links.each(function(link){
		if (!link.readAttribute("processPopupsObserved")) {
			link.observe('click', function(event){
				Event.stop(event);
				var params = {
					title: this.title,
					width: 600
				};
				if (this.getAttribute('h')) 
					params.height = this.getAttribute('h');
				if (this.getAttribute('w')) 
					params.height = this.getAttribute('w');
				//follow the href	
				Modalbox.show(this.href, params);			
				return false;
			});
			//to avoid observing the same thing again and again.
			link.writeAttribute("processPopupsObserved", true);
		}
	});
}//end func


function processLoginPanel(){
	var panel = $("dropdownloginform");
	if(!panel) return;
	//var wrapper = panel.select("div.wrapper");
	var wrapper = Element.select(panel, "div.wrapper");
	if(!wrapper || wrapper.length != 1) return;
	panel.wrapper = wrapper[0];
	
	panel.handle = panel.select("a.handle")[0];
	if(!panel.handle) return;
	//hide the element :Should already be the case.
	panel.wrapper.hide();

	panel.toggle = function(){
		if (this.wrapper.hasClassName("open")) {
			//close it		
			this.hideMe();
		} else {
			//open it
			this.showMe();
		}
	}

	panel.hideMe = function(){
		Effect.Shrink(panel.wrapper, {
			duration:0.5,
			direction: "top-right",
			queue: {
				position: "end",
				scope: "collapsible",
				limit: 1
			},
		 	afterFinish:function(ef){
				ef.effects[0].element.removeClassName("open");
			}
		});
		//remove event listener
		document.stopObserving("click", this.clickObserver);
		panel.stopObserving("mouseleave", this.leaveObserver);
		panel.stopObserving("mouseenter", this.enterObserver);
	}
	panel.showMe = function(){
		Effect.Grow(panel.wrapper, {
			duration:0.5,
			direction: "top-right",
			queue: {
				position: "end",
				scope: "collapsible",
				limit: 1
			},
			afterFinish:function(ef){
				var panel = ef.effects[0].element.up();
				panel.wrapper.addClassName("open");
				document.observe("click", panel.clickObserver);
				panel.observe("mouseleave", panel.leaveObserver);
				panel.observe("mouseenter", panel.enterObserver);
			}
		});		
	}

	panel.clickObserver =  function(event){
		if(this.wrapper.hasClassName("open") && !Position.within(this, Event.pointerX(event), Event.pointerY(event))){
			this.hideMe();
		}
	}.bindAsEventListener(panel);

	panel.leaveObserver = function(event){
		if(this.wrapper.hasClassName("open")){
			//if the panel is open (it should be) then start the timeout
			this.pe = new PeriodicalExecuter(function(pe) {
				if(this.wrapper.hasClassName("open")){
	  				//close it if it is open
	  				this.hideMe();
				}
				//kill the timeout
				pe.stop();
			}.bind(this), 3);
		}
	}.bindAsEventListener(panel);
	
	panel.enterObserver = function(event){
		if(this.pe){
			//kill the close time out if it is running
			this.pe.stop();
		}
	}.bindAsEventListener(panel);

	//make link clickable
	panel.handle.observe("click", function(event){
		Event.stop(event);
		panel.toggle(); 
	 }.bind(this));
}

document.observe("dom:loaded", function(){
	processPopups();
	processLoginPanel();
	if(	window.adminmode && window.bLoggedIn){
		//we are logged in and editing
		qp = document.location.search.toQueryParams();  //convert to struct of sorts
		elm = $('c_' + qp.uuid);  //get the object ID
		if(elm){
			Element.scrollTo(elm);  //scroll to it
		}
	}
}); //start the func on page load


