var customerSliderPage = 0, customerSliderPageTotal, customerSliderPageInterval;

$(function() {
  initSlider();
  initListeners();
  initCustomerSlider();
});

var initListeners = function() {
  $('.href').click(_linkInChild);
  $('#customers .hastip').tooltipsy({offset: [0, 0.1]});
  $('#customers2 .hastip').tooltipsy({offset: [0, 0.1], className: "tooltipsy2"});
  $('#customers').mouseover(stopCustomerSliderInterval);
  $('#customers').mouseleave(startCustomerSliderInterval);
};


var initCustomerSlider = function() {
  customerSliderPageTotal = Math.ceil($('#customers').outerHeight() / $('#customer_container').outerHeight());
  startCustomerSliderInterval();
};

var startCustomerSliderInterval = function() {
   customerSliderPageInterval = setInterval(moveCustomerSlider, 10000);
};

var stopCustomerSliderInterval = function() {
  clearInterval(customerSliderPageInterval);
};

var moveCustomerSlider = function() {
  customerSliderPage += 1;

  $('#customers').fadeOut(1000, function() {
    
    var posX;
    if (customerSliderPage == customerSliderPageTotal) {
      customerSliderPage = 0;
      posX = 0;
    } else 
       posX = -(customerSliderPage * 174);
       
    $('#customers').css({"top": posX}).fadeIn(1000);
    
  });
};

var initSlider = function() {
 
  $('#slider').nivoSlider({
    effect: 'boxRain',
    controlNav: false,
    directionNav: false,
    animSpeed: 2000,
    pauseTime: 7000,
    beforeChange: function() {
      $('#quote').fadeOut(800, function() {
        var data = $('#slider').data('nivo:vars');
        var ar = $(data['currentImage']).attr('src').split('/');
        var total = ar.length;
        var image_name = ar[total - 1];
        
        $('#quote p').text('"' + images[image_name]['quote'] + '"');
        $('#quote span').text("- " + images[image_name]['author']);
        $('#quote').css({'left': images[image_name]['posX'] + 'px', 'right': images[image_name]['posY'] + 'px'}).fadeIn(800);
      })
    }
  });
};

var _linkInChild = function() {
  window.location = $(this).find('a').attr('href');

 return false;
};
