$(function() {
        $('.lightBox').lightBox();
});

//返回页面滚动数组
/*function getPageScroll(){
	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}*/

//弹出层

function popout(jqObj){
	var divwarper = $("#popout");
	var divCon = jqObj;
	jqObj.show();
	
	if($("#overlay").length == 0){
		$("body").append($('<div id="overlay"></div>'));
	}
	var overLay = $("#overlay");
	var _o = {};
	_o.winW = $(window).width();//窗口宽
	_o.winH = $(window).height();//窗口高
	_o.docW = $(document).width();//页面宽
	_o.docH = $(document).height();//页面高
	_o.scrL = $(document).scrollLeft();//横向滚动量
	_o.scrT = $(document).scrollTop();//纵向滚动量
	//alert(_o.winW+"|"+_o.winH+"|"+_o.docW+"|"+_o.docH+"|"+_o.scrL+"|"+_o.scrT+"|");
	overLay.css({width:_o.docW+"px",height:_o.docH+"px",position:"absolute",left:"0px",top:"0px",background:"#000",opacity:"0.5",display:"none","z-index":"1"});
	
	
	var _w = divwarper.width();
	var _h = divwarper.height();
	overLay.fadeIn("fast");
	divwarper.fadeIn("fast");
	divwarper.css({position:"absolute",left:(_o.winW - _w)/2 + _o.scrL,top:(_o.winH - _h)/2 + _o.scrT,"z-index":"2"});
	
	overLay.bind("click",function(){overLay.fadeOut("fast");divwarper.fadeOut("fast");divCon.hide("fast")});
	$("#popBtn a.close").bind("click",function(){divCon.hide();divwarper.hide();overLay.fadeOut("fast")});
}


function popclose(jqObj){
	var divwarper = $("#popout");
	var overLay = $("#overlay");
	
	overLay.fadeOut("fast");
	divwarper.hide("fast");
	jqObj.hide("fast");
}


function PopOutDiv(jqObjId){
	if($("#overlay").length == 0){
		$("body").append($('<div id="overlay"></div><div id="popout"><div id="popCon"><div id="popBtn"><a class="credit" href="#">积分说明</a><a class="zoom1" href="#" title="放大窗口">最大化</a><a class="close" href="#" title="关闭窗口">关闭</a></div></div></div>'));
	}
	var divwarper = $("#popout");
	var divCon = $("#"+jqObjId);
	var popCon = $("#popCon");
	popCon.append(divCon);
	divCon.show();
	var overLay = $("#overlay");
	var _o = {};
	_o.winW = $(window).width();//窗口宽
	_o.winH = $(window).height();//窗口高
	_o.docW = $(document).width();//页面宽
	_o.docH = $(document).height();//页面高
	_o.scrL = $(document).scrollLeft();//横向滚动量
	_o.scrT = $(document).scrollTop();//纵向滚动量
	//alert(_o.winW+"|"+_o.winH+"|"+_o.docW+"|"+_o.docH+"|"+_o.scrL+"|"+_o.scrT+"|");
	overLay.css({width:_o.docW+"px",height:_o.docH+"px",position:"absolute",left:"0px",top:"0px",background:"#000",opacity:"0.5",display:"none","z-index":"1"});
		
	var _w = divwarper.width();
	var _h = divwarper.height();
	overLay.fadeIn("fast");
	divwarper.fadeIn("fast");
	divwarper.css({position:"absolute",left:(_o.winW - _w)/2 + _o.scrL,top:(_o.winH - _h)/2 + _o.scrT,"z-index":"2"});
	
	overLay.bind("click",function(){overLay.fadeOut("fast");divwarper.fadeOut("fast");divCon.hide("fast")});
	$("#popBtn a.close").bind("click",function(){divCon.hide();divwarper.hide();overLay.fadeOut("fast")});
}


function PopCloseDiv(jqObjId){
	var divwarper = $("#popout");
	var overLay = $("#overlay");
	var _jqObj = $("#"+jqObjId);
	var popCon = $("#popCon");
	popCon.empty();
	overLay.fadeOut("fast");
	divwarper.hide("fast");
	_jqObj.hide("fast");
}

function DrawImage(ImgD,iwidth,iheight){   
    var image=new Image();   
    image.src=ImgD.src;   
    if(image.width>0 && image.height>0){   
      if(image.width/image.height>= iwidth/iheight){   
          if(image.width>iwidth){     
              ImgD.width=iwidth;   
              ImgD.height=(image.height*iwidth)/image.width;   
          }else{   
              ImgD.width=image.width;     
              ImgD.height=image.height;   
          }   
      }else{   
          if(image.height>iheight){     
              ImgD.height=iheight;   
              ImgD.width=(image.width*iheight)/image.height;           
          }else{   
              ImgD.width=image.width;     
              ImgD.height=image.height;   
          }   
      }   
    }   
}  





