//Todo - add functionality for form submission
//Script copyright 2009 Kevin Dean
function convertToLink(link){
	if(link.length < 2){ return 'pages/'; }
	return 'pages/'+link;
	}
var navSelectedClassName = 'selected';
var newBase = 'http://www.rollcallentertainment.com/';
var baseTitle = 'Roll Call Entertainment';
var mainNavigationId = 'wrapMain';

//Do not edit past here

var mouseOver = new Object();
mouseOver.type = 'mouseover';
var mouseOut = new Object();
mouseOut.type = 'mouseout';

var originalTitle = document.title;
var base = window.location.href.split('#')[0];
var cache = [];
var evalCache = [];
function refreshLinks(object,className,baseURL,baseNewURL){
	object.getElements('a'+className).each(function(el){
		el.removeEvent('click').addEvent('click', function(e){
			if(el.href == baseNewURL){ window.location.assign(baseURL+'#/'); }
			else{ window.location.assign(baseURL+'#'+el.href.replace(baseNewURL, '')); }
			return false;
		});});
	/*this.getElements('form'+className).each(function(el){
		el.removeEvent('submit').addEvent('submit', function(e){
		new Event(e).stop();
		if(el.action == baseNewURL)
			window.location.assign(baseURL+'#/');
		else
			window.location.assign(baseURL+'#'+el.href.replace(baseNewURL, ''));
		})});*/
	return object;
	};
function redoNavSelections(el){
	if(document.title == el.title+ ' - '+baseTitle || (document.title == baseTitle && el.title == 'Home')){
		if(!el.hasClass(navSelectedClassName)){
			hoverState.run(mouseOver, el.getElement('img'));
			el.addClass(navSelectedClassName);
			}
		}
	else if(el.hasClass(navSelectedClassName)){
		hoverState.run(mouseOut, el.getElement('img'));
		el.removeClass(navSelectedClassName);
		}
	}
window.addEvent('domready', function(){
	cache[0] = new Element('div', {html:$('content').get('html')});
	
	var navigation = $(mainNavigationId).getElements('.navigation');
	var main = $('content').store('currentPage', '');
	refreshLinks($(mainNavigationId),'.navigation', base, newBase);
	$('logo').grab(new Element('img', {'id':'loadIndicator', 'src':'img/loader_off.gif'}), 'top');
	var checkForPage = window.setInterval(function(){
		if(main.retrieve('currentPage') == window.location.hash){ return; }
		if(window.location.hash === ''){
			main.store('currentPage', '').set('html', cache[0].get('html'));
			document.title = originalTitle;
			navigation.each(redoNavSelections);
			return;
			}
		var pageGet = convertToLink(window.location.hash.replace('#', ''));
		if(window.location.hash.length < 3){
			document.title = baseTitle;
			}
		else{
			var rep = pageGet.split('/');
			if(rep[2] == '')
				rep = rep[1]
			else
				rep = rep[2];
			document.title = unescape(rep)+' - '+baseTitle;
			}
		var container = cache[pageGet];
		if($type(container) === false || container.retrieve('lastUpdate') + 600000 < new Date().getTime() || (container.retrieve('fetching') === false && container.get('html') === '')){
			cache[pageGet] = new Element('div');
			var showLoader = window.setTimeout(function(){
				$('loadIndicator').set('src', 'img/loader_on.gif');
				}, 120);
			var request = new Request.HTML({evalScripts:false, onRequest:function(){
					cache[pageGet].store('fetching', true).store('lastUpdate', new Date().getTime());
				}, onCancel:function(){
					clearTimeout(showLoader);
					$('loadIndicator').set('src', 'img/loader_off.gif');
					cache[pageGet] = null;
				}, onSuccess:function(responseTree, responseElements, responseHTML, responseJavascript){
					var waitForLoad = window.setInterval(function(){
						for(i = 0; i < document.images.length; i++){
							if(!document.images[i].complete || (typeof(document.images[i].naturalWidth) != 'undefined' && document.images[i].naturalWidth == 0))
								return false;
							} //Loop to make sure all images in the page are loaded before displaying it.
						cache[pageGet].set('html', responseHTML).store('fetching', false);
						if(pageGet != convertToLink(window.location.hash.replace('#', ''))){
							return;
							}
						refreshLinks(main.store('currentPage', window.location.hash).set('html', responseHTML), '.navigation', base, newBase);
						navigation.each(redoNavSelections);
						evalCache[pageGet] = responseJavascript;
						eval(responseJavascript);
						$('loadIndicator').set('src', 'img/loader_off.gif');
						clearTimeout(showLoader);
						clearInterval(waitForLoad);
						}, 30);
				}, onFailure:function(){
					clearTimeout(showLoader);
					$('loadIndicator').set('src', 'img/loader_off.gif');
					cache[pageGet] = null;
					main.set('html', '<strong>Request timed out!</strong>');
				}}).get(pageGet);
			}
		else{
			refreshLinks(main.store('currentPage', window.location.hash).set('html', container.get('html')), '.navigation', base, newBase);
			eval(evalCache[pageGet]);
			navigation.each(redoNavSelections);
			}
		}, 100);
	});