// Featured listings scroller

var wrapWidth;
var origWidth;
var scrollSpeed = 1;
var imScrolling = false;

jQuery(document).ready(function () {
  // Triple our props for scrolling
  var propChunk = jQuery('.featured_props_list').html();
  origWidth = jQuery('.featured_props_list li.listing').size() * 170; // 170 is the width+padding of each li.listing
  jQuery('.featured_props_list').append(propChunk);
  jQuery('.featured_props_list').append(propChunk);

  // Figure out when to wrap
  wrapWidth = origWidth * -2;

  // Set initial offset
  jQuery('#scroller').css('left', (origWidth * -1) + 'px');

  jQuery('.next-horizontal').hover(
    function() {
      scrollSpeed = 10;
      imScrolling = true;
    },
    function() {
      scrollSpeed = 1;
      imScrolling = false;
    }
  );

  jQuery('.prev-horizontal').hover(
    function() {
      scrollSpeed = -10;
      imScrolling = true;
    },
    function() {
      scrollSpeed = 1;
      imScrolling = false;
    }
  );

/*
  jQuery('li.listing').hover(
    function() {
      imScrolling = false;
    },
    function() {
      imScrolling = true;
    }
  );
*/
});

setInterval(scrollForwards, 50);

function scrollForwards() {
  if (imScrolling == true) {
    var offset = parseInt(jQuery('#scroller').css('left'));
//console.log(offset + ' vs ' + wrapWidth);
    if (offset <= wrapWidth || offset >= 0)
    {
      // Went too far left or right - wrap
      jQuery('#scroller').css('left', (origWidth * -1) + 'px');
    } else {
      jQuery('#scroller').css('left', (offset - scrollSpeed) + 'px');
    }
  }
}

