var clickLock = false ; //prevents everything from getting hosed by doubleclickers.
var numberOfThumbs = 15;

function preload(images, dir) {
	for (var j=0; j < images.length; j++) {
		newImage = new Image();
		newImage.src = dir + images[j].filename;
		}

}

function sendAlert(url) { 
		_gaq.push(['_setAccount', 'UA-82050-1']);
		_gaq.push(['_trackPageview', url]);

		/*pageTracker._trackPageview(url); */
		/*
		if(window.console) {
			window.console.log(url);
		} 
		*/
}

function swapindex(index){
	var image=port.images[index]
	swap(image)
	}

function swap(index, url){
	if (!clickLock) {
		clickLock = true;
		fullResPath = "/webpix/";
		var mainImage = document.getElementById("largeImage");
		var container = mainImage.parentNode;
		var image=images[index];

		fadeaway(mainImage)
		var caption_element = document.getElementById("caption");
		var location_element = document.getElementById("location");
		caption_element.innerHTML = image.fields.caption;
		location_element.innerHTML = image.fields.location;
		
		
		var newImage = new Image();
		newImage.style.opacity = 0;
		newImage.style.filter = "alpha(opacity=0)";
	
		newImage.onload = function(){
			this.id = "largeImage";
			container.replaceChild(this, mainImage); 
			fadein(this); 
		}
		loadingGif = document.getElementById("loading");
		loadingGif.style.visibility = "visible";
		newImage.src = fullResPath + image.fields.web_image;
		document.title = "Photograph: " + image.fields.caption + " | " + image.fields.location;
		sendAlert(url+index+'/');

	}

	return 0 ;
}

function swapThumbs(el, firstThumb, url){
	thumbPath = "/webpix/";
	var container = document.getElementById("thumbnails");
	var selected = document.getElementById("selected");
	selected.id = null
	el.id = "selected"
	while (container.hasChildNodes()) { 
		container.removeChild(container.firstChild);
		}

	// stupid javascript scoping
 	var clickHandler = function(image, url){ 
 		return function(){ swap(image+firstThumb, url); return false; }
 		}
	// end stupid

	for (var index=0;index < numberOfThumbs;index++) {
		var newImage = new Image()
		var newElement;
		if ((index+firstThumb) < images.length) {
			imNum = index+firstThumb
			newImage.src = thumbPath + images[imNum].fields.thumbnail_image;
			newElement = document.createElement("a");
			newElement.href = 	urlbase  + imNum + "/";
			newElement.onclick = clickHandler(index, url);
			newElement.appendChild(newImage);

			captionInfo = document.createElement("span");
			captionInfo.innerHTML = "<b>" + images[index+firstThumb].fields.caption + "</b><br />" + images[imNum].fields.location;
			newElement.appendChild(captionInfo);

			container.appendChild(newElement);
			}
		else {
			newImage.src = "/webpix/graySquare.gif";
			container.appendChild(newImage)
			}
		
	}
	swap(firstThumb, url)

	return 0;
}
function fadeaway(image) {
	oOpacity = .99;
	function out() {
		oOpacity-=.05; 
		image.style.opacity = oOpacity;
		image.style.filter = "alpha(opacity=" + oOpacity*100 + ")";
		if(oOpacity<=0.0) {
			clearInterval(away_interval);
		}
	}
	away_interval = setInterval(out,20);
}

function fadein(image) {
	oOpacity = .0;

	function fadeback() {
		loadingGif = document.getElementById("loading");
		oOpacity+=.05; 
		loadingGif.style.opacity = 1-oOpacity;
		loadingGif.style.filter = "alpha(opacity=" + (1-oOpacity)*100 + ")";
		image.style.opacity = oOpacity;
		image.style.filter = "alpha(opacity=" + oOpacity*100 + ")";
		if(oOpacity>=.99) {
			clearInterval(in_interval);
			loadingGif.style.visibility = 'hidden';
			loadingGif.style.opacity = 1;
			loadingGif.style.filter = "alpha(opacity=100)";
			clickLock = false
		}	
	}
	in_interval = setInterval(fadeback,55);
}
