/* jQuery start */
$(document).ready(function(){

var body = $('body');
body.addClass('js').removeClass('no-js');


/* Detect touch device (iOS only) */

if ( !window.Touch ) { 
  body.addClass('no-touch');
}


/* RWD Filter */

function rwd_width_min (value) {
  if ( $(document).width() >= value ) { return true; }
  else { return false; }
}


/* ScrollTo */

if ( $().scrollTo ) {
  $('.scrollto').click(function() {
    $.scrollTo($($(this).attr("href")), {
      duration: 750
    });
    return false;
  });
}


/* StickyFloat */

if ( $().stickyfloat && rwd_width_min(768) ) {
  // lookup element
  var stickySidebar = $('#sidebar-nav');

  // if element exists, otherwise StickyFloat errors if nothing found
  if ( stickySidebar.length > 0 ) {
    stickySidebar.stickyfloat({ duration: 500 });
  }
}


/* Dynamic Carousel */
/* https://github.com/Wilto/Dynamic-Carousel */

if ($().carousel) {
  $('.product-slider .slider-wrap').carousel({
    slider: '.slider',
    slide: '.slide',
    nextSlide: '.slider-next',
    prevSlide: '.slider-prev',
    speed: 300
  });
}  


/* SimpleSlideshow */

if ($().simpleslideshow) {
  $('#pano-slider').simpleslideshow(5000, 800);
}


/* Crawlbar Slider */
/* Based on: http://jqueryui.com/demos/slider/side-scroll.html */
/*
//scrollpane parts
var scrollPane = $('.scroll-pane');
var scrollContent = $('.scroll-content');

//calculate width of .scroll-content after IMGs are loaded
$(window).load(function(){
  var crawlWidth = 0;

  $(scrollContent).children().each(function() {
    crawlWidth += $(this).outerWidth( true );
  });
  
  console.log('Total: ' + crawlWidth);
  $(scrollContent).css('width', crawlWidth);
});

//build slider
var scrollbar = $( ".scroll-bar" ).slider({
  slide: function( event, ui ) {
    if ( scrollContent.width() > scrollPane.width() ) {
      scrollContent.css( "margin-left", Math.round(
        ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
      ) + "px" );
    } else {
      scrollContent.css( "margin-left", 0 );
    }
  }
});

//append handle helper
var handleHelper = scrollbar.find('.ui-slider-handle')
  .mousedown(function() {
    scrollbar.width( handleHelper.width() );
  })
  .mouseup(function() {
    scrollbar.width( "100%" );
  })
  .wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();

//change overflow to hidden now that slider handles the scrolling
scrollPane.css( "overflow", "hidden" );

//size scrollbar and handle proportionally to scroll distance
function sizeScrollbar() {
  var remainder = scrollContent.width() - scrollPane.width();
  var proportion = remainder / scrollContent.width();
  var handleSize = scrollPane.width() - ( proportion * scrollPane.width() );
  scrollbar.find( ".ui-slider-handle" ).css({
    width: handleSize,
    "margin-left": -handleSize / 2
  });
  handleHelper.width( "" ).width( scrollbar.width() - handleSize );
}

//reset slider value based on scroll content position
function resetValue() {
  var remainder = scrollPane.width() - scrollContent.width();
  var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
    parseInt( scrollContent.css( "margin-left" ) );
  var percentage = Math.round( leftVal / remainder * 100 );
  scrollbar.slider( "value", percentage );
}

//if the slider is 100% and window gets larger, reveal content
function reflowContent() {
    var showing = scrollContent.width() + parseInt( scrollContent.css( "margin-left" ),10 );
    var gap = scrollPane.width() - showing;
    if ( gap > 0 ) {
      scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ),10 ) + gap );
    }
}

//change handle position on window resize
$(window).resize(function() {
  resetValue();
  sizeScrollbar();
  reflowContent();
});

//init scrollbar size
setTimeout( sizeScrollbar, 10 ); //safari wants a timeout
*/





/* RWD Unbinder */
/* Incomplete, needs easy way to reattach events */

/*
$(window).resize(function() {

  if( $(document).width() < 1024 ) {
    $('.scrollto').unbind();
  }

});
*/


}); /* end jQuery */






/* RWD Width Widget */
function getRwdWidget () {
  $('#footer').append('<div id="rwd-width" style="position:fixed; bottom:0; left:50%; background:#000; color:#FFF; padding:5px 10px;">' + $(document).width() + '</div>');
  
  var rwdWidth = $('#rwd-width');
  
  $(window).resize(function() {
    rwdWidth.html( $(document).width() );
  });
}

