﻿// JScript 文件
// base on jQuery
// designed by MQ
// 2009.12.29

// last update 2010.01.16


//使用前需要设置 loading image 的路径
var loadingImg_path="../../images/loadingAnimation.gif";

var imgSrcArr, imgTar, _index, maxIndex, liArr;
var imgIndex=0;
var sto;

$(window).ready(function(){


///////////////////////
    if($.browser.msie)
        document.execCommand("BackgroundImageCache",false,true);
///////////////////////

    imgSrcArr=$('#j_imageChange input').val().split(',');
    imgTar=$('#imgMain');
    maxIndex=imgSrcArr.length;

    _index=0;
    
    //2010.01.17
    isLoadCompleted('first');//见下边  'param=first'  说明
    //
    
});


    function imgDuration(){
        sto=setTimeout('isLoadCompleted(\'undefined\')',5000);
    }
    
    function imgChangeIndex(){
        if(_index+1>maxIndex-1)
            _index=0;
        else
            _index++;
    }
        
    function isLoadCompleted(isIndex){
        var nextImg=new Image();
        if(isIndex==null||isIndex=='undefined'){
            imgChangeIndex();       
            imgTar.fadeTo(250,0,function(){
                if($.browser.version=='6.0')
                    imgTar.attr('src','');
                imageChangeBackground();
                $(nextImg).load(function(){
                    imgFadeIn();
                });
                nextImg.src=imgSrcArr[_index];
            });
        } else if(isIndex=='first'){    /* 'param=first'说明: 当浏览器载入页面后 下载第一张图片时（如果不添加该 else if 段落 全文多处需要更改 有空再做优化*/
            imgTar.fadeTo(0,0,function(){
                if($.browser.version=='6.0')
                    imgTar.attr('src','');
                imageChangeBackground();
                $(nextImg).load(function(){
                    imgFadeIn();
                });
                nextImg.src=imgSrcArr[_index];
            });
        } else {
            _index=isIndex;  
            imgTar.fadeTo(0,0,function(){
                if($.browser.version=='6.0')
                    imgTar.attr('src','');
                imageChangeBackground();
                $(nextImg).load(function(){
                    imgFadeIn(isIndex);
                });
                nextImg.src=imgSrcArr[_index];
            });
        }
    }
    
    function imgFadeIn(isIndex){
        imageChangeBackground_disappear();
        if(isIndex==null||isIndex=='undefined')
            imgFadeOut();
        else
            imgFadeOut(isIndex);
    }
    
    function imgFadeOut(isIndex){
        
        imgTar.fadeTo(1,0.1,function(){
            imgTar.attr('src',imgSrcArr[_index]);
            imgTar.fadeTo(250,1,function(){
                if(isIndex==null||isIndex=='undefined')
                    imgDuration();
            });
        });
    }
    
    
    ///////////////
    function imgFadeInOrOut(_in_or_out){
        if(_in_or_out=='in'){
            imgTar.fadeIn('slow',0.3,function(){
                imgFadeInOrOut('out');
            });
        } else {
            imgTar.attr('src',imgTar[1].attr('src'));
            imgTar.fadeOut('slow',1,function(){
                imgDuration();
            });
        }
    }
    ///////////////
    
    function imageChangeBackground(){
        $('#j_imageChange').css('backgroundImage','url('+loadingImg_path+')');
    }
    function imageChangeBackground_disappear(){
        $('#j_imageChange').css('backgroundImage','url()');
    }
    
