var shotgungallery={

	// Main vars
	enableTitle: true,
	enableTransition: false, // Needs to be cross broswer compatiable so no IE only transitions
	hideimgmouseout: false, // Unloading images is not required

	preloadedimages:[], 
	targetlinks:[], //array to hold participating links (those with rel="enlargeimage:initType")
	alreadyrunflag: false, //flag to indicate whether init() function has been run already come window.onload

	fetchimage:function(linkobj){

		// Load variables from containing ANCHOR tag
		
			// Image URL
			var imagepath=linkobj.getAttribute("href")
			// Target Containing DIV , change this to move content elsewhere on page
			var showcontainer=document.getElementById(linkobj.getAttribute("rev").split("/")[0])
			// Fetch URL for large image to link to
			var dest=linkobj.getAttribute("rev").split("/")[1] //Get URL enlarged image should be linked to, if any
			// Fetch DESCRITPION for image title attribute
			var description=(shotgungallery.enableTitle && linkobj.getAttribute("title"))? linkobj.getAttribute("title") : "" //Get title attr

		// Build HTML Container for new content image
		
			var imageHTML='<img src="'+imagepath+'" style="border-width: 0" />' 

			// Linked image ?
			if (typeof dest!="undefined") 
				imageHTML='<a href="'+dest+'">'+imageHTML+'</a>'

			// Description ?
			if (description!="") 
				imageHTML+='<br />'+description

		// Set CONTAINER HTML
			
			showcontainer.innerHTML=imageHTML
			this.featureImage=showcontainer.getElementsByTagName("img")[0] //Reference enlarged image itself
	},

	// Unload objects and arrays
	unload: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
		}
	},

	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)
	},

	init:function(){

		var pagelinks=document.getElementsByTagName("a")
			for (var i=0; i<pagelinks.length; i++){ 

				if (pagelinks[i].getAttribute("rel") && /shotgunimage/i.test(pagelinks[i].getAttribute("rel"))){
					
					var initType=pagelinks[i].getAttribute("rel").split("/")[1]
					
					this.preloadedimages[this.preloadedimages.length]=new Image()
					this.preloadedimages[this.preloadedimages.length-1].src=pagelinks[i].href

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

					pagelinks[i]["on"+initType]=function(){
						shotgungallery.fetchimage(this)
						return false
					}

					this.targetlinks[this.targetlinks.length]=pagelinks[i]
				}
			
			}

		}

	}
	

// Speed optimisation based on DOM object
if (document.addEventListener)
	shotgungallery.addEvent(document, function(){shotgungallery.alreadyrunflag=1; shotgungallery.init()}, "DOMContentLoaded")
else if (document.all && document.getElementsByTagName("a").length>0){
	shotgungallery.alreadyrunflag=1
	shotgungallery.init()
}

// Object Events
shotgungallery.addEvent(window, function(){
		if (!shotgungallery.alreadyrunflag) shotgungallery.init()}, "load") 

shotgungallery.addEvent(window, function(){
		shotgungallery.unload()}, "unload")