$(document).ready(function(){
  var currentPosition = 0;
  var slideWidth = 925;
  var slides = $('.slide');
  var numberOfSlides = slides.length;
  var alertTimerId = 0;

  // 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
  $('#slideshow')
    .prepend('<span class="control" id="leftControl" style="display:none;"></span>')
    .append('<span class="control" id="rightControl" style="display:none;"></span>');



  //SETUP BUTTONS
  document.getElementById('c0').style.backgroundColor = '938255';
  $('#c0').bind('click', function(){
	clearTimeout(alertTimerId);
	document.getElementById('c'+currentPosition).style.backgroundColor = '540000'; //RESET COLOR
	currentPosition = 0;
	document.getElementById('c'+currentPosition).style.backgroundColor = '938255'; //SET ACTIVE
    $('#slideInner').animate({
      'marginLeft' : slideWidth*(-currentPosition)
    });
	alertTimerId = setTimeout('$(\'#rightControl\').trigger(\'click\');', 10000);
  });
  
  $('#c1').bind('click', function(){
	clearTimeout(alertTimerId);
	document.getElementById('c'+currentPosition).style.backgroundColor = '540000'; //RESET COLOR
	currentPosition = 1;
	document.getElementById('c'+currentPosition).style.backgroundColor = '938255'; //SET ACTIVE
    $('#slideInner').animate({
      'marginLeft' : slideWidth*(-currentPosition)
    });
	alertTimerId = setTimeout('$(\'#rightControl\').trigger(\'click\');', 10000);
  });
  
  $('#c2').bind('click', function(){
	clearTimeout(alertTimerId);
	document.getElementById('c'+currentPosition).style.backgroundColor = '540000'; //RESET COLOR
	currentPosition = 2;
	document.getElementById('c'+currentPosition).style.backgroundColor = '938255'; //SET ACTIVE
    $('#slideInner').animate({
      'marginLeft' : slideWidth*(-currentPosition)
    });
	alertTimerId = setTimeout('$(\'#rightControl\').trigger(\'click\');', 10000);
  });
  
  $('#c3').bind('click', function(){
	clearTimeout(alertTimerId);
	document.getElementById('c'+currentPosition).style.backgroundColor = '540000'; //RESET COLOR
	currentPosition = 3;
	document.getElementById('c'+currentPosition).style.backgroundColor = '938255'; //SET ACTIVE
    $('#slideInner').animate({
      'marginLeft' : slideWidth*(-currentPosition)
    });
	alertTimerId = setTimeout('$(\'#rightControl\').trigger(\'click\');', 10000);
  });
	
  $('#c4').bind('click', function(){
	clearTimeout(alertTimerId);
	document.getElementById('c'+currentPosition).style.backgroundColor = '540000'; //RESET COLOR
	currentPosition = 4;
	document.getElementById('c'+currentPosition).style.backgroundColor = '938255'; //SET ACTIVE
    $('#slideInner').animate({
      'marginLeft' : slideWidth*(-currentPosition)
    });
	alertTimerId = setTimeout('$(\'#rightControl\').trigger(\'click\');', 10000);
  });
	  
  $('#c5').bind('click', function(){
	clearTimeout(alertTimerId);
	document.getElementById('c'+currentPosition).style.backgroundColor = '540000'; //RESET COLOR
	currentPosition = 5;
	document.getElementById('c'+currentPosition).style.backgroundColor = '938255'; //SET ACTIVE
    $('#slideInner').animate({
      'marginLeft' : slideWidth*(-currentPosition)
    });
	alertTimerId = setTimeout('$(\'#rightControl\').trigger(\'click\');', 10000);
  });
  

  
  $('#leftControl').bind('click', function(){
	document.getElementById('c'+currentPosition).style.backgroundColor = '540000'; //RESET COLOR
	currentPosition = currentPosition-1;
	document.getElementById('c'+currentPosition).style.backgroundColor = '938255'; //SET ACTIVE
    $('#slideInner').animate({ 'marginLeft' : slideWidth*(-currentPosition) });
	clearTimeout(alertTimerId);
	if(currentPosition!=0){
		alertTimerId = setTimeout('$(\'#leftControl\').trigger(\'click\');', 7000);
	}else{
		alertTimerId = setTimeout('$(\'#rightControl\').trigger(\'click\');', 7000);
	}
  });
  
  $('#rightControl').bind('click', function(){	
	document.getElementById('c'+currentPosition).style.backgroundColor = '540000'; //RESET COLOR
	currentPosition = currentPosition+1;
	document.getElementById('c'+currentPosition).style.backgroundColor = '938255'; //SET ACTIVE
    $('#slideInner').animate({ 'marginLeft' : slideWidth*(-currentPosition) });
	clearTimeout(alertTimerId);
	if(currentPosition!=numberOfSlides-1){
		alertTimerId = setTimeout('$(\'#rightControl\').trigger(\'click\');', 7000);
	}else{
		alertTimerId = setTimeout('$(\'#leftControl\').trigger(\'click\');', 7000);
	}
  });
  
  
  //SETUP LOOP
  var slide = getQuerystring('slide');
  
  if(slide==2){
	  alertTimerId = setTimeout('$(\'#rightControl\').trigger(\'click\');', 1);
  }else{
	  alertTimerId = setTimeout('$(\'#rightControl\').trigger(\'click\');', 7000);
  }

  


});



function getQuerystring(key, default_){
  if (default_==null) default_=""; 
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}
