/*
	作者: 鱼龙混珠
	说明: 简单的URL地址操作类

*/
function UrlRequest(){
	this.url = location.href;
	
	var queryItem = new Array();
	var self = this;
	function init(){
		if(self.url == '')self.url = location.href;
		queryItem.splice(0,queryItem.length);
		var pos = self.url.indexOf('?');
		if(pos >= 0 && pos < self.url.length){
			var query = self.url.substr(pos + 1);
			var items = query.split('&');			
			for(var i=0; i<items.length; i++){
				var item = items[i].split('=');
				self.set(item[0],item.length>1?item[1]:'');
			}
		}
	}
	//判断是否存在某个键值
	this.exist = function(key){
		for(var i=0; i<queryItem.length; i++){
			if(queryItem[i].key.toLowerCase() == key.toLowerCase()){
				return true;
			}
		}
		return false;
	}
	//获取某个键值的数据
	this.get = function(key){
		for(var i=0; i<queryItem.length; i++){
			if(queryItem[i].key.toLowerCase() == key.toLowerCase()){
				return queryItem[i].value;
			}
		}
		return null;
	}
	//添加键值
	this.set = function(key,value){
		var obj = {};
		obj.key = key;
		obj.value = value;
		//查找判断是否存在
		for(var i=0; i<queryItem.length; i++){
			if(queryItem[i].key.toLowerCase() == key.toLowerCase()){
				queryItem[i].value = value;
				return;
			}
		}
		queryItem[queryItem.length] = obj;
	}
	//页面重跳转
	this.redirect = function(){
		var uri = '';
		if(self.url == '')self.url = location.href;
		var pos = self.url.indexOf('?');
		if(pos < 0){
			uri = self.url + '?';
		}else{
			uri = self.url.substring(0,pos + 1);
		}
		for(var i=0; i<queryItem.length; i++){
			if(i > 0)uri += '&';
			uri += queryItem[i].key + '=' + queryItem[i].value;
		}
		//alert(uri);
		window.location.href = uri;
		return true;
	}

	init();
}

var request = new UrlRequest();


/*
	作者: 鱼龙混珠
	说明: 简单的页码导航类

*/
function SimplePageClass(){
	this.currentPage = 1;	//当前页码
	this.pageCount = 1;		//总的页码
	this.pageSize = 10;		//一页显示的数量
	this.recordCount = 0;	//共有多少条数据
	this.linkCssName = '';	//导航链接的CSS样式名称
	this.dropCssName = '';	//导航下拉控件的CSS样式名称

	var self = this;
	
	this.changePage = function(page){
		//发生页码跳转时触发的事件函数
		request.set("page",page);
		request.redirect();
	}

	this.showPage = function(){
		if(self.pageCount < 1)self.pageCount = 1;
		if(self.recordCount < 1)self.recordCount = 0;
		if(self.currentPage < 2)self.currentPage = 1;
		if(self.currentPage > self.pageCount)self.currentPage = self.pageCount;

		if(self.currentPage < 2){
			document.write('<span class="' + self.linkCssName + '" disabled>首页</span>　');
			document.write('<span class="' + self.linkCssName + '" disabled>上一页</span>　');
		}else{
			document.write('<a href="javascript:simplePage.changePage(1);" class="' + self.linkCssName + '">首页</a>　');
			document.write('<a href="javascript:simplePage.changePage(' + (self.currentPage - 1) + ');" class="' + self.linkCssName + '">上一页</a>　');
		}
		if(self.currentPage < self.pageCount){
			document.write('<a href="javascript:simplePage.changePage(' + (self.currentPage + 1) + ');" class="' + self.linkCssName + '">下一页</a>　');
			document.write('<a href="javascript:simplePage.changePage(' + self.pageCount + ');" class="' + self.linkCssName + '">尾页</a>　');			
		}else{
			document.write('<span class="' + self.linkCssName + '" disabled>下一页</span>　');
			document.write('<span class="' + self.linkCssName + '" disabled>尾页</span>　');
		}
		self.showDropPage();
	}

	this.showDropPage = function(){
		document.write('转到:');
		document.write('<select name="smPageNo" id="smPageNo" onchange="simplePage.changePage(this.value);"');
		if(self.pageCount < 2)document.write(' disabled');
		document.write('>');
		for(var i=1; i<=self.pageCount; i++){
			if(i == self.currentPage){
				document.write('<option value="' + i + '" selected>第' + i + '页</option>');
			}else{
				document.write('<option value="' + i + '">第' + i + '页</option>');
			}
		}
		document.write('</select>');
	}
}

var simplePage = new SimplePageClass();
