var photos        = new Array();

var galleryDiv    = null;
var galleryWidth  = 724;
var galleryHeight = 404;
var positions     = new Array(65, 165, 360, 555, 655);
var sizesWide     = new Array(new Array(263,188), new Array(298,213), new Array(350,250), new Array(298,213), new Array(263,188));
var sizesTall     = new Array(new Array(182,263), new Array(207,298), new Array(243,350), new Array(207,298), new Array(182,263));
var opacities     = new Array(.25,.5,1,.5,.25);
var currentPhoto  = 0;
var nextPhoto     = 0;

function loadXML () {
	fishing({method: "GET", url: "http://edwardlangmedia.com/photosXML", onready: createPhotoArray});
}

function createPhotoArray (request) {
	var data = request.responseXML;
	var urls = data.getElementsByTagName("url");
	for (var i=0; i<urls.length; i++) {
		var url = urls[i].getAttribute("url");
		url     = url.replace(/\\/g,"");
		photos.push(url);
	}
	loader.init(photos,loadingPhotos,setupPhotos);
	var left    = ($("#photoGallery").offsetWidth-300)/2;
	var loadBar = $("#photoGallery").create("div",{id: "loadingBarContainer"},dom.create("div",{id: "loadingBar"},null,{height: "18px", background: "#000"}),{position: "absolute", 'left': left+"px", top: "250px", width: "300px", height: "18px", padding: "1px", background: "#fff"});
}

function loadingPhotos (loaded, total) {
	var width    = 296/total;
	var newWidth = width*loaded;
	$("loadingBar").setStyles({width: newWidth+"px"});
}

function setupPhotos () {
	$("#loadingBarContainer").remove();
	galleryDiv    = $("#photoGallery");
	var sizeVaria = .85;
	animate.framesPerSec = 20;
	
	$("#previousPhotoBtn").onclick = previousPhotoGo;
	$("#nextPhotoBtn").onclick     = nextPhotoGo;
	
	for (var i=0; i<photos.length; i++) {
		this.newImage = galleryDiv.create("img",{src: photos[i], id: "photo"+(i+1), rel: (i+1)}, null, {zIndex: (photos.length-i), visibility: "hidden"});
		this.newImage.onload   = function () {
			if (this.offsetWidth > this.offsetHeight) {
				var next       = parseInt(this.getAttribute("rel"))+1;
				this.setAttribute("rel",next);
				
				if (this.id != "photo1") {
					var width  = sizesWide[next] ? sizesWide[next][0] : sizesWide[0][0];
					var height = sizesWide[next] ? sizesWide[next][1] : sizesWide[0][1];
					var margin = 75+(125-(height/2));
					var opac   = opacities[next] ? opacities[next] : opacities[0];
					sizeVaria -= .10;
				}
				else {
					var width  = sizesWide[next] ? sizesWide[next][0] : sizesWide[0][0];
					var height = sizesWide[next] ? sizesWide[next][1] : sizesWide[0][1];
					var margin = 40;
					var opac   = 1;
				}
				if (next > positions.length-1) {
					var left = positions[4]+200+"px";
				}
				else {
					var left = (positions[next]-(width/2))+"px";
				}
				this.setStyles({position: "absolute", "top": margin+"px", "left": left, "width": width+"px", opacity: opac, visibility: "visible"});
				dom.addAnimate(this);
			}
			else {
				var width      = 243;
				var next       = parseInt(this.getAttribute("rel"))+1;
				this.setAttribute("rel",next);
				if (next > positions.length-1) { var left = positions[4]+200+"px"; }
				else { var left = (positions[next]-(width/2))+"px"; }
				this.setStyles({position: "absolute", "left": left, height: "340px", visibility: "visible"});
			}
			nextPhoto++;
		}
	}
}

