Function.prototype.partial = function(){
    var fn = this, args = Array.prototype.slice.call(arguments);
    return function(){
	   var k = 0;
	   for(var i = 0, l =  args.length; i < l && k < arguments.length; i++){
	       if(args[i] == undefined){
		        args[i] = arguments[k++];
		   }
	   }
	   return fn.apply(this, args);
	}
 }
 Function.prototype.curry = function(){
    var fn = this, args = Array.prototype.slice.call(arguments);
    return function(){
	   return fn.apply(this, args.concat(Array.prototype.slice.call(arguments)))
	 }
 }
 Function.prototype.delay = function(ms){
	 _this = this;
	 return function(){
		 var args = arguments;
		 window.setTimeout(function(){
			_this.apply(null, args);
		 }, ms||0);		 
	 }
 }
 Function.prototype.bind = function() 
 {
    var method = this, args = Array.prototype.slice.call(arguments), obj = args.shift();
    return function(){
    		return method.apply(obj, args.concat(arguments));
    }
 }  
 String.prototype.trim = function(){
	 return this.replace(/^\s+|\s+$/g, "");
 }
 String.prototype.escapeCharacters = function(chars)
 {
     var foundChar = false;
     for (var i = 0; i < chars.length; ++i) {
         if (this.indexOf(chars.charAt(i)) !== -1) {
             foundChar = true;
             break;
         }
     }

     if (!foundChar)
         return this;

     var result = "";
     for (var i = 0; i < this.length; ++i) {
         if (chars.indexOf(this.charAt(i)) !== -1)
             result += "\\";
         result += this.charAt(i);
     }

     return result;
 }

 String.prototype.escapeForRegExp = function()
 {
     return this.escapeCharacters("^[]{}()\\.$*+?|");
 }

 String.prototype.escapeHTML = function()
 {
     return this.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
 }

 String.prototype.collapseWhitespace = function()
 {
     return this.replace(/[\s\xA0]+/g, " ");
 }

 String.prototype.trimLeadingWhitespace = function()
 {
     return this.replace(/^[\s\xA0]+/g, "");
 }

 String.prototype.trimTrailingWhitespace = function()
 {
     return this.replace(/[\s\xA0]+$/g, "");
 }

 String.prototype.trimWhitespace = function()
 {
     return this.replace(/^[\s\xA0]+|[\s\xA0]+$/g, "");
 }

 String.prototype.trimURL = function(baseURLDomain)
 {
     var result = this.replace(new RegExp("^http[s]?:\/\/", "i"), "");
     if (baseURLDomain)
         result = result.replace(new RegExp("^" + baseURLDomain.escapeForRegExp(), "i"), "");
     return result;
 } 
