// 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;
	var slideWidth = $(window).width();
	var inAction = false;
	
  this.find('.slide').each(function(i){
		$this = $(this);
		$this.addClass('s-'+i);
		
		$this.css('left',slideWidth * i);
	});
	
	if(location.hash){
		var hash = location.hash.replace('#','');
		if(isNaN(parseInt(hash))){  // if is string
			var newSlide = $('div.'+ hash +':first').index();
			currentSlide = newSlide;
			if(newSlide > currentSlide){
				animate(newSlide,"right");
			}else{
				animate(newSlide,"left");
			}
		}else if(parseInt(hash) > $('.slide').length){
			currentSlide = 0;
		}else{  // if is number
			currentSlide = parseInt(hash);
			if(hash > currentSlide){
				animate(hash,"right");
			}else{
				animate(hash,"left");
			}
		}
	}
	
	$('.nextButton').click(function(){
		if(inAction==true)return;
		inAction = true;
		currentSlide = currentSlide+1;
		animate(currentSlide,"right");
	});
	
	$('.prevButton').click(function(){
		if(inAction==true)return;
		inAction = true;
		currentSlide = currentSlide-1;
		animate(currentSlide,"left");
	});
	
	$('.mainNav li a').click(function(){
		if(inAction==true)return;
		inAction = true;
		var firstClassName = $(this).attr('href').replace('#','');
		wordAnimate(firstClassName);
	});
	
	function wordAnimate(word){
		var newSlide = $('div.'+ word +':first').index();
		currentSlide = newSlide;
		if(newSlide > currentSlide){
			animate(newSlide,"right");
		}else{
			animate(newSlide,"left");
		}
	}
	
	function animate(currentSlide,direction){
		if(currentSlide > $('.slide').length) return;
		switch (direction){
			case "right":
				mod = "-";
			case "left":
				mod = "";
			default:
				mod = "-";
		}
		
		$('.slideContainer').animate({ left:mod + (slideWidth*currentSlide)}, 1000, function(){
			hideArrows(currentSlide);
			$('.nextButton').attr('href','#'+(parseInt(currentSlide)+1));
			$('.prevButton').attr('href','#'+(parseInt(currentSlide)-1));
			
			activateNav(currentSlide);
			
			inAction = false;
		});
	}
	
	function activateNav(currentSlide){
		var slideObject = $('.s-'+currentSlide);
		
		if(slideObject.hasClass('home')){
			$('li.home').addClass('active').siblings().removeClass('active');
		} else if (slideObject.hasClass('history')) {
			$('li.history').addClass('active').siblings().removeClass('active');
		} else if (slideObject.hasClass('work')){
			$('li.work').addClass('active').siblings().removeClass('active');
		} else if (slideObject.hasClass('contact')){
			$('li.contact').addClass('active').siblings().removeClass('active');
		}
	}
	
	function hideArrows(currentSlide){
		if(currentSlide==0){
			$('.prev').fadeOut();
			$('.next').fadeIn();
		}else if(currentSlide==$('.slide').length-1){
			$('.prev').fadeIn();
			$('.next').fadeOut();
		}else{
			$('.prev, .next').fadeIn();
		}
	}
	
	jQuery.fn.fadeToggle = function(speed, easing, callback) {
	   return this.animate({opacity: 'toggle'}, speed, easing, callback);
	};
	
  // private function for debugging
  function debug($obj) {
    if (window.console && window.console.log) {
      window.console.log($obj);
    }
  }
  
};


})(jQuery);