//////////////////////////PNG 半透明修复
function fixPng(classname){
	if($.browser.msie && $.browser.version <= 6.0){
		var _str = "img."+classname;
		if($(_str).length>0){
			$(_str).each(function(){
				var imgSrc = $(this).attr("src");
				var _width = $(this).width();
				var _height = $(this).height();
				$(this).wrap("<span></span>");
				$(this).parent().css("display","block");
				$(this).parent().css("width",_width);
				$(this).parent().css("height",_height);
				var _filterAttr = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale', src='"+imgSrc+"')";
				$(this).parent().css("filter",_filterAttr);
				$(this).hide();
			});
		}
	}
}
//左侧导航
function sideBar(){
	$(".divider").height($("#mainCon").height());
	$("#sideBar ul").each(function(){
		$(this).find("li:last-child").addClass("last");
	});
	$(".divider").append('<div class="arr"></div>');
	$(".arr").data("pos","left");
	$(".divider").mouseover(function(){
		$(this).addClass("divideron");
		if($(".arr").data("pos")=="left"){
			$(".arr").css("left","-10px");
			$(".arr").removeClass("arrRight");
			$(".arr").addClass("arrLeft");
		}else if($(".arr").data("pos")=="right"){
			$(".arr").css("left","15px");
			$(".arr").removeClass("arrLeft");
			$(".arr").addClass("arrRight");
		}
		$(".arr").show();
	});
	$(".divider").mousemove(function(event){
		$(event.target).offset().left;
		$(event.target).offset().top;
		$(".arr").css("top",event.pageY-$(event.target).offset().top);
	});
	$(".divider").mouseout(function(){
		$(this).removeClass("divideron");
		$(".arr").hide();
	});
	$(".divider").click(function(){
		if($(".arr").data("pos")=="left"){
			$("#sideBar").hide();
			$(".arr").data("pos","right");
			$("#mainCon").css("width","100%");
			$(".arr").hide();
		}else if($(".arr").data("pos")=="right"){
			$("#sideBar").show();
			$(".arr").data("pos","left");
			$("#mainCon").css("width","83%");
			$(".arr").hide();
		}
	});
}
//首页
function toggleForum(){
	var c = $("span.slideBtn");
	c.each(function(){
		$(this).data("stat","on");
		$(this).click(function(){
			if($(this).data("stat") == "on"){
				$(this).prev(".mainBoxContent").slideUp("fast");
				$(this).data("stat","off");
				$(this).text("+");
			}else if($(this).data("stat") == "off"){
				$(this).prev(".mainBoxContent").slideDown("fast");
				$(this).data("stat","on");
				$(this).text("-");
			}
		});
	});
}
function dropList(){
	$(".dropList").each(function(){
		var btn = $(this).find(".current");
		btn.click(function(){
			$("body").click(function(event){if(event.target.className != "current"){$(".dropList ul").hide()}});
			$(this).next().toggle();
		});
	});
}
function postType() {
	var a;
	var u = $("ul#postTopicType");
	$("a.postTopic").mouseover(function(e){
		a = setTimeout(function(){u.css("left",e.target.offsetLeft+2),u.css("top",e.target.offsetTop+30);u.show()},100);
	});
	$("a.postTopic").mouseout(function(e){
		a = setTimeout(function(){u.hide()},500);
	});
	u.mouseover(function(e){
		clearTimeout(a);
	});
	u.mouseout(function(e){
		a = setTimeout(function(){u.hide()},500);
	});
}
//操作主题
function handleTopic(){
	var handleBtn = $("#handleTopic .handleBtn3");
	var submitBtn = $("#handleTopic input.button");
	var wra = $("#handleNotice");
	var divAll = $("#handleNotice>div");
	var div1 = $("#noauthorized");
	var div2 = $("#authorized");
	var div3 = $("#handleok");
	var div4 = $("#handlenotok");
	var _body = $("body");
	var posX;
	var posY;
	var targetW;
	var targetH;	
	var handleType = {
		noauthorized:function(e){
			divAll.hide();
			div1.show();
			wra.fadeIn();
			_body.bind("click",handleType.bodyClick);
		},
		authorized:function(e){
			divAll.hide();
			div2.show();
			wra.fadeIn();
			_body.bind("click",handleType.bodyClick);
		},
		handleok:function(e){
			divAll.hide();
			div3.show();
			setTimeout(function(){wra.fadeOut();_body.unbind("click")},1000);
		},
		handlenotok:function(e){
			divAll.hide();
			div4.show();
			setTimeout(function(){wra.fadeOut();_body.unbind("click")},1000);
		},
		adjustPos:function(triggerBtn){
			posX = triggerBtn[0].offsetLeft;
			posY = triggerBtn[0].offsetTop;
			targetW = triggerBtn[0].clientWidth;
			targetH = triggerBtn[0].clientHeight;
			wra.css("left",posX+targetW+5);
			wra.css("top",posY);
		},
		bodyClick:function(e){
			var _x = wra.offset().left;
			var _y = wra.offset().top;
			var _w = wra[0].clientWidth;
			var _h = wra[0].clientHeight;
			if((e.pageX<_x||e.pageX>_x+_w||e.pageY<_y||e.pageY>_y+_w)){
				wra.fadeOut();
			}else{
				//do nothing
			}
		},
		init:function(){
			handleType.adjustPos(handleBtn);
		}
	};
	handleType.init();
	
	//程序调用
	//handleBtn.bind("click",function(e){handleType.noauthorized(e);return false;});
	//handleBtn.bind("click",function(e){handleType.authorized(e);return false;});
	//submitBtn.bind("click",function(e){handleType.handleok(e)});
	//submitBtn.bind("click",function(e){handleType.handlenotok(e)});
}

