(function($) {
	$.fn.slider = function(options) {		
		var Slider = function(element, options) { this.init(element, options); };
		Slider.prototype = {
			
			data: [],
			settings: {
				opacity_min: 0.7,
				opacity_max: 0.9
			},
			element: {},
			current: 1,
			
			init: function(element, options) {
				
				// check new opacity_max
				if (options && options.opacity_max) {
					options.opacity_max = parseFloat(options.opacity_max);
					if (!options.opacity_max) {
						delete options.opacity_max;
					}
				}
				
				// check new opacity_min
				if (options && options.opacity_min) {
					options.opacity_min = parseFloat(options.opacity_min);
					if (!options.opacity_min) {
						delete options.opacity_min;
					}
				}
				
				$.extend(this.settings, options);
				
				this.element = element; 
				element.css({position: "relative"});
				element.addClass("slider");
				
				element.find("a:first-child").addClass("current");
				element.find("a:not(:first-child)").css("display", "none");
				
				var tmp = [];
				element.children().each(function() {
					tmp.push({
						text: $(this).attr("title"),
						href: $(this).attr("href"),
						img : $(this).children()
					});
				});
				this.data = tmp;
				
				this._buidShadow();
				
			},
			
			_buidShadow: function() {
				this.element.empty();
				
				var opacity_min = this.settings.opacity_min;
				var opacity_max = this.settings.opacity_max;
				
				this.element
					.append($("<a>", {"class": "img", href: this.data[0].href}))
					.append($("<div>", {"class": "shadow"}))
					.append($("<div>", {"class": "shadow_bg"})
						.append($("<a>", {"class": "text", href: this.data[0].href}))
						.append($("<ul>", {"class": "point"}))
						.hover( 
							function() {
								$(".shadow").animate({opacity: opacity_max}, 400);
							},
							function() {
								$(".shadow").animate({opacity: opacity_min}, 400);
							}
						)
					)
				;
				
				for (var i in this.data) {
					
					tmp = $(this.data[i].img).css("opacity", (i == 0 ? 1 : 0));
					$(".img").append(tmp);
					
					$(".text").append($("<span>", {html: this.data[i].text}).css("opacity", (i == 0 ? 1 : 0)));
					
					$(".point").append($("<li>", {rel: parseInt(i) + 1, "class": (i == 0 ? "selected" : '')}));
				}
				
				$(".img img:first-child").addClass("current");
				$(".text span:first-child").addClass("current");
			},
			
			run: function() {
				$(".point .selected").removeClass("selected");
				$(".point li:nth-child(" + this.current + ")").addClass("selected");
				
				$(".img, .text").attr("href", this.data[this.current - 1].href);
				$(".img .current").animate({opacity: 0}, 2000).removeClass("current");
				$(".img img:nth-child(" + this.current + ")").animate({opacity: 1}, 2000).addClass("current");
				
				$(".text .current").animate({opacity: 0}, 2000).removeClass("current");
				$(".text span:nth-child(" + this.current + ")").animate({opacity: 1}, 2000).addClass("current");
			},
			
		}
		
		var slider = new Slider(this, options);
		
		$(".point li:not(.selected)").live("click", function() {
			if (interval) {
				clearInterval(interval);
			}
			slider.current = $(this).attr("rel");
			slider.run();
			
			interval = setInterval(call, 5000);
			
			return 
		});
		
		function call() {
			if (slider.current == slider.data.length) {
				slider.current = 1;
			}
			else {
				slider.current = parseInt(slider.current) + 1;
			}
			slider.run();
		}
		
		var interval = setInterval(call, 5000);
		
		return this;
	}
})(jQuery);
