// JavaScript Document
 
 
function showpreview(type){
	popwincontainerheight=$("#popwincontainer").height();
	$("#description").keyup(function(){
									$("#comment").show();
									$("#comment").html($(this).val().replace(/\n/ig, "<br />"));
									if (type == 'event' || type == 'user'  || type == 'album'  || type == 'image' ){
										var commentheight=$("#comment").parent().height();
										var imgheight=$("#comment").parent().prev().height();
										var addheight=0;
										if (commentheight > imgheight) addheight = commentheight - imgheight
										$("#popwincontainer").css("height",popwincontainerheight+addheight+"px");
										$("#popwincontainer .border").css("height",popwincontainerheight+addheight-4+"px");																			
									}else{
									$("#popwincontainer").css("height",popwincontainerheight+$("#comment").height()+"px");
									$("#popwincontainer .border").css("height",popwincontainerheight+$("#comment").height()-4+"px");						
									}

						

									})
}

function showpreview1(type){
	popwincontainerheight=$("#popwincontainer").height();
	$("#description").keyup(function(){
									$("#comment").show();
									$("#comment").html($(this).val().replace(/\n/ig, "<br />"));
									if (type == 'event' || type == 'user'  || type == 'album'  || type == 'image' ){
										var commentheight=$("#comment").parent().height();
										var imgheight=$("#comment").parent().prev().height();
										var addheight=0;
										if (commentheight > imgheight) addheight = commentheight - imgheight
										$("#popwincontainer").css("height",popwincontainerheight+addheight+"px");
										$("#popwincontainer .border").css("height",popwincontainerheight+addheight-4+"px");																			
									}else{
									$("#popwincontainer").css("height",popwincontainerheight+$("#comment").height()+"px");
									$("#popwincontainer .border").css("height",popwincontainerheight+$("#comment").height()-4+"px");						
									}

						

									})
}
function showmsg(){
	var $this=$("#a_pm_lefttools");
	$.get("portlet/friend/searchNewMessageCount.do",function(data){
								   if ($this.prev().is("em[class='hasLetter']")){
									   $this.prev().remove()
									   }							   
								   if (data!=0){
									   var msgshow=$("<em class=\"hasLetter\"><em class=\"count\">"+$.trim(data)+"</em></em>")
									   $("#a_pm_lefttools").before(msgshow);
									   window.clearInterval(msginterval)
									   }
								   })
	
}

