// JavaScript Document
function $() {
   	return document.getElementById(arguments[0]);
}
var cheatTypeMap = {
	"index.jsp"     : 0,
	"example-1.jsp" : 3,
	"example-2.jsp" : 1,
	"example-3.jsp" : 1,
	"example-4.jsp" : 1,
	"example-5.jsp" : 1,
	"example-6.jsp" : 3,
	"example-7.jsp" : 2,
	"example-8.jsp" : 0
};

function showWindow(wid) {
	var mask = document.getElementById("mask");
	var iframe = document.getElementById("mask-if");
	var w = document.getElementById(wid);
	if (w) {
		var t = parseInt(document.documentElement.scrollTop);
		if (t == 0 || isNaN(t)) {
			t = document.body.scrollTop;
		}
		with (w.style) {
			left = ((document.body.clientWidth - w.clientWidth) / 2) + "px";
			top = (t + 50) + "px";
		}
		var x = [mask, iframe];
		var h = document.body.scrollHeight;
		for (var i = 0; i < x.length; i++) {
			with (x[i].style) {
				//width = document.body.scrollWidth + "px";
				height = h + "px";
			}
		}
		iframe.style.visibility = "inherit";
		mask.style.visibility = "inherit";
		w.style.visibility = "inherit";
	}
	if (wid=="report") {
		initReportFormElems();
		$("result").innerHTML = "&nbsp;";
		document.forms[0].style.display = "block";
		$("succ").style.display = "none";
		$("succ_result").innerHTML = "&nbsp;";
		var filename = location.href.substr(location.href.lastIndexOf('/')+1);
		if (filename.indexOf("?")>0)
			filename = filename.substring(0,filename.indexOf("?"));
		var defaultType = cheatTypeMap[filename];
		if(defaultType == null)  defaultType = 0;
		document.forms[0].type.selectedIndex = defaultType;
		cheatTypeChanged(document.forms[0].type.value);
	}

}

function hideWindow(wid) {
	if (wid=="report" && location.href.indexOf("=")>0)
		location.href = location.href.substring(0,location.href.indexOf("?"));
	var mask = document.getElementById("mask");
	var iframe = document.getElementById("mask-if");
	var w = document.getElementById(wid);
	if (w) {
		iframe.style.visibility = "hidden";
		mask.style.visibility = "hidden";
		w.style.visibility = "hidden";
	}
}

function adjustWindowPosition(wid) {
	var w = document.getElementById(wid);
	if (w && w.style.visibility != "hidden") {
		w.style.left = ((document.body.clientWidth - w.clientWidth) / 2) + "px";
	}
}

function initTab(id) {
	for (var i = 0; ; i++) {
		var e = document.getElementById(id + "-" + i);
		if (e) {
			e.onclick = function(evnt) {
				var e = evnt && evnt.target ? evnt.target : window.event.srcElement;
				var f = true;
				while (f && e.tagName.toLowerCase() != "body") {
					if (e.id && e.id.indexOf(id + "-") == 0 && e.id.lastIndexOf("-") == id.length) {
						f = false;
					} else {
						e = e.parentNode;
					}
				}
				for (var i = 0; ; i++) {
					var x = document.getElementById(id + "-" + i);
					if (x) x.className = id; else break;
				}
				e.className = id + "-curr";
			};
			if(i==0)  getCaseList(2,0);
		} else {
			break;
		}
	}
}

var reportFormElems = [["barname", ""], ["address", ""], ["content", "30字以上，请详细描述事情的时间、地点、人物、经过。"]];

function initReportFormElems() {
	for (var i = 0; i < reportFormElems.length; i++) {
		var e = document.forms[0][reportFormElems[i][0]];
		e.style.color = "#aaa";
		e.value = reportFormElems[i][1];
		e.onkeydown = function(){
			$("result").innerHTML = "&nbsp;";
		};
		e.onfocus = function(){
			for (var i = 0; i < reportFormElems.length; i++) {
				if (this.value == reportFormElems[i][1]) {
					this.value = "";
				}
			}
			this.style.color = "#000";
			$("result").innerHTML = "&nbsp;";
		};
	}
}

