/* 
 * hhSlider 
 * Version: 5.1
 * Created by: Stian Jacobsen
 * Copyright: Stian Jacobsen
 */
 
 (function($) {
		$.fn.hhSlider = function(opt) {
			var defaults = {
				start: 1,
				speed: 400,
				force: 0,
				content: false,
				arrows: true
			}
			var obj = $(this);
			var options = $.extend(defaults, opt);
			var counter = options.start;
			var total = 0;
			
			
			var strToNum = function(str) {
				return str.slice(-1);
			}
			
			var showContent = function(id) {
				if(options.content) {
					var rel = $("li#slide-"+id).children('img').attr('rel');		
					window.setTimeout(function() { 
						$("."+options.content).hide(); 
						$("#"+rel).show(); 
					}, options.speed)
				}
			}
			
			var updateOverlay = function(w,h,l) {
				$("div.nav").css({width:w,height:h,left:l});
			}
			
			var slideTo = function(id) {
				// GO FORWARD
				if(id == "next") {
					var firstSlide = options.force == 1 ? options.start : 1;
					var nextNum = counter == total ? firstSlide : parseInt(counter)+1;
					$(".log").append('<p>ACTION: '+id+'<br />Last ID: '+counter+'<br />Next ID: '+nextNum+'</p>');
					var mov = -$("li#slide-"+nextNum).position().left;
					$('.slider-wrapper').animate({left: mov+'px'},options.speed);
					showContent(nextNum);
					updateOverlay($("li#slide-"+nextNum).children('img').width(),$("li#slide-"+nextNum).children('img').height(),$("li#slide-"+nextNum).position().left);
					counter = nextNum;
				}
				// GO BACKWARDS
				else if(id == "prev") {		
					var nextNum = (counter-1 == 0) ? total : counter-1;
					var goBack = options.force == 1 && nextNum+1 <= options.start ? false : true;
					if(goBack == true) {	
						$(".log").append('<p>ACTION: '+id+'<br />Last ID: '+counter+'<br />Next ID: '+nextNum+'</p>');
						var mov = -$("li#slide-"+nextNum).position().left;
						$('.slider-wrapper').animate({left: mov+'px'},options.speed);
						showContent(nextNum);
						updateOverlay($("li#slide-"+nextNum).children('img').width(),$("li#slide-"+nextNum).children('img').height(),$("li#slide-"+nextNum).position().left);
						counter = nextNum;		
					}
				}
				// GO TO A SPESIFIC SLIDE
				else {
					var doSlide = options.force == 1 && id < options.start ? false : true;
					if(doSlide == true) {
						$(".log").append('<p>ACTION: '+id+'<br />Last ID: '+counter+'<br />Next ID: '+nextNum+'</p>');
						var mov = -$("li#slide-"+id).position().left;
						$('.slider-wrapper').animate({left: mov+'px'},options.speed);
						showContent(id);
						updateOverlay($("li#slide-"+id).children('img').width(),$("li#slide-"+id).children('img').height(),$("li#slide-"+id).position().left);
						counter = id;
					}
				}
			}
			
			
			// WHEN DOCUMENT HAS LOADED EVERY IMAGE, WE DO THIS
			$(window).load(function() {
				var forceArrow = false;
				obj.each(function() {
					total = obj.children().size(); // Count number of slides
					var num = 0;
					// Ad an ID to each slide
					obj.children().each(function(i) {
						num = i+1;
						$(this).attr('id','slide-'+num);
						if(options.force == 1) {
							if(num+1 > options.start) {
								$(this).attr('class','slideItem');
							}
						}
						else {
							$(this).attr('class','slideItem');	
						}
					});
				});		
				// GO TO A PREDEFINED SLIDE
				if(options.start != 1) {
					slideTo(options.start);	
				}
				
				// Keyboard commands
				$(document).bind('keydown',function(e){				
	 				if(e.which == 39) { slideTo('next');}
					else if(e.which == 37) { slideTo('prev');}
					return false;
			 	});
				// Navigation commands
				$(".nav-right").live('click',function() { slideTo('next'); });
				$(".nav-left").live('click',function() { slideTo('prev'); });
				// OnClick commands
				obj.find('.slideItem').live('click',function() { slideTo( strToNum( $(this).attr('id') ) ) });
				// Hover commands
				if(options.arrows == true) {
					$(".nav-right").live('mouseover',
						function() {
							$(this).find('span').addClass('show');									  
						}
					);
					$(".nav-right").live('mouseout',
						function() {
							$(this).find('span').removeClass('show');
						}
					);
					
					$(".nav-left").live('mouseover',
						function() {
							var nextNum = (counter-1 == 0) ? total : counter-1;
						    var goBack = options.force == 1 && nextNum+1 <= options.start ? false : true;
							if(goBack == true) {
								$(this).find('span').addClass('show');									  
							}
						}
					);
					$(".nav-left").live('mouseout',
						function() {
							$(this).find('span').removeClass('show');
						}
					);
				}
			}); // END window.load
			
			
		};
	})(jQuery);
