function grapWindowH() {
	if (self.innerHeight) return self.innerHeight; // all except Explorer
    if (document.documentElement && document.documentElement.clientHeight) return document.documentElement.clientHeight; // Explorer 6 Strict Mode
    if (document.body) return document.body.clientHeight;// other Explorers
    return false;
}


function initGallery() {
	//substring(indexOf(startIndicator)+length of startIndicator, indexOf(endIndicator, pos of startIndicator))
	var pageid = window.location.href.substring(window.location.href.indexOf('?pageid=')+8,window.location.href.indexOf('&',window.location.href.indexOf('?pageid=')));
	var url = "./xmlImages.php";
	var imageLoader = new Ajax.Request(url, {
		method: 'post', 
		parameters: 'pageid=' + pageid,
		onComplete: insertImages
	});
	$('imageOverlay').myFx = new fx.Opacity($('imageOverlay'));
	$('overlay').myFx = new fx.Opacity($('overlay'));
	$('imageOverlay').myFx.setOpacity(0);
	$('imageOverlay').myFx.now = 0;
	$('overlay').myFx.setOpacity(0);
	$('overlay').myFx.now = 0;
	$('overlay').style.height = grapWindowH() + 'px';
}

function showImages() {
	var sections = $A($('scrollingThumbs').childNodes)
	sections.each(function(section) {
		var imageContainers = $A(section.childNodes);
		imageContainers.each(function(imageC) {
			if (imageC.firstChild.myFx.now == 0) imageC.firstChild.myFx.toggle();
		});	
	});
}

function showInFull(id) {
	$('overlay').myFx.clearTimer();
	$('overlay').myFx.custom(0,0.8);
	$('fullImage').onload = function() {
		$('imageOverlay').myFx.toggle();	
	}
	$('fullImage').onclick = function() {
		$('overlay').myFx.clearTimer();
		$('overlay').myFx.custom(0.8,0);
		$('imageOverlay').myFx.clearTimer();
		$('imageOverlay').myFx.toggle();
	}
	$('fullImage').src = './galleryImage.php?type=full&id=' + id;
}

function changeImage(id) {
	$('mediumImage').myFx.options.onComplete = function() {
		$('mediumImage').onload = function() {
			$('mediumImage').myFx.toggle();	
		}
		$('mediumImage').src = './galleryImage.php?type=medium&id=' + id;
		$('mediumImage').myFx.options.onComplete = '';
	}
	$('mediumImage').myFx.clearTimer();
	$('mediumImage').myFx.toggle();
	$('mediumImage').onclick = function() { showInFull(id) };
}

fx.DivScroll = Class.create();
fx.DivScroll.prototype = Object.extend(new fx.Base(), {
	initialize: function(element, scrollElement, options) {
		this.element = element;
		this.scrollElement = scrollElement;
		this.setOptions(options);
		this.now = 0;
		this.maxScroll = parseInt(this.element.scrollWidth - this.element.offsetWidth);
	},
	
	setScroll: function(position) {
		this.now = position;
		this.to = position;
		this.increase();
	},
	
	scrollTo: function(position) {
		this.custom(this.now, position);		
	},
	
	scrollBy: function(amount) {
		this.custom(this.now, this.now+amount);		
	},

	increase: function() {
		this.now = Math.round(this.now);
		//if user try to scroll too far, reset to max scroll
		if(this.to > this.maxScroll) {
			this.to = this.maxScroll;
		}
		if (this.to < 0) {
			this.to = 0;
		}
		//scroll if within bounds
		if (this.to <= this.maxScroll && this.to >= 0) {
			this.scrollElement.style.left = (this.now*-1) + 'px';
		} else {
			this.clearTimer();
		}
	}
});

function insertImages(response) {
	if (response.responseXML.documentElement.getAttribute('success') != 'false') {
		images = $A(response.responseXML.documentElement.childNodes);
		var numSections = Math.floor ( (images.length / 3) + 0.7);
		$('scrollingThumbs').style.width = (numSections * 138) + 'px';
		var i = 1;
		var section = document.createElement('div');
		section.className = 'section';
		images.each( function(image) {
			if (i > 3) {
				$('scrollingThumbs').appendChild(section);
				i = 1;
				section = document.createElement('div');
				section.className = 'section';
			}
			var imageElm = document.createElement('img');
			imageElm.src = './galleryImage.php?type=thumb&id=' + image.getAttribute('id');
			imageElm.id = 'image-' + image.getAttribute('id');
			imageElm.onclick = function() {changeImage(image.getAttribute('id'))};
			imageElm.myFx = new fx.Opacity(imageElm);
			imageContainer = document.createElement('div');
			imageContainer.appendChild(imageElm);
			section.appendChild(imageContainer);
			imageElm.myFx.setOpacity(0);
			imageElm.myFx.now = 0;
			i++;
		});
		$('scrollingThumbs').appendChild(section);
	} else {
		alert('An error occurred: ' + response.responseXML.documentElement.firstChild.data);	
	}
	var fadeTimer = setTimeout(showImages,500);
	$('mediumImage').myFx =  new fx.Opacity($('mediumImage'));
	changeImage(images[0].getAttribute('id'));
	$('thumbContainer').myFx = new fx.DivScroll($('thumbContainer'),$('scrollingThumbs'));
}
