// Gallery object
var gallery = {};

// Initialize
gallery.init = function() {
  // Initial Slides
  gallery.start = 5;
  // Moving Length
  gallery.mvLen = 253;
  // Total Clicks
  gallery.clicked = gallery.clicks = $("#gallery li").length - gallery.start;
  // is animated
  gallery.isAnimated = false;
  
  // Show or hide arrows
  if(gallery.clicks > 0)
    $("#left").css({ 'visibility' : 'hidden' });
  else
    $("#left, #right").css({ 'visibility' : 'hidden' });    

  // Define moving actions
  $("#left").click(function() {
    gallery.moveRight();
    return false;
  });
  $("#right").click(function() {
    gallery.moveLeft();
    return false;
  });
  
}

// Gallery move left
gallery.moveLeft = function() {
  if(!gallery.isAnimated) {
    gallery.isAnimated = true;
    // Show left arrow
    $("#left").css({ 'visibility': 'visible' });
    // Move Thumb
    $("#thumb").animate({ 'left' : parseInt($("#thumb").css('left')) - gallery.mvLen+"px" }, 500, function()  {
      gallery.isAnimated = false;
    });
    // show or hide right arrow
    gallery.clicked--;
    if(gallery.clicked == 0)
      $("#right").css({ 'visibility' : 'hidden' });    
  }
}

// Gallery move right
gallery.moveRight = function()  {
  if(!gallery.isAnimated) {
    gallery.isAnimated = true;
    // Show right arrow
    $("#right").css({ 'visibility': 'visible' });
    // Move Thumb
    $("#thumb").animate({ 'left' : parseInt($("#thumb").css('left')) + gallery.mvLen+"px" }, 500, function()  {
      gallery.isAnimated = false;
    });  
    // show or hide left arrow
    gallery.clicked++;
    if(gallery.clicked == gallery.clicks)
      $("#left").css({ 'visibility' : 'hidden' });
  }
}

// Initiating gallery
$(document).ready(function()  {
  gallery.init();

  $("#thumb li a").click(function() {
    if($(this).parent().attr('class') != 'foto_active') {
      $('.foto_active').removeClass();
      $(this).parent().addClass("foto_active");
			$(".big_image img").fadeOut(200,function(){
					$(".big_image img").attr("src", $(".foto_active a").attr('href'));
					$(".big_image img").fadeIn(200);	
				})
      
    }
    return false;
  });

});
