var tmpstr='',dlgbtt;
(function($){
	// tipWrap: 	提示消息的容器
	// maxNumber: 	最大输入字符
	$.fn.artTxtCount = function(tipWrap, maxNumber){
		var countClass = 'js_txtCount',		// 定义内部容器的CSS类名
			fullClass = 'js_txtFull',		// 定义超出字符的CSS类名
			disabledClass = 'disabled';		// 定义不可用提交按钮CSS类名

		// 统计字数
		var count = function(){
			var btn = $(this).parent().find('.btn'),
				val = $(this).val().length,
				// 是否禁用提交按钮
				disabled = {
					on: function(){
						btn.removeAttr('disabled').removeClass(disabledClass);
					},
					off: function(){
						btn.attr('disabled', 'disabled').addClass(disabledClass);
					}
				};

			if (val == 0) disabled.off();
			if(val <= maxNumber){
				if (val > 0) disabled.on();
				tipWrap.html('\u8FD8\u80FD\u8F93\u5165 ' + (maxNumber - val) + ' \u4E2A\u5B57');
			}else{
				disabled.off();
				tipWrap.html('\u5DF2\u7ECF\u8D85\u51FA ' + (val - maxNumber) + ' \u4E2A\u5B57');
			};

		};
		$(this).bind('keyup change', count);
		return this;
	};
})(jQuery);

$(document).ready(function(){
	/*回顶部*/
	var iebacktotop;
	if($.browser.msie){
iebacktotop=$.dialog({id:'iebacktotop', content: '<p id="back-to-top"><a href="#topbody" title="回到顶部"><span></span></a></p>',fixed:true,drag:false,resize:false,left:'100%',show:false,top: '100%',padding:'0',width:50,height:50,initFn:function(){
	this.DOM.wrap.find("td.aui_main").css({'width':'50px','min-width':'0'});
	this.DOM.wrap.find("td.aui_header").html("");
},closeFn:function(){
	this.hide();
	return false;
}});
	}else{
		$("body").append('<p id="back-to-top" style="display:none"><a href="#topbody" title="回到顶部"><span></span></a></p>');
		$("#back-to-top").css({"position":"fixed","bottom":"30px","right":"30px"});
	}
	$(function () {
		$(window).scroll(function(){
			if ($(window).scrollTop()>100){
				if($.browser.msie){
					iebacktotop.show();
				}else{
					$("#back-to-top").fadeIn(1500);
				}
			} else {
				if($.browser.msie){
					iebacktotop.close();
				}else{
					$("#back-to-top").fadeOut(1500);
				}
			}
		});
		
		$("#back-to-top").click(function(){
			$('body,html').animate({scrollTop:0},1000);
			return false;
		});
	});

	/*输入框初始*/
	$(".watermark").each(function() {
		var thewebsite = $(this).attr("alt");
		$(this).Watermark(thewebsite);
	});

	/*大图小图收起*/
	$(".imgandtube").each(function(){
		$(this).children("dt[video!='']").css("cursor","pointer");
		$(this).children("dt").click(function(){
			if($(this).next("dd").find('p.dnr').length<1){
				var xnr=$(this).parent().next('p.xnr').html();
				$(this).next("dd").append('<p class="dnr">'+xnr+'</p>');
			}
			var bimg=$(this).attr("bimg");
			if(bimg!=""){
				$(this).attr("bimg","");
				$(this).next("dd").find("div.imgandtube_tab>span.loding").show();
				var timg=new Image();
				timg.src=bimg;
				if (timg.width>598){
					$(this).next("dd").append('<img onload="hideloading(this);" onclick="hidebigimg(this);" src="'+bimg+'" width="598" class="it_big" />');
				}else{
					$(this).next("dd").append('<img onload="hideloading(this);" onclick="hidebigimg(this);" src="'+bimg+'" class="it_big" />');
				}				
			}
			var video=$(this).attr("video");
			if(video!=""){
				$(this).next("dd").append(getSwfplayer(video));
			}
			$(this).hide();
			$(this).parent().next('p.xnr').hide();
			$(this).next("dd").show();
		});
		$(this).find(".icon_sqi").click(function(){
			hidebigimg(this);
		});
	});

	/*显示隐藏微薄按钮*/
	$("a.z_weibo").mouseover(function(j){
		var pt=this;
		var bid=$(this).attr("bid");
		var content=eval('tmpstr'+bid);
		$.dialog({follow:document.getElementById('z_weibo'+bid),content:content,drag:false,resize:false,padding:'0 5px',id:'wbdlg'+bid,initFn:function(){
			var that=this;
			var top=parseInt(this.DOM.wrap.css("top"));
			var left=parseInt(this.DOM.wrap.css("left"));
			top-=30;
			this.position(left,top);
			this.DOM.wrap.find("td.aui_header").html("");
			this.DOM.wrap.find("div.aui_content").mouseout(function(){that.close();});
		}});
	});
});
function hideloading(o){
	$(o).parents(".imgandtube_dd").find("div.imgandtube_tab>span.loding").hide();
}
function hidebigimg(o){
	var $top=$(o).parents("div.item");
	$(o).parents(".imgandtube_dd").find("div.player").remove();
	$(o).parents(".imgandtube_dd").hide();
	$(o).parents(".imgandtube").next("p.xnr").show();
	$(o).parents(".imgandtube_dd").prev(".imgandtube_dt").show();
	var st=$top.offset();
	$('body,html').css({scrollTop:st.top});
	$(window).scrollTop(st.top);
}
function getSwfplayer(url){
	var html = '<div class="player"><object width="598" height="370"><param name="allowscriptaccess" value="always"></param><param name="allowFullScreen" value="true"></param><param name="wmode" value="Opaque"></param><param name="movie" value="'+url+'"></param><embed src="'+url+'" width="598" height="370" type="application/x-shockwave-flash"></embed></object></div>';
	return html;
}

