(function( $ ){
	var methods = {
		init : function( options ) {
			var defaults = { 
				timeBetween: 4000,
				timeAnim:400,
				timeDelay:25
			};  
			var options = $.extend(defaults, options);
			
			return this.each(function() {
				var $this = $(this);
				$this.data('top_slider', {
					els : $(this).children(),
					count: $(this).children().length,
					cur : -1,
					timeout: 0,
					timeBetween: options.timeBetween,
					timeAnim: options.timeAnim,
					timeDelay: options.timeDelay,
					isHover: false,
					ready: false,
					visible: false,
					preloadStarted: false,
					nav: $("#top_slider_nav"),
					full: ($.browser.msie && parseInt($.browser.version, 10) == 8)?false:true
				});
				var d = $this.data('top_slider');
				d.els.removeClass("first");
				if(d.els!=null) {
					var l = $('<div class="top_slider_loading"></div>');
					$this.append(l);
				}
				$this.top_slider('preload');
				$this.hover(
					function() {
						$(this).data().top_slider.isHover = true;
						clearTimeout( $this.data().top_slider.timeout );
					},function() {
						$(this).data().top_slider.isHover = false;
						if($(this).data().top_slider.ready) {
							$this.data().top_slider.timeout = setTimeout(function() {$this.top_slider('next');},$this.data().top_slider.timeBetween);
						}
					}
				);
				
			});
		}, 
		
		preload: function() {
			$(this).data().top_slider.preloadStarted = true;
			var $this = $(this);
			var d = $this.data('top_slider');
			var loaded = 0;
			
			for(var i=0; i<d.count; i++) {
				var im = $(d.els[i]).find("img");
				$(im).load(function() {
					loaded++;
					if(d.full) {
						for(i=0;i<6;i++) {
							for(j=0;j<6;j++) {
								s = $('<span style="background: url('+$(this).attr("src")+') no-repeat '+(-1*j*103)+'px '+(-1*i*53)+'px"></span>');
								s.appendTo( $(this).parent() );
							}
						}
					}
					$(this).hide();
					//$('.top_slider_loading').html( Math.round((loaded/d.count)*100)+' %' );
					if(loaded == d.count) {
						$(function() { 
							setTimeout(function() { $this.top_slider('start') },250);
						});
						//
					}
				});
				$(im).error(function() {
					//console.log('error');
				});
				
			}
		},
		start: function() {
			var $this = $(this);
			
			for(i=0; i<$this.data().top_slider.count;i++) {
				var a = $('<a href="#" rel="'+i+'">'+(i+1)+'</a>');
				a.click(function() { $this.top_slider('gotoslide',parseInt($(this).attr("rel"))); return false; });
				a.appendTo($this.data().top_slider.nav);
			}
			//$this.data().top_slider.els.css("top", $this.height()).css("left",0);
			$this.data().top_slider.els.hide().css("left",0);
			$this.find(".top_slider_loading").fadeOut("slow",function() { $(this).remove(); });
			$this.data().top_slider.ready = true;
			$this.top_slider('next');
		},
		next: function() {
			var d = $(this).data('top_slider');
			var i = (d.cur + 1) %(d.count);
			$(this).top_slider('gotoslide',i);
			
		},
		gotoslide: function(i) {
			var $this = $(this);
			var d = $this.data('top_slider');
			if(d.ready) {
				
				clearTimeout( $this.data().top_slider.timeout );
				
				var t = d.timeAnim;
				var timeTotal = (35*d.timeDelay+d.timeAnim);
				if(d.cur!=i) {
					if(d.cur>=0) {
						$(d.els[d.cur]).css("z-index",100);
						/*if(d.full) {
							for(j=0;j<6;j++) {
								for(k=0;k<6;k++) {
									$(d.els[d.cur]).find(".top_slider_el_img span").eq(j*6+k).delay(d.timeDelay*(j*6+k)).fadeTo(d.timeAnim,0.0);
								}
							}
						} else {
							$(d.els[d.cur]).find(".top_slider_el_img img").fadeOut(timeTotal);
						}*/
						$(d.els[d.cur]).find(".top_slider_el_right").stop().animate({top:318},timeTotal,function() {
							$(this).closest(".top_slider_el").hide();
						});
						
						d.nav.find("a").removeClass("on");
					}
					$this.data('top_slider').ready = false;
					$this.data('top_slider').cur = i;
					d.nav.find("a").eq(i).addClass("on");

					//init
					$(d.els[i]).css("z-index",101);
					$(d.els[i]).find(".top_slider_el_img span").css({opacity:0}).show();
					$(d.els[i]).find(".top_slider_el_right").css({"left":950,"top":0})
					
					$(d.els[i]).show();
					if(d.full) {
						for(j=0;j<6;j++) {
							for(k=0;k<6;k++) {
								$(d.els[i]).find(".top_slider_el_img span").eq(j*6+k).delay(d.timeDelay*(j*6+k)).fadeTo(d.timeAnim,1.0);
							}
						}
					} else {
						$(d.els[i]).find(".top_slider_el_img img").fadeIn(timeTotal);
					}
					$(d.els[i]).find(".top_slider_el_right").animate({left:645},timeTotal, function() {
						$this.data('top_slider').ready = true;
						if(! $this.data().top_slider.isHover )
						{
							$this.data('top_slider').timeout = setTimeout(function() {
								$this.top_slider('next');
							},$this.data('top_slider').timeBetween);
						}
					});
				}
			} else {
				
			}
		}
	}
	$.fn.top_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 );


