var itemWidth = 296
var itemsPerSlide = 3;
var duration = 500;
var separatorWidth = 50;

function focusCategory (category) {

  if (category == null) {
    return;
  }
  var left = 0;


  if (category == '1') {
    left = '0px'
  } else {
    left = 0;
    $('.category .top-shelf ul').each(function (i) {
      if (i < parseInt(category) - 1) {
        left = left + itemWidth * $(this).children().length;
      }
    });

    left = 0 - Math.abs(left);
    //separators = category * separatorWidth;
    //separators = 0 - Math.abs(separators);
    //left = left + separators;
    left = left + 'px';
  }

  $('#shelving').animate({
    left: left
  }, 500, function(){
    setActiveCategory('right');
  });
}

function setActiveCategory (direction) {


  var pos     =  parseInt($('#shelving').css( "left"));
  var pos1    = 0;
  var cat     = 1;
  var retval  = 0;

  // reset font weight

  $('#shelving .category').each(function(){
    pos1 = pos1 + Math.abs( parseInt ( $(this).css('width') ) );
    pos2 = Math.abs( parseInt ( $('#shelving').css( "left") ) );


    cat++;

    if (pos == 0) cat = 1;
    var i = 1;

    if (
      pos == 0 ||
      (direction == 'right' && pos1 == pos2) ||
      (direction == 'left' && pos1 - itemWidth == pos2)
      ) {
      if (direction == 'left' && pos != 0) cat--;
      retval = cat;
      $('#shelving-controls-category a').each(function () {
        if (i == cat) {
          $(this).css("font-weight", "bold");
        } else {
          $(this).css("font-weight", "normal");
        }
        i++;
      });
    } else {
    }
    return retval;
  });

}

function slide (direction, shelf) {
  if (shelf.is(':animated')) {
    return;
  }

  var pos =  shelf.css( "left");
  var slider_length = 0;
  var seps = 0;

  // remove any popups
  if ($('#active-popup').css('display') == 'block')
    $('#active-popup').css('display', 'none');

  $('#shelving .separator').each(function (i) {
    seps = seps + 1;
  });

  $('.category .top-shelf ul').each(function (i) {
    slider_length += itemWidth * $(this).children().length;
  });
  slider_length += seps * separatorWidth;

  slider_length -= itemsPerSlide * itemWidth;

  slider_length = 0 - Math.abs(slider_length);
  if (direction == 'left') {
    step = '+=' + itemWidth + 'px';
  }
  else if (direction == 'right') {
    step = '-=' + itemWidth + 'px';
  }

  if (parseInt(pos) < slider_length - itemWidth && direction == 'right') {
    return;
  /*
          elems = 0 - elems;
          step = '+=' + elems + 'px';
           */
  }
  if (parseInt(pos) >= 0 && direction == 'left') {
    return;
  /*
          elems = 0 - elems;
          step = '-=' + elems + 'px';
           */
  }

  shelf.animate({
    left: step
  }, duration, function(){
    setActiveCategory(direction);
  });

}
$(document).ready( function(){

  setActiveCategory('right');

  // shelf slide
  $('#shelving-controls .previous').click(function() {
    slide ('left', $('#shelving'));
    return false;
  });
  $('#shelving-controls .next').click(function() {
    slide ('right', $('#shelving'));
  });

  // category controls
  $('#shelving-controls-category a').click(function() {
    var category = $(this).attr('class');
    focusCategory (category);
    return false;
  });

  // popups
  $('#shelving .hover').hover (
    function (event) {

      var tPosX = event.pageX - 80;
      var tPosY = event.pageY + 180;

      $('#active-popup').css({
        top: tPosY,
        left: tPosX
      });
      $('#active-popup').css('display', 'block');

      var popup = $(this).parent().find ('.popup');
      $('#active-popup').html(popup.html());

	addthis.button(".addthis_button");

	  
	  
    },
    function () {
      return;
    //$('#active-popup').css('display', 'none');
    }
    );

  $('#active-popup').hover (
    function () {
      return;
    },
    function () {
      $(this).css('display', 'none');
    });

  $('#background').hover (
    function () {
      return;
    },
    function () {
      if ($('#active-popup').css('display') == 'block')
        $('#active-popup').css('display', 'none');
    });

});

