// Copyright ©2009 Aaron Vanderzwan
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.



(function($) {
jQuery.fn.slides = function(options) {

  // var opts = $.extend({}, $.fn.slides.defaults, options);
  var opts = jQuery.extend({
    // wait: true
  }, options);
  
	var currentSlide = 0;
	var slidesArray = this;



  this.each(function(i){
	
		$this = $(this);
		$this.addClass('s-'+i);
		
		if(i==currentSlide){
			var newPos = position($this);
			$this.css({'left':newPos})
		
			$(window).resize(function(){
				bodyWidth = $(window).width()+20;
				$this.css({'left':bodyWidth});
				var newPos = position($('.s-'+currentSlide));
				$('.s-'+currentSlide).css({'left':newPos})
			});
		}else{
			var bodyWidth = $(window).width()+20;
			$this.css({'left':bodyWidth});
		}
		
		if(i==0){
			$(this).addClass('firstSlide');
		}
		if(i==slidesArray.length-1){
			$(this).addClass('lastSlide');
		}
	});

	hideArrows(currentSlide);
	
	$('.nextButton').click(function(){
		var slideWidth = $('.s-'+currentSlide).width()+20;
		$('.s-'+currentSlide).animate( { left:"-"+slideWidth}, 1000 );
		
		currentSlide++;
		
		hideArrows(currentSlide);
		changeActive(currentSlide);
		
		newPos = position($('.s-'+currentSlide));
		$('.s-'+currentSlide).animate( { left:newPos }, 1000);
	});
	
	$('.prevButton').click(function(){
		var bodyWidth = $(window).width()+20;
		$('.s-'+currentSlide).animate( { left:bodyWidth}, 1000 );
		
		currentSlide--;
		
		hideArrows(currentSlide);
		changeActive(currentSlide);
		
		newPos = position($('.s-'+currentSlide));
		$('.s-'+currentSlide).animate( { left:newPos }, 1000);
	});
	
	
	function hideArrows(currentSlide){
		$('.prev, .next').show();
		if($('.s-'+currentSlide).hasClass('firstSlide')){
			$('.prev').hide();
		}else if($('.s-'+currentSlide).hasClass('lastSlide')){
			$('.next').hide();
		}
	}
	
	function changeActive(currentSlide){
		$('li').removeClass('active');
		if($('.s-'+currentSlide).hasClass('home')){
			$('li.home').addClass('active');
		} else if($('.s-'+currentSlide).hasClass('storia')){
			$('li.storia').addClass('active');
		}	else if($('.s-'+currentSlide).hasClass('lavoro')){
			$('li.lavoro').addClass('active');
		} 	else if($('.s-'+currentSlide).hasClass('portfolio')){
			$('li.portfolio').addClass('active');
		} 	else if($('.s-'+currentSlide).hasClass('formazione')){
			$('li.formazione').addClass('active');
		} 	else if($('.s-'+currentSlide).hasClass('contatti')){
			$('li.contatti').addClass('active');
		}
	}
	
	function position($object){	
		var bodyWidth = $(window).width();
		var slideWidth = $object.width();
		var newPos = (bodyWidth/2) - (slideWidth/2)
		return newPos;
	}
  // private function for debugging
  function debug($obj) {
    if (window.console && window.console.log) {
      window.console.log($obj);
    }
  }
  
};


})(jQuery);