function iesethome(obj){
	try{  
		obj.style.behavior='url(#default#homepage)';  
		obj.setHomePage('http://www.xiaohua.com');  
	}catch(e){  
		if(window.netscape){
			try{  
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
			}catch(e){  
				alert("抱歉，此操作被浏览器拒绝！\n\n请在浏览器地址栏输入“about:config”并回车然后将[signed.applets.codebase_principal_support]设置为'true'");  
			};  
		}else{  
			alert("抱歉，您所使用的浏览器无法完成此操作。\n\n您需要手动将'http://www.xiaohua.com/'设置为首页。");
		};  
	};
}
function ieaddfav(){
	if(document.all) {
		window.external.addFavorite('http://www.xiaohua.com', '笑话网');
	}else if (window.sidebar) {
		window.sidebar.addPanel('笑话网', 'http://www.xiaohua.com', '');
	}  
}

function getDaren(){
	var maxpage=$("#darenlist").attr("maxpage");
	var page=$("#darenlist").attr("page");
	if(maxpage==0) return;
	maxpage=parseInt(maxpage);
	page=parseInt(page);
	++page;
	if(page>maxpage) page=1;
	$.ajax({
		type: "GET",
		url: "/index.php?c=main&a=daren&page="+page,
		dataType: "html",
		success: function(txt){
			$("#darenlist").html(txt);
		}
	});
}

/*短暂提示 ok 和err*/
function tipok(txt){$.dialog({icon: 'face-smile',id:'tips', content: txt,time:2,fixed:true});}
function tiper(txt){$.dialog({icon: 'face-sad',id:'tips', content: txt,time:2,fixed:true});}

