//
// Exclusive sibcats.ru site engine
// Photogallery support stuff
// Version 1.3 (12/23/2004)
// Copyright (C) 2004 Alexey Nashchekin
//
function galthumbnail(s)
{
	return galbase+s+'/thumbnail/?width='+galthumbw+'&height='+galthumbh+'&sweet=graphite';
}
function galclick(n)
{
	if (galpage < 0) return false;
	var cells = document.getElementById('galtab').rows.item(0).cells;
	var i = n + galpage * galpppg;
	if (i == galselimage) return galtab(0);
	var j = i * 4;
	if (j >= galdata.length) return false;

	var p = galselimage - galpage * galpppg;
	if (p >= 0 && p < cells.length) cells.item(p).className = "galthn";
	cells.item(n).className = "galths";
	galselimage = i;
	p = document.getElementById("galbpi");
	if (p.childNodes.length > 0)
    	p.removeChild(p.childNodes.item(0));
	i = document.createElement("IMG");
	i.src = galbase+galdata[j];
	i.width = galdata[j+1];
	i.height = galdata[j+2];
	i.border = 0;
	p.appendChild(i);
	p.nextSibling.innerHTML = galdata[j+3]+'<BR /><SMALL style="color: #B0B0B0;">Copyright &copy; 2004 Onix Gloria. All rights reserved.</SMALL>';
	galtab(0);
	return false;
}
function galtab(index)
{
	var k = document.getElementById("galitem"+index);
	if (k.style.display != 'none')
		return false;
	var p;
	for (var i = 0; p = document.getElementById("galitem"+i); ++i)
		p.style.display = "none";
	k.style.display = "block";
	return false;
}
function galnextphoto()
{
	var i = galselimage+1;
	if (i >= galdata.length/4) return false;
	galsetpage(Math.floor(i / galpppg));
	return galclick(i % galpppg);
}
function galsetpage(n)
{
	var count = galdata.length / 4;
	var cells = document.getElementById('galtab').rows.item(0).cells;
	var pageCount = Math.floor((count+galpppg-1) / galpppg);
	if (n > pageCount-1) n = pageCount-1;
	else if (n < 0) n = 0;
	if (n == galpage) return false;
	var gfirst = n * galpppg;
	for (var i = 0; i < cells.length; ++i)
	{
		cells.item(i).className = (i == galselimage - gfirst) ? "galths" : "galthn";
		
		var page = Math.floor((i + gfirst) / galpppg);
		var anch = cells.item(i).childNodes.item(0);
		var img = page < anch.childNodes.length ? anch.childNodes.item(page) : null;
		
		if (img == null)
			for (var j = anch.childNodes.length; j <= page; ++j)
				anch.appendChild(img = document.createElement((gfirst + i) < count ? "IMG" : "DIV"));
		for (var j = anch.childNodes.length; --j >= 0; )
			anch.childNodes.item(j).style.display = j == page ? "block" : "none";
		
		if ((gfirst + i) < count && img.width != galthumbw)
		{
			var j = (gfirst + i) * 4;
			img.src = galthumbnail(galdata[j]);
			img.width = galthumbw;
			img.height = galthumbh;
			img.border = 0;
		}
		else if ((gfirst + i) >= count)
		{
			img.style.width = galthumbw+"px";
			img.style.height = galthumbh+"px";
		}

		anch.nextSibling.innerHTML = ((gfirst + i) < count) ? galdata[(gfirst + i) * 4 + 3] : "";
	}
	var pgs = document.getElementById('galpgs').childNodes;
	if (galpage >= 0) pgs.item(galpage+1).className = "galpage";
	pgs.item(n+1).className = "galpags";
	galpage = n;
	return false;
}
