var PImgPlayer = {
        _timer : null,
        _items : [],
        _container : null,
        _index : 0,
        _imgs : [],
        intervalTime : 6000,        //輪播速度 越低越快
        init : function( objID, w, h, time ){
                this.objId = objID;
                this.iOpacity = 1;
                this.intervalTime = time || this.intervalTime;
                this._container = document.getElementById( objID );
                this._container.style.display = "block";
                this._container.style.width = w + "px";
                this._container.style.height = h + "px";
                this._container.style.position = "relative";
                this._container.style.overflow = "hidden";
                var linkStyle = "display: block; TEXT-DECORATION: none;opacity:1;";
                if( document.all ){
                        linkStyle += "FILTER:";
                        linkStyle += "progid:DXImageTransform.Microsoft.Fade(Duration=2)";  //淡入淡出
                }
                //
                var ulStyle = "margin:0 0 10px 0;width:"+w+"px;position:absolute;z-index:999;right:0px;FILTER:Alpha(Opacity=30,FinishOpacity=90, Style=1);overflow: hidden;bottom:-1px;height:16px; border-right:1px solid #fff;";
                var ulStyle2 = "margin:0;padding:5px 5px 0 5px;width:"+w+"px;position:absolute;;overflow: hidden;bottom:-1px;height:24px; border-right:1px solid #fff;font-size:14px;font-weight:bold;text-align:center;background:#fff;";
                //
                var liStyle = "margin:0;list-style-type: none; margin:0;padding:0; float:right;";
                //
                var baseSpacStyle = "clear:both; display:block; width:23px;line-height:18px; font-size:12px; opacity: 0.6;";
                baseSpacStyle += "border:1px solid #fff;border-right:0;border-bottom:0;";
                baseSpacStyle += "text-align:center; cursor:pointer; ";
                //
                var ulHTML = "";
                for(var i = this._items.length -1; i >= 0; i--){
                        var spanStyle = "";   
                        if( i==this._index ){
                                spanStyle = baseSpacStyle + "background:#9268A1;color:#FFFFFF;";
                        } else {
                                spanStyle = baseSpacStyle + "background:#DDDDDE;color:#000000;";
                        }
                        ulHTML += "<li style=\""+liStyle+"\">";
                        ulHTML += "<span onmouseover=\"PImgPlayer.mouseOver(this);\" onmouseout=\"PImgPlayer.mouseOut(this);\" style=\""+spanStyle+"\" onclick=\"PImgPlayer.play("+i+");return false;\" herf=\"javascript:;\">" + (i+1) + "</span>";
                        ulHTML += "</li>";
                }
                //
                var html = "<a href=\""+this._items[this._index].link+"\" target=\"_blank\" style=\""+linkStyle+"\"></a><ul style=\""+ulStyle+"\"> "+ulHTML+" </ul>";
                this._container.innerHTML = html;
                var link = this._container.getElementsByTagName("A")[0];
                link.style.width = w + "px";
                link.style.height = h + "px";
                link.style.background = 'url(' + this._items[0].img + ') no-repeat center top';
                //
                this._timer2 = setInterval( "PImgPlayer.fireFoxSpecial()", 4000 );
                this._timer = setInterval( "PImgPlayer.play()", this.intervalTime );
        },
        addItem : function(_link, _imgURL ){
                this._items.push ( {link:_link, img:_imgURL } );
                var img = new Image();
                img.src = _imgURL;
                this._imgs.push( img );
        },
        play : function( index ){
                clearInterval( this._timer2 );
                this._timer2 = setInterval( "PImgPlayer.fireFoxSpecial()", 4000 );
                if( index!=null ){
                        this._index = index;
                        clearInterval( this._timer );
                        this._timer = setInterval( "PImgPlayer.play()", this.intervalTime );
                } else {
                        this._index = this._index<this._items.length-1 ? this._index+1 : 0;
                }
                var link = this._container.getElementsByTagName("A")[0];
                link.style.opacity=1;
                if(link.filters){
                        var ren = Math.floor(Math.random()*(link.filters.length));
                        link.filters[ren].Apply();
                        link.filters[ren].play();
                }
                link.href = this._items[this._index].link;
               // link.title = this._items[this._index].title;
                link.style.background = 'url(' + this._items[this._index].img + ') no-repeat center top';

                //// title
                //var title = this._container.getElementsByTagName("h1")[0];
                //document.getElementById('tit').innerHTML = this._items[this._index].title;

                //
                var liStyle = "margin:0;list-style-type: none; margin:0;padding:0; float:right;";
                var baseSpacStyle = "clear:both; display:block; width:23px;line-height:18px; font-size:12px; opacity: 0.6;";
                baseSpacStyle += "border:1px solid #fff;border-right:0;border-bottom:0;";
                baseSpacStyle += "color:#fff;text-align:center; cursor:pointer; ";
                var ulHTML = "";
                for(var i = this._items.length -1; i >= 0; i--){
                        var spanStyle = "";
                        if( i==this._index ){
                                spanStyle = baseSpacStyle + "background:#9268A1;color:#FFFFFF;";
                        } else {
                                spanStyle = baseSpacStyle + "background:#DDDDDE;color:#000000;";
                        }
                        ulHTML += "<li style=\""+liStyle+"\">";
                        ulHTML += "<span onmouseover=\"PImgPlayer.mouseOver(this);\" onmouseout=\"PImgPlayer.mouseOut(this);\" style=\""+spanStyle+"\" onclick=\"PImgPlayer.play("+i+");return false;\" herf=\"javascript:;\">" + (i+1) + "</span>";
                        ulHTML += "</li>";
                }
                this._container.getElementsByTagName("UL")[0].innerHTML = ulHTML;
        },
        mouseOver : function(obj){
                var i = parseInt( obj.innerHTML );
                if( this._index!=i-1){
                        obj.style.color = "#ED1C24";
                }
        },
        mouseOut : function(obj){
                var i = parseInt( obj.innerHTML );
                if( this._index!=i-1){
                        obj.style.color = "#000000";
                }
        },
        fireFoxSpecial : function(){
            this.iOpacity-=0.0625;
            this._container.getElementsByTagName("A")[0].style.opacity=this.iOpacity;
            if (this.iOpacity==0){
                this.iOpacity=1;
                return;
            }else{
                setTimeout("PImgPlayer.fireFoxSpecial()",125);
            }
        }
}