/*加关注*/
function follows(uid,url)
{
	$.post(url,{uid:uid},function(result){
		if(result ==1) {
			$('.follow_'+uid).html('已关注');
			tipok('成功加为关注'); 
		}else if(result ==2){
			$('.follow_'+uid).html('关注Ta');
			tipok('您已经取消了关注'); 
		}else{
			tiper(result);
		}
	});
}
/*加喜欢*/
function likes(bid,url)
{
	$.post(url,{bid:bid},function(result){
		if(result ==1) {
			var c=$('.likes_'+bid).attr('c');
			c=parseInt(c);
			++c;
			$('.likes_'+bid).html('收藏(' + c + ')');
			tipok('成功收藏'); 
		}else if(result ==2){
			var c=$('.likes_'+bid).attr('c');
			c=parseInt(c);
			--c;
			$('.likes_'+bid).html('收藏(' + c + ')');
			tipok('您已经取消了收藏'); 
		}else{
			tiper(result);
		}
	});
}
/*好笑*/
function good(bid,url)
{
	url=url.replace(urlpath,'');
	$.post(url,{bid:bid},function(result){
		if(result==1) {
			var c=$('.good_'+bid).attr('c');
			c=parseInt(c);
			++c;
			$('.good_'+bid).html('好笑(' + c + ')');
			tipok('操作成功');
		}else if(result==0){
			tiper('你已经表过态了');
		}else{
			tiper(result);
		}
	});
}
/*不好笑*/
function bad(bid,url)
{
	url=url.replace(urlpath,'');
	$.post(url,{bid:bid},function(result){
		if(result ==1) {
			var c=$('.bad_'+bid).attr('c');
			c=parseInt(c);
			++c;
			$('.bad_'+bid).html('不好笑(' + c + ')');
			tipok('操作成功');
		}else if(result==0){
			tiper('你已经表过态了');
		}else{
			tiper(result);
		}
	});
}
/*显示首页评论框*/
function indexPostTab(type,id,url)
{
	url=url.replace(urlpath,'');
	if(type == 'reprint')
	{
		$('#comment_'+id).hide();
		$('#reprint_'+id).toggle().toggleClass('current');
	}else if(type =='comment'){
		$('#replyTo_'+id).val('');
		$('#comment_'+id).toggle();
		$('#feeds_'+id).hide();
		$('#replyInput_'+id).artTxtCount($('#replyInput_lengthinf_'+id), 100);
		$('#replyTo_'+id).val();

		$('#commentList_'+id).html('loading...');
		$.post(url,{bid:id},function(result){
				$('#commentList_'+id).html(result);
				$('#replyInput_lengthinf_'+id).html('');

		})
	}else if(type =='feeds'){
		$('#comment_'+id).hide();
		$('#feeds_'+id).toggle();
		$('#feedList_'+id).html('loading...');
		$.post(url,{bid:id},function(result){
				$('#feedList_'+id).html(result);

		})
	}
}

/*提交文本框的回复*/
function sendReplay(id,url)
{
	url=url.replace(urlpath,'');
	var ipt = $('#replyInput_'+id).val();
	if(ipt == ''){tiper('内容不能为空喔');return false;}

	  $.post(url,{inputs:ipt,bid:id,repuid:$('#replyTo_'+id).val()},function(result){
		if(result ==1)
		{
			tipok('回复成功');
		}else{
			tiper(result);
		}
			$('#comment_'+id).hide();
			$('#replyInput_'+id).val('');
			$('#comment_btn_'+id).removeClass('current');
	  });
}
function delrep(id,url)
{

	$.dialog({content:'确认删除本回复？',lock:true,yesFn:function(){
			//window.location.href=url;
			$.post(url,function(result){
				if(result == 'ok')
				{
					tipok('已删除');
					$('#feed_'+id).hide('slow');
				}else{
					tiper(result);
				}
			});
		},noFn:true});
}

