﻿$(document).ready(function(){
  var index = 0;
  var width = cwidth($('.slidebox .slidecontainer:first'));
  $('.showcaseImg').attr('src', '../utils/getshowcasephoto.aspx?id=' + $('.showcaseID:first').attr('id'));
  
  //-- init --//
  $('.slidecontainers').width($('.slidebox .slidecontainer').length * width);
  $('.slidebox').height(cheight($('.slidebox .slidecontainer:first')));
  
  $('.sidenav-list li a').click(function(){
    var target = $('.slidecontainers ' + this.hash);
    if (target.length == 1){
      slide($('.slidecontainer').index(target));
    
      return false;
    }
  });
  
  $('.slideprev').click(function(){
    if (index > 0 && $('.slidebox:not(:animated)').length > 0){
      slide(index - 1);
    }
    else
    {
			slide($('.slidebox .slidecontainer').length - 1);
    }
  });
  $('.slidenext').click(function(){
    if (index < ($('.slidebox .slidecontainer').length - 1) && $('.slidebox:not(:animated)').length > 0) {
      slide(index + 1);
    }
    else
    {
			slide(0);
    }
  });
  
  function slide(newindex){
    if (index != newindex){
      var curheight = cheight($('.slidebox .slidecontainer:eq(' + index + ')'));
      var newheight = cheight($('.slidebox .slidecontainer:eq(' + newindex + ')'));
      
      if (newheight > curheight){
        $('.slidebox').animate({ height:newheight }, 200);
      }
        
      $('.slidebox').animate({ scrollLeft:width * newindex }, 500, function(){
        $('.showcaseImg').attr('src', '../utils/getshowcasephoto.aspx?id=' + $('.showcaseID:eq('+index+')').attr('id'));
      });
        
      if (newheight < curheight){
        $('.slidebox').animate({ height:newheight }, 200);
      }
      
      index = newindex;
    }
  }
  
  function cwidth(e){
    return parseInt(e.width(),10) + parseInt(e.css('padding-left'),10) + parseInt(e.css('padding-right'),10);
  }
  
  function cheight(e){
    return parseInt(e.height(),10) + parseInt(e.css('padding-top'),10) + parseInt(e.css('padding-bottom'),10);
  }
});

