(function( $ ){
	var methods = {
		init : function( options ) {
			var defaults = {  
				els: null,
				timeBetween: 4000,
				timeFade:"slow"
			};  
			var options = $.extend(defaults, options);

			return this.each(function() {
				var $this = $(this);
				$this.data('slider', {
					els : options.els,
					htmlEls : $(this).children(".slider_el"),
					count: $(this).children(".slider_el").length,
					cur : -1,
					timeout: 0,
					timeBetween: options.timeBetween,
					timeFade: options.timeFade,
					isHover: false,
					ready: false,
					visible: false,
					preloadStarted: false
				});
				var d = $this.data('slider');
				
				$this.hover(
					function() {
						$(this).data().slider.isHover = true;
						clearTimeout( $this.data().slider.timeout );
					},function() {
						$(this).data().slider.isHover = false;
						if($(this).data().slider.ready) {
							$this.data().slider.timeout = setTimeout(function() {$this.slider('next');},$this.data().slider.timeBetween);
						}
					}
				);
				
				$this.slider('start');
			});
		}, 
		/*preload: function() {
			$(this).data().slider.preloadStarted = true;
			//console.log( $(this).attr("id") );
			var $this = $(this);
			var d = $this.data('slider');
			var loaded = 0;
			for(var i=0; i<d.count; i++) {
				var im = new Image();
				$(im).load(function() {
					loaded++;
					if(loaded == d.count) {
						$this.slider('start');
					}
				});
				$(im).attr("src",d.els[i].img).attr("id",$this.attr("id")+"_img_"+i);
				var el = $('<a class="slider_el" href="'+d.els[i].link+'" rel="lytebox"></a>').hide();
				
				$this.append(el.append(im));
			}
		},*/
		start: function() {
			var $this = $(this);
			if($this.data().slider.count>1) {
				var nb = $('<div class="slider_nav_bg"></div>').hide().fadeTo("fast",0.8);
				$this.append(nb);
				var n = $('<div class="slider_nav"></div>');
				var n2 = $('<div class="slider_nav2"></div>');
				var n3 = $('<div class="slider_nav3"></div>');
				n.append(n2);
				n2.append(n3);
				
				for(i=0; i<$this.data().slider.count;i++) {
					var a = $('<a href="#" class="png_fix" rel="'+i+'">'+(i+1)+'</a>');
					a.click(function() { $this.slider('gotoslide',parseInt($(this).attr("rel"))); return false; });
					a.appendTo(n3);
				}
				$this.append(n);
			} else {
			}
			
			
			//$this.find(".slider_loading").fadeOut("fast",function() { $(this).remove(); });
			//$this.data().slider.htmlEls = $this.children(".slider_el");
			$($this.data().slider.htmlEls).hide().css("left",0);
			
			$this.data().slider.ready = true
			$this.slider('next');
		},
		next: function() {
			
			var d = $(this).data('slider');
			var i = (d.cur + 1) %(d.htmlEls.length);
			$(this).slider('gotoslide',i);
			
		},
		gotoslide: function(i) {
			var $this = $(this);
			
			clearTimeout( $this.data().slider.timeout );
			
			var d = $this.data('slider');
			if(d.cur!=i) {
				if(d.cur>=0) {
					$(d.htmlEls[d.cur]).fadeOut($this.data().slider.timeFade);
					$this.find(".slider_nav a").removeClass("on");
				}
				$this.data('slider').cur = i;
				$this.find(".slider_nav a").eq(i).addClass("on");
				$(d.htmlEls[i]).fadeIn($this.data().slider.timeFade, function() {
					if(! $this.data().slider.isHover )
					{
						$this.data('slider').timeout = setTimeout(function() {$this.slider('next');},$this.data().slider.timeBetween);
					}
				});
			}
		}
	}
	$.fn.slider = function( method ) {
		if ( methods[method] ) {
		  return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
		  return methods.init.apply( this, arguments );
		} else {
		  $.error( 'Method ' +  method + ' does not exist on jQuery.slider' );
		}    
  };
})( jQuery );

$(window).load(function() {
	$("#slider").slider();
});