function scrollPos() {
	if (self.pageYOffset !== undefined) {
		return {
			x: self.pageXOffset,
			y: self.pageYOffset
		};
	}
	var d = document.documentElement||document.body;
	return {
		x: d.scrollLeft,
		y: d.scrollTop
	};
}
function setScrollPos(pos) {
	var e = document.documentElement, b = document.body;
	e.scrollLeft = b.scrollLeft = pos.x;
	e.scrollTop = b.scrollTop = pos.y;
}
function generateRdn(n){
	var str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	for(var ret = "", i = 0; i < n; i++){
		ret += str.charAt(Math.floor(Math.random()*62));
	}
	return ret;	
}
function imgPreLoad(imgs){
	$.each(imgs, function(i, v){
		var o = new Image();
		o.src = v;
	});
}
/*adjust images' size*/
function adjustImg(imgs,maxWidth){
    maxWidth = maxWidth || 90;
    if (!imgs || imgs.length == 0) return;
    imgs.each(function(){
        var _this = this;
        $(this).hide();
        var oImg = new Image();
        var classname = this.className;
        var gif = false;
        oImg.onload = function(){
        	if(!gif){
		        if(this.width>maxWidth){
		             var h = maxWidth*this.height/this.width;
	                 try{
		            	 $(_this).replaceWith("<img title='点击查看原始图片' alt='点击查看原始图片' class='pointer "+(classname||'')+"' onclick='window.open(this.src);return false;' src='"+this.src+"' width='"+maxWidth+"' height='"+h+"' />");
		            	 if(_this.parentNode && _this.parentNode.tagName == "A")_this.parentNode.onclick = new Function("return false;");
		             }catch(e){}
		        }
		        $(_this).show();
		        oImg = null;
		        gif = true;
        	}
	     }
	     oImg.src = this.src;
    });
}
function adjustImg2(imgs, limit){
	if (!imgs || imgs.length == 0) return;
	imgs.each(function(){
		var _this = this;
		$(this).hide();
		var oImg = new Image();
		var classname = this.className;
		var gif = false;
		oImg.onload = function(){
			if(!gif){
				var h = this.height, w = this.width;
				limit = limit?limit:{w:90, h: 80};
				if(w>limit.w){
					h = parseInt(limit.w*h/w,10)||0;
					w = limit.w;
				}
				if(h>limit.h){
					w = parseInt(w*limit.h/h,10)||0;
					h = limit.h;
				}
				$(_this).css({width: w, height: h, display: "block", visibility: "visible"});
				oImg = null;
				gif = true;
			}
		}
		oImg.src = this.src;
	});
}
function adjustImg3(imgs){
	if (!imgs || imgs.length == 0) return;
	imgs.each(function(idx){
		var _this = this;
		$(this).hide();
		var oImg = new Image();
		var classname = this.className;
		var gif = false;
		oImg.onload = function(){
			if(!gif){
				var h = this.height, w = this.width;
				if(w>90){
					h = parseInt(90*h/w,10)||0;
					w = 90;
				}
				if(h>80){
					w = parseInt(w*80/h,10)||0;
					h = 80;
				}
				$(_this).css({width: w, height: h, display: "", visibility: "visible"});
				oImg = null;
				gif = true;
			}
		}
		oImg.src = this.src;
	});
}
function adjustStreamImg(imgsCt, limit, classArr){
	if (!imgsCt || imgsCt.length == 0) return;
	imgsCt.each(function(){
		if($("img", this).length) return;
		var _this = this;
		$(this).hide();
		var oImg = new Image();
		var gif = false;
		oImg.onerror = function(){
		    $(_this).closest(".imgArea").remove();
			oImg = null;
		}
		oImg.onload = function(){
			if(!gif){
				var h = this.height, w = this.width;
				limit = limit?limit:{w:90, h: 80};
				if(w>limit.w){
					h = parseInt(limit.w*h/w,10)||0;
					w = limit.w;
				}
				if(h>limit.h){
					w = parseInt(w*limit.h/h,10)||0;
					h = limit.h;
				}
				$(this).css({width: w, height: h}).appendTo(_this);
				oImg.className= typeof(classArr)==="undefined"?"photo_url fl imgBd":classArr;
				$(_this).show();
				oImg = null;
				gif = true;
			}
		}
		oImg.src = $(this).attr("pic");
	});
}