function setInputNull() {
	for (var i=reportFormElems.length-1; i >=0 ; i--) {
		var e = document.forms[0][reportFormElems[i][0]];
		if(e.value == reportFormElems[i][1])  e.value = "";
		if(e.value == "")  e.focus();
	}
	return true;
}
window.onload = function() {
	if (location.href.indexOf("=")>0)  showWindow("report");
	initTab("city");
};

window.onresize = function() {
	adjustWindowPosition("report");
	adjustWindowPosition("login");
};

var login = false;
var loginUrs = false;
var chklogin = false;

function chkstatus() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var s = eval(http_request.responseText);
			var n = parseInt(s.pop());
			if (n == 100) {
				login = true;
				loginUrs = true;
			} else if (n == 4002 || n == 4003 || n == 4004) {
				loginUrs = true;
			}
			chklogin = true;
			showLoginBox();
		}
	}
}

function showLoginBox() {
	if (!chklogin) {
		send_request("http://idate.163.com/loginstatus.do", chkstatus);
	} else {
		chklogin = false;
		if (!loginUrs) {
			showWindow("login");
			$("username").focus();
		} else {
			showWindow("report");
		}
	}
}

var http_request = false;

function send_request(url,method) {
   http_request = false;
   if(window.XMLHttpRequest) {
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
         http_request.overrideMimeType("text/xml");
      }
   }
   else if (window.ActiveXObject) {
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
	     try {
             http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      window.alert("不能创建 XMLHttpRequest 对象实例.");
      return false;
   }

   http_request.onreadystatechange = method;
   try{
      http_request.open("GET", url, true);
      http_request.send(null);
   }catch(e){alert(e);}
}

/********************** user defined **************************/

function globalEmit(text) {
	if (text == null || text.length <= 0) return text;
	text = text.replace(/\r\n/g, '\\n');
	text = text.replace(/[\r\n]/g, '\\n');
	text = text.replace(/</g, '&lt;');
	text = text.replace(/>/g, '&gt;');
	return text;
}
function execute(stack) {
	while (stack.length > 0) {
		if (Function.prototype.isPrototypeOf(stack[stack.length - 1])) {
			var code = stack.pop();
			code.call(null, stack);
		} else {
			break;
		}
	}
}
function processText(text, cb) {
	var stack = eval(globalEmit(text));
	var code = stack.pop();
	switch (parseInt(code)) {
		case 4001:
			alert("连接超时，请重新登录");
			break;
		case 200:
			execute(stack);	
			if (stack.length > 0 && cb) cb(stack);
			break;
		default:
			alert("未知代码：" + code);
	}
}
function chkld(){
	var ii = navigator.appName.indexOf("Explorer") > -1;
	
	var text = ii ? window.frames["mframe"].document.body.innerText :
				window.frames["mframe"].document.body.textContent;

	if(text != ""){
		try{
			processText(text);
		}catch(e){
			//alert("错误!");
		}
		window.frames["mframe"].document.location='about:blank';
	}
}

function showResult(stask){
	var message = stask.pop();
	if (message.tag == 0) {
		$("result").innerHTML = message.content;
	}else{
		document.forms[0].style.display = "none";
		$("succ").style.display = "block";
		$("succ_result").innerHTML = message.content;
	}
}

//--------------------显示酒吧列表begin------------------
function getCaseList(place, page){
	var ajax = new AjaxWrap();
	ajax.openText("/listcheatcase.do?type="+place+"&page="+page, true);
}
function caseListView(stask){
	var caselistpb = stask.pop();
	var place = stask.pop();
	var infoes = stask.pop();
	var barDiv = document.getElementById("placecontainer");
	barDiv.innerHTML='';
	for(var i=0;i<infoes.length;i++){
		if(i%3==0)  barDiv.innerHTML+='<div class="row">';
		barDiv.innerHTML+='<div class="place">\
							<table>\
								<tr><th>名称：</th><td>'+infoes[i].barname+'</td></tr>\
								<tr><th>地址：</th><td>'+infoes[i].address+'</td></tr>\
							</table>\
							</div>';
		if(i%3==2 || i==infoes.length-1){
				barDiv.innerHTML+='<div class="clear"></div>\
					</div>';
		}
	}	
	barDiv.innerHTML+='<table width="100%" height="30" border="0" style="position:absolute;bottom:10px;left:0">\
                <tr><td align="center" class="mb_fy" >'+renderCheatCasePage(caselistpb,place)+'</td></tr>\
          	</table>';
}


