var sd_padding = 20;
var sd_debug = false;

function sd_show_viewer(e) {
	var target = e.target;

	// + ignore clicks with modifiers
	if(e.altKey == true || e.metaKey) return true;

	while(target.nodeName != 'A') {
		target = target.parentNode;
	}

	// + set nav state
	// 	 + if we're looking at the first image
	if(sd_images.get(0).id == target.id) {
		$("#sd_prev").addClass('disabled');
	} else {
		$("#sd_prev").removeClass('disabled');
	}
	// 	 + if we're looking at the last image
	if(sd_images.get(sd_images.length-1).id == target.id) {
		$("#sd_next").addClass('disabled');		
	} else {
		$("#sd_next").removeClass('disabled');			
	}

	$('#sd_dimmer').fadeIn(100);
	$('.sd_nav').fadeIn(100);
	
	sd_load_image_in_viewer(target);
	
	return false;
}

function sd_load_image_in_viewer(target) {
	var src = target.href;

	// + if a large alternate exists, show it
	// + otherwise show the original since it's less than 1000 px wide
	var image_alternate = $('#'+ target.id +'-alternates .large');
	if( image_alternate !== undefined && image_alternate.length > 0) {
		var ni_src = image_alternate.attr('href');	
	} else {
		var ni_src = src;
	}
	
	// + show dimmer and loading feedback
	// - avoid showing if images are cached?
	$("#sd_loading").fadeIn(100);
	
	// + load the large version of the image into the viewer	
	var viewer = $('#sd_viewer');
	var ni = new Image();
	$(ni).load(function() {

		viewer.css('top', window.pageYOffset ); 
		var width = ni.width;
		var height = ni.height;
		// + check to see if the image is wider than the browser window
		if(width + 3*sd_padding >= $(window).width()) {
			width = Math.ceil($(window).width() - 8*sd_padding);
			height = Math.ceil( width / ni.width * ni.height );
		}
		
		// + check to see if the image is taller than the browser window
		if( height + 2*sd_padding >= $(window).height()) {
			height = Math.ceil($(window).height() - 3*sd_padding);
			width = Math.ceil( height / ni.height * ni.width );
		}
		
		viewer.css('width',  width); 
		// + center horizontally
		viewer.css('margin-left', -1 * (width/2) );
		// + center vertically
		
		$('#sd_viewer img').attr('src', ni.src).attr('width', width - 2*sd_padding).attr('id', 'sd-'+target.id);		
		
		if(!jQuery.support.opacity) {		
			// works in ie7, seems to be the proper approach
			viewer.css('margin-top', Math.ceil( $(document).scrollTop() + ( $(window).height() - height)/2 ) );
			viewer.fadeIn(200);
		} else {
			// works in safari and firefox, for some reason
			viewer.css('margin-top', Math.ceil( ( $(window).height() - height)/2) );
			
			//viewer.css('display', 'block').animate({ top: Math.ceil( ( $(window).height() - height)/2)}, 200 );
			viewer.slideDown(100);
		}

		// + hide loading feedback
		$("#sd_loading").fadeOut(400);
	})
	.attr('src', ni_src);
}

function sd_hide_viewer(e) {
	$("#sd_viewer").hide();	
	$(".sd_nav").hide();		
	$("#sd_dimmer").fadeOut(50);
}

function sd_next_image(e) {
	e.stopPropagation();
	var img = $("#sd_viewer > img");
	var id = img.attr('id').substring(9);
	
	var next = false;	
	var target = null;
	var i=0;
	
	// - inefficient to grab this collection and iterate through it every time
	// 	 + better to grab it once 
	// 	 - and flag the current position
	
	sd_images.each(function() {
		if(next == true) {
			target = this;
			return false;
		}
		if( this.id == 'image-'+id) next = true;
		i++;
	});
	
	// set button state
	$("#sd_prev").removeClass('disabled');
	if(i == sd_images.length - 1 || target == null) {
		$("#sd_next").addClass('disabled');
	} else {
		$("#sd_next").removeClass('disabled');		
	}
	
	if(target != null) {
		sd_load_image_in_viewer(target);
	}
}

function sd_prev_image(e) {
	e.stopPropagation();
	var img = $("#sd_viewer > img");
	var id = img.attr('id').substring(9);

	var prev = false;	
	var target = null;
	var i=0;
	sd_images.each(function() {
		if( this.id == 'image-'+id) prev = true;
		if(prev == true) {
			return false;
		}
		target = this;
		i++;
	});
	
	// set button state
	$("#sd_next").removeClass('disabled');
	if(i == 1 || target == null) {
		$("#sd_prev").addClass('disabled');
	} else {
		$("#sd_prev").removeClass('disabled');		
	}
	
	// load image
	if(target != null) {
		sd_load_image_in_viewer(target);
	}
}

function sd_show_note(e) {
	var target = $(e.target);
	target.animate({left: '-20px'}, {duration: '100', easing: 'easeOutBack'});
}

function sd_hide_note(e) {
	var target = $(e.target);
	target.animate({left: '-30px'}, {duration: '100', easing: 'easeOutBack'});
}

var sd_images = null;	
$(document).ready(function(){

	if( $.browser.msie && $.browser.version < 7) return;
	
	$("#sd_dimmer").click(function(e) {
		sd_hide_viewer(e);
	});

	$("#sd_next").click(function(e) {
		sd_next_image(e);
	});
	
	$("#sd_prev").click(function(e) {
		sd_prev_image(e);
	});	
	
	sd_images = $(".gallery-item a")
	sd_images.click(function(e) {
		return sd_show_viewer(e);
	});
	
	$(".note").hover(
	  function (e) {
		sd_show_note(e);
	  },
	  function (e) {
		sd_hide_note(e);
	  }
	);

});

