﻿//<a onclick="AddFavorite(window.location,document.title)">加入收藏</a>
//<a onclick="SetHome(this,window.location)">设为首页</a>
function AddFavorite(sURL, sTitle)
{
    try
    {
        window.external.addFavorite(sURL, sTitle);
    }
    catch (e)
    {
        try
        {
            window.sidebar.addPanel(sTitle, sURL, "");
        }
        catch (e)
        {
            alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}


function SetHome(obj,vrl){
        try{
                obj.style.behavior='url(#default#homepage)';obj.setHomePage(vrl);
        }
        catch(e){
                if(window.netscape) {
                        try {
                                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                        }
                        catch (e) {
                                alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。");
                        }
                        var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
                        prefs.setCharPref('browser.startup.homepage',vrl);
                 }
        }
}



function Dialog_Open(_url,_w,_h)
{
    window.showModalDialog(_url,null,"dialogWidth="+ _w +"px;dialogHeight="+ _h +"px");
}
function Window_Open(_url,_w,_h,_name)
{
    //获得窗口的垂直位置
    var iTop = (window.screen.availHeight-30-_h)/2;        
    //获得窗口的水平位置
    var iLeft = (window.screen.availWidth-10-_w)/2;           
    window.open(_url,_name,'height='+_h+',,innerHeight='+_h+',width='+_w+',innerWidth='+_w+',top='+iTop+',left='+iLeft+',status=no,toolbar=no,menubar=no,location=no,resizable=no,scrollbars=1,titlebar=no');
}


/********************
*基本描述：
* 图片缩略,使用js的静态类实现
*参数说明：
* @ im : 可以为image对象或img的id
*基本功能:
* Img(im).Resize(nWidth,nHeight)             : 按给定的宽和高进行智能缩小
* Img(im).ResizedByWH(nWidth,nHeight)   : 按给定的宽和高进行固定缩小(会出现图片变形情况)
* Img(im).ResizedByWidth(nWidth)             : 按给定的宽进行等比例缩小
* Img(im).ResizedByHeight(nHeight)           : 按给定的高进行等比例缩小
* Img(im).ResizedByPer(nWidthPer,nHeightPer) : 宽和高按百分比缩小
*使用例子:
* <img id="demo" src="demo.gif" width="191" height="143" onload="Img(this).Resize(200,500);" />
* <img id="demo" src="demo.gif" width="191" height="143" onload="Img('demo').ResizedByPer(200,500);" />
********************/
function Img(im)
{
    ImgCls.Obj = ( im && typeof im == 'object' ) ? im : document.getElementById(im) ;
    if(ImgCls.Obj)
    {
        var imgObj = new Image() ;
        imgObj.src = im.src ;
        ImgCls.OldWidth  = imgObj.width ;
        ImgCls.OldHeight = imgObj.height ;
        imgObj = null ;
        return ImgCls ;
    }
}
var ImgCls =
{
    Obj       : null ,
    OldWidth  : 0 ,
    OldHeight : 0 ,
    
    //按给定的宽和高进行智能缩小
    Resize : function ( nWidth , nHeight )
    {
        var w , h , p1 , p2 ;
        //计算宽和高的比例
        p1 = nWidth / nHeight ;
        p2 = ImgCls.OldWidth / ImgCls.OldHeight ;

        w = 0 ; h = 0 ;
        if( p1 < p2 )
        {
            //按宽度来计算新图片的宽和高
            w = nWidth ;
            h = nWidth * ( 1 / p2 ) ;
        }
        else
        {
            //按高度来计算新图片的宽和高
            h = nHeight ;
            w = nHeight * p2 ;
        }
        if(ImgCls.Obj.width >= nWidth && ImgCls.Obj.height>=nHeight)
             return;
        else
        {
            ImgCls.Obj.width  = w ;
            ImgCls.Obj.height = h ;
        }
    },
    
    //按给定的宽和高进行固定缩小(会出现图片变形情况)
    //by 西楼冷月 20080817 www.chinacms.org | qq : 39949376
    ResizedByWH : function ( nWidth , nHeight )
    {
        ImgCls.Obj.width  = nWidth ;
        ImgCls.Obj.height = nHeight ;
    },
    
    //按给定的宽进行等比例缩小
    ResizedByWidth : function ( nWidth )
    {
        if(ImgCls.Obj.width >  nWidth)
        {
            var p = nWidth / ImgCls.OldWidth ;
            ImgCls.Obj.width  = nWidth ;
            ImgCls.Obj.height = parseInt ( ImgCls.OldHeight * p ) ;
        }
    },
    
    //按给定的高进行等比例缩小
    ResizedByHeight : function ( nHeight )
    {
        var p = nHeight / ImgCls.OldHeight ;
        ImgCls.Obj.height  = nHeight ;
        ImgCls.Obj.width = parseInt ( ImgCls.OldWidth * p ) ;
    },
    
    //宽和高按百分比缩小
    ResizedByPer : function ( nWidthPer , nHeightPer )
    {
        ImgCls.Obj.width  = parseInt(ImgCls.OldWidth * nWidthPer / 100) ;
        ImgCls.Obj.height = parseInt(ImgCls.OldHeight * nHeightPer / 100) ;
    }
}
