$(document).ready(function(){
  var currentPosition = 0;

  var slideWidth = 560; 

  var slides = $('.slide');
  var numberOfSlides = slides.length;

  // Remove scrollbar in JS
  $('#slidesContainer').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
    });

  // Set #slideInner width equal to total width of all slides
  $('#slideInner').css('width', slideWidth * numberOfSlides);

  // Insert controls in the DOM

  $('<span class="controler"  id="leftControl"><img src="/files/image/carousel/buttons_03.png" width="28" height="25" alt="Left" /></span><span class="controler" id="rightControl"><img src="/files/image/carousel/buttons_04.png" width="28" height="25" alt="Right" /></span><span id="playControl"><img class="start" src="/files/image/carousel/buttons_05.png" width="28" height="25" alt="Start" /><img class="stop" src="/files/image/carousel/pause_05.png" width="28" height="25" alt="Stop" /></span>').appendTo("div#control")

  // Hide left arrow control on first load
  manageControls(currentPosition);
var playing = false;
  // Create event listeners for .controls clicks
  $('.controler').bind('click', function(){
    // Determine new position
	if(!playing){
	currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
    
	// Hide / show controls
    manageControls(currentPosition);
    // Move slideInner using margin-left
		trace();
		}else{
		currentPosition = currentPosition-1;
		$(demos).stopTime('controlled');
				$('#playControl img.start').show();
					$('#playControl img.stop').hide();
		playing = !playing;
			active = !active;
		}
  });

  // manageControls: Hides and Shows controls depending on currentPosition
  function manageControls(position){
    // Hide left arrow if position is first slide
	$(demos).stopTime('controlled');
							$('#playControl img.start').show();
					$('#playControl img.stop').hide();
					
					
	    $('#slideInner').animate({
      'marginLeft' : slideWidth*(-currentPosition)
    });

	if(position==0){ $('#leftControl').hide() } else{ $('#leftControl').show() }
	// Hide right arrow if position is last slide
    if(position==numberOfSlides-1){ $('#rightControl').hide() } else{ $('#rightControl').show() }
  }	
  
  
		$('#playControl img.stop').hide();
				var demos = $("#playControl");
				//===
				
				var active = false;
				$(demos).find('.start').css("cursor", "pointer").click(function() {
					playing = true;
					if (!active) {
					$('#playControl img.start').hide();
					$('#playControl img.stop').show();
						active = true;
					if(currentPosition<=numberOfSlides){
					currentPosition = currentPosition+1;
	
				$(demos).everyTime(6000, 'controlled', function() {
					
						
						checkPos();
					trace();
    });

			}		
					}
				})
				$(demos).find('.stop').css("cursor", "pointer").click(function() {
					if (active) {
							currentPosition = currentPosition-1;
						active = !active;
							$(demos).stopTime('controlled');
							$('#playControl img.start').show();
					$('#playControl img.stop').hide();
						playing = !playing;
					}
				});
				
			
			
			 function trace(){
				//$(currentPosition).appendTo("div.tracer")
					//$("<br/>").appendTo("div.tracer")
				
				}


        function checkPos(){
        
            if(currentPosition==numberOfSlides){
                currentPosition = 0;
                currentPosition = currentPosition;
                	   
                $("<p>END OF ITEMS</p>").appendTo("div.tracer")
        	}
    		$('#slideInner').animate({
                  'marginLeft' : slideWidth*(-currentPosition)
    		});
			currentPosition = currentPosition+1;
			if(currentPosition==1){ $('#leftControl').hide() } else{ $('#leftControl').show() }
        	// Hide right arrow if position is last slide
            if(currentPosition==numberOfSlides){ $('#rightControl').hide() } else{ $('#rightControl').show() }
        }
			
    
});