/*
$(document).ready(function() {
  $('#projectLoaded').hide();
  $('.projectThumbs a').click(function() {
    $('#projectLoaded').animate({ 
			height: 'show',
			easing: 'easeInOutCubic'}, 2000, function(){
        $('#projectLoaded').hide();
        $('#projectLoaded2').html($('#projectLoaded').html());
        $('#projectLoaded').html('');
      	$("div.scrollable").scrollable({
      		size: 1,
      		items: '#thumbs',  
      		hoverClass: 'hover',
      		loop: true, 
      		speed: 1800,
      		easing: 'easeOutQuart'
      	});	
        $('#closeProject a').click(function() {
          $('#projectLoaded').show();
          $('#projectLoaded').html($('#projectLoaded2').html());
          $('#projectLoaded2').html('');
          $('#projectLoaded').animate({ 
      			height: 'hide',
      			easing: 'easeInOutCubic'}, 2000, function(){
          });
          return false;
        });
      });
      return false;
  });
});
*/

var current_project = '';
var images = 0;
var images_counter = 0;

$("#preloader").ajaxStart(function(){
 $(this).show();
});
$("#preloader").ajaxComplete(function(){
 $(this).hide();
});

function loadProject(slug)
{
  //alert(current_project+';'+slug)
  if (current_project != slug) {
    $('#projectLoaded').hide();
    $('#projectLoaded2').hide();
    $("#preloader").show();
    $("#projectLoaded").load("/projects/"+slug+"/", {}, function(){prepareProject();});
    current_project = slug;
  } else {
    if ($('#projectLoaded').html() == '') {
      $('#projectLoaded').hide();
      $('#projectLoaded2').hide();
      $("#preloader").show();
      $("#projectLoaded").load("/projects/"+slug+"/", {}, function(){prepareProject();});
      return;
    }
    $('#projectLoaded').animate({ 
      height: 'show',
      easing: 'easeInOutCubic'}, 2000, function(){
        $('#projectLoaded').hide();
        $('#projectLoaded2').html($('#projectLoaded').html());
        $('#projectLoaded').html('');
        initScrollableThumbs();
      });
  }
}

function initScrollableThumbs()
{
	$("div.scrollable").scrollable({
		size: 1,
		items: '#thumbs', 
		//interval: 4000, 
		hoverClass: 'hover',
		loop: true, 
		speed: 1800,
		easing: 'easeOutQuart'
	});		
}
function prepareProject()
{
  images = $('#thumbs>div>img').length;
  images_counter = 0;
  project_opened = false;
  setTimeout('openProject()', 2000);
  $('#thumbs>div>img').each(function(){$(this).load(function(){
    images_counter++;
    if (images_counter == images) {
      openProject();
    }
  })});
}
var project_opened = false;
function openProject()
{
  if (project_opened) return;
  project_opened = true;
  $('#projectLoaded').animate({ 
    height: 'show',
    easing: 'easeInOutCubic'}, 2000, function(){
      $('#projectLoaded').hide();
      $('#projectLoaded2').html($('#projectLoaded').html());
      $('#projectLoaded').html('');
      $('#projectLoaded2').show();
      initScrollableThumbs();
    });
  $("#preloader").hide();
}
function closeProject()
{
  project_opened = false;
  $('#projectLoaded').show();
  $('#projectLoaded').html($('#projectLoaded2').html());
  $('#projectLoaded2').html('');
  $('#projectLoaded').animate({ 
    height: 'hide',
    easing: 'easeInOutCubic'}, 2000, function(){
  });
}
