﻿var lib =
{
    uri: "AjaxRequest.ADS",
    Request: function(strName)
    {
        var strHref = window.document.location.href;
        var intPos = strHref.indexOf("?");
        var strRight = strHref.substr(intPos + 1);
        var arrTmp = strRight.split("&");
        for(var i = 0; i < arrTmp.length; i++)
        {
            var arrTemp = arrTmp[i].split("=");
            if(arrTemp[0].toUpperCase() == strName.toUpperCase()) return arrTemp[1];
        }
        return "";
    },
    AddToFavorite:function(sURL,sTitle)
    {
        if( $.browser.msie ){ window.external.AddFavorite(sURL, sTitle);  }
        else{ window.sidebar.addPanel(sTitle, sURL, "");  }
    },
    ResetPicSize: function(picName,maxWidth,maxHeight)
    {
        $(picName).each(function()
        { 
            this.onload = function()
            {
                var per = this.width / this.height;
   	            if( this.width >= this.height)
   	            {   	            
   	                if( maxWidth > 0)
   	                {
   	                    per = this.height / this.width;
   	                    if( this.width > maxWidth) { this.width = maxWidth; this.height = this.width * per; }
   	                }
   	                else
   	                {
   	                    if( this.height > maxHeight) { this.height = maxHeight; this.width = this.height * per; }
   	                }
   	            }
   	            else
   	            {
   	                if( maxHeight > 0)
   	                {
   	                    if( this.height > maxHeight) { this.height = maxHeight; this.width = this.height * per; }
   	                }
   	                else
   	                {
   	                    per = this.height / this.width;
   	                    if( this.width > maxWidth) { this.width = maxWidth; this.height = this.width * per; }
   	                }
   	            }
   	            this.onload = null;
   	        }
        });
    },
    AddBorder:function(obj,width)
    {
        $(obj).load(function(){
             $(this).css("padding","3px").css("border",width+"px solid #ccc");
        });
    },
    AddPicLink:function(picName)
    {
        $(picName).load(function()
        {
            var src = $(this).attr("src");            
            $(this).wrap("<a target='_blank' href='"+src+"'></a>");
        });
    },
    DoServerError:function(text)
    {
        if( arguments.length > 0)
        {
            alert(arguments[0]);
        }
        else
        {
            alert("服务器忙，请稍候再试！");
        }
    },
    copyToClipboard:function(txt) {
        if($.browser.msie && window.clipboardData) {
           window.clipboardData.clearData();
           window.clipboardData.setData("Text", txt);
           alert("已复制到剪贴板");
        }
        else
        {
            alert("只有IE支持直接复制到剪贴板，其他浏览器请选择前面的链接后复制！");
        }    
    },
    addfriend:function(u){
      $.ajax({
        url:lib.uri,data:"type=user&method=addfriend&uid="+u,dataType:"text",cache:false,
        error:function(){ lib.DoServerError(); },
        success:function(text){
            var ar = new ajaxResult(text);
            var result = ar.getResultText();
            alert(ar.getResultText());            
            if( ar.getResultStatus()){ location.reload(); }
        }
    });
  },
  showQQ:function(){
    var tempSrc='http://sighttp.qq.com/wpa.js?rantime='+Math.random()+'&sigkey=1e0679f96ec2fad17bf927b5eb815dc7981d726d53961e62009aecd53ae116b8';        
    var oldscript=document.getElementById('testJs');
    var newscript=document.createElement('script');
    newscript.setAttribute('type','text/javascript'); newscript.setAttribute('id', 'testJs');
    newscript.setAttribute('src',tempSrc);
    if(oldscript == null){
        document.body.appendChild(newscript);
    }else{
        oldscript.parentNode.replaceChild(newscript, oldscript);
    }
    return false;
  },
  stopKey:function(key){    
    var arr = '8,16,37,38,39,40,46';
    arr = arr.split(',');    
    for(var i=0;i<arr.length;i++){
     if(key == (arr[i]-0)) return false;
    }
    return true;
  }
}

function ajaxResult(str)
{
    this.arr = str.split("|");
    if(this.arr.length <= 0)
    {
        this.arr[0] = "err";
    }
    if( this.arr.length <= 1 )
    {
        this.arr[1] = "没有执行任何操作，可能是服务器没有响应！";
    }
}
ajaxResult.prototype =
{
    success:false,
    getResultStatus: function()
    {
        if( this.arr[0] == "err" )
        {
            return false;
        }
        else if( this.arr[0] == "ok" )
        {
            this.success = true;
            return true;
        }
        return false;
    },
    getResultText: function()
    {
        return this.arr[1];
    }
}
