﻿// 等比缩放图片:<img onload="rs(this)" src=""/>
function rs(el){
//首先判断图片是长的还是宽的。如果是长的就对宽进行变形，否则对高进行变形
    (el.width>el.height+10)?el.width = Math.min(120,el.width):el.height = Math.min(110,el.height);
}

//Div等高：放在</body>后resetDiv("ID","ID","ID")
function resetDiv(){
	if(arguments.length > 1){
		var x = arguments.length;
		var elements=[];
		for(i=0;i<x;i++){
			elements.push(document.getElementById(arguments[i]).scrollHeight);
		}
		var imax = elements[0];
		for(i=0;i<x;i++){
			if(imax < elements[i]){
				imax = elements[i];
			}
		}
		for(i=0;i<x;i++){
			document.getElementById(arguments[i]).style.height = imax+"px";
		}
	}
}

//判断是否为中文
function isChinese(temp) 
{ 
var re = /[^\u4e00-\u9fa5]/;
//test() 方法用于检测一个字符串是否匹配某个模式.
if(re.test(temp)) 
    return false; 
return true; 
} 

//滚动
function DivScroll(tabid,tab1id,tab2id,direction){
	var speed=25; //数字越大速度越慢
	var _stop = true; //是否停止
	var tab= document.getElementById(tabid);
	var tab1 = document.getElementById(tab1id);
	var tab2 = document.getElementById(tab2id);
	tab2.innerHTML=tab1.innerHTML;
	switch (direction)
	{
		case "up" :
			var MyMar=setInterval(MarqueeUp,speed);   //定时滚动
			if(_stop){
			    tab.onmouseover=function() {clearInterval(MyMar)};
			    tab.onmouseout=function() {MyMar=setInterval(MarqueeUp,speed)};
			}
			break;
		default:
			var MyMar=setInterval(MarqueeLeft,speed);   //定时滚动
			if(_stop){
			    tab.onmouseover=function() {clearInterval(MyMar)};
			    tab.onmouseout=function() {MyMar=setInterval(MarqueeLeft,speed)};
			}
			break;
		break;
	}
	/*向左滚动*/
	function MarqueeLeft(){
		if(tab1.offsetWidth-tab.scrollLeft<=0)
			tab.scrollLeft-=tab1.offsetWidth;
		else{
			tab.scrollLeft++;
		}
	}
	/*向上滚动*/
	function MarqueeUp(){
 		if(tab2.offsetTop-tab.scrollTop<=0)
  			tab.scrollTop-=tab1.offsetHeight;
 		else{
  			tab.scrollTop++;
 		}
	}
}

//切换选项卡
function changeTab(group,num,sel)
{
	for(var i=0;i<num;i++) {
		var obj = document.getElementById(group+i.toString());
		if(i == sel) {
			obj.className = "sel";
			document.getElementById(group+"content"+i.toString()).style.display = "";
		}else {
			obj.className = "";
			document.getElementById(group+"content"+i.toString()).style.display = "none";
		}
	}
}

//加入收藏 - 使用方法：<a onclick="AddFavorite(window.location,document.title)">加入收藏</a> 
function AddFavorite(sURL, sTitle) {
    try {
        window.external.addFavorite(sURL, sTitle);
    }
    catch (e) {
        try {
            window.sidebar.addPanel(sTitle, sURL, "");
        }
        catch (e) {
            alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}

//设为首页 - 使用方法：<a onclick="SetHome(this,window.location)">设为首页</a> 
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("此操作被浏览器拒绝！请在浏览器地址栏输入\"about:config\"并回车然后将[signed.applets.codebase_principal_support]设置为'true'"); [Page]
            }
            var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
            prefs.setCharPref('browser.startup.homepage', vrl);
        }
    }
} 

//居中弹出页面
function OpenWindow(url, name, iWidth, iHeight) {
    var url;                             //转向网页的地址;
    var name;                            //网页名称，可为空;
    var iWidth;                          //弹出窗口的宽度;
    var iHeight;                         //弹出窗口的高度;
    //获得窗口的垂直位置
    var iTop = (window.screen.availHeight - 30 - iHeight) / 2;
    //获得窗口的水平位置
    var iLeft = (window.screen.availWidth - 10 - iWidth) / 2;
    window.open(url, name, 'height=' + iHeight + ',,innerHeight=' + iHeight + ',width=' + iWidth + ',innerWidth=' + iWidth + ',top=' + iTop + ',left=' + iLeft + ',status=no,toolbar=no,menubar=no,location=no,resizable=no,scrollbars=0,titlebar=no');
}
