$(document).ready(function() {

  var idx = 0;
  var cur = 0;
  $("#book-slider li").each(function() {
    
    if($(this).hasClass('selected')) { cur = idx; }
    ++idx;
    
  });
  var max = idx - 1;
  
  // scroll list to current position
  if(cur > max - 3) { cur = max - 3 < 0 ? 0 : max - 3; }
  $("#book-slider ul").css('top',(cur * -80) + 'px'); 
  if(cur <= 0) { $("#book-slider .scroll-prev img").attr("src","/images/inactive/book-slider-prev.png");    }
  if(cur >= max - 3) { $("#book-slider .scroll-next img").attr("src","/images/inactive/book-slider-next.png"); }
  
  $("#book-slider .scroll-prev").click(function() {
    if(cur > 0) {
      cur = cur - 1;
      $("#book-slider ul").animate({ top: (cur * -80) + "px" }, 
        500, 
        "swing", 
        function() {
          if(cur == 0) {
            $("#book-slider .scroll-prev img").attr("src","/images/inactive/book-slider-prev.png");   
          } else {
        	$("#book-slider .scroll-prev img").attr("src","/images/hover/book-slider-prev.png");   
          }
          $("#book-slider .scroll-next img").attr("src","/images/base/book-slider-next.png");   
        });
    }
    
    
    
    return false;
  });
  
  $("#book-slider .scroll-next").click(function() {
    if(cur < max - 3) {
      cur = cur + 1;
      $("#book-slider ul").animate({ top: (cur * -80) + "px"}, 
        500, 
        "swing",
        function() {
          if(cur == max - 3) {
        	$("#book-slider .scroll-next img").attr("src","/images/inactive/book-slider-next.png");   
          } else {
        	$("#book-slider .scroll-next img").attr("src","/images/hover/book-slider-next.png");   
          } 
          $("#book-slider .scroll-prev img").attr("src","/images/base/book-slider-prev.png");   
        });     
    }
    
    return false;
  });
  
  $(".remove-photo").click(function() {
    
    var href = $(this).attr('href');
    if(href) {
      
      href = href.replace('remove','removeAjax');
      $.get(
          href,
          {ajax:"true"},
          function(data) {
          
            if(data) {
            
              var id = parseInt(data);
              if(id > 0) {
                $('.photo_'+ id).hide('slow',function() {
                  $(this).remove();
                });
              }
            }
      });
    }
    return false;
  });  
  
  $(".add-photo").click(function() {
  
    var href = $(this).attr('href');
    if(href) {
      
      href = href.replace('add','addAjax');
      $.get(
          href,
          {ajax:"true"},
          function(data) {
      
        var id = parseInt(data);
        if(id > 0) {
          
          buildDialogDiv(false, "Photo Added", 'Your photo has been added to your <a href="/inspiration/myBook">Photo Book</a>.');
          displayDialogWindowIfAvailable();
        } 
      });
    }
    return false;
  });
});