//管理主题
function manageTopic(){
	var _a = $("#manageTopic>span");
	var _b = $("#manageTopic>ul");
	var id;
	_a.mouseover(function(){
		id = setTimeout(function(){_b.show()},200);
	});
	_a.mouseout(function(){
		clearTimeout(id);
		id = setTimeout(function(){_b.hide()},500);
	});
	_b.mouseover(function(){
		clearTimeout(id);
	});
	_b.mouseout(function(){
		id = setTimeout(function(){_b.hide()},500);
	});
}

//安全问题
function sercureQuestion(){
	var _a = $("#logForm .question span");
	var _b = $("#logForm .question ul");
	var _c = $("#logForm .question ul li");
	var _d = $("#logForm .answer");
	_a.click(function(){
		_b.toggle();
	});
	_c.each(function(){
		$(this).click(function(){
			if($(this).text() == "请选择安全问题"){
				_d.hide();
			}else{
				_d.show();
			}
			_a.text($(this).text());
			_b.hide();
		});
	});
}

//主题类型图片
function topicTypeIcon(){
	var _a = $("#topicIcon>span");
	var _b = $("#topicIcon>ul");
	var _c = $("#topicIcon>ul>li");
	_a.click(function(){
		_b.toggle();
	});
	_c.each(function(){
		$(this).click(function(){
			var src = $(this).find("img").attr("src");
			_a.find("img").attr("src",src);
			_b.hide();
		});
	});
}
function topicType(){
	var _a = $("#topicType>span");
	var _b = $("#topicType>ul");
	var _c = $("#topicType>ul>li");
	_a.click(function(){
		_b.toggle();
	});
	_c.each(function(){
		$(this).click(function(){
			_a.text($(this).text());
			_b.hide();
		});
	});
}




//发帖附件
function attachment(){
	var _a = $("table.attachment a.modi");
	_a.each(function(){
		$(this).click(function(){
			var _b = $(this).prev("span");
			var _c = $(this);
			_b.hide();
			 _c.hide();
			_c.before('<input class="text modi" type="text" />');
			_c.after('<a class="confirm" href="#">确定</a><a class="cancel" href="#">取消</a>');
			var _d = _c.prev();
			var _e = _c.next();
			var _f = _c.next().next();
			_d.attr("value",_b.text());
			_d.focus();
			_e.click(function(){
				_b.text(_d.attr("value"));
				_b.show();
				_c.show();
				_d.remove();
				_e.remove();
				_f.remove();
				return false;
			});
			_f.click(function(){
				_b.show();
				_c.show();
				_d.remove();
				_e.remove();
				_f.remove();
				return false;
			});
			return false;
		});
	});
}
//阅读权限设置
function readLevel(){
	var _a = $("a.setLevel");
	var _b = $("ul.memberLevel");
	var _c = $("ul.memberLevel>li");
	var _d = $("span.setLevel");
	_a.click(function(){_b.toggle();return false;});
	_c.each(function(){
		$(this).click(function(){
			_d.text("："+$(this).text());
			_b.hide();
		});
	});
}
//主题售价
function topicPrice(){
	var _a = $("#postSubmit a.setPrice");
	var _b = $("#postSubmit input.setPrice");
	var _c = $("#postSubmit span.setPrice");
	_a.click(function(){
		_b.attr("value",_c.text().split(":")[1]);
		_b.show();
		_c.hide();
		return false;
	});
	_b.blur(function(){
		_c.text("："+_b.attr("value"));
		_b.hide();
		_c.show();
	});
}

