var pageInUse = '';
var pageShown = '';
var portF = new Image();
var navLinks;
var mVars = new Array();
var qVars = new Array();
var firstRun = 1;
var beforeAboutAlbum = '';
var beforeAboutId = '';
if(window.location.href.contains('?')){
	var mainQuery = window.location.href.split('#')[0].split('?')[1].split('&');
	mainQuery.each(function(e){
		var split = e.split('=');
		mVars[split[0]] = split[1];
		});
	pageShown = mVars['s'];
	}
window.addEvent('domready',function(){
	$('wrapContent').getElements('.resetLinks').addEvent('click',function(e){
		new Event(e).stop();
		window.location = '#'+this.href.split('?')[1];
		});
	$('wrapNav').getElements('a[href^=index.php]').addEvent('click',function(e){
		new Event(e).stop();
		firstRun = 0;
		var redirectToString = window.location.href.split('#')[0]+'#';
		if(!this.href.contains('s=about')){
			if(pageShown == 'about' && beforeAboutAlbum != '')
				redirectToString += 'album='+beforeAboutAlbum;
			else if(qVars['album'])
				redirectToString += 'album='+qVars['album'];
			if(qVars['album'] && qVars['id'])
				redirectToString += '&';
			if(pageShown == 'about' && beforeAboutId != '')
				redirectToString += 'id='+beforeAboutId;
			else if(qVars['id'])
				redirectToString += 'id='+qVars['id'];
			}
		else{
			beforeAboutAlbum = albumInUse;
			beforeAboutId = qVars['id'];
			redirectToString += 'id=1';
			}
		if(this.href.contains('?s=') && this.href.split('s=')[1] != qVars['s'])
			window.location = redirectToString+'&'+this.href.split('?')[1];
		else{
			window.location = redirectToString;
			if(pageShown != '' && pageShown != 'undefined')
				new Fx.Tween('use_'+pageShown,{duration:400}).start('opacity',0);
			nav_highlight('home');
			}
		});
	navLinks = $('wrapNav').getElements('a');
	if(!window.location.href.contains('s='))
		navLinks.getLast().className = 'atThis';
	$('portA').set('tween',{duration:300});
	$('portB').set('tween',{duration:300});
	});
var checkHash = window.setInterval(function(){
	if(window.location.hash != '#' && window.location.hash != ''){
		firstRun = 0;
		clearInterval(checkHash);
	}},200);
function checkVars(){
	var query = window.location.hash.replace('#','').split('&');
	qVars['s'] = '';
	qVars['album'] = '';
	qVars['id'] = '';
	query.each(function(e){
		var split = e.split('=');
		qVars[split[0]] = split[1];
		});
	if(!qVars['album']){
		if(mVars['album'])
			qVars['album'] = mVars['album'];
		else
			qVars['album'] = 'Front Page';
		}
	if(!qVars['id']){
		if(mVars['id'])
			qVars['id'] = mVars['id'];
		else
			qVars['id'] = 1;
		}
	if(!window.location.hash.contains('s=') && pageShown != '' && pageShown && firstRun != 1){
		new Fx.Tween('use_'+pageShown,{duration:400}).start('opacity',0);
		navHighlight('home');
		pageShown = '';
		}
	else if(window.location.hash.contains('s=') && qVars['s'] != pageInUse){
		showPage(qVars['s']);
		}
	qVars['album'] = escape(unescape(qVars['album']));
	if(window.location.hash.contains('album=') && qVars['album'] != '' && qVars['album'] != albumInUse){
		var query = 'includes/album.php?album='+qVars['album'];
		if(qVars['id'])
			query += '&id='+qVars['id'];
		new Request.HTML({evalScripts:true,onSuccess:function(tree,elements,html,js){
			var preSrc = '';
			if($('portA').style.opacity == '1')
				preSrc = $('portA').src;
			else if($('portB').style.opacity == '1')
				preSrc = $('portB').src;
			var prelim = new Element('div',{id:'boxPhotos2',html:html});
			prelim.replaces('boxPhotos');
			prelim.set('id','boxPhotos');
			$('portA').setStyle('opacity',1);
			$('portB').setStyle('opacity',0);
			if(preSrc != ''){
			$('portB').set('src',$('portA').src);
			$('portA').set('src',preSrc);
			
			var toChange = window.setInterval(function(){
				if($('portB').complete){
					$('portA').tween('opacity',0);
					$('portB').tween('opacity',1);
					if(toUse == 1)
						toUse = 0;
					else
						toUse = 1;
					clearInterval(toChange);
					}
				},150);
			}
			$('previousButton').addEvent('click',function(e){
				new Event(e).stop();
				window.location = window.location.href.split('#')[0]+'#'+this.href.split('?')[1];
				});
			$('nextButton').addEvent('click',function(e){
				new Event(e).stop();
				window.location = window.location.href.split('#')[0]+'#'+this.href.split('?')[1]
				});
			}}).get(query);
		albumInUse = qVars['album'];
		}
	else if(window.location.hash.contains('id=') && qVars['id'] - 1 != inUse){
		if(qVars['id'] - 2 == -1)
			inUse = images.length - 1;
		else if(qVars['id'] - 2 == -2)
			inUse = images.length - 2;
		else
			inUse = qVars['id'] - 2;
		changeImg();
		}
	}