function nextPhotoGo () {
	if (currentPhoto+1 != photos.length) {
		galleryDiv     = $("#photoGallery");
		
		for (var i=0; i<photos.length; i++) {
			this.nextid  = "#photo"+(i+1);
			this.photo   = $(this.nextid);
			if (i < (currentPhoto+1)) {
				this.photo.style.zIndex = parseInt(this.photo.style.zIndex-1);
			}
			this.next    = parseInt(this.photo.getAttribute("rel"))-1;
			this.photo.setAttribute("rel",this.next);
			if (this.photo.offsetWidth > this.photo.offsetHeight) {
				this.topPos  = sizesWide[this.next] ? (400-sizesWide[this.next][1])/2 : (400-sizesWide[4][1])/2;
				this.leftPos = positions[this.next] ? positions[this.next]-((sizesWide[this.next][0])/2) : this.next > 0 ? 800 : -200;
				if (sizesWide[this.next+1] || sizesWide[this.next-1]) {
					this.photo.move(this.topPos, this.leftPos, 10);
					this.photo.scale(sizesWide[this.next] ? sizesWide[this.next][0] : sizesWide[4][0], sizesWide[this.next] ? sizesWide[this.next][1] : sizesWide[4][1], 10);
					this.photo.fade((opacities[this.next] ? opacities[this.next]*100 : 0),10);
				}
				else {
					this.photo.setOpacity(0);
				}
			}
			else {
				this.topPos  = positions[this.next] ? (400-sizesTall[this.next][1])/2 : (400-sizesTall[4][1])/2;
				this.leftPos = positions[this.next] ? positions[this.next]-((sizesTall[this.next][0])/2) : -200;
				if (sizesTall[this.next+1] || sizesTall[this.next-1]) {
					this.photo.move(this.topPos, this.leftPos, 10);
					this.photo.scale(sizesTall[this.next] ? sizesTall[this.next][0] : sizesTall[4][0], sizesTall[this.next] ? sizesTall[this.next][1] : sizesTall[4][1], 10);
					this.photo.fade((opacities[this.next] ? opacities[this.next]*100 : 0),10);
				}
				else {
					this.photo.setOpacity(0);
				}
			}
		}
		currentPhoto++;
		return true;
	}
	else {
		return false;
	}
}

function previousPhotoGo () {
	if (currentPhoto != 0) {
		galleryDiv     = $("#photoGallery");
		
		for (var i=0; i<photos.length; i++) {
			this.nextid  = "#photo"+(i+1);
			this.photo   = $(this.nextid);
			if (i <= (currentPhoto-1)) {
				this.photo.style.zIndex = parseInt(this.photo.style.zIndex)+1;
			}
			this.next    = parseInt(this.photo.getAttribute("rel"))+1;
			this.photo.setAttribute("rel",this.next);
			if (this.photo.offsetWidth > this.photo.offsetHeight) {
				this.topPos  = sizesWide[this.next] ? (400-sizesWide[this.next][1])/2 : (400-sizesWide[4][1])/2;
				this.leftPos = positions[this.next] ? positions[this.next]-((sizesWide[this.next][0])/2) : 820;
				if (sizesWide[this.next-1]) {
					this.photo.move(this.topPos, this.leftPos, 10);
					this.photo.scale(sizesWide[this.next] ? sizesWide[this.next][0] : sizesWide[4][0], sizesWide[this.next] ? sizesWide[this.next][1] : sizesWide[4][1], 10);
					this.photo.fade((opacities[this.next] ? opacities[this.next]*100 : 0),10);
				}
				else {
					this.photo.setOpacity(0);
				}
			}
			else {
				this.topPos  = positions[this.next] ? (400-sizesTall[this.next][1])/2 : (400-sizesTall[4][1])/2;
				this.leftPos = positions[this.next] ? positions[this.next]-((sizesTall[this.next][0])/2) : -200;
				if (sizesTall[this.next-1]) {
					this.photo.move(this.topPos, this.leftPos, 10);
					this.photo.scale(sizesTall[this.next] ? sizesTall[this.next][0] : sizesTall[4][0], sizesTall[this.next] ? sizesTall[this.next][1] : sizesTall[4][1], 10);
					this.photo.fade((opacities[this.next] ? opacities[this.next]*100 : 0),10);
				}
				else {
					this.photo.setOpacity(0);
				}
			}
		}
		currentPhoto--;
		return true;
	}
	else {
		return false;
	}
}

var loader = {
	finalAction : null,
	individualAction : null,
	checked : 0,
	init : function (array, indAction, doneAction) {
		this.finalAction = doneAction;
		this.individualAction = indAction;
		var newImg = new Array();
		for (var i=0; i<array.length; i++) {
			newImg[i] = dom.create("img",{rel: i, ref: array.length});
			newImg[i].onload = function () {
				loader.checkIn(this);
			}
			newImg[i].setAttribute("src",array[i]);
		}
	},
	checkIn : function (img) {
		this.checked++;
		if (this.checked == img.getAttribute("ref")) {
			this.finalAction();
		}
		else {
			if (this.individualAction) {
				this.individualAction(this.checked, img.getAttribute("ref"));
			}
		}
	}
};

//window.onload     = setupPhotos;
window.onload      = function () {
	loadXML();
};