$(function(){
	
	var Slider = function(onWhat){
		
		this.interval = 10000;
		
		var $slider = $(onWhat);
		var $slides = $slider.find('li');
		var currentSlide = 0;
		var totalSlides = ($slides.length - 1);
		var slideWidth = $($slides[0]).outerWidth();
		var self = this;
		this.autoPlay = false;
		this.loaded = true;
		
		if (totalSlides <= 0){
			this.loaded = false;
			return false;	
		}
		
		this.currentitem = $($slides[currentSlide]);
		this.nextitem = $($slides[currentSlide+1]);
		
		blips = document.createElement('ul');
		for (var i = 0; i <= totalSlides; i++){
			var ul = document.createElement('li');
			if (i == 0){
				ul.className = 'active';	
			}
			blips.appendChild(ul);
			ul = null;
		}
		
		blips_container = document.createElement('div');
		blips_container.className = 'blips';
		
		blips_container.appendChild(blips);
		
		$slider.append(blips_container);
		
		this.$blips = $(blips).find('li');
		
		blips_text = document.createElement('span');
		$(blips_text).css({
					'left' : (this.$blips.outerWidth()*(totalSlides+1))+10,
					'bottom' : 0,
					'position' : 'absolute'
				});
				
		blips_container.appendChild(blips_text);
		
		$(blips_text).text($slides.find(':first img').attr('alt'));
		
		$slider
			.mouseover(
				function(){
					self.clearTimer();
				}
			)
			.mouseout(
				function(){
					if (self.autoPlay){
						self.setTimer();	
					}
				}
			);
		
		
		$slides
			.not(':first')
			.css({
				left : slideWidth
			})
			.end()
			.css({
				position : 'absolute',
				display : 'block',
			});
			
		$slider
			.find('img[loadsrc]')
			.each(
				function(){
					$(this).attr('src', $(this).attr('loadsrc'));	
				}
			)
			.end()
			.prepend('<div class="slider_left"></div><div class="slider_right"></div>');
		
		$slider.find('.slider_right')
			.click(
				function(){
					self.stopAnyCurrentAnimations();
					self.clearTimer();
					self.next();
					self.setTimer();
				}
			);
			
		$slider.find('.slider_left')
			.click(
				function(){
					self.stopAnyCurrentAnimations();
					self.clearTimer();
					self.prev();
					self.setTimer();
				}
			);
		
		this.prev = function(){
			var $currentitem = $($slides[currentSlide]);
			if ((currentSlide-1) >= 0) {
				var $nextitem = $($slides[--currentSlide]);
			}else{
				currentSlide = totalSlides;
				var $nextitem = $($slides[currentSlide]);
			}
			
			this.currentitem = $currentitem;
			this.nextitem = $nextitem;
			
			$currentitem.css('z-index',0);
			
			$nextitem
				.css({
					'left' : 0 - slideWidth,
					'z-index' : 1
				})
				.animate(
					{
						left : 0	
					}
					, 500
					, 'swing'
					, function(){
						self.updateBlip();
						$(blips_text).text($nextitem.find('img').attr('alt'));
					}
				);
			
			$currentitem
				.animate(
					{
						left : slideWidth
					}
					, 500
					, 'swing'
				);	
		}
		
		this.stopAnyCurrentAnimations = function(){
			this.currentitem.stop(true,true)
			this.nextitem.stop(true,true)
		}
		
		this.next = function(){
			var $currentitem = $($slides[currentSlide]);
			if ((currentSlide+1) <= totalSlides) {
				var $nextitem = $($slides[++currentSlide]);
			}else{
				currentSlide = 0;
				var $nextitem = $($slides[currentSlide]);
			}
			
			this.currentitem = $currentitem;
			this.nextitem = $nextitem;
			
			$currentitem.css('z-index',0);
			
			$nextitem
				.css({
					'left' : slideWidth,
					'z-index' : 1
				})
				.animate(
					{
						left : 0	
					}
					, 500
					, 'swing'
					, function(){
						self.updateBlip();
						$(blips_text).text($nextitem.find('img').attr('alt'));
					}
				);
			
			$currentitem
				.animate(
					{
						left : 0 - slideWidth
					}
					, 500
					, 'swing'
				);	
		}
		
		this.updateBlip = function(){
			this.$blips.removeClass('active');
			$(this.$blips[currentSlide]).addClass('active');
		}
		
		this.autoNext = function(){
			this.next();			
			this.setTimer();
		}
		
		this.startShow = function(){
			this.autoPlay = true;
			this.setTimer();
		}
		
		this.setTimer = function(){
			var self = this;
			this.clearTimer();
			this.currentTimeout = window.setTimeout(function(){self.autoNext()},this.interval);	
		}
		
		this.stopShow = function(){
			this.clearTimer();
		}
		
		this.clearTimer = function(){
			window.clearTimeout(this.currentTimeout);		
		}
	}
	
	var mainslider = new Slider('.slider');
	
	if (mainslider.loaded == true){
		mainslider.startShow();
	}
});

