var gallery = {
    index: 0,
    zIndex: 2,
    pictures: [],
    to: null,
    durationSec: 7,
    durationFadeMSec: 750,
    fadeCount: 0,
    currentImage: null,
    current: function() {
        return this.pictures[this.index];
    },
    init: function() {
        var _this = this,
            vCurImage,
            vFirst = true,
            vIndex = 0;

        $('#galleryImages').show();
        
        $('#imageGallery').children().each(function() {
            var vIsImage = $(this).attr('tagName').toUpperCase() == 'IMG';
            if(!vIsImage && !vCurImage)
                return;
            else if($(this).attr('tagName').toUpperCase() == 'P')
                $(this).css('padding-top', 17);

            if(vIsImage) {
                vCurImage = $('<div class="galleryImage"></div>');
                var vDot = $('<a href="#" class="naviDot"></a>');
                if(vFirst) {
//                    $(this).load(function() {

                        vCurImage.css('position', 'static');
//                        $('#imageGallery').css('height', vCurImage.height());
                        $('.imageNavi').css('margin-top', $(this).height() - 8);
//                    });
                    _this.currentImage = vCurImage;
//                    vCurImage.css('position', 'static');
                    vFirst = false;
                    vDot.addClass('current');
//                    $('#imageGallery').css('height', vNewImage.height());
                } else
                    vCurImage.hide();
                vCurImage.appendTo($('#galleryImages'));
                vCurImage[0].naviDot = vDot;
                vDot.appendTo($('.naviBG'));
                var vMyIndex = vIndex++;
                vDot.click(function() {
                    _this.next(vMyIndex);
                    return false;
                });
                _this.pictures.push(vCurImage);
            }

            $(this).appendTo(vCurImage);
        });
        if(this.pictures.length <= 1)
            return;

        $('.imageNavi').show();
        $('.imageNavi').css('margin-left', (238 / 2) - ($('.imageNavi').width() / 2));

        /*
        $('#bannerPrev').click(function() {
            _this.prev();
        });
        $('#bannerNext').click(function() {
            _this.next();
        });
        $('#bannerStop').click(function() {
            _this.stop();
        });
        */
        this.nextTO();
    },
    next: function(pIndex) {
        if(pIndex == undefined)
            this.index = (this.index + 1) % this.pictures.length;
        else
            this.index = pIndex;
        
        this.fadeIn(this.current());
        if(pIndex != undefined)
            this.nextTO();
    },
    nextTO: function() {
        clearTimeout(this.to);
        var _this = this;
        this.to = setTimeout(function() {
            _this.next();
        }, this.durationSec * 1000);
    },
    prev: function() {
        if(--this.index < 0)
            this.index = this.pictures.length - 1;
        this.fadeIn(this.current());
        this.prevTO();
    },
    prevTO: function() {
        clearTimeout(this.to);
        var _this = this;
        this.to = setTimeout(function() {
            _this.prev();
        }, this.durationSec * 1000);
    },
    stop: function() {
        clearTimeout(this.to);
    },
    fadeIn: function(pImage) {
        var vNewImage = pImage.clone();
        vNewImage.hide();
        var _this = this;
        var vPrevImage = this.currentImage;
        vPrevImage.fadeOut(_this.durationFadeMSec);
        $('#imageGallery').css('height', vPrevImage.height());
        $('.imageNavi').css('margin-top', vPrevImage.find('img').first().height() - 6);
        vPrevImage.css('position', 'absolute');
/*        $('#banner').addClass('loading');*/
//        vNewImage.load(function() {
/*            $('#banner').removeClass('loading');*/
            vNewImage.fadeIn(_this.durationFadeMSec, function() {
                if(vPrevImage)
                    vPrevImage.remove();
                if(vFadeCount != _this.fadeCount)
                    return;

                vNewImage.css('z-index', 1);
                _this.zIndex = 1;
            });
//        });
        vNewImage.appendTo('#galleryImages');
/*        vNewImage.attr('src', '/images/banner/' + lang + '/' + pSrc);*/
        vNewImage.css('z-index', ++this.zIndex);
        this.currentImage = vNewImage;
        var vFadeCount = ++this.fadeCount;

        $('#imageGallery').animate({
            height: vNewImage.height()
        }, _this.durationFadeMSec);

        var vMsecHalf = _this.durationFadeMSec / 4;
        $('.imageNavi').fadeOut(vMsecHalf, function() {
            $('.imageNavi').css('margin-top', vNewImage.find('img').height() - 6);
            $('.naviDot').removeClass('current');
            $(pImage[0].naviDot).addClass('current');
            $('.imageNavi').fadeIn(vMsecHalf);
        });
        
        /*
        $('.imageNavi').hide();
        $('.imageNavi').css('margin-top', vNewImage.find('img').height() - 6);
        $('.imageNavi').fadeIn(_this.durationFadeMSec);
        */
    }
};

$(window).load(function() {gallery.init();});
