﻿/*渐隐渐显广告图片切换类
三个参数：
    picsContId：广告图片所在的容器ID，必填
    arrLeftId：左切换按纽ID，不必填
    arrRightId：右切换按纽ID，不必填
HTML代码规则：每张广告图片必须且只能放在一个独立DIV容器中，
    然后将这些DIV容器包含在统一的一个容器中，
    将这个容器设置一个独立的ID，
    这个ID用于在创建此类时传入！
*/
function ShowAdPics(picsContId,arrLeftId,arrRightId)
{
    if(!ShowAdPics.childs)
	{
		ShowAdPics.childs = []
	};
	this.ID = ShowAdPics.childs.length;
	ShowAdPics.childs.push(this);
	if(picsContId == null || picsContId == undefined)
	{
	    return;
	}
	else if(typeof(picsContId) == "object")
	{
	    this.picsCont = picsContId;
	    this.picsContId = picsContId.id;
	}
	else
	{
	    this.picsContId = picsContId;
	    this.picsCont = document.getElementById(picsContId);
	}
	
	if(arrLeftId == null || arrLeftId == undefined)
	{
	    this.arrLeft = null;
	    this.arrLeftId = "";
	}
	else if(typeof(arrLeftId) == "object")
	{
	    this.arrLeft = arrLeftId;
	    this.arrLeftId = arrLeftId.id;
	}
	else
	{
	    this.arrLeftId = arrLeftId;
	    this.arrLeft = document.getElementById(arrLeftId);
	}
	
	if(picsContId == null || picsContId == undefined)
	{
	    this.arrRight = null;
	    this.arrRightId = "";
	}
	else if(typeof(arrRightId) == "object")
	{
	    this.arrRight = arrRightId;
	    this.arrRightId = arrRightId.id;
	}
	else
	{
	    this.arrRightId = arrRightId;
	    this.arrRight = document.getElementById(arrRightId);
	}
	this.picsContPics = this.picsCont.getElementsByTagName("div");
	this.picsContPicsCount = this.picsContPics.length;
	this.picsContPicsIndex = -1;
	this.picsContPicsIndexOld = -2;
	this.isAuto = true;
	
	//根据索引切换内容
	this.showPics = function(index)
	{
        if(this.picsContPicsCount == 0 || index == this.picsContPicsIndex)
            return;
        window.clearTimeout(this.picsCont.tmExpand);
        this.picsContPicsIndexOld = this.picsContPicsIndex;
        
        if(index  >= this.picsContPicsCount)
            this.picsContPicsIndex = 0;
        else if(index < 0)
            this.picsContPicsIndex = this.picsContPicsCount - 1;
        else
            this.picsContPicsIndex = index;

        this.initImgPic(this.picsContPicsIndex);
        this.initImgPic(this.picsContPicsIndex + 1);
        
        this.picsContPics[this.picsContPicsIndex].style.zIndex = 1;
        this.picsContPics[this.picsContPicsIndex].style.display = "";
        if(this.picsContPicsIndexOld >= 0 && this.picsContPicsIndexOld < this.picsContPicsCount)
        {
            fadeOut(this.picsContPics[this.picsContPicsIndexOld].id,1,20,"ShowAdPics.childs[" + this.ID + "].outPicCallback()");
        }
        else
            this.outPicCallback();
	}
    //内容渐隐结束后回调的方法
    this.outPicCallback = function()
    {
        this.picsContPics[this.picsContPicsIndex].style.zIndex = 2;
        if(this.picsContPicsIndexOld > -1 && this.picsContPicsIndexOld < this.picsContPicsCount)
            this.picsContPics[this.picsContPicsIndexOld].style.zIndex = 1;
        fadeIn(this.picsContPics[this.picsContPicsIndex].id,6,0,"ShowAdPics.childs[" + this.ID + "].inPicCallback()");
    }
    //内容渐显结束后回调的方法
    this.inPicCallback = function()
    {
        if(this.picsContPicsIndexOld > -1 && this.picsContPicsIndexOld < this.picsContPicsCount)
        {
            this.picsContPics[this.picsContPicsIndexOld].style.zIndex = 0;
            this.picsContPics[this.picsContPicsIndexOld].style.display = "none";
        }
        if(this.isAuto)
            this.picsCont.tmExpand = window.setTimeout("ShowAdPics.childs[" + this.ID + "].showPicsTimes();", 3000);
    }
    //自动切换方法
    this.showPicsTimes = function()
    {
        this.showPics(this.picsContPicsIndex + 1);
    }
    //初始化图片容器
    this.initImgPic = function(index)
    {
        if(index < this.picsContPicsCount && index >= 0 && this.picsContPics[index].id == "")
        {
            this.picsContPics[index].id = this.picsCont.id + "_" + index;
        }
    }
    if(this.picsContPicsCount > 0)
    {
        this.initImgPic(this.picsContPicsCount - 1);
        this.picsContPics[0].style.zIndex = 2;
        this.initImgPic(0);
        this.showPicsTimes();//开始自动切换
    }
    this.overPics = function()
    {
        window.clearTimeout(this.picsCont.tmExpand);
        this.isAuto = false;
    }
    this.outPics = function()
    {
        this.picsCont.tmExpand = window.setTimeout("ShowAdPics.childs[" + this.ID + "].showPicsTimes();", 3000);
        this.isAuto = true;
    }
    this.upPics = function()
    {
        this.showPics(this.picsContPicsIndex - 1);
    }
    this.nextPics = function()
    {
        this.showPics(this.picsContPicsIndex + 1);
    }
    this.picsCont.onmouseover = Function("ShowAdPics.childs[" + this.ID + "].overPics()");
    this.picsCont.onmouseout = Function("ShowAdPics.childs[" + this.ID + "].outPics()");
    if(this.arrLeft)
    {
        this.arrLeft.onclick = Function("ShowAdPics.childs[" + this.ID + "].upPics()");
    }
    if(this.arrRight)
    {
        this.arrRight.onclick = Function("ShowAdPics.childs[" + this.ID + "].nextPics()");
    }
}
//创建一个渐隐渐显广告图片切换类
var adPics1 = new ShowAdPics("div_index_top_pic","leftBtn","rigthBtn");
