// -------------------------------------------------------------------
// Photo Album Script v2.0- By Dynamic Drive, available at: http://www.dynamicdrive.com
// Mar 11th, 07': Script updated to v2.0
// -------------------------------------------------------------------

function photogallery(garray, cols, rows, twidth, theight, tURL){
	gcount=(typeof gcount=="undefined")? 1 : gcount+1 //global var to keep count of current instance of photo gallery
	this.gcount=gcount
	this.galleryarray=garray
	this.cols=cols
	this.rows=rows
	var twidth= twidth || "700x" //default table width is 700px
	var theight= theight  || "500px"
	var viewallURL= tURL //Store 2 compontents of paginate DIV text inside array
	this.pagecount=Math.ceil(this.galleryarray.length/(cols*rows)) //calculate number of "pages" needed to show the images
	document.write('<table width = "100" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#7cdbff" class="photogallery" id="photogallery-'+gcount+'" style=" border-collapse: collapse; width:'+twidth+'; height:'+theight+';">') //Generate table for Photo Gallery

                	document.write('<tr>')
                  //	document.write('<td width="492">&nbsp;</td>')
                  	//document.write('<td width="109"><img src="images/viewall.gif" width="67" height="17"></td>')
	                  //<td width="179" class="g_l"><a href="#" class="backnext_link">back</a> &lt;&lt; <a href="#" class="backnext_link">1</a> <a href="#" class="backnext_link">2</a> <a href="#" class="backnext_link">3</a> <a href="#" class="backnext_link">4</a> <a href="#" class="backnext_link">5</a> <a href="#" class="backnext_link">6</a> <a href="#" class="backnext_link">&nbsp;next</a> &gt;&gt; </td>
                	document.write('</tr>')


	document.write('<div style="padding-left: 10px;" align= "right" class="browsetext"><a href = "' + viewallURL + '"><img src="images/viewall.gif" width="67" height="17"></a>&nbsp; &nbsp; &nbsp; </div>') //Generate Paginate Div

	document.write('<div style="padding-left: 10px;" align= "right" class="browsetext" id="photogallerypaginate-'+gcount+'"><< >></div>') //Generate Paginate Div


	for (var r=0; r<rows; r++){
		document.write('<tr align="center">')
		for (var c=0; c<cols; c++)
			document.write('<td valign="center"></td>')
		document.write('</tr>')
	}
	document.write('</table>')
	document.write('<tr>')	
	document.write('<td>')		
	document.write('</td>')		
	document.write('</tr>')	


	var gdiv=document.getElementById("photogallery-"+this.gcount)
	var pdiv=document.getElementById("photogallerypaginate-"+this.gcount)
	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)
	gdiv.onclick=function(e){return photogallery.defaultselectaction(e, this)} //attach default custom event handler action to "onclick" event
	return gdiv
}


photogallery.prototype.createImage=function(imgparts){
	var imageHTML='<img src="'+imgparts[0]+'" title="'+imgparts[1]+'" width="135" height="268"/>'
	if (typeof imgparts[4]!="undefined" && imgparts[4]!=""){ //Create URL?
		var linktarget= ""
		imageHTML='<a href="'+imgparts[4]+'" target="'+linktarget+'">'+imageHTML+'</a>'
	}
	if (typeof imgparts[1]!="undefined" && imgparts[1]!="") //Display description?
		imageHTML+='<div class="product_name">'+imgparts[1]+'</div>'		
		imageHTML+='<div class="product_price">$ '+imgparts[2]+'.00</div>'
		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])
	while (currentcell<tablecells.length){ //For unused cells, if any, clear out its contents
		tablecells[currentcell].innerHTML=""
		currentcell++
	}
}

photogallery.prototype.createNav=function(gdiv, pdiv ){
	var instanceOfGallery=this
	var j=0
	var navHTML= '<b><a href="#navigate" rel= '+j+' id= "iback" class="backnext_link" ><< back</a> </b>'
	for (var i=0; i<this.pagecount; i++)
		navHTML+='<a class ="backnext_link" href="#navigate" rel="'+i+'">'+(i+1)+'</a> ' //build sequential nav links
	
	navHTML = navHTML + '<b>  <a href="#navigate" rel= 1 id= "inext" class="backnext_link" > next>> >> &nbsp; &nbsp; &nbsp; </a> </b>'
	pdiv.innerHTML=navHTML
	
	var navlinks=pdiv.getElementsByTagName("a")
	
	this.previouspage=navlinks[0] //Set previous clicked on link to current link for future ref
	for (var i=0; i<navlinks.length; i++){
		navlinks[i].onclick=function(){
			instanceOfGallery.previouspage.className="backnext_link:visited" //"Unhighlight" last link clicked on...
			this.className="backnext_link:active" //while "highlighting" currently clicked on flatview link (setting its class name to "selected"
			instanceOfGallery.showpage(gdiv, this.getAttribute("rel"))
			instanceOfGallery.previouspage=this //Set previous clicked on link to current link for future ref
			j = Number(this.getAttribute("rel"))

			var i_b = (j-1)
			var i_n = (j+1)
			if (j > 0 )
			{	
				if(Number(navlinks.length-j) >3 )
				{	
					document.getElementById("iback").setAttribute("rel", i_b)
					document.getElementById("inext").setAttribute("rel", i_n)				
					navlinks[i_n].className="backnext_link:active"
				}

			}
	
			return false
		}
	}

navlinks[1].className="backnext_link:active" //Select first link by default
}

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)
	}
}