//BOF - Shane - Edit - Functions used to switch between images
function darkbox_left(){
	if(darkbox_count > 0){
		darkbox_count = darkbox_count - 1;
		$("#darkbox_image").attr("src", alt_images[darkbox_count]);
		$(".darkbox-frame .darkbox-canvas #darkbox-right").show();
		if(darkbox_count == 0){
			$(".darkbox-frame .darkbox-canvas #darkbox-left").hide();
		}
	}else{
		darkbox_count = 0;
	}
}

function darkbox_right(){
	if(darkbox_count < alt_images.length - 1){
		darkbox_count = darkbox_count + 1;
		$("#darkbox_image").attr("src", alt_images[darkbox_count]);
		$(".darkbox-frame .darkbox-canvas #darkbox-left").show();
		if(darkbox_count == alt_images.length - 1){
			$(".darkbox-frame .darkbox-canvas #darkbox-right").hide();
		}
	}else{
		darkbox_count = alt_images.length - 1;
	}
}
//EOF - Shane - Edit - Functions used to switch between images

$(document).ready(function() {

		//BOF - Shane - Edit - So we can keep track of image we are displaying
		darkbox_count = 0;
		//EOF - Shane - Edit - So we can keep track of image we are displaying
	
		$('#productMainImage a').click(function() {
			
			var link = $(this);
	
			if(!$('div.darkbox-frame').length) {
				//BOF - Shane - Edit - Added the previous and next buttons
				//darkbox = $('<div class="darkbox-frame"><div class="darkbox-shadow"></div><div class="darkbox-canvas"><div class="darkbox-button"></div></div></div>').appendTo('body');
				darkbox = $('<div class="darkbox-frame"><div class="darkbox-shadow"></div><div class="darkbox-canvas"><div id="darkbox-left"><a href="javascript:darkbox_left();"></a></div><div id="darkbox-right"><a href="javascript:darkbox_right();"></a></div><div class="darkbox-button"></div></div></div>').appendTo('body');
				//EOF - Shane - Edit - Added the previous and next buttons
			}
			
			var frame = darkbox.clone().appendTo('body').addClass('darkbox-frame-on');

			var shadow = frame.find('div.darkbox-shadow').animate({opacity:0.8},300);
			var canvas = frame.find('div.darkbox-canvas');
			var button = frame.find('div.darkbox-button');

			var image = $('<img id="darkbox_image" src="'+ link.attr('href') +'" alt="'+ link.attr('title') +'"/>');

			//BOF - Shane - Edit - Stops previous and next buttons appearing if only one image / no alternative images
			if(alt_images.length == 1){
				$(".darkbox-frame .darkbox-canvas #darkbox-right").hide();
			}
			//EOF - Shane - Edit - Stops previous and next buttons appearing if only one image / no alternative images
			
			image.appendTo(canvas);
			image.load(function(){

				var imageWidth = image.width();
				var imageHeight = image.height();
				var frameWidth = frame.width()-40;
				var frameHeight = frame.height()-40;

				if(imageWidth > frameWidth) {

					imageWidth = frameWidth;
					image.width(imageWidth);					
					while(image.height() > frameHeight) {
						image.width(imageWidth);
						imageWidth--;
					}

					imageHeight = image.height();
				}

				if(imageHeight > frameHeight) {

					imageHeight = frameHeight;
					image.height(imageHeight);						
					while(image.width() > frameWidth) {
						image.height(imageHeight);
						imageHeight--;
					}

					imageWidth = image.width();
				}
				
				canvas.addClass('darkbox-canvas-load').animate({

					width:imageWidth,
					marginLeft:-imageWidth/2,
					height:imageHeight,
					marginTop:-imageHeight/2

				},500,function() {

					canvas.addClass('darkbox-canvas-done');
					button.addClass('darkbox-button-on');
					button.addClass(navigator.platform.toLowerCase().indexOf('mac')+1?'darkbox-button-left':'darkbox-button-right');

					image.animate({opacity:1},500,function() {

						shadow.click(closer);
						button.click(closer);

					});
				});
			});

			var closer = function() {
		
				canvas.remove();
				shadow.animate({opacity:0},300,function() {
					frame.remove();
				});
			}

			$(document).keydown(function(e) {
				if(e.which==27) closer();
				//bof - shane - edit - allows switching between images using the arrors keys
				if(e.which==37) darkbox_left();
				if(e.which==39) darkbox_right();
				//eof - shane - edit - allows switching between images using the arrors keys
			});

			return false;
		});
		
		
		
	});
