// Gallery object
var gallery = {};

// Initialize
gallery.init = function() {

  // Initial Slides
  gallery.start = 3;
  // Moving Length
  gallery.mvLen = $("#gallery li").width();
  // Total Clicks
  gallery.clicked = gallery.clicks = $("#gallery li").length - gallery.start;
  
  // 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;
  });
  
  // Loading initial content
  $("#title").html($("#gallery li:nth-child(1)").find('.full_title').html());
  $("#text").html($("#gallery li:nth-child(1)").find('.desc').html());
  gallery.dynScroll();
  url = $("#gallery li:nth-child(1)").find('.big_image').html();
  $("#big_image").attr('href', url);
  $("#big_image img").attr('src', url);
  
  // Change gallery content on image click
  $("#gallery li a").click(function() {
    // Change Title
    $("#title").html($(this).parent().find('.full_title').html());
    // Change Text
    $("#text").html($(this).parent().find('.desc').html());
    // Scroll Fucntion
    gallery.dynScroll();
    // Gallery Big Image
    url = $(this).parent().find('.big_image').html();
    $("#big_image").attr('href', url);
    $("#big_image img").attr('src', url);
    return false;
  });

}

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

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

// Dynamic scroll
gallery.dynScroll = function()  {
	$("#content").css({ 'top' : '0px' })
	$(".down-arrow a").unbind();
	$(".up-arrow a").unbind();
	scrol.init();
}

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