//弹出层放大缩小
function popOutZoom(){
	var _a = $("#popout");
	var _b = $("#popBorder");
	var _c = $("#popCon");
	var _d = $("#webEditor");
	var _e = $("#postAttach");
	var _aw = _a.width();
	var _ah = _a.height();
	var _bw = _b.width();
	var _bh = _b.height();
	var _cw = _a.width();
	var _ch = _a.height();
	
	var statu = "缩小了";
	var _o = {};
		_o.winW = $(window).width();//窗口宽
		_o.winH = $(window).height();//窗口高
		_o.docW = $(document).width();//页面宽
		_o.docH = $(document).height();//页面高
		_o.scrL = $(document).scrollLeft();//横向滚动量
		_o.scrT = $(document).scrollTop();//纵向滚动量
	var btn = $("#popBtn a.zoom1");
	btn.click(function(){
		if(statu == "缩小了"){
			_a.width(914);
			_b.width(914);
			_c.width(900);
			_d.height(235);
			_e.hide();
			statu = "放大了";
			var _w = _a.width();
			var _h = _a.height();
			_a.css({left:(_o.winW - _w)/2 + _o.scrL});
			$(this).attr("class","zoom2");
			$(this).attr("title","还原窗口");
		}else if(statu == "放大了"){
			_a.width(614);
			_b.width(614);
			_c.width(600);
			_d.height(150);
			_e.show();
			statu = "缩小了";
			var _w = _a.width();
			var _h = _a.height();
			_a.css({left:(_o.winW - _w)/2 + _o.scrL});
			$(this).attr("class","zoom1");
			$(this).attr("title","放大窗口");
		}
		return false;
	});
}

//个人头像
function myPic(){
	var _a = $("#myPic input#changePic");
	var _b = $("#picEdit");
	var _c = $("body");
	var _x;
	var _y;
	var _w;
	var _h;
	_a.click(function(ev){
		_b.fadeIn();
		_x = _b.offset().left;
		_y = _b.offset().top;
		_w = _b[0].clientWidth;
		_h = _b[0].clientHeight;
		ev.stopPropagation();
		_c.click(function(e){
			if((e.pageX<_x||e.pageX>_x+_w||e.pageY<_y||e.pageY>_y+_h)){
				_b.fadeOut();
				_c.unbind("click");
			}
		});
	});
	
	addEvent();
}
function addEvent(){
	var _c = $("#picEdit ul#picList>li");
	var _d = $("#picWra>img");
	var _e = $("#picEdit");
	_c.each(function(){
		$(this).click(function(){
			_d.attr("src",$(this).find("img").attr("src"));
			_e.fadeOut();
		});
	});
}

//短消息详细页
function textReply(){
	var _a = $("#textDetail input.button1");
	var _b = $("#textDetail input.button2");
	var _d = $("#textDetail input.button3");
	var _c = $("#textDetail #textReply");
	_d.bind("click",function(){_c.slideUp();_b.hide();_d.hide();_a.show();});
	_a.bind("click",function(){_c.slideDown();$(this).hide();_b.show();_d.show();});
}

//滚动通知
function noticeScroll(){
	var box = $("#notice");
	var ul = $("#notice ul");
	var li = box.find("li");
	var boxWidth = 0;
	var count = 0;
	var liLength = li.length;
	while(count < liLength){
		boxWidth += li.eq(count).width();
		count += 1;
	}
	ul.width(boxWidth);
	var marquee2 = new Marquee("notice",2,1,box.width(),30,50,0,0)
}

///////////////////////////////////// 文字放大缩小显示
function ts() {
	if($(".postOutput").length){
		$("#normalSize")	.click(function() {
			$(".postOutput").css("font-size","9pt");			
		});
		$("#largeSize")	.click(function() {
			$(".postOutput").css("font-size","12pt");			
		});
	}	
}


$(document).ready(function(){
	if($("img.png").length > 0){
		fixPng("png");
	}
	if($("#sideBar").length>0){
		sideBar();
	}
	if($(".dropList").length>0){
		dropList();
	}
	if($("a.postTopic").length>0){
		postType();
	}
	if($("#handleTopic").length>0){
		handleTopic();
	}
	if($("#manageTopic").length>0){
		manageTopic();
	}
	sercureQuestion();
	topicTypeIcon();
	topicType();
	attachment();
	readLevel();
	topicPrice();
	popOutZoom();
	myPic();
	textReply();
	if($("span.slideBtn").length>0){
		toggleForum();
	}
	if($("#notice").length>0){
		noticeScroll();
	}
    ts();
});
