$activeSwitch = 0;

$(document).ready(function(){
  $('#photobox div').css({opacity:0});
  $('#photobox img').ready(function(){
    $('#photobox .firstimg').animate({opacity:1},1000);
  });
  
  $('#bigimgcontainer img').ready(function(){
    $('#bigimgcontainer .active').css({display:'block'}).animate({opacity:1},1000);
    $('.phototext').css('display','block').html($('#bigimgcontainer .active img').attr('alt'));
  });
  
  // Album fade animation
  $('#thumbcontainer a').hover(function(){
    $(this).fadeTo('normal',.4);
  }, function(){
    $(this).fadeTo('normal',1);
  });
  
  // Links to switch images
  $('#prevbtn').click(function(){
    if($activeSwitch == 0){
      switchImg($('#bigimgcontainer div.active'),'prev');
      $(this).blur();
      return false;
    }
  });
  
  $('#nextbtn').click(function(){
    if($activeSwitch == 0){
      switchImg($('#bigimgcontainer div.active'),'next');
      $(this).blur();
      return false;
    }
  });
  
  // Key binds to switch images
  $(document).bind('keydown', 'left', function(){
    if($activeSwitch == 0){
      switchImg($('#bigimgcontainer div.active'),'prev');
    }
  });
  
  $(document).bind('keydown', 'right', function(){    
    if($activeSwitch == 0){
      switchImg($('#bigimgcontainer div.active'),'next');
    }
  });
  
  // Starting animation if visiting home page
  if($('body.homepage div.slideshow').length > 0){
    setInterval("slideSwitch()", 10000);
  }
  
  // Switch big images with fades
  $('.bigimg a').click(function(){
    switchImg($(this).parent(),'next');
    return false;
  });
  
  // Show the album
  $('#albumname').click(function(){
    $('#bigimgcontainer div.active').css({opacity: 0}).hide().removeClass('active');
    $('#bigimgcontainer').hide();
    $('.phototext').hide();
    $('#pagebtns').hide();
    $('#thumbcontainer').fadeIn();
  });
  
  // Show the big images
  $('#thumbcontainer a').click(function(){
    $imgid = $(this).parent().attr('id');
    $imgid = $imgid.substring(8,$imgid.length);
    
    $('#bigimgcontainer div#bigimbnum'+$imgid).css({opacity: 1}).show().addClass('active');
    $('.phototext').css('display','block').html($('#bigimgcontainer .active img').attr('alt')).show();
    $('#pagebtns').show();
    
    $('#bigimgcontainer').fadeIn();
    $('#thumbcontainer').hide();
    return false;
  });
});

function switchImg($elem, $way){
  if($('#bigimgcontainer').is(':visible')){
    $activeSwitch = 1;
    $active = $elem;
    if($way == 'prev'){
      $next = $active.prev().length ? $active.prev() : $('#bigimgcontainer div:last');
    }else{
      $next = $active.next().length ? $active.next() : $('#bigimgcontainer div:first');
    }
    
    $active.css({opacity: 0}).hide().removeClass('active');
    $next.show().animate({opacity: 1}, 300, function(){
      $next.addClass('active');
      $activeSwitch = 0;
      $('.phototext').css('display','block').html($('#bigimgcontainer .active img').attr('alt'));
    });
  }
}

// Animation on home page
function slideSwitch(){
  if($('#photobox div.firstimg').length != 0){
    $active = $('#photobox div.firstimg');
  }else{
    $active = $('#photobox div.active');
  }
  if($active.length == 0) $active = $('#photobox div:last');
  
  $next = $active.next().length ? $active.next() : $('#photobox div:first');
  $active.addClass('last-active');
  
  $active.animate({opacity: 0}, 4000);
  $next.addClass('active').animate({opacity: 1}, 4000, function(){
    $active.removeClass();
  });
}
