/**
 * jPagination for jQuery分页插件 功能：指定页数内静态分页，超过指定页数后ajax请求下一组静态分页
 * 
 * @author 陈健
 * @version 1.0 beta 4
 * @date 2008-04-23
 * @param config
 *            插件配置
 */
jQuery.fn.pagination = function(config) {
	init("#" + this.attr("id"), config);
}

/**
 * 初始化，主程序
 * 
 * @param t
 *            容器的ID，带#号
 * @param config
 *            插件配置
 */
function init(t, config) {
	// 公有变量
	var valallcity = document.getElementById("allcity");
	var valshanghai = document.getElementById("shanghai");
	var valbeijing = document.getElementById("beijing");
	var valchengdu = document.getElementById("chengdu");
	var valshenzheng = document.getElementById("shenzheng");
	var valother = document.getElementById("other");
	var valprivate = document.getElementById("private");
	var valschool = document.getElementById("school");
	var valcorporate = document.getElementById("corporate");

	var dataStore = config.dataStore;
	var proxyUrl = config.proxyUrl != '' ? config.proxyUrl : 'pgdataproxy.jsp';
	var ajaxParam = config.ajaxParam;
	// 私有变量
	// 数据容器
	var container = '<div class="pgContainer"></div>'
	$(t).html(container);
	var valContainer = $(t + " .pgContainer");
	// 添加工具条
	var mask;
	var valJobCity = "";
	var valJobType = "private";

	getRemoteData();
	if (window.attachEvent) {
		valallcity.attachEvent('onclick', function() {
					getRemoteData();
				}, false);
	} else {
		valallcity.addEventListener('click', function() {
					getRemoteData();
				}, false);
	}
	if (window.attachEvent) {
		valshanghai.attachEvent('onclick', function() {
					getRemoteData();
				}, false);
	} else {
		valshanghai.addEventListener('click', function() {
					getRemoteData();
				}, false);

	}
	if (window.attachEvent) {
		valbeijing.attachEvent('onclick', function() {
					getRemoteData();
				}, false);
	} else {
		valbeijing.addEventListener('click', function() {
					getRemoteData();
				}, false);

	}
	if (window.attachEvent) {
		valchengdu.attachEvent('onclick', function() {
					getRemoteData();
				}, false);
	} else {
		valchengdu.addEventListener('click', function() {
					getRemoteData();
				}, false);
	}
	if (window.attachEvent) {
		valshenzheng.attachEvent('onclick', function() {
					getRemoteData();
				}, false);
	} else {
		valshenzheng.addEventListener('click', function() {
					getRemoteData();
				}, false);

	}
	if (window.attachEvent) {
		valother.attachEvent('onclick', function() {
					getRemoteData();
				}, false);
	} else {
		valother.addEventListener('click', function() {
					getRemoteData();
				}, false);

	}
	if (window.attachEvent) {
		valprivate.attachEvent('onclick', function() {
					getRemoteData();
				}, false);
	} else {
		valprivate.addEventListener('click', function() {
					getRemoteData();
				}, false);

	}
	if (window.attachEvent) {
		valschool.attachEvent('onclick', function() {
					getRemoteData();
				}, false);
	} else {
		valschool.addEventListener('click', function() {
					getRemoteData();
				}, false);

	}
	if (window.attachEvent) {
		valcorporate.attachEvent('onclick', function() {
					getRemoteData();
				}, false);
	} else {
		valcorporate.addEventListener('click', function() {
					getRemoteData();
				}, false);

	}

	/** *******************************init私有函数************************************************** */
	/**
	 * 置为正在检索状态
	 */
	function startLoad() {
		$(t).addClass("container");
		mask = document.createElement('div');
		$(mask).addClass("mask");
		$(mask).css("height", $(t).height());
		$(t).append(mask);
		$(t + " .pgRefresh").addClass("pgLoad");
		$(t + " .pgSearchInfo").html("正在检索中，请稍后...");
		valJobCity = document.getElementById("jobcity").value;
		valJobType = document.getElementById("jobtype").value;
	}

	/**
	 * 置为结束检索状态
	 */
	function overLoad() {
		$(mask).remove();
	}

	/**
	 * 获得远程数据
	 */
	function getRemoteData() {
		startLoad();
		$.ajax({
					type : "POST",
					url : proxyUrl + "?jobcity=" + valJobCity + "&jobtype="
							+ valJobType,
					contentType : "application/json; charset=utf-8",
					// data:{jobcity:valJobCity,jobtype:valJobType},
					dataType : "json",
					timeout : 30000,
					success : function(msg) {
						loadData(msg);
						overLoad();
					},
					error : function() {
						alert("error");
						overLoad();
						return;
					}
				});
	}

	function trim(str) {
		var reg_left = /^[\s]*/g;// 匹配开头的空白
		var reg_right = /[\s]*$/g;// 匹配结尾的空白
		return str.replace(reg_left, "").replace(reg_right, "");
	}

	function getAbstract(val, len) {
		if (val.length > len) {
			return val.substring(0, len);
		} else {
			return val;
		}

	}

	/**
	 * 刷新数据容器
	 */
	function loadData(msg) {
		var view = "";
		var data = msg[0].Content;

		if (data == '' || msg[0].Content == null) {

			valContainer.html(view);
			return;
		}

		$.each(data, function(i, n) {
			if (i % 2 == 0) {
				view += "<table cellspacing='0' cellpadding='0' class='en_listwj'>";
			} else {
				view += "<table cellspacing='0' cellpadding='0' class='en_listwj en_bgtable'>";
			}
			view += "	<tr>";
			view += "		<td rowspan='2' class='en_biaogecs'>";
			view += "			Opening " + data[i].id;
			view += "		</td>";
			view += "		<td class='en_txtleft col bold'>";
			view += "			Title:" + data[i].rate ;
			view += "		</td>";
			view += "		<td rowspan='2' class='en_width131'>";
			var strtext = "";
			var strcity = data[i].city;
			if (strcity.indexOf("海") != 0) {
				strtext = "Shanghai";
			} else if (strcity.indexOf("都") != 0) {
				strtext = "Chengdu";
			} else if (strcity.indexOf("京") != 0) {
				strtext = "Beijing";
			} else if (strcity.indexOf("圳") != 0) {
				strtext = "Shenzheng";
			} else
				strtext = strcity;
			view += "			<input name='' type='button' class='en_an' value='"
					+ strtext + "' />";

			var strtext = "please,login!";
			view += '<input name="" type="button" class="en_an" value="Read full ads" onclick="readfullads(\''
						+ data[i].id + "_" + valJobType + '\')"/>';			
			if (msg[0].Session == 'no') {
			//	view += '<input name="" type="button" class="en_an" value="Read full ads" onclick="alert(\'please,login!\');"/>';
				view += '<input name="" type="button" class="en_an" value="Contact the client" onclick="alert(\'please,login!\')"/>';
			} else {
				view += '<input name="" type="button" class="en_an" value="Contact the client" onclick="contactclient(\''
						+ data[i].toHandlerId + "_" + valJobType + '\')"/>';
			}
			view += "		</td>";
			view += "	</tr>";
			view += "	<tr>";
			view += "		<td class='en_txtleft'>";
					view += getAbstract(data[i].publishInfo, 100);	
			view += "		</td>";
			view += "	</tr>";
			view += "</table>";

		});
		valContainer.html(view);
	}
}

function ShowDailog(PageHref, Title, Height, Width) {
	var dleft = (screen.availHeight - Height) / 2;
	var dtop = (screen.availWidth - Width) / 2;
	var obj = new Object();
	obj.name = Title;
	window.showModalDialog(PageHref, obj, "location=0;status=0;dialogWidth="
					+ Height + "px;dialogHeight=" + Width + "px");
}

function readfullads(data) {
	ShowDailog('jobpublishinfo_' + data + ".html", 'NewWin', '670', '400');
}
function contactclient(data) {
	ShowDailog('contactclient_' + data + ".html", 'NewWin', '670', '300');
}
