var galhiobj;
var galloobj;
var gallerybusy;
var afterloaded;
var beforeloaded;

function galleryFade() {
	if (gallerybusy)
		return;

	gallerybusy = 1;
	afterloaded = 0;
	beforeloaded = 0;

	galhiobj = document.getElementById('highphoto');
	galloobj = document.getElementById('lowphoto');

	setOpacity(galhiobj, 0);
	setOpacity(galloobj, 20);

	for (var i=0;i<21;i++) {
		setTimeout('setOpacity(galhiobj, '+i+')', ((100 * i) + 750));
	}

	setTimeout('clearGalleryBusy()', ((100 * i) + 750));
}

function clearGalleryBusy() {
	gallerybusy = 0;
}

function checkPicLoad(pic) {
	if (pic == 'after') {
		afterloaded = 1;
	} else if (pic == 'before') {
		beforeloaded = 1;
	}
	
	if (afterloaded && beforeloaded) {
		galleryFade();
	} 
}

function doGallery (code) {
	if (gallerybusy)
		return;
	makeRequest('gallery/gallery' + code + '.html', 'photo');
}

function doGalleryBody (page) {
	if (gallerybusy)
		return;
	makeRequest(page + '.html', 'body');
}

function makeRequest(url, type) {
	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Trouble with XMLHTTP instance');
		return false;
	}

	http_request.onreadystatechange = function() { changeContents(http_request, type); };
	http_request.open('GET', url, true);
	http_request.send(null);
}

function changeContents(http_request, type) {
	try {
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				if (type == 'body') {
					document.getElementById('gallerybody').innerHTML = http_request.responseText;
					initMover();
				} else {
					document.getElementById('galleryblock').innerHTML = http_request.responseText;
					
					var afterpic = document.getElementById('highphoto');
					var beforepic = document.getElementById('lowphoto');
					setOpacity(afterpic, 0);
					setOpacity(beforepic, 0);
					afterpic.onload = function() { checkPicLoad('after'); };
					beforepic.onload = function() { checkPicLoad('before'); };
				}
			} else {
				alert(http_request.status + ': Sorry, there was a problem with your request. Please try again.');
			}
		}
	}
	catch( e ) {
		alert('Caught Exception: ' + e.description);
	}
}

var tnboxobj, tnpicobj;
var scrollspeed = 40;
var loaded = 0;
var loop = 0;

function createObject (elem) {
	if (document.getElementById) {
		this.element = document.getElementById(elem);
		this.styleElem = this.element.style;
		this.elemwidth = this.element.offsetWidth;
	} else {
		return false;
	}

	this.scrollthis = scrollThis;
	this.scrollleft = scrollLeft;
	this.scrollright = scrollRight;
	this.scrollit = scrollIt;
	return this;
}

function scrollThis (xpos, ypos) {
	this.x=xpos;
	this.y=ypos;

	this.styleElem.left = xpos+'px';
	this.styleElem.top = ypos+'px';
}

function scrollLeft (incr) {
	minwidth = this.elemwidth - tnboxobj.elemwidth;

	if (this.x > -minwidth) {
		this.scrollthis (this.x-incr, 0);
	} else {
		clearInterval(this.intervalID);
		loop = 0;
	}
}

function scrollRight (incr) {
	if (this.x < 0) {
		this.scrollthis (this.x-incr, 0);
	} else {
		clearInterval(this.intervalID);
		loop = 0;
	}
}

function scrollIt (incr) {
	if ((loaded == 1) && (loop == 0)) {
		loop = 1;

		if (incr > 0) {
			tnpicobj.intervalID=setInterval("tnpicobj.scrollleft ("+incr+")", scrollspeed);
		} else {
			tnpicobj.intervalID=setInterval("tnpicobj.scrollright ("+incr+")", scrollspeed);
		}
	}
}

function initMover() {
	tnboxobj = new createObject ('gallerytnbox');
	tnpicobj = new createObject ('gallerytnpics');

	tnpicobj.scrollthis (0, 0);

	loaded = 1;
}

function startScroll (dir) {
	if (dir == 'left') {
		scrollIt (-10);
	} else {
		scrollIt (10);
	}
}

function stopScroll () {
	clearInterval(tnpicobj.intervalID);
	loop = 0;
}

