(function($){
	$.fn.navigation = function(setting, navigate){
		function memory(){}
		var pos = new memory;
		$$ = $(this);
		with(setting)
			if(typeof count == "number" && typeof now == "number" && typeof page_number){
				memory.prototype.now = now;
				function clicked(){
					if(typeof navigate == "function" && pos.now != $(this).text()  ){
						navigate($(this).text()  )
						$$.find("span.now").removeClass("now");
						$$.find("div.view span").removeClass("now");
						$(this).removeAttr("style").addClass("now");
						memory.prototype.now = $(this).text();
						$$.find("div.scroll div").css({
							left : Math.round(  (pos.now -1)*pos.page_width)
						});
					}
				}
				if(count > page_number){
					$$.append(
						'<div class="total"></div>'+
						'<div class="view"></div>'+
						'<div class="scroller">'+
							'<div>&nbsp;</div>'+
						'</div>'+
						'<div class="scroll">'+
							'<div>&nbsp;</div>'+
						'</div>'
					);
					memory.prototype.page_width = $$.find("div.scroll").width()/count;
					memory.prototype.scroller_width = Math.round(pos.page_width*page_number);
					$$.find("div.total").append("Всего страниц: "+count);
					$$.find("div.scroller").css({
						"margin-bottom" : -$$.find("div.scroller").height(),
						"z-index" : "999",
						width : pos.scroller_width+"px"
					});
					for(var i = 1; i <= page_number; i++){
						$$.find("div.view").append("<span>"+i+"</span>");
						memory.prototype.begin = i;
					}
					memory.prototype.begin = memory.prototype.begin - page_number;
					$$.find("div.view span").hover(
						function(){
							if($(this).text() != pos.now )
								$(this).css({
									backgroundColor : "#eee"
								});
						},
						function(){
							if($(this).text() != pos.now )
								$(this).removeAttr("style");
// 								$(this).css({
// 									backgroundColor : "transparent"
// 								});
						}
					).each(function(){
						if($(this).text() == pos.now )
							$(this)
							.bind("click", clicked)
							.addClass("now");
						else
							$(this).bind("click", clicked);
					});
					$$.find("div.scroll div").css({
						width : Math.round(pos.page_width),
						left : Math.floor(  (pos.now -1)*pos.page_width)
					});
					memory.prototype.width = $$.width();
					memory.prototype.parent = {};
					memory.prototype.parent.l = $$.find("div.scroll").position().left + ($$.find("div.scroller").width()/2);
					memory.prototype.parent.w = $$.find("div.scroll").width() - $$.find("div.scroller").width();
					memory.prototype.parent.left = $$.find("div.scroll").position().left;
					function resize(){
						if($$.width() != pos.width)
							with(setting){
								memory.prototype.page_width = $$.find("div.scroll").width()/count;
								memory.prototype.scroller_width = Math.round(pos.page_width*page_number);
								memory.prototype.parent = {};
								memory.prototype.parent.l = $$.find("div.scroll").position().left + ($$.find("div.scroller").width()/2);
								memory.prototype.parent.w = $$.find("div.scroll").width() - $$.find("div.scroller").width();
								$$.find("div.scroller").css({
									width : pos.scroller_width+"px",
									left : Math.round(pos.page_width * pos.begin)
								});
								$$.find("div.scroll div").css({
									width : Math.round(pos.page_width),
									left : Math.round(  (pos.now -1)*pos.page_width)
								});
								memory.prototype.width = $$.width();
							}
						setTimeout(function(){
							resize();
						}, 500);
					}
					resize();
					var moved = function(position, in_array){
						var page = Math.round(position/pos.page_width);
						var i = 1;
						if(page !== pos.begin){
							$(  $$.find("div.view").get(in_array)  ).find("span").each(function(){
								if(i + page <= count)
									$(this).html(
										i + page
									)
								else
									$(this).html("&nbsp;")
								if($(this).text() == pos.now )
									$(this).css({
										
									})
									.addClass("now");
								else
									$(this)
									.css({
										
									})
									.removeClass("now");
								i++;
							});
							memory.prototype.begin = page;
						}
					}
					$$.find("div.scroll").click(function(e){
						var scroller_width = $$.find("div.scroller").width();
						var scroller_left = $$.find("div.scroller").position().left - $$.find("div.scroll").position().left;
						var scroller_right = scroller_left + scroller_width;
						var __position = e.pageX - $(this).position().left;
						var index = $$.find("div.scroll").index(this);
						if(__position < scroller_left){
							var position = __position;
							moved(position, index);
							$(  $$.find("div.scroller").get(index)  ).css({
								left : __position
							});
						}
						if(__position > scroller_right){
							if(  __position > $$.find("div.scroll").width()  )
								__position = $$.find("div.scroll").width() ;
							var position = __position - scroller_width;
							moved(position, index);
							$(  $$.find("div.scroller").get(index)  ).css({
								left : __position - scroller_width
							});
						}
					});
					$$.find("div.scroller div").drag(function(e){
						var index = $$.find("div.scroller div").index(this);
						var position = Math.round(e.pageX - pos.parent.l);
						if(position >= 0 && position <= pos.parent.w){
							moved(position, index);
							$(  $$.find("div.scroller").get(index)  ).css({
								left : position
							});
						}
					});
					$$.find("div.scroller").drag(function(e){
						var index = $$.find("div.scroller").index(this);
						var position = Math.round(e.pageX - pos.parent.left - pos.clicked);
						if(position >= 0 && position <= pos.parent.w){
							moved(position, index);
							$(this).css({
								left : position
							});
						}
					}).mousedown(function(e){
						memory.prototype.clicked = e.pageX - $(this).position().left
					});
				}
				else
				{
					$(this).append('<div class="total"></div><div class="view"></div>');
					$$.find("div.total").append("Страниц: "+count);
					for(var i = 1; i <= count; i++){
						$$.find("div.view").append("<span>"+i+"</span>");
						memory.prototype.begin = i;
					}
					$$.find("div.view span").hover(
						function(){
							if($(this).text() != pos.now )
								$(this).css({
									backgroundColor : "#eee"
								});
						},
						function(){
							if($(this).text() != pos.now )
								$(this).removeAttr("style");
						}
					).each(function(){
						if($(this).text() == pos.now )
							$(this)
							.bind("click", clicked)
							.addClass("now");
						else
							$(this).bind("click", clicked);
					});
				}
			}
	}
})($);