function showPage(page){
	if($('use_'+pageShown) && pageShown != page && typeof(page) == 'string'){
		new Fx.Tween('use_'+pageShown,{duration:400}).start('opacity',0);
		}
	if(pageShown != page && typeof(page) == 'string'){
		navHighlight(page);
		new Request.HTML({onSuccess:function(tree,elements,html,js){
		if($('use_'+page))
			$('wrapContent').removeChild($('use_'+page));
		var z = new Element('div');
		z.innerHTML = html;
		$('wrapContent').grab(z.firstChild);
		$('wrapContent').getElements('.resetLinks').addEvent('click',function(e){
			new Event(e).stop();
			window.location = '#'+this.href.replace('&','&').split('?')[1];
			});
		$('use_'+page).setStyle('opacity',0);
		new Fx.Tween('use_'+page,{duration:400,link:'chain'}).start('opacity',1);
			}}).get('includes/'+page+'.php');
		pageShown = page;
		}
	}
function navHighlight(useMe){
	navLinks.each(function(x){
		if(x.href.contains(useMe))
			x.className = 'atThis';
		else
			x.className = '';
		});
	if(useMe == 'home')
		navLinks[6].className = 'atThis';
	//var split = this.href.split('#');
	//showPage(split[1]);
	}
function changeImg(dir){
	if(toUse == 1){
		var view = $('portB');
		var load = $('portA');
		toUse = 0;
		}
	else{
		var view = $('portA');
		var load = $('portB');
		toUse = 1;
		}
	if(dir == 1){
		if(inUse - 1 == -1)
			inUse = images.length - 1;
		else
			inUse--;
		}
	else if(dir == 2){
		inUse = 0;
		}
	else{
		if(inUse + 1 == images.length)
			inUse = 0;
		else
			inUse++;
		}
	var loadId = load.get('id');
	load.destroy();
	load = new Element('img',{'id':loadId,'src':images[inUse],'style':{'opacity':0,'visibility':'hidden'}}).inject(view.id,'before');
	load.set('tween',{duration:300});
	var checkLoaded = window.setInterval(function(){
		if(load.complete){
			clearInterval(checkLoaded);
			view.tween('opacity',0);
			load.tween('opacity',1);
			$('portC').setStyle('visibility','hidden');
			}
		else{
			$('portC').setStyle('visibility','visible')
			}
		},200);
	if(inUse+1 < images.length)
		portF.src = images[inUse+1];
	
	if(inUse == 0)
		$('previousButton').href = '?album='+albumInUse+'&id='+images.length;
	else
		$('previousButton').href = '?album='+albumInUse+'&id='+inUse;
	if(inUse + 1 >= images.length)
		$('nextButton').href = '?album='+albumInUse+'&id=1';
	else
		$('nextButton').href = '?album='+albumInUse+'&id='+(inUse + 2);
	}
$('previousButton').addEvent('click',function(e){
	new Event(e).stop();
	window.location = window.location.href.split('#')[0].replace('&','&')+'#'+this.href.split('?')[1].replace('&','&');
	});
$('nextButton').addEvent('click',function(e){
	new Event(e).stop();
	window.location = window.location.href.split('#')[0].replace('&','&')+'#'+this.href.split('?')[1].replace('&','&');
	});
var checkInt = window.setInterval(checkVars,200);