//Todo - add functionality for form submission
//Script copyright 2010 Kevin Dean

if(vIE() != 6){

function convertToLink(link){
	if(link.length < 2){ return 'pages/'; }
	return 'pages/'+link;
	}
var navSelectedClassName = 'selected';
var newBase = 'http://www.everfinerecords.com/';
var baseTitle = 'Everfine Records';
var mainNavigationId = 'wrapAll';
var contentId = 'wrapContent';
var logoId;

//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 = [];
var bannerCache = [];
var sidebarCache = [];
var titleCache = [];
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;
		});});
	return object;
	};
function redoNavSelections(el){
	if((window.location.href.contains(el.href.replace(newBase, '')) && el.title != 'Home') || document.title == baseTitle+' - '+el.title || (document.title == baseTitle && el.title == 'Home')){
		if(!el.hasClass(navSelectedClassName)){
			el.addClass(navSelectedClassName);
			}
		hoverState.run(mouseOver, el);
		}
	else if(el.hasClass(navSelectedClassName)){
		el.removeClass(navSelectedClassName);
		hoverState.run(mouseOut, el);
		}
	}

window.addEvent('domready', function(){
	cache[0] = new Element('div', {html:$(contentId).get('html')});
	var navigation = $(mainNavigationId).getElements('.navigation');
	var main = $(contentId).store('currentPage', '');
	if(siteHidden)
		main.set('html', '').setStyle('visibility', 'visible');
	refreshLinks($(mainNavigationId),'.navigation', base, newBase);
	if(logoId)
		$(logoId).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 == '' || window.location.hash == '#/' || window.location.hash == '#'){
			swapContent(main.store('currentPage', window.location.hash), cache[0].get('html'));
			document.title = originalTitle;
			navigation.each(redoNavSelections);
			return;
			}
		var pageGet = convertToLink(window.location.hash.replace('#', ''));
		if(window.location.hash.length < 3 || titleCache[pageGet] == false || $type(titleCache[pageGet]) == false){
			document.title = baseTitle;
			}
		else{
			document.title = baseTitle+' - '+titleCache[pageGet];
			}
		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.JSON({noCache:true, 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(responseJSON){
					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.*/
						bannerCache[pageGet] = responseJSON['banner'];
						sidebarCache[pageGet] = responseJSON['sidebar'];
						cache[pageGet].set('html', responseJSON['content']).store('fetching', false);
						if($type(responseJSON['title']) != false)
							titleCache[pageGet] = responseJSON['title'];
						else{
							
							var rep = pageGet.split('/');
							if(rep[2] == '')
								rep = rep[1]
							else
								rep = rep[2];
							if($type(rep) == false)
								titleCache[pageGet] = false;
							else
								titleCache[pageGet] = unescape(rep).capitalize();
							}
						if(pageGet != convertToLink(window.location.hash.replace('#', ''))){
							return;
							}
						if(titleCache[pageGet] != false && $type(titleCache[pageGet]) != false)
							document.title = baseTitle+' - '+titleCache[pageGet];
						else
							document.title = baseTitle;
							
						bannerSet(bannerCache[pageGet]);
						sidebarSet(sidebarCache[pageGet]);
						var toRefresh = swapContent(main.store('currentPage', window.location.hash), responseJSON['content']);
						evalCache[pageGet] = responseJSON['javascript'];
						eval(responseJSON['javascript']);
						$$('.contentBox').each(giveUnderlay);
						navigation.each(redoNavSelections);
						refreshLinks(toRefresh, '.navigation', base, newBase);
						//$('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(newBase+pageGet);
			}
		else if(container.get('html') != ''){
			swapContent(main.store('currentPage', window.location.hash), container.get('html'));
			$$('.contentBox').each(giveUnderlay);
			eval(evalCache[pageGet]);
			bannerSet(bannerCache[pageGet]);
			sidebarSet(sidebarCache[pageGet]);
			navigation.each(redoNavSelections);
			}
		}, 100);
	});
}