var _fadeInId = 'mainBanner';
var _fadetime = 5000;
var _fadeSpeed = 8;

var _stats = new Array();
var _slideIndex = new Array();
var _slideIndexNext = new Array();
var _imageDivs = new Array();
var _currentOpacity = new Array();
var _imagesInGallery = new Array();
var _OperaFlg = navigator.userAgent.indexOf('Opera')>=0?true:false;
var _first = 1;

/* -----------------------------------------------------------------------*/
function createParentDivs(imageIndex,_IdName){
    if(imageIndex==_imagesInGallery[_IdName]){
        showGallery(_IdName);
    }else{
        var imgObj = document.getElementById(_IdName + '_' + imageIndex);
        if(_OperaFlg)imgObj.style.position = 'static';
        if(!_imageDivs[_IdName])_imageDivs[_IdName] = new Array();
        _imageDivs[_IdName][_imageDivs[_IdName].length] =  imgObj;
        imgObj.style.visibility = 'hidden';
        imageIndex++;
        createParentDivs(imageIndex,_IdName);
    }
}

/* -----------------------------------------------------------------------*/
function showGallery(_IdName){
    if(_slideIndex[_IdName]==-1)_slideIndex[_IdName]=0; else _slideIndex[_IdName]++;
    if(_slideIndex[_IdName]==_imageDivs[_IdName].length)_slideIndex[_IdName]=1;
    _slideIndexNext[_IdName] = _slideIndex[_IdName]+1;
    if(_slideIndexNext[_IdName]==_imageDivs[_IdName].length)_slideIndexNext[_IdName] = 1;

    _currentOpacity[_IdName]=100;

    // Displaying image divs
    _imageDivs[_IdName][_slideIndex[_IdName]].style.visibility = 'visible';
    if(_OperaFlg)_imageDivs[_IdName][_slideIndex[_IdName]].style.display = 'inline';
    if(navigator.userAgent.indexOf('Opera')<0){
        _imageDivs[_IdName][_slideIndexNext[_IdName]].style.visibility = 'visible';
    }

    if(document.all){// IE rules
        _imageDivs[_IdName][_slideIndex[_IdName]].style.filter = 'alpha(opacity=100)';
        _imageDivs[_IdName][_slideIndexNext[_IdName]].style.filter = 'alpha(opacity=1)';
    }else{
        _imageDivs[_IdName][_slideIndex[_IdName]].style.opacity = 0.99;
        _imageDivs[_IdName][_slideIndexNext[_IdName]].style.opacity = 0.01;
    }
    
    if (_first == 1){
        _first = 0;
        setTimeout('revealImage("' + _IdName + '")', 100);
    }else{
        setTimeout('revealImage("' + _IdName + '")',_fadetime);
    }
}

/* -----------------------------------------------------------------------*/
function revealImage(_IdName){
    _currentOpacity[_IdName] = _currentOpacity[_IdName] - 2;
    if(document.all){
        _imageDivs[_IdName][_slideIndex[_IdName]].style.filter = 'alpha(opacity='+_currentOpacity[_IdName]+')';
        _imageDivs[_IdName][_slideIndexNext[_IdName]].style.filter = 'alpha(opacity='+(100-_currentOpacity[_IdName])+')';
    }else{
        _imageDivs[_IdName][_slideIndex[_IdName]].style.opacity = Math.max(0.01,_currentOpacity[_IdName]/100);
        _imageDivs[_IdName][_slideIndexNext[_IdName]].style.opacity = Math.min(0.99,(1 - (_currentOpacity[_IdName]/100)));
    }
    if(_currentOpacity[_IdName]>0){
        setTimeout('revealImage("' + _IdName + '")',_fadeSpeed);
    }else{
        _imageDivs[_IdName][_slideIndex[_IdName]].style.visibility = 'hidden';
        if(_OperaFlg)_imageDivs[_IdName][_slideIndex[_IdName]].style.display = 'none';
        showGallery(_IdName);
    }
}

/* -----------------------------------------------------------------------*/
AddEvent(window, "load", function() {
    var _TargetObj = document.getElementById(_fadeInId);

    if (_TargetObj){
        _slideIndex[_fadeInId] = -1;
        _slideIndexNext[_fadeInId] = false;

        var _ImgArray = _TargetObj.getElementsByTagName('IMG');
        for(var no=0;no<_ImgArray.length;no++){
            _ImgArray[no].id = _fadeInId + '_' + no;
        }

        _imagesInGallery[_fadeInId] = _ImgArray.length;
        createParentDivs(0,_fadeInId);
    }
});

/* -----------------------------------------------------------------------*/
function AddEvent(targetObj, eventName, eventHandler) {
    if (document.addEventListener){
        targetObj.addEventListener(eventName, eventHandler, false);
        return;
    }
    if (document.attachEvent){
        targetObj.attachEvent("on"+eventName, eventHandler);
    }
} 