var gallery = $("ul.gallery");
var currentImage = 0;
var currentPosition = 0;
var image = $('ul.gallery li:first');
var images = $('ul.gallery li');
var imageWidth = 645;
var totalImages = images.length;
var totalImagesWidth = imageWidth*totalImages;
var galleryPos = "-"+(imageWidth*(totalImages-1));
var descriptionPos = "0px";
var padding = 20;

addGallery = function() {
	$('.gallery').css("width",totalImagesWidth);
	$('.gallery').wrap('<div id="slideshow_mask"></div>')
	$('#slideshow_mask').wrap('<div id="slideshow"></div>');	
}

setDescription = function() {
	
		var info_title = image.find('h3').text();
		var info_desc = image.find('p').text();
		image.append('<div id="first_item" class="description"><div class="info_container"><div class="info"><h3>'+info_title+'</h3><p>'+info_desc+'</p><p class="toggle_desc"><a href="">+ Read More</a></p></div></div><div class="overlay"></div></div>');
		
		var info_height = ( $("#first_item .info").height()+padding );
		$("#first_item").css("height", info_height+"px");
		$("#first_item .overlay").css("height", info_height+"px");
		
		$("#first_item").css("bottom","-"+(info_height-padding)+"px");
		
		$("#first_item").find('a').bind( 'click', function() {
				setToggle("#first_item",info_height)
				return false;
		});	
		
}

setToggle = function(targetDesc, targetHeight) {
	var current = $(targetDesc).css("bottom");
	var button = $(targetDesc).find('a');
	if (current == descriptionPos) {
		button.removeClass("selected");
		$(targetDesc).animate({"bottom": "-"+(targetHeight-padding)+"px" });
	} else {
		$(targetDesc).animate({"bottom": 0 });
		button.addClass("selected");
	}
};

resetToggle = function() {
		var button = $("#first_item").find('a');
		button.removeClass("selected");
		var targetPos = ( $("#first_item .info").height() );
		$("#first_item").animate({"bottom": "-"+(targetPos)+"px" });
}

setThumbScrollPosition= function(scrollPosition) {
	resetToggle();
	var i =0;
	if (i < scrollPosition && currentPosition > galleryPos) {
		var newPosition = currentPosition - imageWidth;
		gallery.animate({"left":newPosition+"px"});
		currentPosition = newPosition;
	} else  {
		var newPosition = currentPosition + imageWidth;
		gallery.animate({"left":newPosition+"px"});
		currentPosition = newPosition;
	}
}

scrollButtonsThumbnails= function() {
	var prev = $("#previous");
	var next = $("#next");
	if(currentPosition == 0) {
		prev.css("visibility","hidden");
	} else {
		prev.css("visibility","visible");
	}
	if(currentPosition == galleryPos) {
		next.css("visibility","hidden");
	} else {
		next.css("visibility","visible");
	}
}

setNav = function() {
	if (totalImages > 1 ) {
		$('<p id="next" class="slide_nav">Next</p>')
		.bind("click", function(){
			setThumbScrollPosition(1);
			scrollButtonsThumbnails();
		})
		.appendTo('#slideshow');
	
		$('<p id="previous" class="slide_nav">Previous</p>')
		.bind("click", function(){
			setThumbScrollPosition(-1);
			scrollButtonsThumbnails();
		})
		.appendTo('#slideshow');
	}
}

$(document).ready(function(){
	addGallery()
	setNav();
	scrollButtonsThumbnails();
	setDescription();
});
