var PagerModel = function(total, limit, page_scale, callback){
	this.last_page = Math.ceil(total / limit);
	this.limit = limit;
	this.page_scale = page_scale;
	this.callback = callback;
	this.current_page = 1;
	this.current_page_group = 1;
	this.last_page_group = Math.ceil(this.last_page / this.page_scale);
	this.max_count = (this.page_scale <= this.last_page)? this.page_scale : this.last_page;
}


var Pager = function(_model, _view){
	this.model = _model;
	this.view = _view;

	this.bind_button;
	this.bind_to_button = function(target, parent){
		parent.bind_button = target;
		jQuery(target).click(
			function(){
				parent.move(this);
			}
		);

		jQuery(target).mouseover(this.view.onmouse);
		jQuery(target).mouseout(this.view.onmouse);
	};

	this.display = function(start_page){
		var start_page = start_page || 1;
		var pagebox = this.view.make_pagebox(start_page, this.model.max_count);

		this.view.show_paging(pagebox);
		this.bind_to_button(this.bind_button, this);
	};

	this.move = function(element){
		this.move.go_page = jQuery.trim(jQuery(element).html()) || 1;

		if(parseInt(this.move.go_page) > 0){
			this.move.page(this, element);
		}
		else if(this.move.go_page == this.view._prev){
			this.move.prev(this, this.model.page_scale);
		}
		else if(this.move.go_page == this.view._next){
			this.move.next(this, this.model.page_scale);
		}
	};

	this.move.page = function(parent, element){
		parent.model.current_page = go_page;
		var go_page = this.go_page;
		var limit = parent.model.limit;

		var start = (go_page - 1) * limit;
		var end = limit * go_page;


		if(typeof(parent.model.callback) == "object"){
			var _class = parent.model.callback._class;
			var _func = parent.model.callback._func;
			_class[_func](start, end);
		}
		else{
			this.callback = parent.model.callback;
			this.callback(start, end);
		}

		parent.view.choix_page(element);
	};

	this.move.prev = function(parent, page_scale){
		var prev_page = ((parent.model.current_page_group - 2) * page_scale) + 1;

		parent.model.current_page_group--;

		if(1 >= prev_page){
//			prev_page = 1;
			parent.model.current_page_group = 1;
		}
		else{
			parent.display(prev_page);
		}
	};

	this.move.next = function(parent, page_scale){
		var next_page = (parent.model.current_page_group * page_scale) + 1;

		var current_page_group = parent.model.current_page_group;
		parent.model.current_page_group++;

		if(parent.model.last_page_group <= current_page_group){
			next_page = ((parent.model.last_page_group - 1) * parent.model.page_scale) + 1;
			parent.model.current_page_group = parent.model.last_page_group;
		}
		else{
			parent.display(next_page);
		}
	};
};

var PagerHelper = function(target){
	this.target = target;

	this._prev = "≪ Prev " + page_scale;
	this._next = "Next " + page_scale + " ≫";

	this.make_page = function(text_node, class_name){
		return "<li class='" + class_name + "'>" + text_node + "</li>";
	};

	this.make_pagebox = function(start_page, max_count){
		var max_count = start_page + max_count ;
		var first_page;

		var page_li = '';
		var page_prev = '';
		var page_next = '';

		page_prev = page_li + this.make_page(this._prev, "Pre_Net page_move_button");
		if(max_count > page_scale){
			page_next = page_li + this.make_page(this._next, "Pre_Net page_move_button");
		}

		for(var i=start_page; i<max_count; i++){
			first_page = (i == 1) ? " Pick" : "";
			page_li = page_li + this.make_page(i, "page_move_button" + first_page);
		}

		return "<ul class='paging'>" + page_prev + page_li + page_next + "</ul>";
	};

	this.show_paging = function(pagebox){
		jQuery(target).html(pagebox);
	}

	this.choix_page = function(element){
		jQuery(".paging li").removeClass("Pick");
		jQuery(element).addClass("Pick");
	}

	this.onmouse = function(){
		jQuery(this).toggleClass("Box_hover");
	}
};

var PagerWSSkin = function(target) {
	this.target = target;

	this._prev = "previous";
	this._next = "next";

	this.make_page = function(text_node, class_name){
		return "<li class='" + class_name + "'>" + text_node + "</li>";
	};

	this.make_pagebox = function(start_page, max_count){
		var max_count = start_page + max_count ;
		var first_page;

		var page_li = '';
		var page_prev = '';
		var page_next = '';

		page_prev = "<a href=\"#\" onclick=\"alert('prev');return false\" class=\"pagenate_previous\">previous</a>";
		if(max_count > page_scale){
			page_next = "<a href=\"#\" onclick=\"alert('next');return false\" class=\"pagenate_next\">next</a>";
		}

		for(var i=start_page; i<max_count; i++){
			first_page = (i == 1) ? " Pick" : "";
			page_li = page_li + this.make_page(i, "page_move_button" + first_page);
		}

		return page_prev + page_next + "<ul>" + page_li + "</ul><div class=\"Clear\"></div>";
	};

	this.show_paging = function(pagebox){
		jQuery(target).html(pagebox);
	}

	this.choix_page = function(element){
		jQuery(".paging li").removeClass("Pick");
		jQuery(element).addClass("Pick");
	}

	this.onmouse = function(){
		jQuery(this).toggleClass("Box_hover");
	}
}