﻿function addLoadEvent(func){
	var oldonload=window.onload;
	if(typeof window.onload!='function'){
		window.onload=func;
	}else{
		window.onload=function(){
			oldonload();
			func();
		}
	}
}

function menuHover(elementId,tagName){
	var element=document.getElementById(elementId);
	if(!element)return false;
	var items=element.getElementsByTagName('li');
	var subItem;
	for(var i=0;i<items.length;i++){
		items[i].onmouseover=function(){
			this.className='hover';
			subItem=this.getElementsByTagName(tagName)[0];
			if (!subItem) return false;
			subItem.className='hover';
		}
		items[i].onmouseout=function(){
			this.className='';
			subItem=this.getElementsByTagName(tagName)[0];
			if (!subItem) return false;
			subItem.className='';
		}
	}
}

function scrollNews(selector,Entry,time,StartIndex)
{
	var _self=this;
	this.Selector=selector;
	this.Entry=Entry;
	this.time = time;
	this.i=StartIndex||0;
	this.Count=$(this.Selector+" ul li").length;
	$(this.Selector+" ul li").hide();//全部隐藏
	$(this.Selector+" ul li").eq(this.i).show();//第i个显示
	$(this.Selector).bind("mouseenter",function(){
	    	if(_self.sI){clearInterval(_self.sI);}
	}).bind("mouseleave",function(){
			_self.showIndex(_self.i++);
	})
	/*生成激活OL项目*/
	for(var j=0;j<this.Count;j++)
		$(this.Selector+" .activeOL").append('<li><a onclick="'+this.Entry+'.showIndex('+j+');" href="#"><img src="images/crystal.gif"></a></li>');
	$(this.Selector+" ol li a").eq(this.i).addClass("active");
	this.sI=setInterval(this.Entry+".showIndex(null)",this.time);
	
	this.GetSelector=function(){return this.Selector;}
	this.showIndex=function(index)
	{
		this.i++;//显示下一个
		if(this.sI){clearInterval(this.sI);}
		this.sI=setInterval(this.Entry+".showIndex()",this.time);
		if (index!=null)
		{
			this.i=index;
		}
		if(this.i==this.Count)
			this.i=0;
		$(this.Selector+" ul li").hide();
		$(this.Selector+" ul li").eq(this.i).slideDown();
		$(this.Selector+" ol li a").removeClass("active");
		$(this.Selector+" ol li a").eq(this.i).addClass("active");
	}
}
function selectGoUrl(elementId){
	var element=document.getElementById(elementId);
	if (!element) return false;
	element.onchange=function(){
		if (this.value=='') return false;
		var external = window.open(this.value);
		return external.closed;
	}
}

document.onclick = function(e)
{
  var target = e ? e.target : window.event.srcElement;
  while (target && !/^(a|body)$/i.test(target.nodeName))
  {
    target = target.parentNode;
  }
  if (target && target.getAttribute('rel'))
  {
  	if (target.rel == 'external'){
	    var external = window.open(target.href);
	    return external.closed;
    } else if (target.rel == 'setAsHomepage'){
    	//alert(target.href);
    	setAsHomepage(target,target.href);
    	return false;
    } else if (target.rel == 'addToBookmark'){
    	//alert(target.href);
    	addToBookmark(target.title,target.href,"");
    	return false;
    }
  }
}

function setAsHomepage(element,Url){
	try{
		element.style.behavior='url(#default#homepage)';element.setHomePage(Url);
	}
	catch(e){
		if(window.netscape) {
			try {
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
			}  
			catch (e) 
			{ 
				//alert("抱歉！您的浏览器不支持直接设为首页。请在浏览器地址栏输入“about:config”并回车然后将[signed.applets.codebase_principal_support]设置为“true”，点击“加入收藏”后忽略安全提示，即可设置成功。");
				return false;  
			}
			var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
			prefs.setCharPref('browser.startup.homepage',Url);
		}
	}
}

function addToBookmark(WebsiteName,Url,Description)   
{
	if (window.sidebar) {  
		window.sidebar.addPanel(WebsiteName,Url,Description); //Mozilla browser  
	} else if( document.all ) { //IE browser  
		window.external.AddFavorite(Url, WebsiteName);   
	} else if( window.opera && window.print ) { //Not Support Now  
		return true;  
	}  
} 


addLoadEvent(function(){menuHover('nav','ul')});

