// JavaScript Document
// Navigation Ojbect
$Nav = {
	pageToLoad: null,
	loadOptions: {},
	init: function() {
		// Set the page content off-stage and show the "loading" throbber
		//$('page_container').style.left = "-468px";
		jQuery('#page_container').css({'left': '-468px'});
		// Show the loading throbber:
		Biscuit.Crumbs.ShowThrobber();
		// Have the onPageLoaded() function run after all images are loaded:
		checkImageStatus('$Nav.onPageLoaded();');
	},
	onPageLoaded: function() {
		var normal_load = true;
		if (location.hash && location.hash.match(/#page_/)) {
			var page_to_load = '/'+location.hash.substring(6); // # plus 'page_'
			// Make sure it does not match the url path, so we don't double-load the page:
			if (page_to_load != top.location.pathname) {
				normal_load = false;
				this.pageToLoad = page_to_load;
				this.load_page();
			}
		}
		if (normal_load) {
			this.pageToLoad = location.pathname;
			this.slide_in();
		}
	},
	page_switch: function (url) {
		// Ensure that Lightview is closed in case a link was clicked in a lightview popup
		Lightview.hide();
		// Set the page to be loaded for the load_page() function
		this.pageToLoad = '/'+url;
		// Default to "return false" at the end of the function
		this.loadOptions.doReturn = true;
		// Add options, if any, supplied by the second argument
		for (var i in arguments[1]) {
			this.loadOptions[i] = arguments[1][i];
		}
		window.location.href="#page_"+url;
		Biscuit.Crumbs.ShowThrobber();
		this.slide_load();
		if (this.loadOptions.doReturn === true) {
			return false;
		}
	},
	slide_load: function() {
		this.slide_out(function() {
			$Nav.load_page();
		})
	},
	slide_out: function(callback) {
		jQuery('#page_container').css({'left': '0px'});
		jQuery('#page_container').animate({'left': '-468px'},'slow','swing',callback);
	},
	slide_in: function() {
		Biscuit.Console.log("Page should now slide in");
		jQuery('#page_container').css({'left': '-468px'});
		jQuery('#page_container').animate({ 'left': '0px'},'slow','swing',function() {
			Biscuit.Crumbs.HideThrobber();
		})
	},
	load_page: function() {
		var params = {}
		// Apply any additional parameters:
		for (var i in this.loadOptions) {
			if (i != "doReturn") {		// Never needed by server
				params[i] = this.loadOptions[i];
			}
		}
		var loadPage = this.pageToLoad;
		this.pageToLoad = null;
		// Load in the content via AJAX, and slide the content back in after successful load:
		new Ajax.Updater('page_content',loadPage,{
			method: 'get',
			requestHeaders: Biscuit.Ajax.RequestHeaders('update'),
			parameters: params,
			evalScripts: true,
			onComplete: function() {
				checkImageStatus('$Nav.slide_in();');
			}
		});
	},
	set_title: function(pagename,pagetitle) {
		// This function needs to be called by a script placed at the top of the content file being loaded or the site won't work!
		$('page_title').className = "title_"+pagename;
		$('page_title').update('<h1>'+pagetitle+'</h1>');
	},
	add_click_handlers: function() {
		// Add the click handlers to the text menu to over-ride the href with js navigation
		$$('div#textmenu a').each(function(el) {
			if (!el.href.match(/\/logout.*/)) {
				el.observe("click",function(event) {
					// Prevent the default action from occuring:
					Event.stop(event);
					// Call $Nav.page_switch() with the element's id as the argument:
					$Nav.page_switch(el.id);
				});
			}
		});
		$('zoom_it').observe('click',function(event) {
			Event.stop(event);
			$Nav.zoom_page();
		})
	},
	add_internal_link_handlers: function() {
		$$('div#page_content a.internal').each(function(el) {
			if (!el.href.match(/\/logout.*/)) {
				Biscuit.Console.log("Adding slide handler for "+el.href);
				el.observe("click",function(event) {
					// Prevent the default action from occuring:
					Event.stop(event);
					var pagename = el.href;
					if (pagename.match(/http:\/\/([^\/]+)/)) {
						pagename = pagename.replace(/http:\/\/([^\/]+)\//,"");
					}
					else if (pagename.substr(0,1) == "/") {
						pagename = pagename.substr(1,el.href.length);
					}
					$Nav.page_switch(pagename);
				});
			}
		});
	},
	add_tab_handlers: function(default_open_id) {
		// Add the click handlers to tabs, if they exist
		if ($('tabs') !== null) {
			$$('#tab_pages div.tab_content').each(function(el) {
				if (el.id != default_open_id) {
					$(el).hide();
				}
			});
			$$('ul#tabs li a').each(function(el) {
				el.observe("click",function(event) {
					Event.stop(event);
					$Nav.tab_switch(el.id);
				});
			});
		}
	},
	contact: function(email_address) {
		this.page_switch({contact_address: email_address});
	},
	tab_switch: function(tab_id) {
		$$('#tab_pages div.tab_content').each(function(el) {
			if (el.id != 'content_'+tab_id) {
				var my_tab_id = el.id.substring(8);
				el.hide();
				$(my_tab_id).removeClassName('selected');
			}
		});
		$('content_'+tab_id).show();
		$(tab_id).addClassName('selected');
	},
	zoom_page: function() {
		var page_title = $$('#page_title h1')[0].innerHTML;
		Lightview.show({
			href: '#page_content',
			rel: 'inline',
			title: page_title,
			options: {
				topclose: false,
				width: 850,
				height: 450,
				scrolling: false
			}
		});
	}
}
