dojo.addOnLoad(function() {	

	// DOJO Powered LightBox Gallery
	// Copyright 2009 PetSalonWebSites.com
						
	var linkNode = dojo.query(".entry"); 
	var bodyNode = dojo.query("div#contentlayer");
	var opacNode = '<div id="opacitylayer"></div>';
	var imgNode = '<div id="gallerywrapper"><div id="imagewrapper"></div></div>';
	var captionNode = '<div class="imagecaption"></div>';
	var nextEntry = '<div class="nextimg">next</div>';
	var lastEntry = '<div class="lastimg">back</div>';
	var exitImg = '<div class="exitimg">close gallery</div>';	

	var pgHeight = dojo.query("div#sitewrapper").style("height"); //get the page height

	linkNode.connect("onclick",function(evt) {	
						
		bodyNode.addContent(opacNode,"begin");		// add the backmost div, the opacity layer
						
		var opacLayer = dojo.query("div#opacitylayer");

		opacLayer.style({
			"width": "100%",
			"height": "100%",
    	"backgroundColor": "#bd5a66",
			"zIndex": "1"
		});		
											
		opacLayer.animateProperty({	
			duration: 450,	
			properties:{
				opacity: {start: "0", end: .75},
				height: {start: "0", end: pgHeight}
			}
		}).play();	
						
		bodyNode.addContent(imgNode,"last");	// Building the actual image layer			
		
		var wrapperLayer = dojo.query("div#gallerywrapper");
		var imgLayer = dojo.query("div#imagewrapper");
					
		var imgName = this.name;
		var tempImg = document.createElement("img"); 
		tempImg.src = this.name;
		
		var caption = this.title; // get the caption			
		
		wrapperLayer.addContent(captionNode,"after");	// inserting a caption	
	
		var imgCaption = dojo.query("div .imagecaption");		
	
		imgCaption.forEach(function addCap(x){
			x.innerHTML = nextEntry + lastEntry	+ exitImg;
		});
		
	
		// inserting the node used to exit from the image
		
		var exitItem = dojo.query("div .exitimg");

		exitItem.connect("onclick",function(evt) { exitNode(); });	

		// traverse the linkNode array to get another gallery entry	

		var nextItem = dojo.query("div .nextimg");

		// have to find the images place in the DOM to correctly get the next image

		nextItem.connect("onclick",function(evt)  {
			
			imgChange(imgName,"next");
			
		});

		var lastItem = dojo.query("div .lastimg");

		lastItem.connect("onclick",function(evt)  {
			
			imgChange(imgName,"last");
			
		});
		
		var leftVal = "-" + tempImg.width/2 + "px"; // offset from left

		wrapperLayer.style({
			"margin": "0 0 0 50%", 	
			"width": tempImg.width + "px",
			"left": leftVal,
    	"height": tempImg.height + "px"
		});

		imgLayer.style({
			"margin": "0 0 0 50%", 
			"left": leftVal,
    	"height": tempImg.height + "px",
			"backgroundImage": "url("  + imgName + ")",
			"backgroundRepeat": "no-repeat"
		});	
	
		imgLayer.animateProperty({	
			duration: 1000,						
			properties:{
				width: {start: "0", end: tempImg.width},
				opacity: {start: "0", end: 1.0}
			}
		}).play();

		imgCaption.style({
			"margin": "0 0 0 50%", 
			"top": tempImg.height + 80 + "px",
			"width": tempImg.width + "px",
			"left": leftVal
		});	
		
		imgCaption.animateProperty({	
			duration: 1300,						
			properties:{
				opacity: {start: "0", end: 1.0}
			}
		}).play();						
						
	});				
					
});		


function imgChange(currentImg,moveTo) {
	var linkNode = dojo.query(".entry"); 
	var captionNode = '<div class="imagecaption"></div>';
	var exitImg = '<div class="exitimg">close gallery</div>';
	var nextEntry = '<div class="nextimg">next</div>';
	var lastEntry = '<div class="lastimg">back</div>';	
	
	var i = 0;
	var currentPosition = 0; // current position in the array
	
	if(moveTo == "next") {

		for(i = 0; i < linkNode.length; i++) {

			if(linkNode[i].name == currentImg){
				if(i == linkNode.length - 1){
					var nextImg = linkNode[0].name;
					break;
				}
				else {	
					currentPosition = i;			
					var nextImg = linkNode[currentPosition + 1].name;
					break;
				}			
			}
		}
	}
	else {
		for(i = 0; i < linkNode.length; i++) {

			if(linkNode[i].name == currentImg){
				if(i == 0){
					var nextImg = linkNode[linkNode.length - 1].name;
					break;
				}
				else {	
					currentPosition = i;			
					var nextImg = linkNode[currentPosition - 1].name;
					break;
				}			
			}
		}

	}
	
	var tempImg = document.createElement("img"); 
	tempImg.src = nextImg;		

	var imgLayer = dojo.query("div#imagewrapper");
	var imgName = nextImg;
	var wrapperLayer = dojo.query("div#gallerywrapper");
	var caption = this.title; // get the caption			
	
	var imgCaption = dojo.query("div .imagecaption");		

	imgCaption.forEach(function addCap(x){
		x.innerHTML = nextEntry + lastEntry + exitImg;
	});

	// inserting the node used to exit from the image
		
	var exitItem = dojo.query("div .exitimg");

	exitItem.connect("onclick",function(evt) { exitNode(); });	

	// traverse the linkNode array to get another gallery entry	

	var nextItem = dojo.query("div .nextimg");

	// have to find the images place in the DOM to correctly get the next image

	nextItem.connect("onclick",function(evt)  {			
		imgChange(imgName,"next");
	});

	var lastItem = dojo.query("div .lastimg");

	lastItem.connect("onclick",function(evt)  {
		imgChange(imgName,"last");		
	});
		
	var leftVal = "-" + tempImg.width/2 + "px"; // offset from left

	wrapperLayer.style({
		"margin": "0 0 0 50%", 	
		"width": tempImg.width + "px",
		"left": leftVal,
   	"height": tempImg.height + "px"
	});

	imgLayer.style({
		"margin": "0 0 0 50%", 
		"left": leftVal,
   	"height": tempImg.height + "px",
		"backgroundImage": "url("  + imgName + ")",
		"backgroundRepeat": "no-repeat"
	});	
	
	imgLayer.animateProperty({	
		duration: 1000,						
		properties:{
			width: {start: "0", end: tempImg.width},
			opacity: {start: "0", end: 1.0}
		}
	}).play();

	imgCaption.style({
		"margin": "0 0 0 50%", 
		"top": tempImg.height + 80 + "px",
		"width": tempImg.width + "px",
		"left": leftVal
	});	
		
	imgCaption.animateProperty({	
		duration: 1300,						
		properties:{
			opacity: {start: "0", end: 1.0}
		}
	}).play();					
		
}

function exitNode() {
	// orphan the current image
	dojo.query("div#imagewrapper").orphan();
	dojo.query("div#opacitylayer").orphan();
	dojo.query("div#gallerywrapper").orphan();
	dojo.query("div.imagecaption").orphan();
}

						