/*
 * jQuery Slider - v1.0 - 10/4/2011
 * 
 * Copyright (c) 2011 Daan van Ham
 */

// Script: jQuery Slider
//
// *Version: 1.0, Last updated: 2011/04/10*
// 
// 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

(function($) {

	$.fn.slider = function(options)
	{	var opts = $.extend({}, $.fn.slider.defaults, options);
		
		var active = 0;
		
		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.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()
			{	active++;
		  	if(active > opts.slides-1) active = 0;
		    rotate(); 
		  }, 4000);
		};
		
		rotate = function()
		{	var position = active * control; 
			
			if (opts.type == 'horizontal')
			{	$('#sliderContainer').animate({'left' : '-'+position+'px'},700);
			} else if (opts.type == 'vertical')
			{	$('#sliderContainer').animate({'top' : '-'+position+'px'},700);
			}
			
			$('#sliderNavigation .active').removeClass('active');
			$('#sliderNavigation ul li').children().each(function()
			{	if ($(this).attr('rel') == active) 
				{	$(this).addClass('active');
				}
			});
		};
		
		$('#sliderNavigation .sliderLink').mouseover(function()
		{	$(this).addClass('hover');
		}).mouseout(function()
		{	$(this).removeClass('hover');
		}).click(function()
		{	$(this).blur();
			if (!$(this).hasClass('active'))
			{	var position = ($(this).attr('rel') * control);
				$('#sliderContainer').stop();
				if (opts.type == 'horizontal')
				{	$('#sliderContainer').animate({'left' : '-'+position+'px'},700);
				} else if (opts.type == 'vertical')
				{	$('#sliderContainer').animate({'top' : '-'+position+'px'},700);
				}
				active = $(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,
		slideWidth: $('.sliderItem').width(),
		slideHeight: $('.sliderItem').height()
	};
	
})(jQuery);
