var Rules = {
	'#local-nav ul a, #local-nav ul span': function(element) {
		// Remove the border from the first local nav element
		var item = element.parentNode;
		var sib = getPreviousSiblingElement(item);
		if (!sib) {
			element.style.border = "none";
		}
	}, 
	'.subjects a:click': function(element, event) {
		displayGuides(element,event);
		Event.stop(event);
	},
	'html:loaded': function(element) {
		createDarkOverlay(element, document.getElementsByTagName('body')[0]);
	}
}

function createDarkOverlay(element, body_home) {
	dark_overlay = document.createElement('div');
	dark_overlay.setAttribute('id', "dark-overlay");
	dark_overlay.onclick = function() {
		//removeCommAList();
		closeDefinition($('popup-holder'));
		this.hide();
	}
	body_home.appendChild(dark_overlay);
	$('dark-overlay').setStyle({
		width: '100%',
		left: '0',
		top: '0',
		position: 'absolute',
		height: getPageSize()[1] + "px",
		background: "transparent url(/research-guides/images/overlay-dark.png) repeat scroll 0 0",
		display: "none",
		zIndex: 10
	});
}


function getPreviousSiblingElement(element) {
	var node = element.previousSibling;
	while (node) {
		if (node.nodeType == 1) {
			return node;
		}
		node = node.previousSibling;
	}
	return null;
}

function displayGuides(element,event) {

	var mouse_click_x = getMousePosX(event);
	var mouse_click_y = getMousePosY(event);
	
	
	var base = element.href.lastIndexOf('/');
	var categoryId = element.href.substring(base + 1);
	var rtime = new Date().getTime();
	var url = '/research-guides/show/sub_category_part/' + categoryId + '?count=10&random=' + rtime;
	//we add the random bit so that browsers don't cache the files themselves unless told
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(transport) {
		
			popupGuides(transport.responseText, event, mouse_click_x, mouse_click_y);
		}
	});
}

function getPageSize() {
	var xScroll, yScroll;
		
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
		
	var windowWidth, windowHeight;
		
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}


function popupGuides(responseText, event, mouse_click_x, mouse_click_y) {
	$('dark-overlay').style.height = getPageSize()[1] + "px";
	$('dark-overlay').show();

	// Remove any other popup definitions on the screen to remove clutter
	if (document.getElementById("popup-holder")) {
		var last_popup = document.getElementById("popup-holder");
		last_popup.parentNode.removeChild(last_popup);
	}
	body_element = document.getElementsByTagName("body")[0];
	
	// Create a container to hold the popup definition background image 
	// and an inner container to hold the text. add the definition to it.
	var popup_holder = document.createElement("div");
	popup_holder.setAttribute("id","popup-holder");
//	var guide_element = document.getElementById(element.id + "-def");
	var guide_holder = document.createElement("div");
	guide_holder.innerHTML = responseText;
	guide_holder.setAttribute("id","guide_holder");
	popup_holder.appendChild(guide_holder);

	// Attach ...
	body_element.insertBefore(popup_holder,body_element.firstChild);

//	body_element.style.margin = "0";
	// The user will need to close the popup definition, 
	// create a clickable 'X' to close the definition and append 
	// it to the definition.
	var close_link = document.createElement("a");
	var close_text = document.createTextNode("X");
	close_link.setAttribute("href","#");
	close_link.onclick = function() {
		closeDefinition($(popup_holder)); //,close_link);
		$('dark-overlay').hide();
		return false;
	}
	close_link.appendChild(close_text);
	close_link.style.color = "#fff";
	close_link.style.textDecoration = "none";
	close_link.style.display = "block";
	close_link.style.fontSize = "11px";
	close_link.style.height = "10px";
	close_link.style.width = "10px";
	close_link.style.position = "absolute";
	close_link.style.zIndex = "19";
	popup_holder.appendChild(close_link);

	guide_holder.style.borderBottom = "1px solid #aaa";
	guide_holder.style.fontSize = "1.3em";
	guide_holder.style.padding = "1em .5em 2em";
	popup_holder.style.display = "block";
	popup_holder.style.position = "absolute";
	popup_holder.style.width = "200px";
	popup_holder.style.backgroundRepeat = "no-repeat";
	popup_holder.style.zIndex = "20";
	popup_holder.style.top = mouse_click_y - 70 + "px";

	if (document.body.clientWidth / 2 > mouse_click_x) {
		popup_holder.style.left = mouse_click_x + "px";
		popup_holder.style.backgroundImage = "url(/research-guides/images/research-guides-point-left.gif)";
		popup_holder.style.padding = "20px 0 0 50px";
		close_link.style.top = "3px";
		close_link.style.right = "3px";
	}	else {
		popup_holder.style.left = mouse_click_x - 280 + "px";
		popup_holder.style.backgroundImage = "url(/research-guides/images/research-guides-point-right.gif)";
		popup_holder.style.padding = "20px 50px 0 0";
		close_link.style.top = "3px";
		close_link.style.right = "53px";
	}
}

function getMousePosX(e) {
	var posx = 0;

	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
	}
	return posx;
}

function getMousePosY(e) {
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageY) 	{
		posy = e.pageY;
	}
	else if (e.clientY) 	{
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	return posy;
}

function closeDefinition(container) { //,close_link) {
	container.remove()
	//close_link.parentNode.removeChild(close_link);
	//container.parentNode.removeChild(container);
}