/*取消链接*/
function cancelConnect(url)
{
	url=url.replace(urlpath,'');
	$.post(url,function(rs){
			window.location.reload();
	})

}
/*删除博客*/
function delblogs(id,url)
{
	url=url.replace(urlpath,'');
	$.dialog({content:'确认删除这篇文章？',lock:true,yesFn:function(){
		//window.location.href=url;
		$.post(url,function(result){
			if(result == 'ok')
			{
				tipok('已删除');
				$('#blog_'+id).hide('slow');
			}else{
				tiper(result);
			}
		});
	},noFn:true});
}
/*保存单条关键词*/
function addbodytag(id,url)
{
	url=url.replace(urlpath,'');
	$.post(url,{"id":id,"bodytag":$("#bodytag"+id).val()},function(result){
		if(result == 'ok')
		{
			tipok('已保存');
		}else{
			tiper('发生错误');
		}
	});
}
/*首页快捷发布*/
function indexSelFbTag(o){
	$(o).toggleClass("a01");
	if($("#indexFbTagDiv a.a01").length>3){
		$(o).toggleClass("a01");
		tiper('标签最多只能选择三个');
		return false;
	}
}
function indexFbSubmit(o){
	var title = $('#indexFbTitle').val(); 
	var text = $('#indexFbContent').val();
	var alttext = $('#indexFbContent').attr('alt');
	title=$.trim(title);
	if(text == '' || text == alttext){tiper('内容不能为空喔'); $('#indexFbContent').focus();return false}
	if(!setIndexFbTags()){ tiper('标签至少要选择一个');return false;	}
	var types=$('#blog-types').val();
	if(types!=1){
		$('#indexFbTitle').val(text);
		$('#indexFbContent').val("");
	}
	if(types==4){
		var umus = $('#videolist').attr('type')+'|'+$('#videolist').attr('img')+'|'+$('#videolist').attr('pid')+'|'+text+'|'+$('#videolist').attr('url')+'[YB]';
		if(umus ==''){ tiper('请添加一个网络视频');return false;}
		$('#indexFbUrlmusic').val(umus);
	}
	$(o).toggle();
	$('#frmIndexFb').submit();
}
function setIndexFbTags()
{
	var tag_str = ''; 
	$("#indexFbTagDiv a.a01").each(function(){
		tag_str += $(this).attr('tag') + ',';
	});
	$('#indexFbTag').val(tag_str); //写入标签
	if($('#indexFbTag').val() == ''){
		return false;
	}else{
		return true;
	}
}

/*发送站内信*/
function sendpm(uid,info)
{
	if(info ==undefined){info=''}
	$.dialog.open('/index.php?c=user&a=postpm&uid='+uid+'&info='+info, {id:'sendmail_box',fixed:true,lock:true,background:'#CCC',opacity:'0.4'});
}

//默认父框架刷新网页 为1 关闭父页面
function doPmSubmit(call)
{
	var u = $('#postpm').find('input[name=uid]').val();
	var i = $('#postpm').find('textarea').val();
	if(i == ''){tiper('请填写内容');return false;}
	$('#submit_button').hide();
	$('#submit_tip').show();
	$.post('/index.php?c=user&a=postpm&send=true',{uid:u,info:i},function(rs){

		if(rs == -1){alert('发送频率限制,请30秒后再试'); if(call ==1){art.dialog.open.api.close();}}
		if(rs == -2){alert('自己不能给自己发送');}
		if(rs == 0){alert('内容没有填写');}

		if(rs == 1){
			art.dialog({ content: '发送成功了',time:2,	closeFn:
			 function(){
					if(call ==1){art.dialog.open.api.close();}else{window.location.reload();}
				 }
			});
		}else{
			$('#submit_button').show();
			$('#submit_tip').hide();
		}
	})
}

/*删除通知*/
function delnotice(id,url)
{
	url=url.replace(urlpath,'');
	art.dialog.confirm('删除后对方也无法查看,确定吗?', function(){
		$.post(url,function(result){ if(result == 1){ $('#notice_'+id).hide('slow');}else{ tiper(result); }		});
	})

}

/*已读通知*/
function isreadnotice(id,url)
{
	url=url.replace(urlpath,'');
	$.post(url,function(result){
		if(result == 1)
		{
			$('#notice_'+id).hide('slow');
		}else{
			tiper(result);
		}
	});
}