$(document).ready(function(){ 
						   	jQuery.ajaxSetup ({cache:false});
						   $("div.txtcontent").find("img").each(function(){
																		 var $this=$(this);
																		 var img=new Image();
																		 img.src=$this.attr("src");
																		 var autoScaling=function(){
																			 if (img.width>600){$this.css("width","590px")}
																			 }
																		 if (img.complete) autoScaling()
																		 $(img).load(function(){autoScaling()})
																		 })
						   
						   
						   
						   //定时检测短消息
						   showmsg();

						   msginterval=window.setInterval("showmsg()",30000);
						   	//分享
						      $("a[id='shareit']").click(function(){ //分享
//						      							alert('share');
														
														 var itemId = $("#itemId").val();
															  var type=$("#itemType").val();
														 var $this=$(this);
														// alert(type + title + content + itemId + userName + userId + "afc :" + others);
														 $.ajax({
														 
																url:"portlet/share/sharePopup.do?type="+type+"&itemId="+itemId,
																cache:false,
																success:function(html){
																	//$.msgbox.closemsgbox();
																	var types="";
																	if(type == 'thread'){
																		types="分享话题"
																	} else if (type == 'blog'){
																		types="分享日志";
																	} else if(type == 'event'){
																		types="分享活动";
																	} else if(type == 'user'){
																		types = "分享用户";
																	} else if(type == 'article'){
																		types= "分享文章";
																	}  else if(type == 'album'){
																		types= "分享相册";
																	}  else if(type == 'image'){
																		types= "分享图片";
																	}
																	else if(type == 'oldarticle'){
																		types= "分享杂志文章";
																	}
																		
																	var $msgbox=$.msgbox.popupwin(250,180,types,html);
																	var $msg=$msgbox.find("span[id='msg']")
																	$msgbox.find("input[id='close']").click(function(){$.msgbox.closemsgbox()});
																	$msgbox.find("input[id='confirm']").click(function(){
																													$(this).attr("disabled",true);
																												   var txtcomment=$msgbox.find("a[id='txtcomment']").val();
																												    var description = encodeURI($("#description").val());
//																												    var others = $("#others").val();
																												   $msg.html("数据提交中....");
//																												   alert(others);
																												   $.post("portlet/share/shareSth.do?type="+type+"&itemId="+itemId+"&description="+description,{txtcomment:txtcomment},function(){$msg.html("数据提交成功！");$.msgbox.closemsgbox(1000,function(){var diggblock=$this.parent().find("a[class='diggblock']").find("span").text()/1+1;$this.parent().find("a[class='diggblock']").find("span").text(diggblock)})})
																												   })
																	
																	if(type == 'event'){showpreviewevent();}
																	else{showpreview1(type);}
																	
//																	popwincontainerheight=$msgbox.height();
//																	var $comment=$msgbox.find("#comment");
//																	var $description=$msgbox.find("#description")
//																	$description.keyup(function(){
//																								$comment.show();
//																								$comment.html($(this).val().replace(/\n/ig, "<br />"));
//																								$msgbox.css("height",popwincontainerheight+$description.height()+"px");
//																								$msgbox.find(".border").css("height",popwincontainerheight+$description.height()-4+"px");
//																								})
																	
																	return false;
																	},
														 		error:function(){
																	//$.msgbox.closemsgbox();
																	$.msgbox.msgbox(150,100,"分享日志","页面读取失败！");
																	},
														 		beforeSend:function(){
																	//$.msgbox.closemsgbox();
																	$.msgbox.msgbox(150,100,"分享日志","页面读取中！");
																	},
																complete: function(){
																	 //$.msgbox.popupwin(450,280,"设置我的话题吧complete",html)
																	 return false;
																   }
																	
																})
															  return false;
														   })

						  $("a[id='sharethread']").click(function(){ //分享
//						      							alert('share');
														
														 var itemId = $(this).attr("itemId");
														var type=$(this).attr("itemType");
														 var $this=$(this);
														// alert(type + title + content + itemId + userName + userId + "afc :" + others);
														 $.ajax({
														 
																url:"portlet/share/sharePopup.do?type="+type+"&itemId="+itemId,
																cache:false,
																success:function(html){
																	//$.msgbox.closemsgbox();
																	
																	var types="分享话题"
																	
																		
																	var $msgbox=$.msgbox.popupwin(250,180,types,html);
																	var $msg=$msgbox.find("span[id='msg']")
																	$msgbox.find("input[id='close']").click(function(){$.msgbox.closemsgbox()});
																	$msgbox.find("input[id='confirm']").click(function(){
																													$(this).attr("disabled",true);
																												   var txtcomment=$msgbox.find("a[id='txtcomment']").val();
																												    var description = encodeURI($("#description").val());
//																												    var others = $("#others").val();
																												   $msg.html("数据提交中....");
//																												   alert(others);
																												   $.post("portlet/share/shareSth.do?type="+type+"&itemId="+itemId+"&description="+description,{txtcomment:txtcomment},function(){$msg.html("数据提交成功！");$.msgbox.closemsgbox(1000,function(){var diggblock=$this.parent().find("a[class='diggblock']").text()/1+1;$this.parent().find("a[class='diggblock']").text(diggblock);$this.parent().parent().find("span[id='sharenumber']").text(diggblock)})})
																												   })
																	
																	if(type == 'event'){showpreviewevent();}
																	else{showpreview1(type);}
																	
//																	popwincontainerheight=$msgbox.height();
//																	var $comment=$msgbox.find("#comment");
//																	var $description=$msgbox.find("#description")
//																	$description.keyup(function(){
//																								$comment.show();
//																								$comment.html($(this).val().replace(/\n/ig, "<br />"));
//																								$msgbox.css("height",popwincontainerheight+$description.height()+"px");
//																								$msgbox.find(".border").css("height",popwincontainerheight+$description.height()-4+"px");
//																								})
																	
																	return false;
																	},
														 		error:function(){
																	//$.msgbox.closemsgbox();
																	$.msgbox.msgbox(150,100,"分享日志","页面读取失败！");
																	},
														 		beforeSend:function(){
																	//$.msgbox.closemsgbox();
																	$.msgbox.msgbox(150,100,"分享日志","页面读取中！");
																	},
																complete: function(){
																	 //$.msgbox.popupwin(450,280,"设置我的话题吧complete",html)
																	 return false;
																   }
																	
																})
															  return false;
														   })
						//   msginterval=window.setInterval("showmsg()",1000);

						   
						   				   
						   //发送私信给好友
						   
						   $("a[id='a_sendpm']").click(function(){
																var $this=$(this);
																var friendid=$this.attr("fid");
																
																var friendname=$this.attr("fvalue");
																$.get("porlet/friend/sendForward.do",{friendid:friendid,friendname:friendname},function(data){
																													  
																													  var $msgbox=$.msgbox.popupwin(350,120,"私信",data);
																													  $msgbox.find("#btsendpm").click(function(){
																													  //alert(friendid);
																																							   $(this).next().html("发送中...");
																																							   var title=$msgbox.find("#pmtitle").val();
																																							   var content=$msgbox.find("#pmcontent").val();
																																							   if (title=="" || content=="") {
																																								   $(this).next().html("标题或内容不能为空！");
																																								   return false;
																																								   }
																																							   
																																							   $.post("porlet/friend/sendMessage.do",{friendid:friendid,title:title,content:content},function(){$.msgbox.closemsgbox(2000);})
																																							   
																																							   })
																													  
																													  })
																
																
																
																return false;
																})
						   
						   //弹出下载插件页面
						   $("#downloadsharetools").click(function(){
																	 $.ajax({
																			url:"jsp/share/download.jsp",
																			cache:false,
																			success:function(html){
																				//$.msgbox.closemsgbox();
																				$.msgbox.popupwin(350,120,"下载右键分享插件",html);
																				//showpreview();
																				},
																			error:function(){
																				//$.msgbox.closemsgbox();
																				$.msgbox.msgbox(150,100,"下载右键分享插件","页面读取失败！")
																				},
																			beforeSend:function(){
																				//$.msgbox.closemsgbox();
																				$.msgbox.msgbox(150,100,"下载右键分享插件","页面读取中！");
																				},
																			complete: function(){
																				 //$.msgbox.popupwin(450,280,"设置我的话题吧complete",html)
																			   }
																				
																			})
																	 return false;
																   
																   
																   })
						   
						   })

