firstImage = 0;
loaded = new Array();
loaded[firstImage] = false;

var galleries = {
	active : 0,
	galCount : 0,
	all : new Array(),
	registerGallery : function( gal ) {
		gal.numImages = 0;
		gal.currentPhoto = 0;
		$c(gal.images).each(function() {
			gal.numImages++;
		});
		this.all[this.galCount] = gal;
		
		this.galCount++;
	},
	get : function(){
		return this.all[this.active];
	},
	set : function(id){
		this.active = id;
	},
	init : function() {
		b = window.location.toString();
		if(b.substring('?')) {
			urlfrag = b.substr(b.indexOf('?') + 1);
			
			$c(this.all).each(function(g,i){
				if(g.url == urlfrag)
					galleries.set(i);
			});
		}
	}
}

function addEvent(obj, evType, fn){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false; 
	} 
}
function addLoader(func) {
	return addEvent(window, 'load', func);
}
function $pn(type, id, child) {
	el = document.createElement(type);
	if(id)
		el.id = id;
	if(child)
		el.appendChild(child);
	return el;
}
function $pt(v) { return document.createTextNode(v) };

function addToBody(node) {
	document.getElementsByTagName('body')[0].appendChild(node);
}


function nextPhoto() {
	changeTo(galleries.get().currentPhoto + 1);
}
function prevPhoto() {
	changeTo(galleries.get().currentPhoto - 1);
}

function changeTo(photoIndex) {
	gallery = galleries.get();
	old = $('myImage');
	z = new fx.Opacity('myImage', {duration: 500, onComplete: function(){
		old.id = 'myImageOld';
		Element.remove(old);
		newImage = $pn('img', 'myImage');
		newImage.className = 'centeredImage';
		newImage.src = gallery.images[photoIndex].url;
		newImage.style.opacity = 0;

		newImage.onload = function() {
			newImage.style.display = '';
			newImage.style.marginTop = "-" + ((newImage.offsetHeight + $('footer').offsetHeight) / 2) + "px";
			newImage.style.marginLeft = "-" + (newImage.width / 2) + "px";
			newImage.style.top = '50%';
			newImage.style.left = '50%';
			
			b = new fx.Opacity(newImage, {duration: 500 });
			b.now = 0;
			b.toggle();
			$('thisPhotoNum').firstChild.nodeValue = photoIndex + 1;	
			galleries.get().currentPhoto = photoIndex;
			
			if((photoIndex + 1) == gallery.numImages) {
				Element.addClassName($('nextLink'), 'disabled');
			} else {
				Element.removeClassName($('nextLink'), 'disabled');		
			}
			
			if(photoIndex == 0) {
				Element.addClassName($('prevLink'), 'disabled');
			} else {
				Element.removeClassName($('prevLink'), 'disabled');				
			}
		}	

		addToBody(newImage);
				
	}});
	z.toggle();
}

function haveFinishedLoading() {
	b = true;
	$c(loaded).each(function(a) {
			if(a != true)
				b = false;
		});
	if (b) {
			Element.remove('loading');
			initThisGallery();
			changeTo(0);
	} else {
		alert("not done loading yet");
	}
}

function initPage() {
	
	galleries.init();

	addToBody($pn('div', 'footer', $pt(' ')));
	addToBody($pn('div', 'logo', $pt(' ')));
	addToBody($pn('div', 'copyright', $pt(' ')));
	
	
	
	about = $pn('a', 'aboutLink', $pt('about'));
	about.href = '#';
	about.onclick = function() { 
		this.blur(); 
		mb = $pn('div', 'modalBackground');
		addToBody(mb);
		
		ab = $pn('div', 'aboutWindow');
		ab.appendChild($pn('h2', false, $pt(galleries.get().title)));
		ab.appendChild($pn('div', false, $pt(galleries.get().about)));
		addToBody(ab);
		
		ab.style.marginTop = ab.offsetHeight / 2;
		
		mb.onclick = function() {
			Element.remove(ab);
			Element.remove(mb);
		}
		
		return false;	
	}
	aboutDiv = $pn('div', 'about', about);
	aboutDiv.appendChild($pt(' |  '));
	more = $pn('a', 'moreLink', $pt('more photos'));
	more.href = '#';
	more.onclick = function() { this.blur(); $('gallerySelector').flex.toggle(); return false;}
	
	aboutDiv.appendChild(more);
	addToBody(aboutDiv);
	
	
	next = $pn('a', 'nextLink', $pn('span'));
	next.href = "#";
	next.className = 'disabled';
	next.onclick = function() { this.blur(); nextPhoto(); return false; }
	addToBody(next);
	
	prev = $pn('a', 'prevLink', $pn('span'));
	prev.href = "#";
	prev.className = 'disabled';
	prev.onclick = function() { this.blur(); prevPhoto(); return false; }
	addToBody(prev);
	
	valin = $pn('a', 'pager', $pn('span', 'thisPhotoNum', $pt('0')));
	valin.appendChild($pt(' / '));
	valin.appendChild($pn('span', 'totalPhotos', $pt('0')));
	addToBody(valin);
	
	ul = $pn('ul', 'gallerySelector');
	
	$c(galleries.all).each(function(g, i){
		myLiA = $pn('a', false, $pt(g.title));
		myLiA.href = "#" + i;
		myLiA.onclick = function(){ this.blur(); changeGalleryTo(i); return false; }
		ul.appendChild($pn('li', 'selector' + i, myLiA));
	});
	
	ul.flex = new fx.Height(ul, { duration: 500 });
	
	addToBody(ul);
}

function changeGalleryTo(i) {
	$('gallerySelector').flex.toggle();
	galleries.set(i);
	initThisGallery();
	changeTo(0);
}

function initThisGallery() {
	v = galleries.get();
	
	logo = $('logo');
	if(logo.firstChild)
		Element.remove(logo.firstChild);
	logo.appendChild($pt(v.title));
	
	
	copyright = $('copyright');
	if(copyright.firstChild)
		Element.remove(copyright.firstChild);
	copyright.appendChild($pt(v.copyright));
	
	
	totalPhotos = $('totalPhotos');
	if(totalPhotos.firstChild)
		Element.remove(totalPhotos.firstChild);
	totalPhotos.appendChild($pt(v.numImages));

}

// boot script

if(!addLoader(function(){ return true; })) {
	// we don't have addLoader() support. die.
	window.location = 'error.html';
}

if(!document.getElementById) {
	window.location = 'error.html';
}


addLoader(function() { 
	
	initPage();
	//setup the html
	
	first = $pn('img', 'myImage');
	first.className = 'centeredImage';
	first.src = "http://is.patrick.geek.nz/macnz/global/macnz-header.png";
	first.style.visibility = 'hidden';
	first.style.opacity  = 0;
	addToBody(first);
	first.onload = function() {
		first.style.display = '';
		first.style.top = '50%';
		first.style.left = '50%';
		first.style.marginTop = "-" + ((first.offsetHeight + $('footer').offsetHeight) / 2) + "px";
		first.style.marginLeft = "-" + (first.width / 2) + "px";
		loaded[firstImage] = true;
		haveFinishedLoading();
	}
	
});

