﻿$(document).ready(function($){
						   
	_initFriendsay();
	  	  
})





function _initFriendsay(){
	
   	$("#friendsay a").click(function(){

	   $("#friendsay span").addClass("edit");
	   
	  var  friendcontent  = $(".friendcontent").html();
	  
	       temp           = $(".friendcontent").html();
	   
	   var friendcontent  = friendcontent.replace(/<br\/?>/ig,"\n"); 
	   
	   $("#friendsay span").html('<a href="javascript:friendsaySave();">保存</a>  <a href="javascript:friendsayCanle();">取消</a>');
	   
	   $(".friendcontent").html('<textarea>'+friendcontent+'</textarea>');
		
	})
}

function friendsayCanle(){
		   
		  $("#friendsay span").removeClass("edit");
		
		  $("#friendsay span").html('<a href="javascript:;">编辑</a>');
		
		if(temp!="")
		{
		  $(".friendcontent").html(temp);
		}
		
		  _initFriendsay();
}


function friendsaySave(){
	
          var friendsay  = $(".friendcontent textarea").val();
		  
		  var Len       = calculate_byte(friendsay)
		  
		  if(friendsay=="")
		  {
		     alert("请输入你的主人絮语");
		  }
		  else if(Len>500)
		  {
			  alert("小纸条主题最多可填个110中文或者250个字符");
		  }
		  else
		  {
			  <!-- start -->
						$.post(
					   "/"+domain+"/friend/friendsaysave",
						{friendsay:friendsay},
						 function(msg)
						{
							  if(msg=="login")
							  {
									  alert("请你先登录");	 
							  }
							  else if(msg!="")
							  {
								  $(".friendcontent").html(msg);
								  
								  temp="";
								  
								  friendsayCanle();
							  }
						
					    });
			   <!-- end -->
	      }
}

function calculate_byte( sTargetStr ) {
        var sTmpStr, sTmpChar;
        var nOriginLen = 0;
        var nStrLength = 0;
         
        sTmpStr = new String(sTargetStr);
        nOriginLen = sTmpStr.length;

        for ( var i=0 ; i < nOriginLen ; i++ ) {
                sTmpChar = sTmpStr.charAt(i);

                if (escape(sTmpChar).length > 4) {
                        nStrLength += 2;
                } else if (sTmpChar!='\r') {
                        nStrLength ++;
                }
        }
        return nStrLength;    
}



function DrawImage(ImgD,w,h){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= w/h){
if(image.width>w){
ImgD.width=w;
ImgD.height=(image.height*w)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
//ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>h){
ImgD.height=h;
ImgD.width=(image.width*h)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
//ImgD.alt=image.width+"×"+image.height;
}
}
}


