var currimg = "";
var loaded = false;
var next = "";
var prev = "";
var nextcap = "";
var prevcap = "";
var nextimg = null;
var previmg = null;
var waitforit = true;

function gallimg(e)
{
	if(loaded || waitforit)
	{
		return false;
	}
	loaded = true;
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	
	passrc = targ.src;	
	currcapt = targ.getAttribute("alt");
	currimg = stripname(passrc);
	var imgsrcs = "gallery/" + currimg + "large.png";
	currimg = currimg + ".png"
	
	var thisdiv = document.createElement("div");
	var thlddiv = thisdiv.cloneNode(false);
	var topdiv = thisdiv.cloneNode(false);
	var transbanner = thisdiv.cloneNode(false);
	var capspan = thisdiv.cloneNode(false);
	var brdrdiv = thisdiv.cloneNode(false);
	var hedone = document.createElement("h1");
	hedone.className = "nomarpad fl";
	brdrdiv.className = "mid central";
	capspan.appendChild(document.createTextNode(currcapt));
	capspan.style.position = "relative";
	capspan.style.paddingLeft = "10px";
	capspan.style.color = "#FFFFFF";
	capspan.style.fontSize = "1.1em";
	capspan.style.fontFamily = "Verdana, Helvetica, sans-serif";
	capspan.style.marginTop = "36px";
	//capspan.style.fontWeight = "bold";
	capspan.style.width = "639px";
	capspan.style.textAlign = "left";
	capspan.className = "fl talf";
	capspan.style.borderTop = "solid 1px #B5C3C5";
	capspan.style.borderBottom = "solid 1px #B5C3C5";
	topdiv.className = "mid tam";
	topdiv.style.width = "771px";
	var contdiv = thisdiv.cloneNode(false);
	contdiv.className = "fr";
	contdiv.style.width = "123px";
	contdiv.style.marginTop = "23px";
	contdiv.style.marginBottom = "-158px";
	contdiv.style.marginRight = "11px";
	contdiv.style.position = "relative";
	contdiv.style.left = "11px";
	var imgback = document.createElement("img");
	var imgclose = imgback.cloneNode(false);
	var imgfront = imgback.cloneNode(false);
	imgback.setAttribute("src","galleft.png");
	imgback.setAttribute("alt","Previous Image");
	imgfront.setAttribute("src","galright.png");
	imgfront.setAttribute("alt","Next Image");
	imgclose.setAttribute("src","galclose.png");
	imgclose.setAttribute("alt","Close Gallery");
	thisdiv.style.visibility = "hidden";
	thisdiv.style.left = "0px";
	thisdiv.style.height = "100%";
	thisdiv.style.width = "100%";
	thisdiv.setAttribute("id","galldiv");
	var thisimg = document.createElement("img");
	thisimg.setAttribute("src",imgsrcs);
	if(typeof document.body.style.maxHeight == "undefined")
	{
		topdiv.style.width = "769px";
		capspan.style.marginTop = "31px";
		contdiv.style.marginTop = "17px";
		capspan.style.width = "640px";
		capspan.style.display = "block";
		capspan.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='semidark.png',sizingMethod='scale')";
		contdiv.style.width = "120px";
		contdiv.style.left = "22px";
		imgback = document.createElement("div");
		imgback.style.width = "40px";
		imgback.style.height = "38px";
		imgback.style.position = "absolute";
		imgback.style.left = "0px";
		imgfront = imgback.cloneNode(true);
		imgfront.style.position = "absolute";
		imgfront.style.left = "80px";
		imgclose = imgback.cloneNode(true);
		imgclose.style.position = "absolute";
		imgclose.style.left = "40px";
		imgback.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='galleft.png',sizingMethod='image')";
		imgfront.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='galright.png',sizingMethod='image')";
		imgclose.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='galclose.png',sizingMethod='image')";
		thlddiv.style.top = -220 - document.getElementById("gallerytab").offsetHeight + "px";//"-1240px";
		thisdiv.style.position = "absolute";
		thisdiv.style.top = "-10px";
		thisdiv.style.paddingTop = "10px";
		thisdiv.style.height = (document.documentElement.clientHeight + 20) + "px";
		thisdiv.style.width = (document.documentElement.clientWidth) + "px";
		//document.body.childNodes[0].style.overflow = "hidden";
		document.body.appendChild(thisdiv);
		//document.getElementById("galldiv").style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity=0)";
		//document.body.firstChild.firstChild.addEvent("load",tset);
		window.onresize = function()
		{
			document.getElementById("galldiv").style.width = (document.documentElement.clientWidth) + "px";
			document.getElementById("galldiv").style.height = (document.documentElement.clientHeight + 20) + "px"
		};
	}
	else
	{
		thisdiv.style.backgroundImage = "url('semidark.png')";
		thisdiv.style.top = "0px";
		thisdiv.style.opacity = "0";
		capspan.style.backgroundImage = "url('semidark.png')";
		//document.body.style.overflow = "hidden";
		document.body.appendChild(thisdiv);
		
	}
	if(document.attachEvent)
	{
		imgclose.attachEvent("onclick",quitit);
		imgback.attachEvent("onclick",shiftleft);
		imgfront.attachEvent("onclick",shiftright);
	}
	else if(document.addEventListener)
	{
		imgclose.addEventListener("click",quitit,false);
		imgback.addEventListener("click",shiftleft,false);
		imgfront.addEventListener("click",shiftright,false);
	}
	brdrdiv.style.height = thisdiv.style.height;
	brdrdiv.style.backgroundColor = "transparent";
	document.body.lastChild.appendChild(brdrdiv);
	gfdrhandle = document.getElementById("galldiv");
	if (typeof document.body.style.maxHeight != "undefined")
	{
	  // IE 7, mozilla, safari, opera 9
	  gfdrhandle.style.position = "fixed";
	  thlddiv.style.top = document.getElementById("gallerytab").offsetHeight + "px";
	}
	else
	{
		window.onscroll = function()
		{
			gfdrhandle.style.top = document.documentElement.scrollTop - 10 + "px"
		};
	}
	thisimg.style.visibility = "hidden";
	gfdrhandle.style.visibility = "visible";
	gassignop();
	thisimg.style.marginTop = "22px";
	thlddiv.style.width = "100%";
	thlddiv.setAttribute("text-align","middle");
	thlddiv.className = "tam";
	thlddiv.style.zIndex = "50000";
	thlddiv.style.position = "absolute";
	thlddiv.style.top = "0px";
	thlddiv.style.left = "0px";
	imgback.style.border = "solid 1px #B5C3C5";
	imgback.style.borderRightWidth = "0px";
	imgfront.style.border = "solid 1px #B5C3C5";
	imgfront.style.borderRightWidth = "0px";
	imgclose.style.border = "solid 1px #B5C3C5";
	imgclose.style.borderRightWidth = "0px";
	transbanner.className = "transbanner";
	hedone.style.color = "#B5C3C5";
	document.body.insertBefore(thlddiv,document.body.firstChild);
	document.body.firstChild.appendChild(topdiv);
	document.body.firstChild.firstChild.appendChild(transbanner);
	document.body.firstChild.firstChild.firstChild.appendChild(hedone);
	document.body.firstChild.firstChild.lastChild.firstChild.appendChild(document.createTextNode("HYTECH WATER LTD"));
	document.body.firstChild.firstChild.appendChild(contdiv);
	document.body.firstChild.firstChild.lastChild.appendChild(imgback);
	document.body.firstChild.firstChild.lastChild.appendChild(imgclose);
	document.body.firstChild.firstChild.lastChild.appendChild(imgfront);
	document.body.firstChild.firstChild.lastChild.childNodes[1].style.cursor = "pointer";
	document.body.firstChild.firstChild.lastChild.childNodes[0].style.cursor = "pointer";
	document.body.firstChild.firstChild.lastChild.childNodes[2].style.cursor = "pointer";
	document.body.firstChild.firstChild.appendChild(capspan);
	document.body.firstChild.firstChild.lastChild.style.marginRight = "-150px";
	document.body.firstChild.firstChild.appendChild(thisimg);
	gfadeon();
	currimg = stripname(currimg);
	cacheimages();
	//document.body.firstChild.firstChild.addEventListener("load",tset,false);
}


