
function photogallery(garray, cols, rows, twidth, theight, paginatetext){
	this.galleryarray=garray
	this.cols=cols
	this.rows=rows
	var twidth=twidth || "267px" 
	var theight=theight || "190px"
	var ptext=(typeof paginatetext=="object")? paginatetext : ["Browse Gallery:", ""] 
	this.pagecount=Math.ceil(this.galleryarray.length/(cols*rows)) 
	
	if(this.galleryarray.length>0){
		document.write('<tr><td bgcolor="#FFFFFF">');
		document.write('<table width="100%" border="0" cellpadding="2" cellspacing="0" id="gallerytable">');
		document.write('<tbody><tr class="galleryrow">');
		document.write('<td class="productname" width="75%">Photo Gallery</td>');
		document.write('<td class="navtd">');
		document.write('<div class="photonavlinks" id="photogallerypaginate"></div>') //Generate Paginate Div
		document.write('</td></tr>');
		document.write('<tr><td colspan=2>');
		
		document.write('<table align="center" class="photogallery" id="photogallery" style="width:'+twidth+'; height:'+theight+';">') //Generate table for Photo Gallery
		for (var r=0; r<rows; r++){
			document.write('<tr>')
			for (var c=0; c<cols; c++)
				document.write('<td width="50%" valign="top" align="center"></td>')
			document.write('</tr>')
		}
		document.write('</table>')
		document.write('</td></tr>');
		document.write('</tbody></table>');
		document.write('<input type="hidden" name="currentId" id="currentId" value="0">');
		document.write('</td></tr>');
		
		
		var gdiv=document.getElementById("photogallery")
		var pdiv=document.getElementById("photogallerypaginate")
		//gdiv.onselectphoto=function(imgobj, linkobj){return true} 
		//custom event handler "onselectphoto", invoked when user clicks on an image within gallery
		this.showpage(gdiv, 0)
		this.createNav(gdiv, pdiv, ptext,0)
		//gdiv.onclick=function(e){return photogallery.defaultselectaction(e, this)} 
		//attach default custom event handler action to "onclick" event
	}else{
		gdiv="";
	}
	return gdiv
}


photogallery.prototype.createImage=function(imgparts,i){
	var imageHTML='<img src="'+imgparts[0]+'" title="'+imgparts[1]+'" width="96"/>'
	if (typeof imgparts[2]!="undefined" && imgparts[2]!=""){
		imageHTML='<a href="'+imgparts[2]+'" title="'+imgparts[1]+'" rel="ibox" itemno="'+i+'">'+imageHTML+'</a>'
	}
	return imageHTML
}


photogallery.prototype.showpage=function(gdiv, pagenumber){
	var totalitems=this.galleryarray.length 
	//total number of images
	var showstartindex=pagenumber*(this.rows*this.cols) 
	//array index of div to start showing per pagenumber setting
	var showendindex=showstartindex+(this.rows*this.cols) 
	//array index of div to stop showing after per pagenumber setting
	var tablecells=gdiv.getElementsByTagName("td")
	for (var i=showstartindex, currentcell=0; i<showendindex && i<totalitems; i++, currentcell++) 
	//Loop thru this page's images and populate cells with them
		tablecells[currentcell].innerHTML=this.createImage(this.galleryarray[i],i)
	while (currentcell<tablecells.length){ 
	//For unused cells, if any, clear out its contents
		tablecells[currentcell].innerHTML='<img src="images/spacer.gif" height="86">'
		currentcell++
	}
	//init_ibox();
}

photogallery.prototype.createNav=function(gdiv, pdiv , ptext,i){
	var instanceOfGallery=this
	var totalpage=this.pagecount;
	var prev;
	var next;
	var prevShow=1;
	var nextShow=1;
	var navHTML=""
	if(i=="") i=0;
	i = parseInt(i);
	
	prev = i-1;
	if(prev<0) prevShow=0;
	if(prev<0) prev =0

	next = i+1;
	if(next>=this.pagecount) nextShow=0;
	if(next>=this.pagecount) next=this.pagecount-1;

	if(prevShow==1) navHTML='<div id="navleft" class="nav"><a href="javascript://" title="left" rel="'+prev+'"><img src="images/leftArrow.jpg" border="0"></a></div>';
	//alert(next + ':'+ this.pagecount);
	if(nextShow==1) navHTML=navHTML+'<div id="navright" class="nav"><a href="javascript://" title="right" rel="'+next+'"><img src="images/rightArrow.jpg" border="0"></a></div>';
	pdiv.innerHTML=navHTML
	
	var navlinks=pdiv.getElementsByTagName("a");
	for (var i=0; i<navlinks.length; i++){
		navlinks[i].onclick=function(){
			if(this.getAttribute("rel")<totalpage || this.getAttribute("rel")>=0){
			//instanceOfGallery.setNav(this.getAttribute("rel"))
				instanceOfGallery.showpage(gdiv, this.getAttribute("rel"))
				//instanceOfGallery.previouspage=this 
				//Set previous clicked on link to current link for future ref
				instanceOfGallery.createNav(gdiv, pdiv , ptext,this.getAttribute("rel"))
				
				return false
			}else{
				if(this.getAttribute("title")=="left") alert("First Page");
				if(this.getAttribute("title")=="right") alert("Last Page");
				return false;
			}
		}
	}
}

photogallery.defaultselectaction=function(e, gdiv){ 
//function that runs user defined "onselectphoto()" event handler
	var evtobj=e || window.event
	var clickedobj=evtobj.target || evtobj.srcElement
	if (clickedobj.tagName=="IMG"){
		var linkobj=(clickedobj.parentNode.tagName=="A")? clickedobj.parentNode : null
		return gdiv.onselectphoto(clickedobj, linkobj)
	}
}