var crispygallery={

	// Main vars
	totalImages: 0,
	enableTitle: true,
	enableTransition: false, // Needs to be cross broswer compatiable so no IE only transitions
	hideimgmouseout: false, // Unloading images is not required
	currentImagePath:'' ,
	wrapCycle: true, // Changes whether next/previous is cyclic or stops at end

	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=(crispygallery.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
			this.currentImagePath = imagepath
			showcontainer.innerHTML=imageHTML
			this.featureImage=showcontainer.getElementsByTagName("img")[0] //Reference enlarged image itself
	},

	fetchimage_next:function() {

				var collectNext = false
				var imagesLoaded = 0

				// Loop threw collection , detect next available object and wrap 
				var pagelinks=document.getElementsByTagName("a")
				for (var i=0; i<pagelinks.length; i++){ 
					if (pagelinks[i].getAttribute("rel") && /crispyimage/i.test(pagelinks[i].getAttribute("rel"))) {
						
						imagesLoaded+=1

						if (collectNext == true)
						{
							crispygallery.fetchimage(pagelinks[i])
							collectNext = false
						} else {

								// Either image URL matches so we load next , or its at the start so we load image 2
								if ((pagelinks[i].getAttribute("href") == this.currentImagePath) || ((imagesLoaded > 1) && (this.currentImagePath == '')))
								{
									// Mark next collection flag			
									collectNext = true
								}
						}
			
					}
				}
	},

	fetchimage_previous:function() {

				var collectNext = false
				var imagesLoaded = 0

				// Loop threw collection , detect next available object and wrap 
				var pagelinks=document.getElementsByTagName("a")
				for (var i=0; i<pagelinks.length; i++){ 
					if (pagelinks[i].getAttribute("rel") && /crispyimage/i.test(pagelinks[i].getAttribute("rel"))) {
						
						imagesLoaded+=1

						// Move backwards as long as we are not at picture 1
						if ((pagelinks[i].getAttribute("href") == this.currentImagePath) && (imagesLoaded > 1))
							{
								crispygallery.fetchimage(pagelinks[i-1])
							}
			
					}
				}
	},

	fetchimage_rnd:function(){
		var randomImg = new Array
		// Find all available image sources
		var pagelinks=document.getElementsByTagName("a")
			for (var i=0; i<pagelinks.length; i++){ 
				if (pagelinks[i].getAttribute("rel") && /crispyimage/.test(pagelinks[i].getAttribute("rel"))){
					randomImg[randomImg.length]=pagelinks[i]
				}
			}
		var rand_no = Math.ceil(randomImg.length*Math.random())
		this.fetchimage(randomImg[rand_no])
	},
				

	// 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") && /crispyimage/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(){
						crispygallery.fetchimage(this)
						return false
					}
					
				}
			
			}

		}

	}
	

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

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

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


// Thankyou for using 'Crispy Gallery" by ShotgunFront Ltd pregramming, code by Steve Morgan 2008 UK
