// This overrides the _goToItem method from gallery.js
// and adds two new methods '_copyContent' & '_displayAnimation'
jQuery.extend(Gallery.prototype, {
	init: function () {
		this._setControlEvents();
		this._setCount(this.current_item);
		this._setAutoPlay();
		
		// Just add one more aspect: make it show the initial image
		this._goToItem(0);
	},
	
	_goToItem: function (item_index) {
		this._copyContent(item_index);
		this._setCount(item_index);
	},
	
	_copyContent: function (item_index) {
		var element = this.items[item_index];
		var headline = $(element).children("h3").text();
		var description = $(element).children(".description").text();
		var year = $(element).children("p.year").text();
		var image = {
			src: $(element).children("img").attr("src"),
			alt: $(element).children("img").attr("alt")
		};
		var scope = this;
		$(".imagedetails h3").animate({marginLeft: -25, opacity: "hide"}, 500, function () {
			$(".imagedetails h3").text(headline);
			$(".imagedetails h3").css({marginLeft: 25}).animate({marginLeft: 0, opacity: "show"}, 500);
		});
		
		$(".imagedetails .description").animate({marginLeft: -25, opacity: "hide"}, 500, function () {
			$(".imagedetails .description").text(description);
			$(".imagedetails .description").css({marginLeft: 25}).animate({marginLeft: 0, opacity: "show"}, 500);			
		});
		
		$(".imagedetails p.date").text(year);
		$("#gallery-wrapper img:first").animate({marginLeft: -25, opacity: "hide"}, 500, function () {
			$("#gallery-wrapper img:first").attr("src", image.src).attr("alt", image.alt) // Set the source and alt 
			$("#gallery-wrapper img:first").css({marginLeft: 25}).animate({marginLeft: 0, opacity: "show"}, 500); // Animate 
		});
	}	
});

$(function () {
	// Only add to gallery if text is present in image description.
	var items = new Array();
	for(i=0; i<$(".gallery-item").length; i++){
		if($(".gallery-item:eq("+i+")").children().text().length > 5){
			items.push($(".gallery-item:eq("+i+")"));
		}
	}
		
	new Gallery($("#gallery-wrapper"), items, $('.imagedetails .navigation .left'), $('.imagedetails .navigation .right'), {count_display: $(".imagedetails .navigation .current")});
});