//;(function($){
//		   $.fn.forgetPassword=function(options){
//			   var settings={
//				   userNameInput:""
//				   };	   
//			   var op = $.extend(settings, options);
//			   var offsetleft=$(this).offset().left;
//			   var offsettop=$(this).offset().top+$(this).innerHeight();
//			   }
//		   })( jQuery );
;(function($){ //忘记密码
		   $.fn.forgetPassword=function(options){
			   var settings={
				   userNameInput:""
				   };
			   var op = $.extend(settings, options);
			   
			   var offSetLeft=$(this).offset().left;
			   var offSetTop=$(this).offset().top+$(this).innerHeight();
			   var $forgetPasswordPopwin;
			  
			   //显示输入用户名的窗口
			   function showInputPopwin(){
				   $.ajax({
						  url:"jsp/user/forgetPassword.jsp",
						  cache:false,
						  success:function(html){
						     
							  $forgetPasswordPopwin=$.msgbox.popupwin(250,100,"取回密码",html);
							  sendPassword($forgetPasswordPopwin);
							  },
						  error:function(){$.msgbox.msgbox(150,100,"取回密码","页面读取失败！")},
						  beforeSend:function(){$.msgbox.msgbox(150,100,"取回密码","页面读取中！")},
						  complete: function(){}
						  })
				   }
				   
			   //发送密码
			   function sendPassword($obj){
				   var $userNameObj=$obj.find("#username");
				   var $aSendPassword=$obj.find("#a_sendpassword");
				   var $sendPasswordMsg=$obj.find("#sendpasswordmsg");
				   
				   $aSendPassword.click(function(){
												 $sendPasswordMsg.html("");
												 if ($userNameObj.val()==""){
													 //$sendPasswordMsg.html("请输入需要取回密码的用户名！");
													 $aSendPassword.next().html("请输入需要取回密码的用户名！");
													 }else{ 
													 $aSendPassword.next().html("密码发送中！");
													 var $this=$(this);
													 $this.hide();
													 $.ajax({
													 		type: "POST",
															url:"portlet/user/sendPassword.do?callback=?",
															data:"username="+$userNameObj.val(),
															cache:false,
															dataType: "jsonp",
															success:function(html){
																//alert(html);
																if (html.error == 1 ){
																	$aSendPassword.next().html("密码发送至您的邮箱！");
																	$.msgbox.closemsgbox(1000);
																	}else{
																	$aSendPassword.next().html("密码取回失败！");
																	$this.show();
																	}
																},
															error:function(){$aSendPassword.next().html("密码取回失败！");},
															beforeSend:function(){$aSendPassword.next().html("密码发送中！");},
															complete: function(){}
															})
													 }
												 
												 })
				   
				   }
			   
			   
			   
			   return $(this).click(function(){
											 showInputPopwin();//.find("#username");
											 return false;
											 })
			   }
		   
		   })( jQuery );

