// -------------------------------------------------------------------
// 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, paginatetext){
	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 ptext=(typeof paginatetext=="object")? paginatetext : ["Browse Maps:&nbsp;&nbsp;<br /><br />", ""] //Store 2 compontents of paginate DIV text inside array
var ptext=(typeof paginatetext=="object")? paginatetext : ["<strong>Map #:</strong> ", ""] //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('<div class="photonavlinks" id="photogallerypaginate-'+gcount+'"></div>') //Generate Paginate Div


	document.write('<table class="photogallery" id="photogallery-'+gcount+'" 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 valign="top" align="center"></td>')
		document.write('</tr>')
	}
	document.write('</table>')
//document.write('<div class="photonavlinks" id="photogallerypaginate-'+gcount+'"></div>') //Generate Paginate Div

	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)

//CHANGE BELOW
	this.createNav(gdiv, pdiv, ptext, garray)

	//gdiv.onclick=function(e){return photogallery.defaultselectaction(e, this)} //attach default custom event handler action to "onclick" event

gdiv.ondblclick=function(e){return photogallery.defaultselectaction(e, this)} //attach default custom event handler action to "onclick" event
		
return gdiv
}


photogallery.prototype.createImage=function(imgparts){

	var mapnumb;
	for (i=0;i<mapset.length;i++){
		if(mapset[i][0]==imgparts[0]){
	mapnumb=i;
	break;
	}else{
	}
	}
	var requestedmap="'"+mapsetnumb+":"+mapnumb+"'";

	var imageHTML='<h2 class="maptitle">'+imgparts[1]+'</h2>'
tempURL="'"+"http://www.bcgis.com/website/gisweb/maprequestform.htm?"+imgparts[1]+" - "+imgparts[2]+"|"+imgparts[3]+"'";

	imageHTML+='<strong><i>Request This Map Print:</i></strong>&nbsp;&nbsp;&nbsp;';

	var ordered=false;
	var requestedMapsArray1=getCookie("requestedMaps");
	
	
	if(requestedMapsArray1==null){
		
imageHTML+='<input type="checkbox"  id="'+requestedmap+'" onclick="javascript:addToOrder('+requestedmap+');"/><br/>';
		//imageHTML+='<a href="" onclick="javascript:addToOrder('+requestedmap+'); return false;"><img  id="'+requestedmap+'" class="nomap" src="http://www.bcgis.com/website/gisweb/images/dollar_bill.jpg" alt="Request This Map" /></a><br/>';
			} else {
requestedMapsArray=requestedMapsArray1.split("|");

	for(i=0;i<requestedMapsArray.length;i++){
		if(requestedMapsArray[i]==requestedmap){
		ordered=true;
		break;
		}
	}
		if(ordered==true){

		//imageHTML+='<a href="" onclick="javascript:addToOrder('+requestedmap+'); return false;"><img  id="'+requestedmap+'" class="nomap" src="http://www.bcgis.com/website/gisweb/images/doit.gif" alt="Remove This Map" /></a><br/>';
		imageHTML+='<input type="checkbox"  id="'+requestedmap+'" checked onclick="javascript:addToOrder('+requestedmap+');"/><br/>';

		} else {

imageHTML+='<input type="checkbox"  id="'+requestedmap+'" onclick="javascript:addToOrder('+requestedmap+');"/><br/>';
		//imageHTML+='<a href="" onclick="javascript:addToOrder('+requestedmap+'); return false;"><img  id="'+requestedmap+'" class="nomap" src="http://www.bcgis.com/website/gisweb/images/dollar_bill.jpg" alt="Request This Map" /></a><br/>';
		}

	}		
		var imageHTML1='<img class="map" src="'+imgparts[0]+'.jpg" title="Click to open '+imgparts[1]+' in PDF format"/>'

	var linktarget=imgparts[3] || ""
	imageHTML+='<a href="'+imgparts[0]+'.pdf" target="'+linktarget+'">'+imageHTML1+'</a>'
	imageHTML+='<h3 class="size">'+imgparts[2]+'</h3>'
	
	return imageHTML
}


//photogallery.prototype.createImage=function(imgparts){
//	var imageHTML='<img src="'+imgparts[0]+'" title="'+imgparts[1]+'"/>'
//	if (typeof imgparts[2]!="undefined" && imgparts[2]!=""){ //Create URL?
//		var linktarget=imgparts[3] || ""
//		imageHTML='<a href="'+imgparts[2]+'" target="'+linktarget+'">'+imageHTML+'</a>'
//	}
//	if (typeof imgparts[1]!="undefined" && imgparts[1]!="") //Display description?
//		imageHTML+='<br />'+imgparts[1]
//	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 , ptext, garray){


	var instanceOfGallery=this
	var navHTML=""

	for (var i=0; i<this.pagecount; i++)

		navHTML+='<a href="#navigate" rel="'+i+'">'+ptext[1]+(i+1)+'</a> ' //build sequential nav links
	
	pdiv.innerHTML=ptext[0]+' '+navHTML

	var navlinks=pdiv.getElementsByTagName("a")
	navlinks[0].className="current" //Select first link by default
	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(){
navlinks[i].onmouseover=function(){
			instanceOfGallery.previouspage.className="" //"Unhighlight" last link clicked on...
			this.className="current" //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

			return false
		}

//navlinks[i].onclick=function(){
//CHANGED TO DOUBLE CLICK FOR MAP NUMBERS

navlinks[i].ondblclick=function(){

numblink=garray[this.getAttribute("rel")][0]+'.pdf';
newwin1(numblink);

}

	}

}

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") && (clickedobj.className=="map")){
		var linkobj=(clickedobj.parentNode.tagName=="A")? clickedobj.parentNode : null
		return gdiv.onselectphoto(clickedobj, linkobj)
	}
}
