function _initGalleria() {
    jQuery(function($) {
        // galleria
        $('ul.gallery').galleria({
            history: false,
            clickNext : true,
            onImage: function(image,caption,thumb) {
//                if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) // FF/Win fades large images terribly slow
//                    image.css('display','none').fadeIn(1000);
                image.attr('title','Next image >>');
                if ( image.attr('width') > image.attr('height') ) { // change margin
                    $('img.replaced').css({'margin-left':'14px','margin-top':'80px'});
                }
                thumb.parent('li').addClass('active');
//                thumb.parent('li').parent('ul').children('li').children('img').fadeTo('fast',0.3);
//                thumb.fadeTo('fast',1);
                thumb.parent('li').parent('ul').children('li').children('img').css({'opacity':0.3});
                thumb.css({'opacity':1});
            },
            onThumb : function(thumb) {
                var _li = thumb.parents('li');
                var _fadeTo = _li.is('.active') ? '1' : '0.3';
                thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
                thumb.css({opacity:_fadeTo});
                thumb.hover(
//                    function() { thumb.fadeTo('fast',1); },
//                    function() { _li.not('.active').children('img').fadeTo('fast',0.3); }
                    function() {
                        thumb.css({ 'opacity':1 });
                        thumb.parent('li').css({'cursor':'normal'});
                        thumb.parent('li').not('.active').css({
                            'background-image':'url(http://www.elegant-apps.com/images/model/select.png)',
                            'background-color':'#403e3c',
                            'cursor':'pointer'
                        });
                    },
                    function() {
                        _li.not('.active').children('img').css({'opacity':0.3});
                        _li.css({ 'background-image':'', 'background-color':'' });
                    }
                );
            }
        });
    });
}

function _initGalleryArea() {
    jQuery(function($) {
        // galleriaArea
        var anchors = $('#content').find('a');
        for ( var i = 0; i < anchors.length; i++ ) {
            var anchor = anchors[i];
            var href = anchor.getAttribute('href');
            if ( href && href.match(/jpg$|gif$|png$/) ) {
                anchor.setAttribute('rel', href);
                $(anchor).click(function() {
                    egalleryShow(this.href);
                    return false;
                });
            }
        }
    });
}

function _createOverlay() {
    // backGround
    var bgWidth = ( $('body').width() > $('#container').width() ) ? $('body').width() : $('#container').width();
    var bgHeight = ( $('body').height() > $('#container').height() ) ? $('body').height() : $('#container').height();
    $('body').append(
        $('<div id="galleryOverlay"></div>')
        .css({
            'position':'absolute','top':'0px','left':'0px', 'z-index':'9999',
            'width': bgWidth,
            'height': bgHeight,
            'background-color':'#000000','opacity':'0.7'
        })
        .hide()
    );
    $('#galleryOverlay').click(function() { egalleryClose(); });
}

function _createGallery(rel) {
    // galleriaArea
//    $('#galleriaArea').append( $('<a title="close" id="TB_closeWindowButton" href="#">閉じる</a>').click(function() { egalleryClose(); return false;} ));
    $('#galleriaArea').append( $('<div id="galleria_header">&nbsp;</div>') );
    $('#galleriaArea').append( $('<div id="ul"></div>').append ( $('<ul></ul>').addClass('gallery').addClass('clearfix') ) );
    $('#galleriaArea').append( $('<div id="galleria_footer">&nbsp;</div>') );
    $('#galleriaArea').append( $('<a title="close" id="TB_closeWindowButton" href="#">閉じる</a>').click(function() { egalleryClose(); return false;} ));

    var anchors = $('#content').find('a');
    var deposB = []; // big thumbnail
    var deposS = []; // small thumbnail
    for ( var i = 0; i < anchors.length; i++ ) {
        var anchor = anchors[i];
        var href = anchor.getAttribute('href');
        if ( href.match(/jpg$|gif$|png$/) ) {
            var w = 0;
            var s = '';
            var imgs =  $(anchor).children('img');
            for ( var j = 0; j < imgs.length; j ++ ) {
                if ( $(imgs[j]).attr('class') != 'plus' ) {
                    w = parseInt( $(imgs[j]).width() );
                    s = href.replace(/l\.png$/, 's.png');
//                    s = $(imgs[j]).attr('src');
                    break;
                }
            }
//            var w = parseInt( $(anchor).children('img').width() );
//            var s = $(anchor).children('img').attr('src');
            if ( s == '' ) { s = href; }
            if ( w > 30 )
                deposB.push({'href':href,'thumb':s});
            else
                deposS.push({'href':href,'thumb':s});

//            if ( href == rel )
//                $('#galleriaArea ul.gallery').append( $('<li class="active"></li>').append( $('<img class="noscale" src="'+href+'" title="" width="30" height="30" />') ) );
//            else
//                $('#galleriaArea ul.gallery').append( $('<li></li>').append( $('<img class="noscale" src="'+href+'" title="" width="30" height="30" />') ) );

        }
    }
    var depos = deposB.concat(deposS);
    for ( var i = 0; i < depos.length; i++ ) {
        var href = depos[i];
        if ( href.href == rel ) {
            $('#galleriaArea ul.gallery').append(
                $('<li class="active"></li>').append(
                    $('<a href="'+href.href+'"></a>').append(
                        $('<img class="noscale" src="'+href.thumb+'" title="" width="30" height="30" />') 
                    )
                ) 
            );
        }
        else {
            $('#galleriaArea ul.gallery').append(
                $('<li></li>').append(
                    $('<a href="'+href.href+'"></a>').append(
                        $('<img class="noscale" src="'+href.thumb+'" title="" width="30" height="30" />') 
                    )
                ) 
            );
        }
    }
}

