// -------------------------------------------------------------------
// Image Thumbnail Viewer II- By Dynamic Drive, available at: http://www.dynamicdrive.com
// Last updated: Feb 5th, 2007
// 
// http://www.dynamicdrive.com/dynamicindex4/thumbnail2.htm
//
// last here on Mar 16
//-------------------------------------------------------------------

var thumbnailviewer2={


	//Should "title" attribute of link be used as description?
	enableTitle: true,

	//Enable fading transition in IE?
	enableTransition: false,

	//Hide enlarged image when mouse moves out of anchor link? (if enlarged image is hyperlinked, always set to false!)
	hideimgmouseout: false, 



	///////////// Note changes to imageHTML content /////////////////////////


	//IE specific multimedia filter string
	iefilterstring: 'progid:DXImageTransform.Microsoft.GradientWipe(GradientSize=1.0 Duration=0.7)', 


	//Detect browser support for IE filters
	iefiltercapable: document.compatMode && window.createPopup? true : false,

	//array to preload enlarged images (ones set to display "onmouseover")
	preloadedimages:[],


	//array to hold participating links (those with rel="enlargeimage:initType")
	targetlinks:[],

	//flag to indicate whether init() function has been run already come window.onload
	alreadyrunflag: false, 


	loadimage:function(linkobj){

		//Get URL to enlarged image
		var imagepath=linkobj.getAttribute("href")

		//Reference container on page to show enlarged image in
		var showcontainer=document.getElementById(linkobj.getAttribute("rev").split("::")[0])

		//Get URL enlarged image should be linked to, if any
		//don't want this to link so allow dest to be undefined (by commenting out next line)
		//var dest=linkobj.getAttribute("rev").split("::")[1]

		//Get title attr
		var description=(thumbnailviewer2.enableTitle && linkobj.getAttribute("title"))? linkobj.getAttribute("title") : ""
		
		// Get associated image data from p just above the a link 
		// but it's likely that the previous sibling node is just whitespace so 
		// continue climbing to an element, which we hope is a p
		
		
		var imageDataText = get_previous_Element(linkobj)
		
		
		function get_previous_Element(n)
		{
		x=n.previousSibling;
		
		// keep climbing until we get a non-whitespace type (this is not robust,
		// but it works if you stick to the "<p>data</p><a>image</a>pattern in the html)
		
		while (x.nodeType != 1)
		  {
		  x=x.previousSibling;
		  }
		
		// return the text of the p object
		return x.childNodes[0].nodeValue;
		}
		
		// Just reverse the order?
		// rebuild a p with the image data text for placement under the image
		var imageDataElem = '<p>' + imageDataText + '</p>'
		
		//Construct HTML for enlarged image -- imageDataElement is text under image
				var imageHTML = '<img src="'+imagepath+'" style="border-width: 0"/>' + imageDataElem
			
		
		//Hyperlink the enlarged image?
		if (typeof dest!="undefined") 
			imageHTML='<a href="'+dest+'">'+imageHTML+'</a>'

		//Use title attr of the link as description?
		if (description!="") 
			imageHTML+='<br />'+description

		//Is this an IE browser that supports filters?	
		if (this.iefiltercapable){ 
			showcontainer.style.filter=this.iefilterstring
			showcontainer.filters[0].Apply()
		}


	showcontainer.innerHTML=imageHTML

	//Reference enlarged image itself
	this.featureImage=showcontainer.getElementsByTagName("img")[0]

	//When enlarged image has completely loaded
	this.featureImage.onload=function(){

		//Is this an IE browser that supports filters?
		if (thumbnailviewer2.iefiltercapable) 
			showcontainer.filters[0].Play()
		}



	//If an error has occurred while loading the image to show
	this.featureImage.onerror=function(){

		//Is this an IE browser that supports filters?
		if (thumbnailviewer2.iefiltercapable) 
			showcontainer.filters[0].Stop()
		}
	},

	hideimage:function(linkobj){
	
	//Reference container on page to show enlarged image in
	var showcontainer=document.getElementById(linkobj.getAttribute("rev").split("::")[0]) 
	showcontainer.innerHTML=""
	},

	//Clean up routine on page unload
	cleanup:function(){ 
		if (this.featureImage){this.featureImage.onload=null; this.featureImage.onerror=null; this.featureImage=null}
		this.showcontainer=null
		for (var i=0; i<this.targetlinks.length; i++){
		this.targetlinks[i].onclick=null
		this.targetlinks[i].onmouseover=null
		this.targetlinks[i].onmouseout=null
		}
	},
	
	//assign a function to execute to an event handler (ie: onunload)
	addEvent:function(target, functionref, tasktype){ 
		var tasktype=(window.addEventListener)? tasktype : "on"+tasktype
		if (target.addEventListener)
			target.addEventListener(tasktype, functionref, false)
		else if (target.attachEvent)
			target.attachEvent(tasktype, functionref)
	},


	//Initialize thumbnail viewer script
	init:function(){ 


	//True or false: IE filters supported and is enabled by user
	this.iefiltercapable=(this.iefiltercapable && this.enableTransition) 
	var pagelinks=document.getElementsByTagName("a")


	//BEGIN FOR LOOP
	for (var i=0; i<pagelinks.length; i++){ 


		//Begin if statement: Test for rel="enlargeimage"
		if (pagelinks[i].getAttribute("rel") && /enlargeimage:/i.test(pagelinks[i].getAttribute("rel"))){ 
			var initType=pagelinks[i].getAttribute("rel").split("::")[1] //Get display type of enlarged image ("click" or "mouseover")


			//If type is "mouseover", preload the enlarged image for quicker display
			if (initType=="mouseover"){ 
				this.preloadedimages[this.preloadedimages.length]=new Image()
				this.preloadedimages[this.preloadedimages.length-1].src=pagelinks[i].href

				//Cancel default click action
				pagelinks[i]["onclick"]=function(){ 
					return false
				}
			}

			//Load enlarged image based on the specified display type (event)
			pagelinks[i]["on"+initType]=function(){ 
				thumbnailviewer2.loadimage(this) //Load image
				return false
			}

			if (this.hideimgmouseout)
			pagelinks[i]["onmouseout"]=function(){
				thumbnailviewer2.hideimage(this)
			}

			//store reference to target link
			this.targetlinks[this.targetlinks.length]=pagelinks[i] 
		} //end if statement
	} //END FOR LOOP


	} //END init() function

}

//Take advantage of "DOMContentLoaded" event in select Mozilla/ Opera browsers for faster init
if (document.addEventListener)
	//Initialize script on page load
	thumbnailviewer2.addEvent(document, function(){thumbnailviewer2.alreadyrunflag=1; thumbnailviewer2.init()}, "DOMContentLoaded") 
else if (document.all && document.getElementsByTagName("a").length>0){ //Take advantage of "defer" attr inside SCRIPT tag in IE for instant init
	thumbnailviewer2.alreadyrunflag=1
	thumbnailviewer2.init()
}

//Default init method: window.onload
thumbnailviewer2.addEvent(window, function(){if (!thumbnailviewer2.alreadyrunflag) thumbnailviewer2.init()}, "load") 
thumbnailviewer2.addEvent(window, function(){thumbnailviewer2.cleanup()}, "unload")
