//			gallery.js

/*
Prepare a gallery page for behavior:
- clicking on a thumbnail changes the large img source
- change the querystring for the 'buy this print' link
*/

var oBigImg;
var oBuyPrint;


/*==============================
initialization routines
==============================*/
function jsInit()
{

	var x;
	x = jsInitMenu();
	x = jsGalleryInit();

debugFlag = false;
}

/*==============================
gallery initialization routines
==============================*/
function jsGalleryInit()
{
//debugFlag = true;
debugAlert("jsGalleryInit()");

		// bail if not DOM-aware
		if (!document.getElementById) return;
		if (!document.getElementsByTagName) return;

	// get global objects
	oBigImg = document.getElementById("bigimg");
debugAlert("oBigImg.id = " + oBigImg.id);
	oBuyPrint = document.getElementById("buyprint");

	// assign behavior to thumbnails
	var oThumbs = document.getElementById("thumbnails");
debugAlert("oThumbs.id = " + oThumbs.id);
	var aThumbs = oThumbs.getElementsByTagName("A");
debugAlert("aThumbs.length = " + aThumbs.length);

	for (var iThumb = 0; iThumb < aThumbs.length; iThumb++)
	{
		aThumbs[iThumb].onclick = jsThumbClick;
		aThumbs[iThumb].removeAttribute("href");				// disable the hyperlink
/*
		// get the large image name
		var sImgSrc = aThumbs[iThumb].firstChild.src;
		//var regexp = /thumb/;
		aThumbs[iThumb] = replace(sImgSrc, /thumb/, "");
*/
	}
debugFlag = false;
}

/*----------------------------*/
function jsThumbClick(e)
/*----------------------------*/
{
//debugFlag = true;
debugAlert("jsThumbClick " + this.tagName + " " + this.id);

	// stop event propagation
		if (!e) var e = window.event;
	e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();

//debugAlert("oBigImg.id = " + oBigImg.id);
//debugAlert("this.firstChild.tagName = " + this.firstChild.tagName);

		oBigImg.style.visibility = "hidden";

		var sImgSrc = this.firstChild.src;
//debugAlert("this.firstChild.src = " + sImgSrc);
		var regexp = /_thumb_/;
		var sSrc = sImgSrc.replace(regexp, "_gallery_");
debugAlert("oBigImg.src = " + sSrc);
		oBigImg.src = sSrc;
		oBigImg.style.visibility = "visible";

		var iPos = sSrc.lastIndexOf("_");
		var sPrintId = sSrc.substring(iPos+1);	// 05.jpg
		iPos = sPrintId.indexOf(".");
		sPrintId = sPrintId.substring(0,iPos);	// 05
debugAlert("sPrintId = " + sPrintId );
		sTemp = oBuyPrint.href;				// purchase.asp?p=print,FM,01
		var iPos = sTemp.lastIndexOf(",");
		oBuyPrint.href = oBuyPrint.href.substring(0,iPos+1) + sPrintId;
debugAlert("oBuyPrint.href = " + oBuyPrint.href.substring(0,iPos) + " + " + sPrintId);
		
debugFlag = false;
}

