var interval;
var galleryNum = 4;
var galleryNumber;
var imageNum = 0;
var imgSrc;
var img;
var photoNums = [];
var photoCache = [];
var allowAnimation = true;
var galleries = [];
var dimensions;
var centerHeight;
var centerWidth;
var browser;
var picHolder;
var loader = document.createElement('img');
loader.src = "images/loader.gif";
loader.width = 48;
loader.height = 48;
loader.className = "loader";
loader.id = "loader";
$(document).ready(function(){
	window.dimensions = windowdimensions();
	window.centerHeight = dimensions[1] / 2 - 250;
	window.centerWidth = dimensions[0] / 2 - 250;
	document.getElementById('picHolder').appendChild(window.loader);
	window.browser=navigator.appName;
	window.picHolder = document.getElementById('picHolder');
	swfobject.addLoadEvent(forceRedraw);
	function forceRedraw() {
		if(document.getElementById('flashContent'))
		document.getElementById("flashContent").style.display = "block";
	}
});

var xmlhttp = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
	xmlhttp = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			alert('error loading AJAX');
		}
	}
}
if (!xmlhttp) {
	alert('error loading AJAX');
}
function nextImage(galNum,fromError) {
	//alert("working");
	if(!fromError) var fromError = false;
	$(window.img).fadeOut(50,function(){
		if(fromError == false) {
			window.picHolder.replaceChild(window.loader,window.img);
			window.loader.style.marginTop = window.img.height/2-24;
			window.loader.style.marginLeft = window.img.width/2-24;
		}
		if(window.imageNum != window.photoNums[galNum]-1) {
			window.imageNum++;
			getImage(galNum,window.imageNum);
		} else {
			window.imageNum = 0;
			getImage(galNum,window.imageNum);
		}
	});
}
function prevImage(galNum) {
	if(window.imageNum != 1-1) {
		$(window.img).fadeOut(50,function(){
			window.picHolder.replaceChild(window.loader,window.img);
			window.loader.style.marginTop = window.img.height/2-24;
			window.loader.style.marginLeft = window.img.width/2-24;
			window.imageNum--;
			getImage(galNum,window.imageNum);
		});
	}
}
function getImage(galNum,imgNum) {
	xmlhttp.open("GET", "getImage.php?galNum="+galNum+"&imgNum="+imgNum, true);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4) {
			window.imgSrc = xmlhttp.responseText;
			//alert(xmlhttp.responseText);
			setImageDimensions(true);
		}
	}
	xmlhttp.send(null);
	//window.
}
function setImageDimensions(allowAnimation) {
	window.img = new Image();
	window.img.onerror = function() {
		alert("There was an error loading this image.");
		nextImage(window.galleryNumber,true);
		allowAnimation = false;
	}
	window.img.onload = function() {
		//alert("loaded");
		window.picHolder.replaceChild(window.img,window.loader);
		if(window.img.width > window.img.height) {
			window.img.height = 480 * window.img.height / window.img.width;
			window.img.width = 480;
		} else if(window.img.width < window.img.height) {
			window.img.width = 480 * window.img.width / window.img.height;
			window.img.height = 480;
		}
		if(allowAnimation == true) {
			window.centerHeight = window.dimensions[1] / 2 - window.img.height / 2;
			window.centerWidth = window.dimensions[0] / 2 - window.img.width / 2;
			$("#viewer").animate({
				width: window.img.width+20,
				height: window.img.height+70,
				top: window.centerHeight+getScrollTop(),
				left: window.centerWidth
			},500,function(){
				$(window.img).fadeIn(200);
			});
			$("#photoControls").animate({
				width: window.img.width+20
			},500);
		}
	}
	window.img.src = window.imgSrc;
	window.img.style.display = "none";
	window.img.id = "image";
}
function openViewer(galNum,imgNum) {
	getImage(galNum,imgNum);
	var blackout = document.getElementById('blackout');
	var viewer = document.getElementById('viewer');
	//alert(window.centerHeight+getScrollTop());
	blackout.style.top = getScrollTop()+"px";
	//alert(getScrollTop());
	viewer.style.top = window.centerHeight+getScrollTop()+"px";
	viewer.style.left = window.centerWidth+"px";
	$(blackout).slideDown(500, function() {
		$(viewer).fadeIn(500);
	});
	window.galleryNumber = galNum;
	window.interval = setInterval("centerViewer()",100);
}
function closeViewer() {
	var blackout = document.getElementById('blackout');
	var viewer = document.getElementById('viewer');
	window.imageNum = 0;
	$(viewer).fadeOut(500,function(){
		$(blackout).slideUp(500,function(){
			if(document.getElementById('image')) {
				window.picHolder.replaceChild(window.loader,window.img);
				window.loader.style.marginTop = window.img.height/2-24;
				window.loader.style.marginLeft = window.img.width/2-24;
			}
		});
	});
	clearInterval(window.interval);
}
function windowdimensions() {
	if(window.innerWidth !==undefined) {
		var a= [window.innerWidth,window.innerHeight];	
	} else {
		var d= (document.documentElement.clientWidth) ? document.documentElement : document.body;
		var a = [d.clientWidth,d.clientHeight];
	}
	return a;
}
function getScrollTop() {
	//alert(navigator.appName);
	switch(navigator.appName)
	{
	  case 'Microsoft Internet Explorer':
	    //var myregex = /MSIE 7\.0/i;
	    //var myArray = navigator.appVersion.match(myregex);
	    /*if(myArray)*/ return document.documentElement.scrollTop;
	    //else return document.body.scrollTop;
	  	break;
	  case "Opera":
	  	return document.documentElement.scrollTop;
		break;
	  case "Netscape":
	  	return document.documentElement.scrollTop;
		break;
	  default:
	    return document.body.scrollTop;
	}
}
function centerViewer() {
    var blackout = document.getElementById('blackout');
	var viewer = document.getElementById('viewer');
	blackout.style.top = getScrollTop()+"px"
	viewer.style.top = window.centerHeight+getScrollTop()+"px";
	viewer.style.left = window.centerWidth+"px";
}
function getNextGalleries() {
	window.photoCache = [];
	xmlhttp.open("GET", "getGalleries.php?galNum="+window.galleryNum, true);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4) {
			document.getElementById('wrap').innerHTML = xmlhttp.responseText;
			window.galleryNum+=4;
		}
	}
	xmlhttp.send(null);
}
