if (!navigator.userAgent.match("MSIE")) window.onDomReady = DomReady;
function DomReady(fn){
	if(document.addEventListener){
		document.addEventListener("DOMContentLoaded", fn, false);
	}else{
		document.onreadystatechange = function(){readyState(fn)}
	}
}
function readyState(fn){
	if (document.readyState == "interactive") fn();
}
if (!navigator.userAgent.match("MSIE")){
	window.onDomReady(init);
}else{
	window.onload = init;
}

/////////////////////////////////////////////////////////////////////

function init(){
	gallery(document.getElementById('gallery'));
}

function gallery(galleryObj){

	this.buttonMouseOver = function (){
		this.className = this.className+"H";
	}

	this.buttonMouseOut = function (){
		this.className = this.className.replace('H','');
	}

	this.barMouseOver = function (){
		clearTimeout(barTimeout);
		fadeBarIn(Number(galleryBarObj.style.opacity));
	}

	this.barMouseOut = function (){
		clearTimeout(barTimeout);
		//barTimeout = setTimeout("this.fadeBarOut("+Number(galleryBarObj.style.opacity)+")",800);
		barTimeout = setTimeout(function(){fadeBarOut(Number(galleryBarObj.style.opacity))},800);
	}

	this.fadeBarOut = function (opacity){
		setOpacity(opacity,galleryBarObj);
		if (opacity > 0.4) {
			clearTimeout(barTimeout);
			//barTimeout = setTimeout("this.fadeBarOut("+(opacity-0.01)+")",15);
			barTimeout = setTimeout(function(){fadeBarOut(opacity-0.01)},15);
		}else{
			setOpacity(0.4,galleryBarObj);
		}
	}

	this.fadeBarIn = function (opacity){
		setOpacity(opacity,galleryBarObj);
		if (opacity < 0.65) {
			clearTimeout(barTimeout);
			//barTimeout = setTimeout("this.fadeBarIn("+(opacity+0.01)+")",15);
			barTimeout = setTimeout(function(){fadeBarIn(opacity+0.01)},15);
		}else{
			setOpacity(0.65,galleryBarObj);
		}
	}

	this.previousImage = function (){
		if (!changing){
			image1Obj.style.visibility = "hidden";
			currentImage = (currentImage == 0) ? images.length-1 : currentImage-1;
			image1Obj.src = images[currentImage];
			//loadingImageObj.style.visibility = 'visible';
		}
	}

	this.nextImage = function (){
		if (!changing){
			image1Obj.style.visibility = "hidden";
			currentImage = (currentImage == images.length-1) ? 0 : currentImage+1;
			image1Obj.src = images[currentImage];
			//loadingImageObj.style.visibility = 'visible';
		}
	}

	this.pauseImage = function (){
		if (!changing){
			clearTimeout(timeout);
			titleObj.innerHTML = "Paused.";
		}
	}

	this.loaded = function (){
		galleryObj.style.width = image1Obj.width+"px";
		galleryObj.style.height = image1Obj.height+"px";
		changing = true;
		image1Obj.style.visibility = "visible";
		titleObj.innerHTML = "<strong>" + titles[currentImage] + "</strong> (" + (currentImage+1) + "/" + images.length + ")";
		fadeInImage(0);
	}

	this.fadeInImage = function (opacity){
		setOpacity(opacity,image1Obj);
		//setOpacity(1-opacity,loadingImageObj);
		if (opacity < 1) {
			clearTimeout(timeout);
			//timeout = setTimeout("fadeInImage("+(opacity+0.01)+")",15);
			timeout = setTimeout(function(){fadeInImage(opacity+0.01);},15);
		}else{
			setOpacity(1,image1Obj);
            if (navigator.userAgent.match("MSIE")) return;
			galleryImageObj.style.background = "url("+image1Obj.src+") no-repeat";
			//loadingImageObj.style.visibility = 'hidden';
			changing = false;
			clearTimeout(timeout);
			//timeout = setTimeout("nextImage()",5000);
			timeout = setTimeout(function(){nextImage()},5000);
		}
	}

	this.setOpacity = function (opacity, obj){
		obj.style.KHTMLOpacity = opacity;
		obj.style.MozOpacity = opacity;
		obj.style.opacity = opacity;
		opacity *= 100;
		opacity = (opacity == 100)?99.999:opacity;
		obj.style.filter = "alpha(opacity:"+opacity+")";
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////

	var statusObj, galleryBarObj, galleryImageObj, loadingImageObj, titleObj;
	var butNextObj, butPauseObj, butPrevObj;
	var timeout, barTimeout, changing = false;
	var currentImage = 0;

	/* hard coded for now because the list parser is a bit broke */
	var images = new Array();
	images[0] = './images/gallery/adr_banner.jpg';
	images[1] = './images/gallery/gallery1.jpg';
	images[2] = './images/gallery/gallery2.jpg';
    images[3] = './images/gallery/gallery3.jpg';
    images[4] = './images/gallery/gallery4.jpg';
    images[5] = './images/gallery/gallery5.jpg';
    images[6] = './images/gallery/gallery6.jpg';

	var titles = new Array();
    titles[0] = '';
	titles[1] = 'Adrenaline';
	titles[2] = 'Guns. Lots of guns';
	titles[3] = 'Locked and loaded';
	titles[4] = 'Team party';
	titles[5] = "Who's there?";
    titles[6] = 'Run like the wind';

	statusObj = document.createElement('div');
	titleObj = document.createElement('div');
	titleObj.className = 'galleryTitle';
	galleryBarObj = document.createElement('div');
	galleryBarObj.className = 'galleryBar';
	galleryImageObj = document.createElement('div');
	galleryImageObj.className = 'galleryImage';
	
	galleryBarObj.onmouseover = barMouseOver;
	galleryBarObj.onmouseout = barMouseOut;

	// parse the OL or LI that has the images defined
	/*
	for (var i=0;i<galleryObj.childNodes.length;i++) {
		if (galleryObj.childNodes[i].tagName == "OL" || galleryObj.childNodes[i].tagName == "UL"){
			imageList = galleryObj.childNodes[i];
			
			for (var j=0;j<imageList.childNodes.length;j++) {
				if (imageList.childNodes[j].tagName == "LI"){
					var node = imageList.childNodes[j];
					var url = node.childNodes[0].href;
					var title = node.childNodes[0].innerHTML;
					images[j] = url;
					titles[j] = title;
					alert(images[j] + "  " + titles[j]);
				}
			}
			
			imageList.parentNode.removeChild(imageList);
			
			break;
		}
	}
	*/
	// remove the list
	//galleryObj.removeChild(galleryObj.childNodes[1]);

    /*
	for (var i=0;i<galleryObj.childNodes.length;i++) {
		if (galleryObj.childNodes[i].tagName == "OL" || galleryObj.childNodes[i].tagName == "UL"){
			galleryObj.removeChild(galleryObj.childNodes[i]);
		}
	}
    */

	/*
	butNextObj = document.createElement('div'); //document.getElementById('butNext');
	butNextObj.className = 'galleryNext';
	butPauseObj = document.createElement('div'); //document.getElementById('butPause');
	butPauseObj.className = 'galleryPause';
	butPrevObj = document.createElement('div'); //document.getElementById('butPrev');
	butPrevObj.className = 'galleryPrevious';

	butNextObj.onclick = nextImage;
	butPauseObj.onclick = pauseImage;
	butPrevObj.onclick = previousImage;

	butNextObj.onmouseover = buttonMouseOver;
	butPauseObj.onmouseover = buttonMouseOver;
	butPrevObj.onmouseover = buttonMouseOver;
	butNextObj.onmouseout = buttonMouseOut;
	butPauseObj.onmouseout = buttonMouseOut;
	butPrevObj.onmouseout = buttonMouseOut;
	*/
	galleryObj.appendChild(galleryImageObj);
	galleryObj.appendChild(galleryBarObj);
		galleryBarObj.appendChild(statusObj);
			statusObj.appendChild(titleObj);/*
		galleryBarObj.appendChild(butPrevObj);
		galleryBarObj.appendChild(butPauseObj);
		galleryBarObj.appendChild(butNextObj);

	var preloadPrev = new Image();
	preloadPrev.src = './images/gallery_prev_h.gif';
	var preloadPause = new Image();
	preloadPause.src = './images/gallery_pause_h.gif';
	var preloadNext = new Image();
	preloadNext.src = './images/gallery_next_h.gif';
	*/
	setOpacity(0.4,galleryBarObj);
	/*
	loadingImageObj = document.createElement('img');
	loadingImageObj.id = 'loadingImage';
	loadingImageObj.src = './images/loading.gif';
	loadingImageObj.className = "loadingImage";
	statusObj.appendChild(loadingImageObj);
	*/
	image1Obj = document.createElement('img');
	image1Obj.id = 'galleryImage1';
	image1Obj.src = images[currentImage];
	image1Obj.onload = loaded;
	setOpacity(0,image1Obj);
	galleryImageObj.appendChild(image1Obj);
	
	galleryBarObj.style.color = "#fff";
}