var keyword = "",
	page = 1,
	maxpage = 0,
	lzname = "",
	reserved = false;
var cdata;

$(function() {
	//判断只看楼主
	if(localStorage.seelz == "1") {
		$('#seelz').text('取消只看楼主');
		$('#seelz').removeClass('am-btn-primary');
	} else {
		localStorage.seelz = 0;
	}
	//显示loading框
	$('#alert-loading').modal("open");
	//初始化事件
	keyword = decodeURIComponent(getQueryString("k"));
	callbackObj.web_collectionget();
	//收藏处理
	$('#star').click(function(){
		
		var obj = [];
		if(cdata!=null & cdata!="")
		{
			obj = eval("("+cdata+")");
			var cango = true;
			for (var i=0;i<obj.length;i++) {
				
				if(obj[i].tid==keyword)
				{
					alert('已经收藏过了');
					cango=false;
					break;
				}
			}
			if(cango)
			{
				var o = new Object();
				o.tid = keyword;
				o.title = $('#tt').text();
				o.author = lzname;
				obj.push(o);
				callbackObj.web_collectionset(JSON.stringify(obj));
				alert('收藏成功');
			}
		}
		else
		{
			var o = new Object();
			o.tid = keyword;
			o.title = $('#tt').text();
			o.author = lzname;
			obj.push(o);
			callbackObj.web_collectionset(JSON.stringify(obj));
			alert('收藏成功');
		}
		callbackObj.web_collectionget();
	});
	

	if(decodeURIComponent(getQueryString("reserved")) == "1") {
		lzname = decodeURIComponent(getQueryString("lzname"));
		$('#reversed').addClass('am-disabled');
		$('#reversed').html("处于倒序状态");
		page = Number(decodeURIComponent(getQueryString("page")));
		maxpage = page;
		reserved = true;
		$('#pagejump').attr('placeholder', page + '/' + page);
		setTimeout(function() {
			callbackObj.web_thread_callback(keyword, page + "", localStorage.seelz, lzname, "1")
		}, 500);
	} else {
		setTimeout(function() {
			callbackObj.web_thread_callback(keyword, page + "", localStorage.seelz, lzname, "0");

		}, 500);
	}
	//点击只看楼主
	$('#seelz').click(function() {
		//localStorage.seelz=>1为只看楼主
		//这里是不看楼主的结果
		if(localStorage.seelz != "1") {
			localStorage.seelz = "1";
		} else {
			localStorage.seelz = "";
		}
		location.reload();
	});
	//刷新
	$('#reload').click(function() {
		location.reload();
	});
	//回复
	$('#reply').click(function() {
		setTimeout(function() {
			callbackObj.web_tidreply(keyword);
		}, 1000)
	});
	$('#reversed').click(function() {
		callbackObj.web_thread_enter3(keyword, maxpage + "", lzname);
	})
	$('#jump').click(function() {
		if(Number($('#pagejump').val()) <= maxpage) {
			page = Number($('#pagejump').val()) - 1;
			if(reserved) {
				$("#addthis").append("<p style='text-align:center'>————以下为" + $('#pagejump').val() + "页以上内容————</p>");
			} else {
				$("#addthis").append("<p style='text-align:center'>————以下为" + $('#pagejump').val() + "页以下内容————</p>");
			}

			var $w = $(window);
			$w.smoothScroll({
				position: $(document).height() - $w.height()
			});
		} else {
			$('#pagejump').val('');
		}
	});
	//下拉刷新
	$(window).scroll(function() {　　
		var scrollTop = $(this).scrollTop();　　
		var scrollHeight = $(document).height();　　
		var windowHeight = $(this).height();　　
		if(scrollTop + windowHeight == scrollHeight) {　　　　
			if(!lock) {
				lock = true;
				$('#btn-add').attr('style', '');
				var flag = false;
				if(reserved) {
					flag = (page != 1) ? true : false;
				} else {
					flag = (maxpage > page) ? true : false;
				}
				if(flag) {
					if(!reserved) {
						page++;
						setTimeout(function() {
							callbackObj.web_thread_callback(keyword, page + "", localStorage.seelz, lzname, "0");
							$('#btn-add').attr('style', 'display:none ;');
						}, 500);
						$('#pagejump').attr('placeholder', page + '/' + maxpage);
					} else {
						page--;
						setTimeout(function() {
							callbackObj.web_thread_callback(keyword, page + "", localStorage.seelz, lzname, "1");
							$('#btn-add').attr('style', 'display:none ;');
						}, 500);
						$('#pagejump').attr('placeholder', page + '/' + maxpage);
					}

				} else {
					$('#btn-add').attr('style', 'display:none ;');
				}
			}　　
		}
	});

});
function setcdata(cd) {
	cdata = cd;
}
function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) return r[2];
	return null;
}
//添加楼层
function addfloor(id, name, time, lv, content, hand, is_lz, maxlzlpage, pid) {
	$('#alert-loading').modal("close");
	if(name == "err") {
		//alert(is_lz);
	} else {
		$("#sub" + id).data('maxpage', maxlzlpage);
		//alert('aaqqa');
		if(is_lz == 1) {
			$("#sub" + id).before('<li class="am-comment am-comment-primary list-left">' +
				'<a onclick="callbackObj.web_user_see(\'' + name + '\');" ><img style="float:left;" src="http://tb.himg.baidu.com/sys/portrait/item/' + hand + '" alt="" class="am-comment-avatar" width="48" height="48" />' +
				'</a><div class="am-comment-main "><header class="am-comment-hd "><div class="am-comment-meta">' +
				'<span class="am-badge am-badge-primary am-radius">LV.' + lv + '</span>     <a onclick="callbackObj.web_user_see(\'' + name + '\');" class="am-comment-author">' + name + '&nbsp;&nbsp;' +
				'</a>&nbsp;&nbsp;<time datetime="' + time + '" title="' + time + '">' +
				time + '</time><span style="float:right;" onclick=\'callbackObj.web_tidreply3("' + keyword + '","' + id + '","' + $("#sub" + id).attr('floor') + '","' + name + '","' + pid + '","' + $('#panel_'+id).data('pg')+ '","' + lzname+ '","' + localStorage.seelz+ '")\' class="am-badge am-badge-primary am-radius">回复</span></div></header><div class="am-comment-bd">' + content + '</div></div></li>');
		} else {
			$("#sub" + id).before('<li class="am-comment am-comment-flip list-unlz">' +
				'<a onclick="callbackObj.web_user_see(\'' + name + '\');" ><img style="float:right;" src="http://tb.himg.baidu.com/sys/portrait/item/' + hand + '" alt="" class="am-comment-avatar" width="48" height="48" />' +
				'</a><div class="am-comment-main "><header class="am-comment-hd "><div class="am-comment-meta">' +
				'<span class="am-badge am-badge-primary am-radius">LV.' + lv + '</span>     <a onclick="callbackObj.web_user_see(\'' + name + '\');" class="am-comment-author">' + name + '&nbsp;&nbsp;' +
				'</a>&nbsp;&nbsp;<time datetime="' + time + '" title="' + time + '">' +
				time + '</time><span style="float:right;" onclick=\'callbackObj.web_tidreply3("' + keyword + '","' + id + '","' + $("#sub" + id).attr('floor') + '","' + name + '","' + pid + '","' + $('#panel_'+id).data('pg')+ '","' + lzname+ '","' + localStorage.seelz+ '")\' class="am-badge am-badge-primary am-radius">回复</span></div></header><div class="am-comment-bd">' + content + '</div></div></li>');
		}
		//alert()

	}
}

