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

// 用户操作

function _initType(){
	
    $("a[@type]").click(function(){
			
		if(loginStatus=="guest")
		{
				alert("请你先登录");  
		}
		else
		{
			var type = $(this).attr("type");
			
			if(type=="scrip")
			{
			      scrip();
			}
			else if(type=="guide")
			{ 
			      guide();
			}
			else if(type=="friend")
			{
			      friend();
			}
		}
	})	
}

//传小纸条

function scrip(){
	
	if(sdommin!="")
	{
       //window.location.href = ;	
	   window.open('/'+sdommin+'/manage/scripsend/username/'+uesrname);
	}
}

//添加督导

function guide(){
	
       var height     = window.document.body.scrollHeight; 
	   
	   var top        = document.documentElement.scrollTop+200; 
	   
	   var left       = document.body.scrollWidth/2-250;
	   
	   var html;
	   
	   var  username  =  $(".visualize .username").text();
	   
	   html='<div id="scrip"></div><div class="scripcontent"><div><h3>加为督导</h3>';
	   
	   html+='<p>您要邀请<span>'+username+'</span>做您的督导吗？</p><h2><textarea>给对方留言...</textarea></h2>';
	   
	   html+='<h4><input type="button" value="确定" /><input type="button" value="取消" /></h4></div></div>';
	   
	   $("body").append(html);
	   
	   $("#scrip").height(height);

	   $(".scripcontent").css("top",top);
	   
	   $(".scripcontent").css("left",left);

        
	   $(".scripcontent textarea").focus(function(){
	
	       $(this).val("");
	   
	       $(this).unbind("focus");
		   
	   })

	    $(".scripcontent input:first").click(function(){
								
			 var content  =   $(".scripcontent textarea").val();	
			 
			 var len      =  calculate_byte(content);
			 
			 if(len>100)
			 {
				 alert("内容最多允许100个中文或 200个字符");
			 }
			 else
			 {
				 if(content=="给对方留言...")
				 {
					 content ="";
				 }					
		   <!-- start -->
				$.post(
			   "/"+uesrname+"/space/guide",
			  {content:content},
				 function(msg)
					{
			
						if(msg=="refuse")
						{
							alert("自己不能加自己为督导");
					    }
						else if(msg=="login")
						{
						    alert("请你先登录，再发加为督导请求");
						}
						else if(msg==1)
						{
						    alert("发送成功，请等待对方确认 ^_^");
						   
						   $(".scripcontent input:last").click();
						}
						else if(msg=="wait")
						{
						    alert("您已经给TA发送过督导邀请啦，请等待对方确认 ^_^");
						   
						    $(".scripcontent input:last").click();
						}
						else if(msg=="friend")
						{
						    alert("对方已经是您的督导,无需再邀请 ^_^");
						   
						    $(".scripcontent input:last").click();
						}
					}
				);
	       <!-- end -->
			 }
	   }) 
	  
	   $(".scripcontent input:last").click(function(){
											  
		    $(".scripcontent").remove();
			
			$("#scrip").remove();
	  }) 
}

//加为好友

function friend(){
	
	
       var height  = window.document.body.scrollHeight; 
	   var top     = document.documentElement.scrollTop+200; 
	   var left   = document.body.scrollWidth/2-250;
	   var html;
	   
	   var  username  =  $(".visualize .username").text();
	   
	   html='<div id="scrip"></div><div class="scripcontent"><div><h3>加为好友</h3>';
	   
	   html+='<p>您要加<span>'+username+'</span>为好友吗？</p><h2><textarea>给对方留言...</textarea></h2>';
	   
	   html+='<h4><input type="button" value="确定" /><input type="button" value="取消" /></h4></div></div>';
	   
	   $("body").append(html);
	   
	   $("#scrip").height(height);

	   $(".scripcontent").css("top",top);
	   
	   $(".scripcontent").css("left",left);

        
	   $(".scripcontent textarea").focus(function(){
	
	       $(this).val("");
	   
	       $(this).unbind("focus");
	   
	   })

	    $(".scripcontent input:first").click(function(){
								
			 var content  =   $(".scripcontent textarea").val();	
			 
			 var len      =  calculate_byte(content);
			 
			 if(len>100)
			 {
				 alert("内容最多允许100个中文或 200个字符");
			 }
			 else
			 {
				 if(content=="给对方留言...")
				 {
					 content ="";
				 }					
		   <!-- start -->
				$.post(
			   "/"+uesrname+"/space/friend",
			  {content:content},
				 function(msg)
					{
			
						if(msg=="refuse")
						{
							alert("自己不能加自己为好友 ^_^");
					    }
						else if(msg=="login")
						{
						    alert("请你先登录，再发加为好友请求^_^");
						}
						else if(msg==1)
						{
						    alert("好友邀请发送成功，请等待对方确认 ^_^");
						   
						   $(".scripcontent input:last").click();
						}
						else if(msg=="wait")
						{
						    alert("您已经给TA发送过好友邀请啦，请等待对方确认 ^_^");
						   
						    $(".scripcontent input:last").click();
						}
						else if(msg=="friend")
						{
						    alert("对方已经是您的好友,无需再添加 ^_^");
						   
						    $(".scripcontent input:last").click();
						}
					}
				);
	       <!-- end -->
			 }
	   }) 
	  
	   $(".scripcontent input:last").click(function(){
											  
		    $(".scripcontent").remove();
			
			$("#scrip").remove();
	  }) 
}


function AddFavorite(sURL,sTitle) {

       try{
            window.external.addFavorite(sURL, sTitle);
       }
       catch (e)       
	   {
         try         
		   {
                window.sidebar.addPanel(sTitle, sURL, "");
           }

           catch (e){

               alert("加入收藏失败，请使用Ctrl+D进行添加");
           }

 } } 



	   
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;
}
}
}


