$(function() {
	
	$("div.scrollable").scrollable({size:5}).circular();
	//preload gallery images 
	$("ul.items li a").each(function() {
		var newImgPath = $(this).attr("href");
		img = new Image();
		img.src=newImgPath;
	});
	
	
	$("ul.items li a").click(function() {
		
		//$(".main_project_image img").remove();
		
		if( !$(this).parents(".image_gallery").hasClass("opened") ) {
			
			$("<img class='gallery_main_img' src="+$(this).attr('href')+" alt=''></img>")
			.appendTo($(this).parents(".image_gallery").find(".main_image"));
			
			$(this).parents(".image_gallery").find(".main_image").slideDown();
			$(this).parents(".image_gallery").addClass("opened");
			$(this).parents(".image_gallery").find(".open_gallery").text("Click to close gallery");
			
			$.scrollTo($(this).parents(".image_gallery"),800);	
			return false;	
		}
		else {
			$(this).parents(".image_gallery").find(".main_image img").attr("src", $(this).attr('href'));
		}
		
		return false;
		
	});
	
	$("a.open_gallery").click(function() {
		
		//$(".main_project_image img").remove();
		
		if( !$(this).parents(".image_gallery").hasClass("opened") ) {
			
			$("<img class='gallery_main_img' src="+$(this).parent('.image_gallery').find('ul.items li:first-child a').attr('href')+" alt=''></img>")
			.appendTo($(this).parent(".image_gallery").find(".main_image"));
			$(this).parent(".image_gallery").find(".main_image").slideDown();
			$(this).text("Click to close gallery");
			
			$(this).parents(".image_gallery").addClass("opened");
			
			$.scrollTo($(this).parents(".image_gallery"),800);	
		}
		else {
				$(this).parent(".image_gallery").find(".main_image").slideUp( 500, function(){ 
						
						$(this).parents(".image_gallery").find(".main_image img").remove();
					});
				$(this).text("Click to see the full images");

				$(this).parents(".image_gallery").removeClass("opened");

				$.scrollTo($(this).parents(".image_gallery"),800);
		}
		
		return false;
		
	});

});