//添加帖子 public void web_tidreply2(string tid, string pid, string floor, string page, string lz_name, string is_lz)
function addthread(name, time, lv, content, hand, is_lz, floor, title, pid,p) {
	$('#alert-loading').modal("close");
	if(name == "err") {
		//alert(is_lz);
	} else {
		lock = false;
		$('#pagejump').attr('placeholder', page + '/' + maxpage);
		if(floor=="1")
		{
			$("#addthis").append('<li data-pg="'+p+'" id="panel_'+pid+'" class="am-comment am-comment-primary">' +
				'<a onclick="callbackObj.web_user_see(\'' + name + '\');" ><img src="http://tb.himg.baidu.com/sys/portrait/item/' + hand + '" alt="" class="am-comment-avatar" width="48" height="48" />' +
				'</a><div class="am-comment-main "><header class="am-comment-hd "><div class="am-comment-meta">' +
				'<span class="am-badge am-radius">' + floor + '楼</span>     <span class="am-badge am-badge-primary am-radius">LV.' + lv + '</span>     <a onclick="callbackObj.web_user_see(\'' + name + '\');" class="am-comment-author">' + name + '&nbsp;&nbsp;' +
				'</a>&nbsp;&nbsp;<time datetime="' + time + '" title="' + time + '">' +
				time +
				'</time>' +
				'<span style="float:right;" onclick=\'callbackObj.web_tidreply2("' + keyword + '","' + pid + '","' + floor + '","' + p+ '","' + lzname+ '","' + localStorage.seelz+ '")\' class="am-badge am-badge-primary am-radius">回复</span></div></header><div class="am-comment-bd">' + content + '</div></div></li>');

		}
		else if(is_lz == 1) {
			$("#addthis").append('<li data-pg="'+p+'" id="panel_'+pid+'" class="am-comment am-comment-primary">' +
				'<a onclick="callbackObj.web_user_see(\'' + name + '\');" ><img src="http://tb.himg.baidu.com/sys/portrait/item/' + hand + '" alt="" class="am-comment-avatar" width="48" height="48" />' +
				'</a><div class="am-comment-main "><header class="am-comment-hd "><div class="am-comment-meta">' +
				'<span class="am-badge am-radius">' + floor + '楼</span>     <span class="am-badge am-badge-primary am-radius">LV.' + lv + '</span>     <a onclick="callbackObj.web_user_see(\'' + name + '\');" class="am-comment-author">' + name + '&nbsp;&nbsp;' +
				'</a>&nbsp;&nbsp;<time datetime="' + time + '" title="' + time + '">' +
				time +
				'</time><span style="float:right;margin-left:5px" onclick=\'refreshfloor("' + keyword + '","' + p+ '","' + floor + '","' + pid + '")\' class="am-badge am-badge-success am-radius">刷新本楼</span>' +
				'<span style="float:right;" onclick=\'callbackObj.web_tidreply2("' + keyword + '","' + pid + '","' + floor + '","' + p+ '","' + lzname+ '","' + localStorage.seelz+ '")\' class="am-badge am-badge-primary am-radius">回复</span></div></header><div class="am-comment-bd">' + content + '</div></div></li>');
		} else {
			$("#addthis").append('<li data-pg="'+p+'" id="panel_'+pid+'" class="am-comment am-comment-flip">' +
				'<a onclick="callbackObj.web_user_see(\'' + name + '\');" ><img src="http://tb.himg.baidu.com/sys/portrait/item/' + hand + '" alt="" class="am-comment-avatar" width="48" height="48" />' +
				'</a><div class="am-comment-main "><header class="am-comment-hd "><div class="am-comment-meta">' +
				'<span class="am-badge am-radius">' + floor + '楼</span>     <span class="am-badge am-badge-primary am-radius">LV.' + lv + '</span>     <a onclick="callbackObj.web_user_see(\'' + name + '\');" class="am-comment-author">' + name + '&nbsp;&nbsp;' +
				'</a>&nbsp;&nbsp;<time datetime="' + time + '" title="' + time + '">' +
				time + '</time><span style="float:right;margin-left:5px" onclick=\'refreshfloor("' + keyword + '","' + p+ '","' + floor + '","' + pid + '")\' class="am-badge am-badge-success am-radius">刷新本楼</span>' +
				'<span style="float:right;" onclick=\'callbackObj.web_tidreply2("' + keyword + '","' + pid + '","' + floor + '","' + p+ '","' + lzname+ '","' + localStorage.seelz+ '")\' class="am-badge am-badge-primary am-radius">回复</span></div></header><div class="am-comment-bd">' + content + '</div></div></li>');
		}
		$('#tt').text(title);
	}
}

