function getel(str)
{
	return document.getElementById(str);
}

var imageholder = new Array();

var msie = 0;
var isOpera = 0;
var iswebkit = 0;
function setup()
{
	var ua = navigator.userAgent;
	var m = ua.match(/MSIE\s([^;]*)/);
    if (m&&m[1]) {
    	msie=parseFloat(m[1]);}
	m=ua.match(/Opera[\s\/]([^\s]*)/);
	if (m&&m[1]) {
		isOpera=parseFloat(m[1]);}
    m=ua.match(/AppleWebKit\/([^\s]*)/);
    if (m&&m[1]) {
        iswebkit=parseFloat(m[1]);}
}

function getViewportHeight()
{
	var height = self.innerHeight, // Safari, Opera
    mode = document['compatMode'];
    if ( (mode || msie) && !isOpera ) { // IE, Gecko
    	height = (mode == 'CSS1Compat') ?
        	document.documentElement.clientHeight : // Standards
            document.body.clientHeight; // Quirks
    }
    return height;
}

function getViewportWidth()
{
	var width = self.innerWidth,  // Safari
    mode = document['compatMode'];
    if (mode || msie) { // IE, Gecko, Opera
    	width = (mode == 'CSS1Compat') ?
        	document.documentElement.clientWidth : // Standards
            document.body.clientWidth; // Quirks
    }
    return width;
}

function getDocumentWidth()
{
	var scrollWidth = (document['compatMode'] != 'CSS1Compat' || iswebkit) ? document.body.scrollWidth : document.documentElement.scrollWidth;
    var w = Math.max(scrollWidth, getViewportWidth());
    return w;
}

function getDocumentHeight()
{
	var scrollHeight = (document['compatMode'] != 'CSS1Compat' || iswebkit) ? document.body.scrollHeight : document.documentElement.scrollHeight;
	var h = Math.max(scrollHeight, getViewportHeight());
    return h;
}

function getDocumentScrollTop()
{
	return Math.max(document['documentElement'].scrollTop, document.body.scrollTop);
}

var g_selimage = 0;
var g_path = "";
var g_dispimg = new Array();
var g_timer = null;
var g_max = 0;
var gcats = new Array();
gcats["soupkitchen"] = 5;
gcats["care"] = 6;
gcats["computer"] = 2;
gcats["auto"] = 6;
gcats["solar"] = 4;
gcats["gym"] = 9;
gcats["beading"] = 10;
gcats["bags"] = 5;
gcats["ceramics"] = 6;
gcats["children"] = 14;
gcats["soccer"] = 5;
gcats["marimba"] = 7;
gcats["church"] = 9;
gcats["staff"] = 6;
gcats["youth"] = 5;
gcats["township"] = 8;
gcats["farming"] = 2;
gcats["vocational"] = 10;
gcats["karate"] = 4;

gcats["shop"] = 1;

gcaptions = new Array();
gcaptions["gym"] = new Array();
gcaptions["gym"][1] = "With the new vocational training centre, these men are no longer forced to occupy the church for their exercises";
gcaptions["gym"][2] = "With the new vocational training centre, these men are no longer forced to occupy the church for their exercises";
function gallerystart(cat,id)
{
	g_max = gcats[cat];
	g_selimage = id;
	g_caps = gcaptions[cat];
	g_path = "gallery/"+cat+"/";
    var mask = getel("mask");
    mask.style.height = getDocumentHeight() + "px";
    mask.style.width = getDocumentWidth() + "px";
	mask.style.visibility = "visible";
	if (!g_dispimg[id])
	{
		g_dispimg[id] = new Image();
		g_dispimg[id].src = g_path+"image"+id+".jpg";
	}
	if (g_timer)
	{
		window.clearInterval(g_timer);
	}
	g_timer = window.setInterval("gallerycheckimage()",50);
}

