var pageRequest = "";

function makeAjaxRequestUrl(method) {
	var request = "?" + method;
	if (file_name != "index.php") {
		request = file_name + request;
	}

	return request;
}

function addOverlay(page) {
	if($('overlay')) {
		removeOverlay();
	}

	var size = $(page).getSize();
	var position = $(page).getPosition();

	var overlay = new Element("DIV");

	overlay.id = "overlay";
	overlay.setStyles($(page).getCoordinates());
	overlay.setStyle('z-index', 1);
	overlay.setStyle('position', 'absolute');
	overlay.setStyle('background-color', '#CCC');
	overlay.setOpacity(0.7);

	overlay.injectInside(document.body);
}

function removeOverlay() {
	if ($('overlay')) {
		$('overlay').remove();
	}
}

function loadPage(oldPage, newPage) {
	var query = {
		'new' : newPage
	};

	if (oldPage == newPage) {
		if ($(oldPage)) {
			return false;
		} else {
			var sidebar_links = $$('.sidebar a');
			var location = false;
			sidebar_links.each(function(link, i) {
				if (link.hash.test(oldPage)) {
					location = link.getParent().getParent().id;
				}
			});
			if (location) {
				oldPage = location;
			} else {
				return false;
			}
		}
	}

	addOverlay(oldPage);

	pageRequest = oldPage;

	new Ajax (makeAjaxRequestUrl('load_page'), {
		method : 'post',
		postBody : query,
		onComplete : finishLoadPage
	}).request();

	return false;
}

function finishLoadPage(pageContent) {
	var pageDiv = $(pageRequest);
	var pageContentDiv = $(pageRequest + "_content");
	var headerDiv = pageDiv.getPrevious();

	var newPage = Json.evaluate(pageContent);

	var sideBarLinks = $$('#' + pageDiv.id + ' .sidebar a');

	// Update any sidebar links, to make sure they fire their content
	// into the correct DIV.
	sideBarLinks.each(function(link, i) {
		var onclick = link.getProperty('onclick');
		var pages = onclick.replace("loadPage('","").split("', ");

		link.setProperty('onclick', "loadPage('" + newPage.name + "', " + pages[1]);
                if(link.hash.test(window.location.hash)) {
                       	link.addClass('active');
                } else if (link.hasClass('active')) {
                       	link.removeClass('active');
                }
	});

	// We want to remove the sidebar, so that we can load a new sidebar,
	// in case the new display page has sidebar content. At the same time,
	// we cache it to make sure we don't lose the menu.
	var sideBarDiv = $$('#' + pageDiv.id + ' .sidebar');
	sideBarDiv = sideBarDiv[0];
	sideBarDiv.remove();

	// Load the content and update the title:
	pageContentDiv.innerHTML = newPage.content;
	headerDiv.getFirst().innerHTML = newPage.title;
	// Set the link to the new hash so that hashing works when we
	// switch accodion elements.
	headerDiv.getFirst().setProperty('href', '#' + newPage.name);

	//Update the ids of the two DIVs:
	pageDiv.id = newPage.name;
	pageContentDiv.id = newPage.name + "_content";

	// Build the new sidebar.  If there was a sidebar brought down from
	// the Ajax request, get it.  Otherwise, generate a new sidebar that
	// we can fill with content.
	var newSideBar;
	if ($$('#' + pageDiv.id + '_content .sidebar').length > 0) {
		newSideBar = $$('#' + pageDiv.id + '_content .sidebar');
		newSideBar = newSideBar[0];
		newSideBar.remove();
	} else {
		newSideBar = new Element("DIV");
		newSideBar.addClass('sidebar');
		
	}
	if (newSideBar.getChildren().length > 0) {
		sideBarDiv.getFirst().injectBefore(newSideBar.getFirst());
	} else {
		sideBarDiv.getFirst().injectInside(newSideBar);
	}

	// Add the sidebar to the content DIV, in the correct place (so that
	// a new page load won't blow away the menue):
	newSideBar.injectBefore($(pageDiv.id + '_content'));

	// Update the page size, as it may (likely) have changed:
	newSize = $(pageDiv.id + '_content').getSize().scrollSize.y;
	if (newSideBar.getSize().scrollSize.y > newSize) {
		newSize = newSideBar.getSize().scrollSize.y;
	}
	new Fx.Style(pageDiv, 'height', {duration: 300}).start(newSize);

	// Clear out the overlay, we are open for business:
	removeOverlay();
}

function loadAccordion() {
	var pages = $$('div.page');
	var links = $$('h3.link');

	var link_color = "#fad991";

	/*links.each(function(link,i) {
		link.fx = link.effects({duration: 300, transition: Fx.Transitions.Cubic.easeIn});
		link.defaultColor = link.getStyle('background-color');
	});*/

	function checkHash(){
		var found = 0;
		// If there's no hash, we want to open the first accordion:
		if (window.location.hash == "") {
			return 0;
		}

		$$('h3.link a').each(function(link, i){
			if (link.hash.test(window.location.hash)) {
				//myAccordion.display(i);
				found = i;
			} 

			var content = link.getParent().getNext();
			var sidebar_links = $$('#' + content.id + " .sidebar a");
			sidebar_links.each(function(l) {
				if (l.hash.test(window.location.hash)) {
                        		l.addClass('active');
					loadPage(link.hash.replace("#",""), l.hash.replace("#",""));
					if (!found) {
						//myAccordion.display(i);
						found = i;
					}
                		} else if (l.hasClass('active')) {
                        		l.removeClass('active');
                		}
			});
		});
		return found;
	}

	var myAccordion = new Fx.Accordion(links, pages, { opacity: false, start: false, transition: Fx.Transitions.quadOut,
		onActive: function(link) {
			//link.fx.start({'background-color': link_color});
		},
		onBackground: function(link) {
			//link.fx.start({'background-color': link.defaultColor});
		},
		display: checkHash()
	});


	//if(!checkHash()) myAccordion.display(0);
}
