﻿var picPlayer = function (imageWrapper, imageBox, miniImageWrapper, miniImageBox, flagClass) {
    this.currentImage = 0; //å½“å‰çš„çŠ¶æ€[ä¹Ÿè¢«èµ‹å€¼ç»™æ”¹å˜åŽçš„çŠ¶æ€] 0è¡¨ç¤ºå½“å‰æ˜¾ç¤ºçš„æ˜¯ç¬¬1å¤§å›¾å’Œç¬¬1å°å›¾ï¼Œå…¶ä»–çš„éšè—æˆ–è€…åŠé€æ˜Ž
    this.beforeImage = null; //å˜åŒ–å‰çš„çŠ¶æ€
    this.ieOpacity1 = 0; //for IE
    this.ieOpacity2 = 100;
    this.currentPasuse = null;
    this.currentTimeout = null; //setTimeout
    this.currentInterval = null; //setTimeout	setTimeoutè¿˜æ˜¯é€’å½’è°ƒç”¨å¥½ç”¨å•Š

    this.images = document.getElementById(imageWrapper).getElementsByTagName(imageBox);
    this.miniImages = document.getElementById(miniImageWrapper).getElementsByTagName(miniImageBox);
    //this.explain=document.getElementById(explainWrapper).getElementsByTagName(explainBox);
    this.maxImagesNumber = this.miniImages.length; //å›¾ç‰‡æœ€å¤§æ•°é‡
    this.flagClass = flagClass;
};

picPlayer.prototype.current = function ()	//å½“å‰çš„çŠ¶æ€
{
    for (var i = 0; i < this.maxImagesNumber; i++)	//å½“å‰çš„å°å›¾ç‰‡çš„çŠ¶æ€
    {
        //imageBox.style.filter="alpha(opacity="+inteval+")";
        this.miniImages[this.currentImage].className = this.flagClass;
        //this.explain[this.currentImage].style.display="block";		
        if (i != this.currentImage) {
            this.miniImages[i].className = "";
            //this.explain[i].style.display="none";		
        }
    }

    for (var i = 0; i < this.maxImagesNumber; i++)	//å½“å‰çš„å¤§å›¾ç‰‡çš„çŠ¶æ€
    {
        this.images[this.currentImage].style.opacity = 1.0;
        this.images[this.currentImage].style.filter = "alpha(opacity=" + 100 + ")"; //IE
        this.images[this.currentImage].style.zIndex = 99;
        if (i != this.currentImage) {
            this.images[i].style.opacity = 0.0;
            this.images[i].style.filter = "alpha(opacity=" + 0 + ")";
            this.images[i].style.zIndex = "";
        }
    }
};

picPlayer.prototype.pasuse = function (time) {
    var self = this;
    this.currentPasuse = setTimeout(function () {
        self.timeout(time);
    }, time);
};

picPlayer.prototype.timeout = function (time)	//å°å›¾ç‰‡æ”¹å˜,å¤§å›¾ç‰‡æ”¹å˜
{
    var self = this;
    this.beforeImage = this.currentImage; //æŠŠå˜åŒ–å‰çš„çŠ¶æ€ä¿å­˜èµ·æ¥
    this.currentImage++; //è·³åˆ°ä¸‹ä¸€ä¸ªå›¾
    this.currentImage = this.currentImage < this.maxImagesNumber ? this.currentImage : 0;
    this.currentTimeout = setTimeout(function () { self.interval(); }, time);
};

picPlayer.prototype.interval = function ()	//æ¸å˜çš„è½¬åŒ–ï¼Œä»¥åŠä¸‹é¢å°å›¾ç‰‡çš„åˆ‡æ¢
{
    if (null == this.images[this.currentImage])
        return;
    if (this.images[this.currentImage].style.opacity == 1.0 || this.ieOpacity1 == 100) {
        clearTimeout(this.currentInterval);
        //å¤ä½
        this.ieOpacity1 = 0;
        this.ieOpacity2 = 100;

        //å°å›¾æ”¹å˜,å¤§å›¾zIndexå€¼çš„æ”¹å˜
        for (var i = 0; i < this.maxImagesNumber; i++) {
            this.miniImages[this.currentImage].className = this.flagClass;
            //this.explain[this.currentImage].style.display="block";
            this.images[this.currentImage].style.zIndex = 99;

            if (i != this.currentImage) {
                this.miniImages[i].className = "";
                this.images[i].style.zIndex = "";
            }
        }
        //ç»§ç»­ç­‰å¾…è§¦å‘
        this.timeout(10000);
        return;
    }
    var self = this;
    this.images[this.currentImage].style.opacity = parseFloat(this.images[this.currentImage].style.opacity) + 0.1; //éœ€è¦æ˜¾ç¤ºçš„é€æ¸å‡ºçŽ°
    //for IE
    this.ieOpacity1 += 10;
    this.images[this.currentImage].style.filter = "alpha(opacity=" + this.ieOpacity1 + ")";

    this.images[this.beforeImage].style.opacity = parseFloat(this.images[this.beforeImage].style.opacity) - 0.1; //éœ€è¦éšè—çš„é€æ¸æ¶ˆå¤±
    //for IE
    this.ieOpacity2 -= 10;
    this.images[this.beforeImage].style.filter = "alpha(opacity=" + this.ieOpacity2 + ")";

    this.currentInterval = setTimeout(function () { self.interval(); }, 50);
};

picPlayer.prototype.myMouse = function () {

    var self = this;
    for (var i = 0; i < this.maxImagesNumber; i++) {
        this.miniImages[i].onmouseover = function () {
            clearTimeout(self.currentPasuse);
            clearTimeout(self.currentTimeout);

            self.currentImage = parseInt(this.name) - 1; //èŽ·å–é¼ æ ‡é€‰åˆ°çš„å›¾ç‰‡çš„å€¼
            self.current();
        }
        this.miniImages[i].onmouseout = function () {
            self.pasuse(1000);
        }
    }
}