function refreshfloor(tid,p,floor,pid) {
	
	$('#panel_' +pid ).html("<h3 style='text-align:center;width:100%;margin:30px 0;'>正在刷新...<span style='margin-left:5px;' class=\"am-icon-cog am-animation-spin am-animation-reverse\"></span></h3>");
	setTimeout(function(){
		callbackObj.web_thread_floor_refresh(tid,p,floor, lzname,localStorage.seelz);
	},200);
	
}
function refreshflooradd(name, time, lv, content, hand, is_lz, floor, pid,p)
{
	if(name == "err") {
		//alert(is_lz);
	} else {
		if(is_lz == 1) {
			$('#panel_' + pid).html(
				'<a onclick="callbackObj.web_user_see(\'' + name + '\');" ><img src="http://tb.himg.baidu.com/sys/portrait/item/' + hand + '" alt="" class="am-comment-avatar" width="48" height="48" />' +
				'</a><div class="am-comment-main "><header class="am-comment-hd "><div class="am-comment-meta">' +
				'<span class="am-badge am-radius">' + floor + '楼</span>     <span class="am-badge am-badge-primary am-radius">LV.' + lv + '</span>     <a onclick="callbackObj.web_user_see(\'' + name + '\');" class="am-comment-author">' + name + '&nbsp;&nbsp;' +
				'</a>&nbsp;&nbsp;<time datetime="' + time + '" title="' + time + '">' +
				time +
				'</time><span style="float:right;margin-left:5px" onclick=\'refreshfloor("' + keyword + '","' + p+ '","' + floor + '","' + pid + '")\' class="am-badge am-badge-success am-radius">刷新本楼</span>' +
				'<span style="float:right;" onclick=\'callbackObj.web_thread_reply2("' + keyword + '","' + pid + '","' + floor + '")\' class="am-badge am-badge-primary am-radius">回复</span></div></header><div class="am-comment-bd">' + content + '</div></div>');
		} else {
			$('#panel_' + pid).html(
				'<a onclick="callbackObj.web_user_see(\'' + name + '\');" ><img src="http://tb.himg.baidu.com/sys/portrait/item/' + hand + '" alt="" class="am-comment-avatar" width="48" height="48" />' +
				'</a><div class="am-comment-main "><header class="am-comment-hd "><div class="am-comment-meta">' +
				'<span class="am-badge am-radius">' + floor + '楼</span>     <span class="am-badge am-badge-primary am-radius">LV.' + lv + '</span>     <a onclick="callbackObj.web_user_see(\'' + name + '\');" class="am-comment-author">' + name + '&nbsp;&nbsp;' +
				'</a>&nbsp;&nbsp;<time datetime="' + time + '" title="' + time + '">' +
				time + '</time><span style="float:right;margin-left:5px" onclick=\'refreshfloor("' + keyword + '","' + p+ '","' + floor + '","' + pid + '")\' class="am-badge am-badge-success am-radius">刷新本楼</span>' +
				'<span style="float:right;" onclick=\'callbackObj.web_thread_reply2("' + keyword + '","' + pid + '","' + floor + '")\' class="am-badge am-badge-primary am-radius">回复</span></div></header><div class="am-comment-bd">' + content + '</div></div>');
		}
	}
	$(window).smoothScroll({position:Number($('#panel_' + pid).offset().top)-30})
}
//加载更多楼层
function openfloor(id) {
	if(Number($("#sub" + id).attr('tag')) > Number($("#sub" + id).data('maxpage'))) {
		$("#sub" + id).attr('style', "display:none;")
	} else {
		$('#alert-loading').modal("open");
		setTimeout(function() {
			callbackObj.web_thread_floor(id, keyword, $("#sub" + id).attr('tag'), lzname);
			$("#sub" + id).attr('tag', Number($("#sub" + id).attr('tag')) + 1 + "");
			if(Number($("#sub" + id).attr('tag')) > Number($("#sub" + id).data('maxpage'))) {
				$("#sub" + id).attr('style', "display:none;")
			} else {
				$("#sub" + id).text("-还有[" + (Number($("#sub" + id).attr('title')) - 30 * (Number($("#sub" + id).attr('tag')) - 1)) + "]楼没有加载，点击加载-")
			}
		}, 500);

	}
}

//打开图片
function img_watch(obj) {
	//alert(obj.id);
	if($('#' + obj.id).hasClass('img_t')) {
		$('#' + obj.id).removeClass('img_t');
		$('#' + obj.id).addClass('img_b');
	} else {
		$('#' + obj.id).addClass('img_t');
		$('#' + obj.id).removeClass('img_b');

	}
}
//错误Callback
function errrepoter(err) {
	alert(err);
	if(localStorage.errtime == undefined) {
		localStorage.errtime = 1;
		location.reload();
	} else if(localStorage.errtime < 3) {
		localStorage.errtime++;
		location.reload();
	} else {
		localStorage.errtime = undefined;
		alert('失败次数过多，网页将返回到您上一次访问的页面');
		history.back(-1);
	}
}