function gallerycheckimage()
{
	if (g_dispimg[g_selimage].width&&g_dispimg[g_selimage].height)
	{
		window.clearInterval(g_timer);
		g_timer = null;
		var w = g_dispimg[g_selimage].width;
		var h = g_dispimg[g_selimage].height;
		var lb = "<table style='background-color:#fff' border='0' cellpadding='0' cellspacing='0'><tr><td id='gpic' style='background:url("+g_dispimg[g_selimage].src+");width:"+w+"px;height:"+h+"px'><table border='0' cellspacing='0' cellpadding='0' style='width:"+w+"px'><tr><td ";
		if (g_max>1)
		{
			lb+= "onmouseover='getel(\"gprevicon\").style.visibility=\"visible\"' ";
		}
		lb+= "onmouseout='getel(\"gprevicon\").style.visibility=\"hidden\"' style='width:40%;height:"+h+"px;vertical-align:middle;text-align:left;cursor:pointer' onclick='galleryprev()'><img id='gprevicon' style='visibility:hidden' src='gfx/prevlabel.gif' /><td style='text-align:center;vertical-align:middle;width:20%'><img id='gloadingicon' src='gfx/blank.gif' width='32' width='32' /></td><td";
		if (g_max>1)
		{
			lb+= " onmouseover='getel(\"gnexticon\").style.visibility=\"visible\"'";
		}
		lb+="onmouseout='getel(\"gnexticon\").style.visibility=\"hidden\"' style='cursor:pointer;width:40%;height:"+h+"px;vertical-align:middle;text-align:right' onclick='gallerynext()'><img id='gnexticon' style='visibility:hidden' src='gfx/nextlabel.gif' /></td></td></tr></table></td></tr><tr><td style='height:40px'><table border='0' cellpadding='0' cellspacing='0' style='height:40px;width:"+w+"px'><tr><td style='text-align:left;font-color:#444;font-size:16px;font-weight:bold;vertical-align:middle;padding:10px;cursor:pointer'>Image "+Math.min(g_selimage+1,g_max)+" of "+g_max+"</td><td style='text-align:center;font-color:#444;font-size:14px;vertical-align:middle;padding:10px'>";
		if (g_caps)
		{
			if (g_caps[g_selimage])
			{
				lb+=g_caps[g_selimage];
			}
		}
		lb += "</td><td style='text-align:right;vertical-align:middle;padding:10px' onclick='killgallery()'><img style='cursor:pointer' src='gfx/closelabel.gif' /></td></tr></table></td></tr></table>";
		var lc = getel("lightcontainer");
		lc.style.left = ((getViewportWidth()/2)-(w/2))+"px";
		lc.style.top = ((getViewportHeight()/2)-20-(h/2)+getDocumentScrollTop())+"px";
		lc.innerHTML = lb;
		lc.style.visibility = "visible";
	}
}

function gallerychangeimage()
{
    var mask = getel("mask");
    mask.style.height = getDocumentHeight() + "px";
    mask.style.width = getDocumentWidth() + "px";
	mask.style.visibility = "visible";
	getel("gloadingicon").src = "gfx/loading.gif";
	if (!g_dispimg[g_selimage])
	{
		g_dispimg[g_selimage] = new Image();
		g_dispimg[g_selimage].src = g_path+"image"+g_selimage+".jpg";
	}
	if (g_timer)
	{
		window.clearInterval(g_timer);
	}
	g_timer = window.setInterval("gallerycheckimage()",50);
}

function galleryprev()
{
	if (g_max>1)
	{
		g_selimage--;
		if (g_selimage<0)
		{
			g_selimage=g_max-1;
		}
		gallerychangeimage();
	}
}

function gallerynext()
{
	if (g_max>1)
	{
		g_selimage++;
		if (g_selimage>=g_max)
		{
			g_selimage=0;
		}
		gallerychangeimage();
	}
}

function killgallery()
{
	g_dispimg = new Array();
	var mask = getel("mask");
	mask.style.height = "0px";
	mask.style.width = "0px";
	mask.style.visibility = "hidden";
	var lc = getel("lightcontainer");
	lc.style.height = "0px";
	lc.style.width = "0px";
	lc.style.visibility = "hidden";
}