/*pagination*/
function pagination(curPage, totalPage,reverse) {
	var html = [];
	var prev = reverse ? "下一页" : "上一页";
	var next = reverse ? "上一页" : "下一页";
	if (curPage > 0) html.push("<li><a href='javascript:gotoPage(" + (curPage - 1) + ");'>"+prev+"</a></li>");
	var fstDot = false, sndDot = false;
	for (var i = 1; i <= totalPage; i++) {
		var displayPage;
		if (!reverse){
			displayPage=i;
		}else {
			displayPage=totalPage-i+1;
		}
		if (curPage == i - 1) {
			html.push("<li class='activeNum'><strong>" + displayPage + "</strong></li>");
			continue;
		}
		if (totalPage > 7) {
			if (i == 1 || i == 2 || i == totalPage || i == totalPage - 1 || (i >= curPage && i <= curPage + 2)) {
				html.push("<li><a href='javascript:gotoPage(" + (i - 1) + ")'>" + displayPage + "</a></li>");
			} else {
				if (curPage < 4 && i < 5) {
					html.push("<li><a href='javascript:gotoPage(" + (i - 1) + ")'>" + displayPage + "</a></li>");
					continue;
				}
				if (i < curPage + 1 && !fstDot) {
					html.push("<li style='border:0'>...</li>");
					fstDot = true;
				}
				if (i > curPage + 1 && !sndDot) {
					html.push("<li style='border:0'>...</li>");
					sndDot = true;
				}
			}
		} else html.push("<li><a href='javascript:gotoPage(" + (i - 1) + ")'>" + displayPage + "</a></li>");
	}
	if (curPage < totalPage - 1) html.push("<li><a href='javascript:gotoPage(" + (curPage + 1) + ");'>"+next+"</a></li>");
	return reverse?html.reverse().join(""):html.join("");
}

function gotoPage(page) {
    var href = window.location.href;
    window.location.href = href.indexOf("cp=") > -1 ? (href.replace(/cp=\d+/,"cp="+page)) : (href.indexOf("?") > -1 ? (href+"&cp="+page) : (href+"?cp="+page) );
}
/***open win***/
/*
 *obj:Object:{
 *width:Number "窗体宽度", title:String "标题", content:String "内容", style:String "样式",mode:Number 1(确定、取消 ),2(确定),3(无确定取消), 
 *drag:Boolean, ok:String "确认按钮", cancel:String "取消按钮",top:Number "上边距",left:Number "左边距", zIndex:Number} 
 *mask:Boolean 
 *ok_callback:Function 确定回调函数
 *cancel_callback:Function 取消回调函数
 */