function tset()
{
	alert("newset"+nextimg.getAttribute("src"));
}

function stripname(name)
{
	var namearr = name.split('/').reverse()[0];
	return (namearr.split('.'))[0];
}

function killkids(root)
{
	while(root.hasChildNodes())
	{
		if(root.firstChild.hasChildNodes())
		{
			killkids(root.firstChild);
		}
		root.removeChild(root.firstChild);
	}
}

function quitit()
{
	window.onresize = null;
	loaded = false;
	gfadeoff();
	killkids(document.body.firstChild);
	document.body.removeChild(document.body.firstChild);
	killkids(document.body.lastChild);
	document.body.removeChild(document.body.lastChild);
}

function shiftright()
{
	currimg = next;
	currcapt = nextcap;
	nextimg.style.visibility = "visible";
	document.body.firstChild.firstChild.replaceChild(nextimg,document.body.firstChild.firstChild.lastChild);
	document.body.firstChild.firstChild.lastChild.previousSibling.replaceChild(document.createTextNode(nextcap),document.body.firstChild.firstChild.lastChild.previousSibling.firstChild);
	cacheimages();
}

function shiftleft()
{
	currimg = prev;
	currcapt = prevcap;
	previmg.style.visibility = "visible";
	document.body.firstChild.firstChild.replaceChild(previmg,document.body.firstChild.firstChild.lastChild);
	document.body.firstChild.firstChild.lastChild.previousSibling.replaceChild(document.createTextNode(prevcap),document.body.firstChild.firstChild.lastChild.previousSibling.firstChild);
	cacheimages();
}

function cacheimages()
{
	var imgarr = new Array;
	var captarr = new Array;
	for(x in document.images)
	{
		if(document.images[x].className == "gimg")
		{
			imgarr.push(stripname(document.images[x].getAttribute("src")));
			captarr.push(document.images[x].getAttribute("alt"));
		}
	}
	for(x in imgarr)
	{
		if(imgarr[x] == currimg)
		{
			prev = imgarr[(--x)%imgarr.length];
			prevcap = captarr[(x)%imgarr.length];
			if(x == "-1")
			{
				prev = imgarr.pop();
				prevcap = captarr.pop();
			}
			++x;
			next = imgarr[(++x)%imgarr.length];
			nextcap = captarr[(x)%imgarr.length];
		}
	}
	nextimg = document.body.firstChild.firstChild.lastChild.cloneNode(false);
	previmg = document.body.firstChild.firstChild.lastChild.cloneNode(false);
	nextimg.setAttribute("src","gallery/"+next+"large.png");
	previmg.setAttribute("src","gallery/"+prev+"large.png");
	//document.body.firstChild.firstChild.addEventListener("load",tset,false);
}
