﻿
//---------------------------------------------------
//      功能：圖片播放                              |        
//      By  ：nvay_chen 2008.12.16                  |
//---------------------------------------------------        
 
 //入口 
 var Index = {
        TimeSpace   : 2,            //時間間隔
        IsAutoPlay  : false,         //是否自動播放 true = 是，false = 否
        CurrIndex   : 0,            //當前圖片播放索引
        ImgCount    : 0,            //圖片數量
        ImgContent  : "AlbumImage", //圖片播放器容器
        TimeObj     : null,         //時間處理器
        IMGXML      : "",           //圖片XML
        WebPath     : "",           //頁面地址
        MeumBarID   : "mycarousel", //菜單控件條ID
        Start       : null,         //開始自動播放
        Stop        : null,         //停止
        Pause       : null,         //暫停   
        Clear       : null,         //清除時間處理器
        GoFirst     : null,         //轉到第幾項，isContinuePlay是否繼續播放
        IntiImg     : null,
        DrawImage   : null,          //按比例縮放圖片ImgD=圖片ID,imageWidth=圖片高,imageHeight=圖片寬 
        MouseClickGoNext :null,
        OpenNewWindow :   null   
        

    };
    
    Index.Files = [];
    Index.IntiImg = function Init(xml) {
        this.IMGXML = xml;
        var xDoc = Index.loadXML(this.IMGXML);
        var nodeList = xDoc.getElementsByTagName("DataRow");
        for (var i = 0; i < nodeList.length; i++) {
            var File = (nodeList[i].childNodes[0].firstChild) ? nodeList[i].childNodes[0].firstChild.nodeValue : "";
            var Alt = (nodeList[i].childNodes[1].firstChild) ? nodeList[i].childNodes[1].firstChild.nodeValue : "";
            var Img = { File: this.WebPath + File, Alt: Alt};
            Index.Files.push(Img);
        }
        this.ImgCount = this.Files.length;
        if (this.ImgCount > 0) {
            this.GoPage(this.CurrIndex);
            document.getElementById("spanImgCount").innerHTML = this.ImgCount;   
        }
        else {
            document.getElementById(this.ImgContent).style.display = "none";
        }          
    }
    
    
    Index.FillImg = function() {
        var imgContent = document.getElementById(this.ImgContent);
        imgContent.src = this.Files[this.CurrIndex].File;
        imgContent.alt = "下一張";
        document.getElementById("AlbumPiC_info").innerHTML = this.Files[this.CurrIndex].Alt;
        if (!this.IsAutoPlay) {
        }
        document.getElementById("hid_CurrPage").value = this.CurrIndex + 1; 
        document.getElementById("spanCurrIndex").innerHTML = this.CurrIndex + 1;                            
    }
    
    Index.FillMeumBar = function()
    {
        var obj_MeumBar = document.getElementById(this.MeumBarID);
        var arrMeumBars = [];
        for(var i=0; i<this.Files.length; i++)
        {
            arrMeumBars.push("<li><img onclick=\"Index.GoPage(" + i + ")\" src=\"" + this.Files[i].File + "\" alt=\"" + this.Files[i].Alt + "\" width=\"140\" height=\"105\" border=\"0\" /></li>");
        }
        obj_MeumBar.innerHTML = arrMeumBars.join("");
    }

    Index.GoPage = function(pageNum) {
        this.CurrIndex = pageNum;
        this.FillImg();
    }

    Index.PagePre = function() {
    this.IsAutoPlay = false;
    window.clearTimeout(this.TimeObj);
        if (this.CurrIndex > 0)
            this.CurrIndex--;
        this.FillImg();
    }

    Index.PageNext = function() {
        this.IsAutoPlay = false;
        window.clearTimeout(this.TimeObj);
        if (this.CurrIndex < this.ImgCount - 1)
            this.CurrIndex++;
        this.FillImg();
    }
    
    

    Index.AutoPaly = function() {
        window.clearTimeout(this.TimeObj);
        if (this.IsAutoPlay) {
            if (this.CurrIndex < this.ImgCount - 1) {
                this.CurrIndex++;
            }
            else {
                this.CurrIndex = 0;
            }
            this.GoPage(this.CurrIndex);
        }
        this.TimeObj = window.setTimeout("Index.AutoPaly()", this.TimeSpace * 1000);
    }
    
    Index.MouseClickGoNext = function()
    {
        if (this.CurrIndex < this.ImgCount - 1)
            this.CurrIndex++;
        this.FillImg();
        if(this.CurrIndex == this.ImgCount - 1)
        {
            if(document.getElementById(this.ImgContent))
            {
                document.getElementById(this.ImgContent).alt = "已經是最後一張";
            }
        }
            
    }
    
    Index.Start = function()
    {
        if(this.IsAutoPlay)
            this.AutoPaly();
    }
    
    Index.Pause = function()
    {
        window.clearTimeout(this.TimeObj);
    }
    
    Index.GoFirst = function(isContinuePlay)
    {
        this.CurrIndex = 0;
        if(this.IsAutoPlay)
             this.IsAutoPlay = isContinuePlay;
        this.GoPage(this.CurrIndex);
    }
    
    Index.Stop = function()
    {
        this.Pause();
        this.GoFirst;
    }
    
    Index.Clear = function()
    {
        this.Stop();
    }
    
    Index.OpenNewWindow = function(obj)
    {
        if(typeof(obj) == "string")
        {
            obj = document.getElementById(obj)
        }
        if(obj.tagName == "IMG")
        {
            window.open(obj.src,"_blank");
        }
    }
    
    
    //按比例縮放圖片ImgD=圖片ID,imageWidth=圖片高,imageHeight=圖片寬  
    //eg:<img src="URL" onload="javascript:DrawImage(this,80,80);" border="0" width="80" height="80" /> </img> 
    Index.DrawImage = function(ImgD,imageWidth,imageHeight) 
    { 
        var flag=false; 
        var image=new Image(); 
        image.src=ImgD.src; 
        if(image.width>0 && image.height>0)
        { 
            flag=true; 
            if(image.width/image.height>= imageWidth/imageHeight)
            { 
                if(image.width>imageWidth)
                { 
                    ImgD.width=imageWidth; 
                    ImgD.height=(image.height*imageWidth)/image.width; 
                }
                else
                { 
                    ImgD.width=image.width; 
                    ImgD.height=image.height; 
                } 
            } 
            else
            { 
                if(image.height>imageHeight)
                { 
                    ImgD.height=imageHeight; 
                    ImgD.width=(image.width*imageHeight)/image.height; 
                }
                else
                { 
                    ImgD.width=image.width; 
                    ImgD.height=image.height; 
                } 
            } 
        } 
    }
        
    

    

    function GoPre() {
        Index.PagePre();
    }

    function GoNext() {
        Index.PageNext();

    }
    
    function GoFirst()
    {
       Index.GoFirst(false);
    }
    
    function GoToPage(pageNum)
    {
        Index.IsAutoPlay = false;
        Index.GoPage(pageNum);
    }
    
    function AutoPlay() {
        Index.IsAutoPlay = !Index.IsAutoPlay;
        if(Index.IsAutoPlay)
            document.getElementById("aAutoPlay").innerHTML = "停止播放";
        else
            document.getElementById("aAutoPlay").innerHTML = "自動播放"; 
        Index.AutoPaly();
    }
    
    function ChangSpan(obj)
    {
        var span = obj.options[obj.selectedIndex].value;
        Index.TimeSpace = span;
    }
    
    
 Index.loadXML = function(strXml) {
    var xDoc;
    try {
        xDoc = new ActiveXObject("MSXML2.DOMDocument");
        xDoc.async = false;
        xDoc.loadXML(strXml);
        return xDoc;
    }
    catch (e) {
        try {
            var parser = new DOMParser();
            xDoc = parser.parseFromString(strXml, 'text/xml');
            return xDoc;
        }
        catch (e) {
        
            alert(e.message);
            return null;
        }
        
    }
  }
    