function egalleryShow(rel) {
    // create overlay
    _createOverlay();

    // galleria
    _createGallery(rel);
    _initGalleria();

    var w = $('#galleriaArea').width() || 508;
    var h = $('#galleriaArea').height() || 554;

    var marginL = parseInt(( $(window).width() - w ) / 2);
    var marginT = parseInt(( $(window).height() - h ) / 2);

    if ( marginL <= 0 ) { marginL = 20; }
    if ( marginT <= 0 ) { marginT = 20; }
    marginL += $(window).scrollLeft();
    marginT += $(window).scrollTop();

    // show
    $('select').hide();
    $('#galleryOverlay').fadeIn('fast',function() {
        $('#galleriaArea').css({
            'position':'absolute',
            'top': parseInt(marginT + ( h / 2 ))+'px',
            'left':marginL + 'px',
            'width': w + 'px',
            'height': '0px',
            'z-index':'10000'
        })
        .animate({
            'height': h + 'px',
            'top': marginT + 'px'
        })
        .show();
        if ( $.browser.opera && $('img.replaced').width() > $('img.replaced').height() ) {
            $('img.replaced').css({'margin-left':'14px','margin-top':'80px'});
        }
    });
    document.onkeydown = function(e) {
        var keycode;
        if ( e == null ) // ie
            keycode = event.keyCode;
        else
            keycode = e.which;
        if ( keycode == 27 ) // esc
            egalleryClose();
    };
    document.onkeyup = function(e) {
        var keycode;
        if ( e == null ) // ie
            keycode = event.keyCode;
        else
            keycode = e.which;
        if ( keycode == 27 ) // esc
            egalleryClose();
    };
    document.onkeypress = function(e) {
        var keycode;
        if ( e == null ) // ie
            keycode = event.keyCode;
        else
            keycode = e.which;
        if ( keycode == 27 ) // esc
            egalleryClose();
    };

    return false;
}

function egalleryClose() {
    try {
        var marginT = parseInt( $('#galleriaArea').css('top').replace(/px/,'') );
//        var h = $('#galleriaArea').height() || 554;
//        var marginT = parseInt(( $(window).height() - h ) / 2);
//        if ( marginT <= 0 ) { marginT = 20; }
//        marginT += $(window).scrollTop();

        $('#galleriaArea')
        .animate({
            'height': '0px',
            'top': parseInt( marginT + $('#galleriaArea').height() / 2 )+'px'
        }, 500, function () {
            $('#galleriaArea').hide();
            $('#galleryOverlay').fadeOut('fast',function() {
                $('#galleriaArea').children().remove();
                $('body').children().remove('#galleryOverlay');
            });
        });
//        .hide();

    }
    catch(e) {
    }
    document.onkeydown = '';
    document.onkeyup = '';
    document.onkeypress = '';
    $('select').show();
    return false;
}


function initGallery() {
    _initGalleryArea();
}

