//Quando o DOM está pronto
jQuery(document).ready(function($){

  var currentPosition = 0;
  var slideWidth = 160;
  var slides = jQuery('.slide');
  var numberOfSlides = slides.length;

  var currentPosition_sidebar = 0;
  var slideHeight_sidebar = 160;
  var slides_sidebar = jQuery('.sidebar_gallery_products');
  var numberOfSlides_sidebar = slides_sidebar.length;

  // Remove scrollbar in JS
  jQuery('#slidesContainer').css('overflow', 'hidden');

  // Remove scrollbar in JS
  jQuery('#slidesContainer_sidebar').css('overflow', 'hidden');
  
  
  
  // Wrap all .slides with #slideInner div
  slides
    .wrapAll('<div id="slideInner"></div>')
    // Float left to display horizontally, readjust .slides width
	.css({
      'float' : 'left',
      'width' : slideWidth
    });
    
  // Wrap all .slides with #slideInner div
  slides_sidebar
    .wrapAll('<div id="slideInner_sidebar"></div>')
    // Float left to display horizontally, readjust .slides width
	.css({
      'height' : slideHeight_sidebar
    });    



  // Set #slideInner width equal to total width of all slides
  jQuery('#slideInner').css('width', slideWidth * numberOfSlides);
  
  // Set #slideInner width equal to total width of all slides
  jQuery('#slideInner_sidebar').css('height', slideHeight_sidebar * numberOfSlides_sidebar);



  // Insert controls in the DOM
  jQuery('#slideshow')
    .append('<div id="div_control"><span class="control" id="leftControl" title="Previous">< Previous</span><span class="control" id="rightControl" title="Next">Next ></span></div>');

  // Insert controls in the DOM
  jQuery('#slideshow_sidebar')
    .append('<div id="div_control_sidebar"><span class="control_sidebar" id="upControl" title="Previous">< Previous</span><span class="control_sidebar" id="downControl" title="Next">Next ></span></div>');





  // Hide left arrow control on first load
  manageControls(currentPosition);
  
  // Hide left arrow control on first load
  manageControls_sidebar(currentPosition_sidebar);  
  

  // Create event listeners for .controls clicks
  jQuery('.control')
    .bind('click', function(){
    // Determine new position
	currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
    
	// Hide / show controls
    manageControls(currentPosition);
    // Move slideInner using margin-left
    jQuery('#slideInner').animate({
      'marginLeft' : slideWidth*(-currentPosition)
    });
  });



  // Create event listeners for .controls clicks
  jQuery('.control_sidebar')
    .bind('click', function(){
    // Determine new position
	currentPosition_sidebar = ($(this).attr('id')=='downControl') ? currentPosition_sidebar+1 : currentPosition_sidebar-1;
    
	// Hide / show controls
    manageControls_sidebar(currentPosition_sidebar);
    // Move slideInner using margin-left
    jQuery('#slideInner_sidebar').animate({
      'marginTop' : slideHeight_sidebar*(-currentPosition_sidebar)
    });
  });

  // manageControls: Hides and Shows controls depending on currentPosition
  function manageControls(position){
    // Hide left arrow if position is first slide
	if(position==0){ jQuery('#leftControl').hide() } else{ jQuery('#leftControl').show() }
	// Hide right arrow if position is last slide
    if(position==numberOfSlides-6){ jQuery('#rightControl').hide() } else{ jQuery('#rightControl').show() }
  }
    function manageControls_sidebar(position){
    // Hide left arrow if position is first slide
	if(position==0){ jQuery('#upControl').hide() } else{ jQuery('#upControl').show() }
	// Hide right arrow if position is last slide
    if(position==numberOfSlides_sidebar-3){ jQuery('#downControl').hide() } else{ jQuery('#downControl').show() }
  }
});
