var hasLoaded = false;

var NUMBER_OF_PICTURES = 3;
var PICTURES_ID_PREFIX = "picGalleryNoScript_";
var PICTURES_LINK_ID_PREFIX = "pictureLink_";

var currentPic = 0;

var LOADING_DIV = "picGallery_loading";
var NEXT_DIV = "next";
var BACK_DIV = "back";
var NAVBOX_DIV = "navBox";
var THUMBNAILS_DIV = "thumbnails";

// pic Gallery colours
var currentPicColor = "#6A6dbe";
var unselectedColor = "#009";
var deadLinkColor = "#ccc";

function init() {
	hasLoaded = true;
	showDiv(0);
	document.getElementById(LOADING_DIV).style.display = 'none';
	document.getElementById(NAVBOX_DIV).style.display = 'block';
	document.getElementById(PICTURES_LINK_ID_PREFIX + currentPic).style.background = currentPicColor;

	if (document.getElementById(THUMBNAILS_DIV)) {
		document.getElementById(THUMBNAILS_DIV).style.display = 'block';
	}
}

function setNoOfImages(noOfImages) {
	NUMBER_OF_PICTURES = noOfImages;
}

function doBgColor() {
	for(var i = 0; i < NUMBER_OF_PICTURES; i++) {
		document.getElementById(PICTURES_LINK_ID_PREFIX + i).style.background = unselectedColor;
	}		
	document.getElementById(PICTURES_LINK_ID_PREFIX + currentPic).style.background = currentPicColor;
}

function doNextColor() {
	document.getElementById(BACK_DIV).style.color = unselectedColor;
	if(currentPic < (NUMBER_OF_PICTURES - 1)) {
		document.getElementById(NEXT_DIV).style.color = unselectedColor;
	} else {
		document.getElementById(NEXT_DIV).style.color = deadLinkColor;
	}		
}

function doBackColor(){
	document.getElementById(NEXT_DIV).style.color = unselectedColor;

	if(currentPic > 0) {
		document.getElementById(BACK_DIV).style.color = unselectedColor;
	} else {
		document.getElementById(BACK_DIV).style.color = deadLinkColor;
	}		
}

function showDiv(nDiv) {	
	for(var i = 0; i < NUMBER_OF_PICTURES; i++) {	
		document.getElementById(PICTURES_LINK_ID_PREFIX + i).style.background = unselectedColor;
		document.getElementById(NEXT_DIV).style.color = unselectedColor;
		document.getElementById(BACK_DIV).style.color = unselectedColor;		
		var buttonid = i;
		var id = PICTURES_ID_PREFIX + i;	
		var node = document.getElementById(id);
		if(node && i==nDiv) {
			node.style.display='block';		
			node.style.visibility='visible';						
			currentPic = i;
			document.getElementById(PICTURES_LINK_ID_PREFIX + currentPic).style.background = currentPicColor;
		} else if(node) {
			node.style.display='none';
			node.style.visibility='hidden';
		}		
	}
}

function showDivNext() {
	if(currentPic < (NUMBER_OF_PICTURES - 1)) {
		showDiv(currentPic + 1);	
		doBgColor();	
		doNextColor();
	}   
}

function showDivPrev() {
	if(currentPic > 0) {
		showDiv(currentPic - 1);
		doBgColor();	
		doBackColor();		
	}
}

function ColorOver(o) {
	o.style.backgroundColor = currentPicColor;	
}

function ColorOut(o) {
	o.style.backgroundColor = unselectedColor;
	document.getElementById(PICTURES_LINK_ID_PREFIX + currentPic).style.background = currentPicColor;
}

if (document.getElementById) {
	document.onLoad = init();
}

