
function js_include(_path){
    _path = _path.toString();
    var _elm = document.createElement('script');
    _elm.type = 'text/javascript';
    _elm.charset = 'utf-8';
    _elm.src = _path;
    var _head = document.getElementsByTagName('head')[0];
    _head.appendChild(_elm);
}

/*-----------------------------------------------
	top
-----------------------------------------------*/
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

/*-----------------------------------------------
	top random image script
-----------------------------------------------*/

var _fadeInClass = 'mainVisual';
var _fadetime = 5000;
var _fadeSpeed = 10;

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;
/* -----------------------------------------------------------------------*/
document.getElementsByClassName = function (className) {
    var i, j, eltClass;
    var AllObj   = document.getElementsByTagName ? document.getElementsByTagName("*") : document.all;
    var ClassObj = new Array();
    for (i = 0; i < AllObj.length; i++) {
        eltClass = AllObj[i].className.split(/\s+/);
        for (j = 0; j < eltClass.length; j++) {
            if (eltClass[j] == className) {
                ClassObj.push(AllObj[i]);
                break;
            }
        }
    }
    return ClassObj;
}
/* -----------------------------------------------------------------------*/
function AddEvent(targetObj, eventName, eventHandler) {
    if (document.addEventListener){
        targetObj.addEventListener(eventName, eventHandler, false);
        return;
    }
    if (document.attachEvent){
        targetObj.attachEvent("on"+eventName, eventHandler);
    }
}

/* -----------------------------------------------------------------------*/
function createParentDivs(imageIndex,_IdName){
    if(imageIndex==_imagesInGallery[_IdName]){
        showGallery(_IdName, 1);
    }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 get_next_Id(_now, _max){
    if (_max < 2 ){
        return _now;
    }
    var _next = Math.floor(Math.random()*_max);
    while (1) {
        if (_now != _next){
            return _next;
        }
        _next = Math.floor(Math.random()*_max);
    }
}

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

    _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;
    }
    
    setTimeout('revealImage("' + _IdName + '")',_fadetime);

    return 1;
}

/* -----------------------------------------------------------------------*/
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)));
    }

    var ActiveObj = _imageDivs[_IdName][_slideIndexNext[_IdName]].parentNode;
    if (ActiveObj.tagName == "A" ){
       ActiveObj.style.display = 'inline';
    }

    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';

        var ActiveObj = _imageDivs[_IdName][_slideIndex[_IdName]].parentNode;
        if (ActiveObj.tagName == "A" ){
           ActiveObj.style.display = 'none';
        }
        showGallery(_IdName, 0);
    }
}


/* -----------------------------------------------------------------------*/
AddEvent(window, "load", function() {
    var _Banners = document.getElementsByClassName(_fadeInClass);
    for (var i=0; i<_Banners.length; i++) {
        var _TargetObj = _Banners[i];
        if (_TargetObj){
            var _fadeInId = _fadeInClass + '_' + i;
            _slideIndexNext[_fadeInId] = false;
            var _ImgArray = _TargetObj.getElementsByTagName('IMG');
            var _first = Math.floor(Math.random()*_ImgArray.length);
            _slideIndex[_fadeInId] = _first;
            for(var no=0;no<_ImgArray.length;no++){
                _ImgArray[no].id = _fadeInId + '_' + no;
                if (no == _first){
                    var AObj = _ImgArray[no].parentNode;
                    if (AObj.tagName == "A" ){
                        AObj.style.display = 'inline';
                    }
                    _ImgArray[no].style.visibility = 'visible';
                }else{
                    var AObj = _ImgArray[no].parentNode;
                    if (AObj.tagName == "A" ){
                        AObj.style.display = 'none';
                    }
                    _ImgArray[no].style.visibility = 'hidden';
                }
            }
            _imagesInGallery[_fadeInId] = _ImgArray.length;
            createParentDivs(0,_fadeInId);
        }
    }
});


/*-----------------------------------------------
	pageTop
-----------------------------------------------*/
js_include('/js/smoothScroll.js');