function AjaxWrap(callBack) {
	this.req = null;
	this.callBack = callBack;
	if (window.XMLHttpRequest) {
		this.req = new XMLHttpRequest();
	}else if (window.ActiveXObject){
		try {
			this.req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				this.req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				alert("unsuport navigate");
			}
		}
	}	
}
function response(req,cb,bXML) {
	return function(){
		if (req.readyState == 4) {
			if (req.status == 200) {			
				if(bXML){
					cb(req.responseXML);
				}else{
					processText(req.responseText, cb);
				}			}			
		}
	}
}
AjaxWrap.prototype.openText = function(url, sync){
	this.req.onreadystatechange = response(this.req, this.callBack, false);
	this.req.open("POST", url, sync);
	this.req.send("");
}
//--------------------显示酒吧列表end------------------

function cheatTypeChanged(value) {
	showElement("barLine",(value==1));
}
function showElement(id, bshow){
	var tag = bshow ? 'block' : 'none';
	var maintable = document.getElementById(id);
	
	if(maintable){
		maintable.style.display = tag;
	}
}
function renderCheatCasePage(pb,place){
	return renderPageDiv(pb,"getCaseList(%0,%p)",[place]);
}
function renderPageDivFunction(funstring, params, page){
	if(!params)  params = new Array();
	for(var i=0; i<params.length; ++i){
		funstring = funstring.replace("%"+i,params[i]);
	}
	funstring = funstring.replace("%p",page);
	return funstring;
}
function renderPageDivLoop(begin, end, current, funstring, params){
	var loopret = "";
	for(var i = begin ; i <= end; ++i){
		if(i == current){
			loopret += "<span class='sed' style='font-weight:bold;color:#676767;font-size:14px;'>" + i + "</span>";
		}else{
			loopret += "<span href='#' onclick='"+renderPageDivFunction(funstring,params,i)+";return false;' class='num' onMouseOver='overIndex(this);' onMouseOut='outIndex(this);'>" + i + "</span>"
		}
	}
	return loopret;
}
function renderPageDiv(pb, funstring, params){
	var current = parseInt(pb.current);
	var per = parseInt(pb.per);
	var total = parseInt(pb.total);
	var ret = "";
	var totpage = parseInt(Math.ceil(total / per).toFixed());
	var offset = (totpage!=11?1:0);
	
	if(totpage <= 1)  return ret;
	ret = "<div class='pageLink' style='margin-bottom:5px;'>";
	if(current > 1){
		ret += "<span class='num' href='#' onclick='"+renderPageDivFunction(funstring,params,current-1)+";return false;' onMouseOver='overIndex(this);' onMouseOut='outIndex(this);'><img src='/images/icn_left.gif'>&nbsp;上一页</span>"; 
	}

	if(totpage<=10){
	//不足10个记录
		ret += renderPageDivLoop(1, totpage, current, funstring, params);
	}else if(current<=8){
	//偏向列首,在前10个记录
		ret += renderPageDivLoop(1, 10, current, funstring, params);
		ret += "...";
		ret += renderPageDivLoop(totpage-offset, totpage, current, funstring, params);
	}else if(current>=totpage-7){	
	//偏向列尾,在后10个记录
		ret += renderPageDivLoop(1, 1+offset, current, funstring, params);
		ret += "...";
		ret += renderPageDivLoop(totpage-9, totpage, current, funstring, params);
	}else{
	//在列中
		ret += renderPageDivLoop(1, 2, current, funstring, params);
		ret += "...";
		ret += renderPageDivLoop(current-4, current+4, current, funstring, params);
		ret += "...";
		ret += renderPageDivLoop(totpage-1, totpage, current, funstring, params);
	}
	
	if(current < totpage){
		ret += "<span class='num' href='#' onclick='"+renderPageDivFunction(funstring,params,current+1)+";return false;' onMouseOver='overIndex(this);' onMouseOut='outIndex(this);'>下一页&nbsp;<img src='/images/icn_right.gif'></span>"; 
	}
	ret += "</div>";
	return ret;
}
function overIndex(span){
	span.style.backgroundColor = '#f2f6fb';	
	span.style.border = '1px solid #d0dbe7';
}

function outIndex(span){
	span.style.backgroundColor = '';	
	span.style.border = '#ffffff 1px solid';	
}
