// Globals
var objGallery;
var listGalleryItems = new Array();

/*
	20090904 DWH opacity functionality adapted from:
		http://brainerror.net/scripts/javascript/blendtrans/
*/

function opacity(id, opacStart, opacEnd, millisec) {
	var step = 5;
	//speed for each frame
	var speed = Math.round(millisec / (100/step));
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if (opacStart > opacEnd) {
		for(var i = opacStart; i >= opacEnd; i -= (step*(timer+1))) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
		// to be sure we end up where we need to be
		setTimeout("changeOpac(" + opacEnd + ",'" + id + "')",(timer * speed));
	} else if(opacStart < opacEnd) {
		for(var i = opacStart; i <= opacEnd; i += (step*(timer+1))) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
		// to be sure we end up where we need to be
		setTimeout("changeOpac(" + opacEnd + ",'" + id + "')",(timer * speed));
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	if ( obj = listGalleryItems[id] ) {
		obj.style.opacity = (opacity / 100);
		obj.style.filter = "alpha(opacity=" + opacity + ")";
	}
}

function galleryGetItems() {
	var x = objGallery.getElementsByTagName('div');

	for ( var i = 0; i < x.length; i++ ) {
		if ( x[i].className == 'galleryitem' ) {
			listGalleryItems.push(x[i]);
		}
	}
}

function galleryChange(step) {
	var curritem = -1;
	for ( var i = 0; i < listGalleryItems.length; i++ ) {
		if ( listGalleryItems[i].style.display != 'none' ) {
			curritem = i;
		}
	}

	var nextitem = (curritem + step + listGalleryItems.length) % listGalleryItems.length;

	if ( curritem != -1 ) {
		opacity(curritem, 100, 0, 800);
		listGalleryItems[curritem].style.display = 'none';
	}
	listGalleryItems[nextitem].style.display = 'block';
	opacity(nextitem, 0, 100, 1000);
}

function galleryInit() {
	galleryGetItems();

	if ( document.getElementById('gallerynavprev') )
		document.getElementById('gallerynavprev').onclick = function () {
			galleryChange(-1);
			return true;
		}
	if ( document.getElementById('gallerynavnext') )
		document.getElementById('gallerynavnext').onclick = function () {
			galleryChange(1);
			return true;
		}
	if ( document.getElementById('galleryloading') )
		document.getElementById('galleryloading').style.display = 'none';

	galleryChange(1);
}

function pageOnLoad() {
	objGallery = document.getElementById('gallery');

	if ( !objGallery ) {
		objGallery = document.getElementById('gallerysimple');
	}

	if ( objGallery ) {
		galleryInit();
	}
}
