$(function() {  

  var doc = $(this);
  
  // Auto redirect
  if (window.location.hash != '') {    
    var hash = window.location.hash.substr(1,window.location.hash.length);
    var i = parseInt(hash, 10);
    if ( i > 0 && $('#slide_'+i).length > 0) {
      $('#ui-slides a.active:first').removeClass('active');
      $('#dl'+i).addClass('active');
      
      $("div.slide:visible:first").hide();
      $("#slide_"+i).show();
      
      show_text(i);
    }
  }  
  
  
  // Open overlay
  $('#ui-slides a').click(function(e){         
    var link = $(this);
    
    // Remove class
    $('#ui-slides a.active:first').removeClass('active');
    link.addClass('active');
    
    $("div.slide:visible:first").hide();
    $("#slide_"+link.text()).show();
    
    show_text(link.text());
    window.location.hash = link.text();
        
    e.preventDefault();
  });
  
  
  $(".slide").click(function(e){
    var slide = $(this);
    var total_slides = $(".slide").length;

    var match = /slide_(\d+)/.exec(slide.attr('id'));
    var current_index = parseInt(match[match.length-1],10);
    var next_index = (current_index >= total_slides) ? 1 : (current_index+1);
    
    slide.hide();
    $('#slide_'+next_index).show();

    $('#ui-slides a.active:first').removeClass('active');
    $('#dl'+next_index).addClass('active');

    show_text(next_index);
    window.location.hash = next_index;         

    e.preventDefault();
  }); 
  
  
  //$("#sceen img:eq("+rand(3)+")").show();
  $("#sceen img:visible").css("z-index", 10);
  
  //$("#sceen img").click(function(e){
  //  var sceen = $(this)
  //  var total_images = $("#sceen img").length
  //
  //  var match = /img_(\d+)/.exec(sceen.attr('id'))
  //  var current_index = parseInt(match[match.length-1],10)
  //  var next_index = (current_index >= total_images) ? 1 : (current_index+1)
  //  
  //  sceen.hide()
  //  $('#img_'+next_index).show()
  //  
  //  e.preventDefault()
  //})
  
  if ( $("#sceen").length > 0 ) {
    doc.oneTime(3500, function() {
      fade_in_next_slide();
    });     
  }
    
});


function show_text(i) {
  // Show text
  if ( $(".left-column div:not('.h1')").length > 0 ) {
    var show_class = $('#slide_'+i).children('img').attr('class');      
    $("."+show_class, $(".left-column")).show();
    $(".left-column div:not('.h1, ."+show_class+"')").hide();     
  }
  
};


function fade_in_next_slide() {
  var sceen = $("#sceen img:visible");
  var total_images = $("#sceen img").length;
  var match = /img_(\d+)/.exec(sceen.attr('id'));
  var current_index = parseInt(match[match.length-1],10);
  var next_index = (current_index >= total_images) ? 1 : (current_index+1);
    
  sceen.css("z-index", 10);
  sceen.addClass('previous');
  $('#img_'+next_index).css("z-index", 20);
  $('#img_'+next_index).fadeIn('slow', delay_for_next_slide);
  
};

function delay_for_next_slide() {
  var prev = $("#sceen img.previous:first").hide();
  prev.removeClass('previous');
  $(this).oneTime(3500, function() {
    fade_in_next_slide();
  });
};


function rand(n) {
  return Math.round((n)*Math.random());
};

