/*
 * jQuery Slider - v1.1 - 2011/12/08
 * 
 * Copyright (c) 2011 Daan van Ham
 */

// Script: jQuery Slider
//
// *Version: 1.0, Last updated: 2011/04/10*
// *Version: 1.1, Last updated: 2011/12/08*
// 
// About: Support and Testing
// 
// Information about what version or versions of jQuery this plugin has been
// tested with and what browsers it has been tested in.
// 
// jQuery Version  - 1.5.2+
// Browsers Tested - Internet Explorer 6-9, Firefox 3.6-4, Safari 5, Chrome 10
// 
// About: Release History
// 
// 1.0 - initial release
// 1.1 - added new option for smooth sliding
//		 - new functionality > Plugin now creates navigation div on demand
//												 > it now is impossible to have unequal amount of slides vs navigation links

;(function($) {

	$.fn.slider = function(options)
	{	var opts = $.extend({slideWidth:$('.sliderItem').width(),slideHeight:$('.sliderItem').height()}, $.fn.slider.defaults, options);
		var active = 0;
		var next = 0;
		var container = $(this);
		
		if (isNaN(active)) 
		{	debug('active is not a number: '+active);
			return false;
		}
		if (isNaN(opts.slideWidth)) 
		{	debug('slideWidth is not a number: '+opts.slideWidth);
			return false;
		}
		if (isNaN(opts.slideHeight)) 
		{	debug('slideHeight is not a number: '+opts.slideHeight);
			return false;
		}
		if (isNaN(opts.slides)) 
		{	debug('slides is not a number: '+opts.slides);
			return false;
		}
		
		if ( opts.navigation )
		{	container.append('<div id="sliderNavigation"><ul></ul></div>');
			for(i=0;i<opts.slides;i++)
			{	container.find('#sliderNavigation ul').append( '<li><a href="#" class="sliderLink'+(i==0?' active':'')+'" rel="'+i+'">'+i+'</a></li>')
			}
		}
		
		if (opts.type == 'horizontal')
		{	var control = opts.slideWidth;
		} else if (opts.type == 'vertical')
		{	var control = opts.slideHeight;
		} else 
		{	debug('invalid slider-type')
			return false;
		}

		rotateSwitch = function()
		{	play = setInterval(function()
			{	rotate(); 
		    active++;
		  	if(active > opts.slides-1) active = 0;
		  }, opts.interval);
		};
		
		rotate = function()
		{	var position = active * control; 
			if (opts.type == 'horizontal')
			{	$('#sliderContainer').animate({'left' : '-'+opts.slideWidth+'px'},700,function(){$(this).css('left','0');});
			} else if (opts.type == 'vertical')
			{	$('#sliderContainer').animate({'left' : '-'+opts.slideHeight+'px'},700,function(){$(this).css('left','0');});
			}
			
			setTimeout('swapSlides("rotate",'+active+')',700);
			
			next=(active+1);
			if (next>(opts.slides-1)) 
			{	next=0;
			}
			
			$('#sliderNavigation .active').removeClass('active');
			$('#sliderNavigation ul li').children().each(function()
			{	if ($(this).attr('rel')==next) 
				{	$(this).addClass('active');
				}
			});
		};
		
		swapSlides = function(type,active)
		{ if(type=='rotate')
			{	$('.sliderItem').each(function()
				{	if($(this).attr('id')=='slide'+active)
					{ var el = $(this);
						$(this).remove();
						$('#sliderContainer').append( el );
					}
				});
			}else
			{	$('.sliderItem').each(function()
				{	if($(this).attr('id')!='slide'+active)
					{ var el = $(this);
						$(this).remove();
						$('#sliderContainer').append( el );
					}
				});
			}
		}
		
		$('#sliderNavigation .sliderLink').mouseover(function()
		{	$(this).addClass('hover');
		}).mouseout(function()
		{	$(this).removeClass('hover');
		}).click(function()
		{	$(this).blur();
			if (!$(this).hasClass('active'))
			{	var clicked = $(this).attr('rel');
				var position = $('#slide'+clicked).position();
				$('#sliderContainer').stop();
				if (opts.type == 'horizontal')
				{	$('#sliderContainer').animate({'left' : '-'+position.left+'px'},700,function(){$(this).css('left','0');});
					setTimeout('swapSlides("click",'+clicked+')',700);
				} else if (opts.type == 'vertical')
				{	$('#sliderContainer').animate({'left' : '-'+opts.slideHeight+'px'},700,function(){$(this).css('left','0');});
				}
				active=Math.floor($(this).attr('rel'));
				$('#sliderNavigation ul li').children().each(function()
				{	$(this).stop();
					if ($(this).attr('rel') != active) 
					{	$(this).removeClass('active');
					}
				});
				$(this).addClass('active');
				
				clearInterval(play);
    		rotateSwitch();
	    }
	    return false;
		});
		
		rotateSwitch();
	};
	
  function debug(error)
  {	if (window.console && window.console.log)
    {	window.console.log(error);
    }
  }
	
	$.fn.slider.defaults = {
		type: 'horizontal',
		slides: 4,
		smooth : false,
		interval : 4000,
		navigation : true
	};
	
})(jQuery);
