(function($) {
	$.fn.slider = function(options) {
		var defaults = {
			'timeout': 1000,
			'items': 1,
			'randomize': false
		};
		var options = $.extend(true, {}, defaults, options);
		return this.each(function() {
			var el = $(this);
			var one = 0;
			var no = 0;
			var total = 0;
			var maxMargin = 0;
			var timeOut = null;
			var list = $("ul li", $(el));
			var thumbs = null;
			var loaded = false;
			if(options.randomize === true) {
				$.shuffle(list);
				$('ul', $(el)).empty().html(list);
				list = $("ul li", $(el));
			}
			var next = function() {
				goTo(calculateNext(no));
			}
			var prev = function() {
				goTo(calculatePrevious(no));
			}
			var goTo = function(n) {
				if(timeOut != null) {
					clearTimeout(timeOut);
				}
				no = n;
				var margin = (one * options.items * no);
				margin = (margin > maxMargin) ? maxMargin : margin;
				$('ul', $(el)).animate({
					'opacity': 0.4
				}, 'normal', function() {
					$(this).css({
						'margin-left': -margin + 'px',
						'opacity': 0.3
					});
					$(this).animate({
						'opacity': 1
					}, 'normal', function() {
						thumbs.removeClass('current');
						$(thumbs[no]).addClass('current').parent().trigger('classChange');
						timeOut = setTimeout(function() {
							goTo(calculateNext(no));
						}, options.timeout);
					});
				});
			}
			var calculateNext = function(no) {
				return (no + 1 >= total) ? 0 : ++no;
			}
			var calculatePrevious = function(no) {
				return (no - 1 < 0) ? (total-1) : --no;
			}
			var create = function() {
				var wrapper = $('<div>').attr({
					'class': 'wrapper'
				}).css({
					'width': options.width + 'px',
					'height': options.height + 'px',
					'overflow': 'hidden'
				});
				
				$(el).find('ul').wrap(wrapper).css({
					'width': '1000000px',
					'padding': 0,
					'margin': 0,
					'listStyleType': 'none',
					'float': 'left'
				}).find('li').css({
					'padding': 0,
					'margin': 0,
					'listStyleType': 'none',
					'float': 'left'
				});
				$('img', $(list[0])).bind('load', function() {
					if(!loaded) {
						one = $(list[0]).outerWidth();
						navigation();
						loaded = true;
					}
				}).bind('error', function() {
					if(!loaded) {
						one = $(list[0]).outerWidth();
						navigation();
						loaded = true;
					}
				});//.attr('src', $(this).attr('src') + '?' + new Date().getTime());
				if($('img', $(list[0]))[0].complete) {
					if(!loaded) {
						one = $(list[0]).outerWidth();
						navigation();
						loaded = true;
					}
				}
			}
			var navigation = function() {
				/* buttons */
				var left = $('<div>').attr({
					'class': 'leftNav'
				}).css({
					'position': 'relative',
					'bottom': 0,
					'padding': 0,
					'margin': 0,
					'width': '9px',
					'height': '28px',
					'cursor': 'pointer',
					'float': 'left',
					'background': 'url(/img/left_arrow.gif) no-repeat top left'
				}).bind('click', function() {
					prev();
				});
				var right = $('<div>').attr({
					'class': 'rightNav'
				}).css({
					'position': 'relative',
					'bottom': 0,
					'width': '9px',
					'height': '28px',
					'padding': 0,
					'margin': 0,
					'cursor': 'pointer',
					'float': 'left',
					'background': 'url(/img/right_arrow.gif) no-repeat top left'
				}).bind('click', function() {
					next();
				});
				/* thumbs */
				var div = $('<div>').attr({
					'class': 'thumbs'
				}).css({
					'position': 'relative',
					'bottom': 0,
					'float': 'left',
					'padding': 0,
					'margin': 0,
					'text-align': 'center',
					'height': '28px',
					'width': ($(el).width() - 18) + 'px'
				});
				total = Math.ceil(list.length / options.items);
				maxMargin = one * (list.length - options.items);
				$(div).append($('<div>').attr({
					'class': 'thumbHolder'
				}).css({
					'width': (total * 13) + 'px',
					'margin': '0 auto'
				}));
				for(var i = 0; i < total; i++) {
					var button = $('<div>').attr({
						'class': 'button',
						'rel': i
					}).css({
						'background': 'url(/img/blue_dot_light.gif) no-repeat top left',
						'width': '7px',
						'height': '28px',
						'margin': '0 3px',
						'cursor': 'pointer',
						'float': 'left'
					}).hover(function() {
						$(this).css({
							'background': 'url(/img/blue_dot_dark.gif) no-repeat top left'
						});
					}, function() {
						$(this).css({
							'background': 'url(/img/blue_dot_light.gif) no-repeat top left'
						});
					}).bind('click', function() {
						goTo($(this).attr('rel'));
					});
					$('.thumbHolder', $(div)).append(button).bind('classChange', function() {
						thumbs.each(function() {
							if($(this).hasClass('current')) {
								$(this).css({
									'background': 'url(/img/blue_dot_dark.gif) no-repeat top left'
								});
							} else {
								$(this).css({
									'background': 'url(/img/blue_dot_light.gif) no-repeat top left'
								});
							}
						})
					});
				}
				$(el).append($(left)).append($(div)).append($(right));
				thumbs = $('.thumbHolder div', $(el));
				$(thumbs[0]).addClass('current').parent().trigger('classChange');
				timeOut = setTimeout(function() {
					goTo(1);
				}, options.timeout);
			}
			create();
		});
	};
})(jQuery);
jQuery.shuffle = function(arr) {
	for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
	return arr;
}

