// Haverford news galleries
// requires jQuery 1.4

$(function() { // on DOM ready
	if(news_photos.length>1) { // if a gallery is needed
		var image = $('#newsimg'),
			photos = new Array(),
			current = 0,
			height = 0,
			width = image.width();
		image.height(image.height()) // fix its height
			.empty(); // and empty it
		$.each(news_photos,function(index) { // with each photo for the story
			news_photos[index].element = $('<div class="news_image"><img src="'+this.file+'" alt="Photo" width="'+this.width+'" height="'+this.height+'"/>'+(this.caption ? '<div class="news_caption">'+this.caption+'</div>' : '')+'</div>').appendTo(image);
			news_photos[index].element.css('opacity',0).show();
			height = Math.max(height,news_photos[index].element.height()); // set the height to this height, if larger
			width = Math.max(width,this.width);
			news_photos[index].element.hide();
		});
		image.height(height); // set the div height appropriately
		image.width(width);
		image.find('.news_image').height(height);
		news_photos[0].element.show().css('opacity',1);
		image.append('<div id="news_image_controls"><a href="#" title="Previous image" id="news_image_prev" class="disabled">&laquo;</a><a href="#" title="Next image" id="news_image_next">&raquo;</a></div>');
		image.find('#news_image_prev,#news_image_next').click(function() {
			if($(this).is('.disabled')) return false; // return immediately the control is disabled
			current = $(this).is('#news_image_prev') ? current-1 : current+1; // increment/decrement the current
			image.find('.news_image').stop().css('z-index',0); // stop all news image animation, and remove the current image class
			news_photos[current].element.css('z-index',100).show().fadeTo(500,1,function() { // fade to the new image div, then
				$(this).siblings('.news_image').css('opacity',0).hide(); // hide the others
			});
			$('#news_image_prev').toggleClass('disabled',current==0);
			$('#news_image_next').toggleClass('disabled',current==news_photos.length-1);
			return false;
		});
	}
});