/*QQ微薄分享*/
function postToQQWb(url,content,pic){
	if(content.indexOf('【视频】')<0){
		url='';
	}else{
		url=' '+url;
	}
	var param = {
		c:'share',
		a:'index',
		//url:'http://www.xiaohua.com',
		url:url,
		pic:pic,
		title:content+'★喜欢请收听@xiao_hua_com',
		assname:'xiao_hua_com',
		appkey:'801079336'
	}
	var temp = [];
	for( var p in param ){
		temp.push(p + '=' + encodeURIComponent( param[p] || '' ) )
	}
	var _url='http://share.v.t.qq.com/index.php?' + temp.join('&');
	return '<a href="javascript:;" title="转播到腾讯微博" class="tmblog" wburl="'+_url+'" onclick="openToQQWb(this);"><img src="http://mat1.gtimg.com/app/vt/images/share/b24.png" border="0" ></a>';

}
function openToQQWb(o){
	window.open( $(o).attr('wburl'),'', 'width=620, height=530, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, location=yes, resizable=no, status=no' );
}
/*Sina微薄分享*/
function postToSinaWb(url,content,pic){
	var param = {
		url:'http://www.xiaohua.com',
		type:'5',
		count:'',
		appkey:'1213332251',
		title:content,
		pic:pic,
		ralateUid:'笑话网_xiaohua',
		language:'zh_cn',
		rnd:new Date().valueOf()
	}
	var temp = [];
	for( var p in param ){
		temp.push(p + '=' + encodeURIComponent( param[p] || '' ) )
	}
	var _url='http://hits.sinajs.cn/A1/weiboshare.html?' + temp.join('&');
	return '<iframe allowTransparency="true" frameborder="0" scrolling="no" src="' + _url + '" width="106" height="24"></iframe>';
}

/*滚动读取数据*/
var  globalcount;	 //feedtolbar state
function feedToolBar(id){
	$(document).ready(function(){
		$(window).bind("scroll", function(event){
			var fold = $(window).height() + $(window).scrollTop();

			if( fold >= $('#'+id).offset().top-20){
				p = parseInt($('#'+id).attr('page'));
				nextpage = p+1;
				url =$('#'+id).attr('query');

				maxpage = parseInt($('#'+id).attr('max'));
				if(globalcount == 1){return false;}//如果进入队列的就跳出

				if(p >= maxpage){
					$('#'+id).html('<a href="javascript:void(0)" onclick="continueShow(\''+id+'\')"><img src="/tpl/image/hand.gif" class="loading"/>点击查看更多...</a>');
				}else{
					globalcount = 1; //已经进入队列了
					$('#'+id).html('<img src="/tpl/image/loading.gif" class="loading"/><span>请稍等</span>');
					$.post(url,{page:p},function(rs){
						if(rs != ''){
							area = $('#'+id).attr('area');
							$('#'+area).append(rs);
							$('#'+id).attr('page',nextpage);
							globalcount = 0; //出队列
						}else{
							$('#'+id).html('没有更多了');
							globalcount = 1; //出队列
						}
					});
				}
			}
		});
	});
}
function continueShow(id){
	p = parseInt($('#'+id).attr('page'));
	url =$('#'+id).attr('query');

	nextpage = p+1;
	$('#'+id).html('<img src="/tpl/image/loading.gif" class="loading"/><span>请稍等</span>');
	$.post(url,{page:p},function(rs){
		if(rs.length < 10){
			$('#'+id).html('没有更多了');
			globalcount = 1; //出队列
		}else{
			area = $('#'+id).attr('area');
			$('#'+area).append(rs);
			$('#'+id).attr('page',nextpage);
			$('#'+id).html('<a href="javascript:void(0)" onclick="continueShow(\''+id+'\')"><img src="/tpl/image/hand.gif" class="loading"/>点击查看更多...</a>');
		}
	});
}