;(function($) {
		  $ieframe = $('<iframe class="popupwinieframe" frameborder="0" src="#"></iframe>').css({ position: "absolute", display: "none", zIndex: -1});
jQuery.msgbox={ //弹出窗口
//	a:$('<iframe class="popupwinieframe" frameborder="0" src="#"></iframe>').css({ position: "absolute", display: "none", zIndex: -1}),
	confirmbox:function(title,content,leftto,topto){
///alert( $ieframea.attr("class"));
		var popwinstr="<div id=\"popwincontainer\">";//<div id=\"header\"></div>
		popwinstr+="<div class=\"border\"><div id=\"content\"><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td>$content</td></tr></table></div></div></div>";
		$.msgbox.closemsgbox();
		
		//this.ieframe = $('<iframe class="popupPanel_ieframe" frameborder="0" src="#"></iframe>').css({ position: "absolute", display: "none", zIndex: -1});
		$("#container").before(popwinstr.replace("$content",content).replace("$title",title));
		$("#popwincontainer").prepend($ieframe);
		$("#close").click(function(){
								   $.msgbox.closemsgbox();return false;
								   })
		tablewidth=$("#popwincontainer > .border > #content > table").width()+16;
		$ieframe.css({height:($("#popwincontainer").height())+"px",width:($("#popwincontainer").width())+"px"});
		$ieframe.show();
		$("#popwincontainer").css({"left":leftto+"px","top":topto+"px","position":"absolute","width":tablewidth+"px"});
		$("#popwincontainer").draggable({cursor:"move",revert: false,ghosting: true, opacity: 0.7,handle:"#popwincontainer"});
		return $("#popwincontainer");
		
		},
	msgbox:function(width,height,title,content){
		
		var popwinstr="<div id=\"popwincontainer\">";//<div id=\"header\"></div>
		popwinstr+="<div class=\"border\"><div id=\"title\"><span id=\"close\" title=\"关闭\">X</span>$title</div>";
		popwinstr+="<div id=\"content\"><table style=\"width:100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td align=\"center\" valign=\"middle\">$content</td></tr></table></div></div></div>";
		this.ieframe = $('<iframe class="popupPanel_ieframe" frameborder="0" src="#"></iframe>').css({ position: "absolute", display: "none", zIndex: -1});
		$.msgbox.closemsgbox();
		$("#container").before(popwinstr.replace("$title",title).replace("$content",content));
		$("#popwincontainer").prepend(this.ieframe);
		$("#popwincontainer > .border > #title > #close").click(function(){
															   $.msgbox.closemsgbox();return false;
															   })
		this.ieframe.css({height:(height/1+4)+"px",width:(width/1+4)+"px"});
		$("#popwincontainer").css({height:(height/1+4)+"px",width:(width/1+4)+"px"});
		$("#popwincontainer > .border").css({height:(height/1)+"px",width:(width/1)+"px"});
		this.ieframe.show();		
		$("#popwincontainer > .border > #content").css({height:(height/1-35)+"px",width:(width/1-10)+"px"});
		//$("#popwincontainer > #content > table").css({height:(height/1-65)+"px"});
		scrolltop=$(window).scrollTop()
		leftto=$("#popwincontainer").offset().left-(width/2);
		if (($(window).height()/2) > (height/2)) {
			topto=$("#popwincontainer").offset().top-(height/2);
		}else{
			topto=$(window).height()/2;//-((height/2) - ((height/2) - (document.body.clientHeight/2)));
		}		
		topto=(topto+scrolltop);
		$("#popwincontainer").css({"left":leftto+"px","top":topto+"px","position":"absolute"});
		//$("#popwincontainer").animate({opacity:1},3000);
		$("#popwincontainer").draggable({cursor:"move",revert: false,ghosting: true, opacity: 0.7,handle:"#popwincontainer > #title"});
		return $("#popwincontainer");
		
		},		
	popupwin:function(width,height,title,content){
		var popwinstr="<div id=\"popwincontainer\">";//<div id=\"header\"></div>
		popwinstr+="<div class=\"border\"><div id=\"title\"><span id=\"close\" title=\"关闭\">X</span>$title</div>";
		popwinstr+="<div id=\"content\"><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td align=\"center\" valign=\"top\">$content</td></tr></table></div></div></div>";	//<div id=\"footer\"></div>
		this.ieframe = $('<iframe class="popupPanel_ieframe" frameborder="0" src="#"></iframe>').css({ position: "absolute", display: "none", zIndex: -1});
		
		$.msgbox.closemsgbox();
		$("#container").before(popwinstr.replace("$title",title).replace("$content",content));
		$("#popwincontainer").prepend(this.ieframe);
		$("#popwincontainer > .border > #title > #close").click(function(){
															   $.msgbox.closemsgbox();return false;
															   })
		
		//$("#popwincontainer > #content").css({height:(height/1-54)+"px",width:(width/1-10)+"px"});
		
		

		
		//表格内写内容读取中
		
		//判断是否有图片
		
		//判断图片是否读取完成
		
		
		//取得内容具体的宽高
		innerheight=$("#popwincontainer > .border > #content > table").height()+34;
//		if (height<innerheight){
//			height=innerheight+34;
//			}
		//宽未定义
		innerwidth=$("#popwincontainer > .border > #content > table").width()+16;		
		
		
		height=innerheight;//height<innerheight?innerheight:height;
		width=width>innerwidth?width:innerwidth;
		
		$("#popwincontainer > .border > #content > table").css({height:(height/1)+"px",width:(width/1-8)+"px"});
	
		this.ieframe.css({height:(height/1+4)+"px",width:(width/1+4)+"px"});
		$("#popwincontainer").css({height:(height/1+4)+"px",width:(width/1+4)+"px"});
		$("#popwincontainer > .border").css({height:(height/1)+"px",width:(width/1)+"px"});
		
		
		
		
//		marginleft=-(width/2);
//		if (($(window).height()/2) > (height/2)) {
//			marginto=-(height/2);
//		}else{
//			marginto=-($(window).height()/2);//-((height/2) - ((height/2) - (document.body.clientHeight/2)));
//		}	
		scrolltop=$(window).scrollTop()
		leftto=$("#popwincontainer").offset().left-(width/2);
		if (($(window).height()/2) > (height/2)) {
			topto=$("#popwincontainer").offset().top-(height/2);
		}else{
			topto=$(window).height()/2;//-((height/2) - ((height/2) - (document.body.clientHeight/2)));
		}		
		topto=(topto+scrolltop);
		$("#popwincontainer").css({"left":leftto+"px","top":topto+"px","position":"absolute"});
		//this.ieframe.css({"left":leftto+"px","top":topto+"px","position":"absolute"});
		this.ieframe.show();
		//$("#popwincontainer").animate({opacity:1},3000);#popwincontainer > 
		$("#popwincontainer").draggable({cursor:"move",revert: false,ghosting: true, opacity: 0.7,handle:"#popwincontainer > #title"});
		return $("#popwincontainer");
		},
	closemsgbox:function(time,callbackfunction){
		//$("#popwincontainer").animate({opacity:0},2000,function(){$("#popwincontainer").remove()})//.remove()
		if (time){
			window.setTimeout(function(){$("#popwincontainer").hide("fast",callbackfunction).remove()},time)
			}
		else{
			$("#popwincontainer").remove();
			}
		
		}
	
	}
})(jQuery);