/*焦点切换*/
(function(){
    if(!Function.prototype.bind){
        Function.prototype.bind = function(obj){
            var owner = this,args = Array.prototype.slice.call(arguments),callobj = Array.prototype.shift.call(args);
            return function(e){e=e||top.window.event||window.event;owner.apply(callobj,args.concat([e]));};
        };
    }
})();
var player = function(id){
    this.ctn = document.getElementById(id);
    this.adLis = null;
    this.btns = null;
    this.animStep = 0.2;//动画速度0.1～0.9
    this.switchSpeed = 3;//自动播放间隔(s)
    this.defOpacity = 1;
    this.tmpOpacity = 1;
    this.crtIndex = 0;
    this.crtLi = null;
    this.adLength = 0;
    this.timerAnim = null;
    this.timerSwitch = null;
    this.init();
};
player.prototype = {
    fnAnim:function(toIndex){
        if(this.timerAnim){window.clearTimeout(this.timerAnim);}
        if(this.tmpOpacity <= 0){
            this.crtLi.style.opacity = this.tmpOpacity = this.defOpacity;
            this.crtLi.style.filter = 'Alpha(Opacity=' + this.defOpacity*100 + ')';
            this.crtLi.style.zIndex = 0;
            this.crtIndex = toIndex;
            return;
        }
        this.crtLi.style.opacity = this.tmpOpacity = this.tmpOpacity - this.animStep;
        this.crtLi.style.filter = 'Alpha(Opacity=' + this.tmpOpacity*100 + ')';
        this.timerAnim = window.setTimeout(this.fnAnim.bind(this,toIndex),50);
    },
    fnNextIndex:function(){
        return (this.crtIndex >= this.adLength-1)?0:this.crtIndex+1;
    },
    fnSwitch:function(toIndex){
        if(this.crtIndex==toIndex){return;}
        this.crtLi = this.adLis[this.crtIndex];
        for(var i=0;i<this.adLength;i++){
            this.adLis[i].style.zIndex = 0;
        }
        this.crtLi.style.zIndex = 2;
        this.adLis[toIndex].style.zIndex = 1;
        for(var i=0;i<this.adLength;i++){
            this.btns[i].className = '';
        }
        this.btns[toIndex].className = 'on'
        this.fnAnim(toIndex);
    },
    fnAutoPlay:function(){
        this.fnSwitch(this.fnNextIndex());
    },
    fnPlay:function(){
        this.timerSwitch = window.setInterval(this.fnAutoPlay.bind(this),this.switchSpeed*1000);
    },
    fnStopPlay:function(){
        window.clearTimeout(this.timerSwitch);
    },
    init:function(){
        this.adLis = this.ctn.getElementsByTagName('li');
        this.btns = this.ctn.getElementsByTagName('cite')[0].getElementsByTagName('span');
        this.adLength = this.adLis.length;
        for(var i=0,l=this.btns.length;i<l;i++){
            with({i:i}){
                this.btns[i].index = i;
                this.btns[i].onclick = this.fnSwitch.bind(this,i);
                this.btns[i].onclick = this.fnSwitch.bind(this,i);
            }
        }
        this.adLis[this.crtIndex].style.zIndex = 2;
        this.fnPlay();
        this.ctn.onmouseover = this.fnStopPlay.bind(this);
        this.ctn.onmouseout = this.fnPlay.bind(this);
    }
};

/*
POWER BY XIZI:
class glide_menu INTRODUCE:
	param:menu, menu_tag, content, content_tag, style, time
	menu:菜单块id;
	menu_tag:菜单的子元素的标签名,如li,div,ul
	content:内容块的id
	content_tag:内容块的子元素的标签名,如li,div,ul
	style:当前菜单显示的样式名
	time:自动滑动的时间时隔,如3000 = 3秒，如不想自动滑动，为0

*/
(function(){
    if(!Function.prototype.bind){
        Function.prototype.bind = function(obj){
            var owner = this,args = Array.prototype.slice.call(arguments),callobj = Array.prototype.shift.call(args);
            return function(e){e=e||top.window.event||window.event;owner.apply(callobj,args.concat([e]));};
        };
    }
})();

var glide_menu = function (menu, menu_tag, content, content_tag, style, time){
	this.menu		= document.getElementById(menu).getElementsByTagName(menu_tag);
	this.content	= document.getElementById(content).getElementsByTagName(content_tag);
	this.style		= style;
	this.time		= time;
	this.num		= 0;
	this.current_id = 0;
	this.isstop		= 0;
	this.timerAnim	= null;
	this.init();
};
glide_menu.prototype = {
	stop:function(toIndex)
	{
		this.current_id = toIndex;
		if(this.current_id >= this.num)
		{
			this.current_id = 0;
		}
		this.isstop = 1;
		this.change();
	},
	goon:function()
	{
		this.isstop = 0;
		this.change();
	},
	change:function()
	{
		if(this.timerAnim){window.clearTimeout(this.timerAnim);}
		for(var i = 0; i < this.num; i ++)
		{
			this.menu[i].className = '';
			this.content[i].style.display = 'none';
		}
		this.menu[this.current_id].className = this.style;
		this.content[this.current_id].style.display = '';
		if(this.time != 0 && !this.isstop)
		{
			this.current_id ++;
			if(this.current_id >= this.num)
			{
				this.current_id = 0;
			}
			this.timerAnim = window.setTimeout(this.change.bind(this),this.time);//如果是写成this.stop()，即出错，不明白是怎么回事，WHY?
		}
	},
	init:function()
	{
		
		if(this.menu.length != this.content.length)
		{
			alert('菜单栏的块数量跟内容栏的块的数量不一致');
			return false;
		}
		else
		{
			this.num = this.menu.length;
		}
		
		for(i = 0; i < this.num; i ++)
		{
			this.menu[i].onmouseover = this.stop.bind(this,i);   //如果是写成this.stop.bind(i)，即出错，不明白是怎么回事，WHY?
			this.menu[i].onmouseout = this.goon.bind(this);
		}
		this.change();
	}
}
/*伸缩广告*/
var intervalId = null; 
function slideAd(id,nStayTime,sState,nMaxHth,nMinHth)
{ 
	this.stayTime=nStayTime*1000 || 3000; 
	this.maxHeigth=nMaxHth || 299; 
	this.minHeigth=nMinHth || 2.5; 
	this.state=sState || "down" ; 
	var obj = document.getElementById(id); 
	if(intervalId != null)window.clearInterval(intervalId); 
	function openBox()
	{ 
		var h = obj.offsetHeight; 
		obj.style.height = ((this.state == "down") ? (h + 10) : (h - 10))+"px"; 
		if(obj.offsetHeight>this.maxHeigth)
		{ 
			window.clearInterval(intervalId); 
			intervalId=window.setInterval(closeBox,this.stayTime); 
		} 
		if (obj.offsetHeight<this.minHeigth)
		{ 
			window.clearInterval(intervalId); 
			obj.style.display="none"; 
		} 
	} 
	function closeBox()
	{ 
		slideAd(id,this.stayTime,"up",nMaxHth,nMinHth); 
	} 
	intervalId = window.setInterval(openBox,15); 
}
