/* 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



/*
 * Builds a login form dropdown form around the moodle link in the main nav
 * Slug of page needs to be "moodle-login"
 */
function processLoginPanel(){
	var container = $$("li.slug_moodle-login")[0];
	if(!container) return;
	container.makePositioned();
	container.link = container.select("a")[0];
	if(!container.link) return;
	container.panel = $$("#login-panel")[0];
	if(!container.panel) return;
	//create back reference
	container.panel.container = container;
	container.panel.makePositioned();
	
	//container.panel.absolutize();
	var cdims = container.getDimensions();
	var pdims = container.panel.getDimensions();

	//Create a little cover up to make a visual gap between the borders so the tab and panel "flow" into one
	container.coverup = new Element("div", {
			id:"loginpanel-coverup"
		}).setStyle({
				height: "3px",
				backgroundColor:container.panel.select("div")[0].getStyle("backgroundColor"),
				backgroundColor:"#183B61",
				position:"absolute",
				width: (cdims.width - 2) + "px",
				zIndex: 20
			});
	$$("body")[0].insert(container.coverup);

	container.coverup.hide();
	
	//ensure that the panel is on top of all other content, and that the cover up is on top of that.
	container.panel.setStyle({zIndex:200});
	container.coverup.setStyle({zIndex:201});

	container.doPositioning = function(){
		var cdims = container.getDimensions();
		var pdims = container.panel.getDimensions();
		this.panel.clonePosition(this, {
				setWidth:false,
				setHeight:false,
				offsetLeft: -(pdims.width - cdims.width),
				offsetTop: cdims.height - 4  //needs to be enough to cover rounded corners
			});
		this.coverup.clonePosition(this, {
			setWidth:false,
			setHeight:false,
			offsetLeft: 1,
			offsetTop: (cdims.height - 4)
		});			
	}

	container.toggle = function(){
		if (this.panel.hasClassName("open")) {
			//close it		
			this.hidePanel();
		} else {
			//open it
			this.showPanel();
		}
	}

	container.hidePanel = function(){
		Effect.BlindUp(this.panel, {
			duration:0.3,
			queue: {
				position: "end",
				scope: "collapsible",
				limit: 1
			},
		 	afterFinish:function(ef){
				ef.element.removeClassName("open");
				ef.element.container.coverup.hide();
			}
		});
		//remove event listener
		document.stopObserving("click", this.clickObserver);
		//this.stopObserving("mouseleave", this.leaveObserver);
		//this.stopObserving("mouseenter", this.enterObserver);
	}
	
	container.showPanel = function(){
		//console.log("showPanel:" + this.panel.id);
		container.doPositioning();
		container.coverup.show();
		Effect.BlindDown(this.panel, {
			duration:0.3,
			queue: {
				position: "end",
				scope: "collapsible",
				limit: 1
			},
			afterFinish:function(ef){
				//console.log("showPanel.afterFinish()");
				var container = ef.element.container;
				container.panel.addClassName("open");
				document.observe("click", container.clickObserver);
				//container.observe("mouseleave", container.leaveObserver);
				//container.observe("mouseenter", container.enterObserver);
			}
		});		
	}

	container.clickObserver =  function(event){
		if(this.panel.hasClassName("open") && !Position.within(this.panel, Event.pointerX(event), Event.pointerY(event))){
			this.hidePanel();
		}
	}.bindAsEventListener(container);

	container.leaveObserver = function(event){
		if(this.panel.hasClassName("open")){
			//if the listitem is open (it should be) then start the timeout
			this.pe = new PeriodicalExecuter(function(pe) {
				if(this.panel.hasClassName("open")){
	  				//close it if it is open
	  				this.hidePanel();
				}
				//kill the timeout
				pe.stop();
			}.bind(this), 30);
		}
	}.bindAsEventListener(container);
	
	container.enterObserver = function(event){
		if(this.pe){
			//kill the close time out if it is running
			this.pe.stop();
		}
	}.bindAsEventListener(container);

	//make link clickable
	container.link.observe("click", function(event){
		Event.stop(event);
		container.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


