QueryString = function(str) {
	var str = str ? str : document.location.href;
	this.argv = new Array();
	this.queryString = str.replace(/^[^\?]+\?/, '').replace(/#(.*)$/, '');
	if (!this.queryString) this.queryString = '';
	var _argv = this.queryString.split('&');
	for(var i=0; i<_argv.length; i++) {
		var _key = _argv[i].substring(0, _argv[i].indexOf('='));
		var _val = _argv[i].substring(_argv[i].indexOf('=')+1);
		if(!_key || _argv[i].indexOf('=') == -1) continue;
		this.argv[_key] = _val;
	}
}

QueryString.prototype.setVar = function(key,val) {
	if (typeof key == 'object') {
		for (var item in key) this.argv[item] = key[item];
	} else {
		this.argv[key] = val;
	}
	return this.getVar();
}

QueryString.prototype.getVar = function(key) {
	if (key) {
		return this.argv[key] ? this.argv[key] : '';
	} else {
		var _item = new Array();
		var h = $H(this.argv);
		var h_temp = unescape(h.toQueryString()).split('&');
		for(var i=0; i<h_temp.length; i++) {
			var __key = h_temp[i].substring(0, h_temp[i].indexOf('='));
			var __val = h_temp[i].substring(h_temp[i].indexOf('=')+1);
			if(!__key || h_temp[i].indexOf('=') == -1) continue;
			if (__key != 'mode' && __key != 'no' && __key != 'y') _item[_item.length] = __key + '=' + __val;
		}

		tempurl = location.href.split('?');
		return tempurl[0] + '?' +_item.join('&');
	}
}

Paging = function(total) {
	this.config = {
		pageVariable: 'page',
		pageBet: 'bets',
		numberFormat: '%n',
		showFirstLast: true,// 맨처음, 맨 마지막으로 가는 링크를 만들것인가.
		thisPageStyle: 'color:red;',
		itemPerPage: 20,	// 리스트 목록수
		pagePerView: 10,	// 페이지당 네비게이션 항목수
		showPrevBet: null,	// 페이지당 네비게이션 항목수
		showNextBet: null,	// 페이지당 네비게이션 항목수
		prevIcon: null,	// 이전페이지 아이콘
		nextIcon: null,	// 다음페이지 아이콘
		firstIcon: null,	// 첫페이지로 아이콘
		lastIcon: null,	// 마지막페이지 아이콘
		prevBetIcon: null,	// 검색구간이전
		nextBetIcon: null	// 검색구간다음
	}

	this.totalItem = total;
	this.qs = new QueryString;
	this.search = new QueryString;

	this.calculate = function() {
		this.totalPage = Math.ceil(this.totalItem / this.config.itemPerPage);
		this.currentPage = this.qs.getVar(this.config.pageVariable);
		if (!this.currentPage) this.currentPage = 1;
		if (this.currentPage > this.totalPage) this.currentPage = this.totalPage;
		this.lastPageItems = this.totalPage % this.config.itemPerPage;

		this.prevPage = this.currentPage-1;
		this.nextPage = this.currentPage+1;
		this.seek = this.prevPage * this.config.itemPerPage;
		this.currentScale = parseInt(this.currentPage / this.config.pagePerView);
		if (this.currentPage % this.config.pagePerView < 1) this.currentScale--;
		this.totalScale = parseInt(this.totalPage / this.config.pagePerView);
		this.lastScalePages = this.totalPage % this.config.pagePerView;
		if (this.lastScalePages == 0) this.totalScale--;
		this.prevPage = this.currentScale * this.config.pagePerView;
		this.nextPage = this.prevPage + this.config.pagePerView + 1;
	}

	this.toString = function() {
		var ss, se;
		var prevBetBtn = '';
		var nextBetBtn = '';
		var firstBtn = '';
		var lastBtn = '';
		var prevBtn = '';
		var nextBtn = '';

		this.calculate();

		if (this.config.showPrevBet) {
			if (this.config.prevBetIcon) prevBetBtn = '<img src="'+this.config.prevBetIcon+'" border="0" align="absmiddle">';
			else prevBetBtn = '[이전검색]';
			prevBetBtn = prevBetBtn.link(this.search.setVar(this.config.pageBet,this.config.showPrevBet).replace(/&page=[0-9]*/, ''));
		} else {
			prevBetBtn = '';
		}

		if (this.config.showNextBet) {
			if (this.config.nextBetIcon) nextBetBtn = '<img src="'+this.config.nextBetIcon+'" border="0" align="absmiddle">';
			else nextBetBtn = '[계속검색]';
			nextBetBtn = nextBetBtn.link(this.search.setVar(this.config.pageBet,this.config.showNextBet).replace(/&page=[0-9]*/, ''));
		} else {
			nextBetBtn = '';
		}

		if (this.config.showFirstLast) {
			if (this.config.firstIcon) firstBtn = '<img src="'+this.config.firstIcon+'" border="0" align="absmiddle">';
			else firstBtn = '[처음]'; //'☜';
			firstBtn = firstBtn.link(this.qs.setVar(this.config.pageVariable,1));

			if (this.config.lastIcon) lastBtn = '<img src="'+this.config.lastIcon+'" border="0" align="absmiddle">';
			else lastBtn = '[마지막]'; //'☞';
			lastBtn = lastBtn.link(this.qs.setVar(this.config.pageVariable,this.totalPage));
		} else {
			firstBtn = lastBtn = '';
		}

		if (this.config.prevIcon) prevBtn ='<img src="'+this.config.prevIcon+'" border="0" align="absmiddle"> ';
		else prevBtn = '◀';
		if (this.currentPage > this.config.pagePerView) {
			prevBtn = prevBtn.link(this.qs.setVar(this.config.pageVariable,this.prevPage));
		}

		ss = this.prevPage + 1;
		if ((this.currentScale >= this.totalScale) && (this.lastScalePages != 0)) se = ss + this.lastScalePages;
		else if (this.currentScale <= -1) se = ss;
		else se = ss + this.config.pagePerView;

		var navBtn = '';
		var navBtnt = new Array();
		 k =0;
		for(var i = ss; i<se; i++) {
			var pageText = this.config.numberFormat.replace(/%n/g,i);
			if (i == this.currentPage) {
				_btn = '<span style="color:black;font-weight:bold;">'+pageText+'</span>';
			} else {
				_btn = '<a href="'+this.qs.setVar(this.config.pageVariable,i)+'" style="'+this.config.otherPageStyle+'" onMouseOver=setHover("#5E3D05")>'+pageText+'</a>'
			}
			navBtnt[k] = _btn;
			k++;
		}
		navBtn = navBtnt.join(" I ");

		if (this.config.prevIcon) nextBtn =' <img src="'+this.config.nextIcon+'" border="0" align="absmiddle">';
		else nextBtn = '▶';
		if (this.totalPage > this.nextPage) {
			nextBtn = nextBtn.link(this.qs.setVar(this.config.pageVariable,this.nextPage));
		}


		return prevBetBtn+' '+firstBtn+' '+prevBtn+navBtn+nextBtn+' '+lastBtn+' '+nextBetBtn;
	}
}