function renjianWindow(obj, mask, ok_callback, cancel_callback, close_callback){
	var mode_str = "";
	switch(obj.mode){
	   case 2:
	     mode_str = '\
           <div class="open_window_ft clearfix">\
           <table class="fl"><tbody><tr><td class="vertical-middle lfTips"></td></tr></tbody></table>\
           <table class="fr"><tbody><tr><td class="vertical-middle"><input type="submit" class="button50 open_window_ok" value="发送"/></td></tr></tbody></table>\
		   </div>';
	   break;
	   case 3:
	   break;
	   default:
	     mode_str = '\
	           <div class="open_window_ft clearfix">\
	           <table class="fl"><tbody><tr><td class="vertical-middle lfTips"></td></tr></tbody></table>\
	           <table class="fr"><tbody><tr><td class="vertical-middle"><input type="submit" class="button50 open_window_ok" value="发送"/></td><td class="vertical-middle"><a class="open_window_cancel" href="#">取消</a></td></tr></tbody></table>\
			   </div>';
	}
	var win_template = '\
		<div class="open_window">\
		    <div class="open_window_content clearfix">\
		        <table class="open_window_hd" cellpadding="0" cellspacing="0" width="100%">\
				     <tr>\
					        <td class="tp_lf"></td>\
							<td class="tp_ct"><h5>人间网</h5><span class="close close_win" href="javascript:void(0)" title="关闭">×</span></td>\
							<td class="tp_rt"></td>\
					 </tr>\
	           </table>\
			   <table cellpadding="0" cellspacing="0" width="100%">\
				     <tr>\
					        <td class="ct_lf"></td>\
							<td class="open_window_bd"></td>\
							<td class="ct_rt"></td>\
					 </tr>\
               </table>\
               <table cellpadding="0" cellspacing="0" width="100%">\
				     <tr>\
					        <td class="bt_lf"></td>\
							<td class="bt_ct">&nbsp;</td>\
							<td class="bt_rt"></td>\
					 </tr>\
			   </table>\
		   </div>\
		</div>';
	obj.title = obj.title ? obj.title: "人间网";
	$("div.open_window").remove();
	var open_win = $(win_template);
	$(document.body).append(open_win);
	open_win.hide();
	mask && !$.hasMask && $.mask();
	if(obj.width) open_win.width(obj.width);
	open_win.center(obj.top, obj.left, obj.zIndex);
	open_win.show();
	if(obj.title) open_win.find(".open_window_hd h5").html(obj.title);
	if(obj.content) open_win.find(".open_window_bd").html(obj.content).append(mode_str);
	if(obj.mode != 3){
		if(obj.ok) open_win.find(".open_window_ok").val(obj.ok);
		if(obj.cancel) open_win.find(".open_window_cancel").html(obj.cancel);
	}	
	if(obj.drag) open_win.find(".open_window_hd").drag(open_win[0]);
	if(obj.style) $.addStyleSheet(obj.style);
	open_win.find("input.open_window_ok").click(function(){
		ok_callback && ok_callback.call(this);
		if(obj.remove) open_win.remove();
		ok_callback || $.hasMask && $.removeMask();
		return false;
	});
	open_win.find(".open_window_cancel").click(function(){
		if(cancel_callback){
			cancel_callback.call(this);
		}else{
			open_win.remove();
			$.hasMask && $.removeMask();
		}
		return false;
	});	
	open_win.find(".close_win").click(function(){
		if(close_callback){
			close_callback.call(this);
		}else{
			open_win.remove();
			$.hasMask && $.removeMask();			
		}
		return false;
	});	
	open_win.append('<iframe style="position:absolute;width:100%; height:100%; left: 0; top: 0; z-index: -1; filter:alpha(opacity=0);opacity:0" frameborder="0"></iframe>');
	return open_win;
}
function showTips(content, config, removeCall, noClose){
	var tempStr = '\
	    <div class="tips_wrap">\
	        <div class="tips_close"></div>\
	        <div class="tips_hd"></div>\
	        <div class="tips_ct"></div>\
	        <div class="tips_bt"></div>\
	    </div>\
	';
	 var oTips = $(tempStr);
	 $(document.body).append(oTips);
	 config && oTips.css(config);
	 oTips.find(".tips_ct").html(content);
	 if(!noClose){
		 oTips.find(".tips_close").click(function(){
			 oTips.hide();
			 oTips.remove();
			 removeCall && removeCall();
		 });
	 }else{
		 oTips.find(".tips_close").hide();
	 }
	 oTips.append('<iframe style="position:absolute;width:100%; height:100%; left: 0; top: 0; z-index: -1; filter:alpha(opacity=0);opacity:0" frameborder="0"></iframe>');
	 return oTips;
}
/*logo out*/
function logoout(){
	   if($.cookie.getCookie("password")){
	      $.cookie.remove("password");       
	   }     
	   location.href="/logout.html";
}
function fixText(source, length)
{
	if(charCount(source) > length)
	{
		while(charCount(source) > length - 3)
		{
			source = source.slice(0, -1);
		}
		while(charCount(source) < length) source += ".";
	}
	function charCount(text)
	{
	   return text.replace(/[^\x00-\xff]/g,"11").length;
	}
	return source;
}
function copyText(sText){
	if(window.clipboardData && window.clipboardData.setData("Text", sText)){
		alert("复制成功！ 按Ctrl+V 粘贴");return;
	}
	//alert("按Ctrl+C复制链接地址，按Ctrl +V粘贴到聊天窗口发送给好友");
}
function calRelTime(sTime, eTime){
	if(!sTime||!eTime) return false;
    var interval = eTime - sTime;
    var subDate = Math.floor(interval/(60 * 60 * 24 * 1000));
    if (interval < 0) interval = 0;
    var second = Math.floor(interval / 1000);
    if (second < 60) return (second?second:1)  + "秒前";
    else if (second < 60 * 60) return Math.floor(second / 60) + "分钟前";
    else if (second < 60 * 60 * 24) return Math.floor(second / 60 / 60) + "小时前";
    else if (second < 60 * 60 * 24 * 2 && subDate == 1) return "昨天";
    else if (second < 60 * 60 * 24 * 3 && subDate == 2) return "前天";
    else if (second < 60 * 60 * 24 * 30) return subDate + "天前";
    else if (second < 60 * 60 * 24 * 365) return Math.floor(second / (60 * 60 * 24 * 30)) + "月前";
    else if (second < 60 * 60 * 24 * 365 * 2) return "去年";
    else if (second < 60 * 60 * 24 * 365 * 3) return "前年";
    else return Math.floor(second / 60 / 60 / 24 / 365) + "年前";	
}
function controlFormSubmit(e, oTextarea){
	if(e && e.which == 13 && e.metaKey){
		oTextarea[0].blur();
		oTextarea.closest("form").trigger("submit");
		if(e.stopPropagation) e.stopPropagation();
		return true;
	}
	return false;
}
function enterKey(evt){
	evt=$.event.fix(evt);
	if(evt.which==13){
		return true;
	}	
	return false;
}
function desEdit(){
    var _value = $(this).html();
    var oParent = $(this.parentNode);
    oParent.html("<div style='border: 1px solid #ccc; padding: 2px; height: 36px; overflow-y: auto;'><textarea onblur='desEditReset.call(this)'>"+_value+"</textarea></div>");
    oParent.find("textarea").focus();
    oParent.find("textarea")[0].select();
    top.adjustIfrHt();
    return false;
}
function desEditReset(){
    var _value = $(this).val();
    $(this.parentNode.parentNode).html('<a onclick="return desEdit.call(this)" class="inline_edit" href="#">'+_value+'</a>');
    top.adjustIfrHt();
}
function attatchTitleEdit(){
    var _title = $(this).html();
    var oParent = $(this.parentNode);
    oParent.html("<input onkeydown='if(enterKey(window.event||arguments[0])) return false' onblur='attatchTitleEditReset.call(this)' />");
    oParent.find("input").focus();
    oParent.find("input").val(_title)[0].select();
    top.adjustIfrHt();
    return false;
}
function attatchTitleEditReset(){
   var _value = $(this).val();
   if(!_value.trim()){
       top.showMessage("标题不能为空");
       var _this = this;
       try{controlSendDisable(true);}catch(e){}
       setTimeout(function(){_this.focus();}, 6);
       return false;
    }
   try{controlSendDisable();}catch(e){}
   $(this.parentNode).html('<a onclick="return attatchTitleEdit.call(this)" href="#" class="inline_edit">'+ _value +'</a>');
   top.adjustIfrHt();
}
function closeNavDes(actionName){
    $(this).remove();
	$.post("/dd/updateUserBehavior.ajax",{"behavior":actionName});	
}
function reportStatus(sid){
	if(confirm("你确定要举报这条内容吗？")){
		if(sid)
			$.post("/dd/status/impeach.html",{statusId: sid}, function(reply){
				if(reply == "OK") top.showMessage("已举报成功,感谢您的举报");
				else top.showMessage(reply);
			});
	}
}
function stopPropagation(e){
	if(e) e.stopPropagation();	
}
//get flash version
function getFlashVersion() {
	var f="-1";
	var n=navigator;
	if (n.plugins && n.plugins.length) {
		for (var i=0;i<n.plugins.length;i++) {
		   if (n.plugins[i].name.indexOf('Shockwave Flash')!=-1) {
			f=n.plugins[i].description.split('Shockwave Flash ')[1];
			break;
		   }
		}
	} else if (window.ActiveXObject) {
		for (var i=10;i>=2;i--) {
		   try {
			var fl=eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash."+i+"');");
			if (fl) { f=i + '.0'; break; }
		   }catch(e) {}
		}
	}
	if(f == "-1")
		return f;
	else
		 return f.substring(0,f.indexOf(".")+2);
}