(function($) {
    $.fn.mygallery = function() {
        return this.each(function() {   
            $.mygallery();
        });
    };
    
    $.mygallery = function()
    {
//        $.easing.def = 'linear';
        var settings = {
            changeGalleryType:  'slide',
            changeGallerySec:   1000,
            changePhotoType:    'fade',
            changePhotoSec:     1000,
            listBorderON:          '#000000',
            listBorderOFF:         '#d6d6d6',
            opacityON:          1,
            opacityOFF:         0.7
        };
        
        $currentGID = $('#galleryThumbs .carouselGallery:visible').attr('id').split('_')[1];
        $currentPID = $('#galleryPhoto table:visible img').attr('id').split('_')[1];
        
        
        
        $('#galleryCtrl p').hover(
            function () {
                $(this).css('color', '#999999');
            },
            function () {
                $(this).css('color', '#414140');
            }
        );
        
        $('#prevPhoto').click(function () {
            $prevPID = $('#galleryPhoto_'+$currentPID).parents('table').prev('table').find('img').attr('id').split('_')[1];
            $prevGID = $('#galleryThumbP_'+$prevPID).parents('.carouselGallery').attr('id').split('_')[1];
            $.mygallery.doCtrl($prevGID, $prevPID, settings);
            $currentGID = $prevGID;
            $currentPID = $prevPID;
            $.mygallery.setCtrl($currentPID, settings);
        });
        $('#nextPhoto').click(function () {
            $nextPID = $('#galleryPhoto_'+$currentPID).parents('table').next('table').find('img').attr('id').split('_')[1];
            $nextGID = $('#galleryThumbP_'+$nextPID).parents('.carouselGallery').attr('id').split('_')[1];
            $.mygallery.doCtrl($nextGID, $nextPID, settings);
            $currentGID = $nextGID;
            $currentPID = $nextPID;
            $.mygallery.setCtrl($currentPID, settings);
        });
        $.mygallery.setCtrl($currentPID, settings);
        
        
        if(/*@cc_on!@*/false){
            $('#galleryPhoto img').each(function () {
                $(this).parents('td').css('filter', 'shadow(color=#999999,direction=135,strength=8)');
            });
            $('#galleryThumbs img').each(function () {
                $(this).parents('td').css('filter', 'shadow(color=#999999,direction=135,strength=2)');
            });
        }
        
        $('#galleryThumbs img').each(function () {
            $(this).css('cursor', 'pointer');
            
            $.mygallery.effectThumbDefault($(this), settings);
            $gid = $(this).parents('.carouselGallery').attr('id').split('_')[1];
            if ($(this).attr('id').split('_')[1] == $('#galleryPreview_'+$gid).attr('value'))
                    $.mygallery.effectThumbCurrent($(this), settings);
            
            $(this).hover(
                function () {
                    $.mygallery.effectThumbCurrent($(this), settings);
                },
                function () {
                    if ($(this).attr('id').split('_')[1] != $currentPID)
                            $.mygallery.effectThumbDefault($(this), settings);
                }
            );
            
            $(this).click(function(){
                $pid = $(this).attr('id').split('_')[1];
                if ($pid != $currentPID) {
                    $currentPID = $pid;
                    $('#galleryPreview_'+$currentGID).attr('value', $pid);

                    $.mygallery.changePhoto($pid, settings, settings.changePhotoType);

                    $gid = $(this).parents('.carouselGallery').attr('id').split('_')[1];
                    $.mygallery.effectThumbDefault($('#galleryThumb_'+$gid+' img'), settings);
                    if ($pid == $('#galleryPreview_'+$gid).attr('value'))
                            $.mygallery.effectThumbCurrent($(this), settings);
                    
                    $.mygallery.setCtrl($currentPID, settings);
                }
            });
        });

        $('#galleryList img').each(function () {
            $(this).css('cursor', 'pointer');

            $.mygallery.effectListDefault($(this), settings);
            if ($(this).attr('id').split('_')[1] == $currentGID) {
                $.mygallery.effectListCurrent($(this), settings);
                
                $.mygallery.showInfo($(this), settings);
            }

            $(this).hover(
                function () {
                    $.mygallery.effectListCurrent($(this), settings);
                },
                function () {
                    if ($(this).attr('id').split('_')[1] != $currentGID)
                            $.mygallery.effectListDefault($(this), settings);
                }
            );

            $(this).click(function(){
                $gid = $(this).attr('id').split('_')[1];
                if ($gid != $currentGID) {
                    $pid = $('#galleryPreview_'+$gid).attr('value');

                    //require scrollTo
//                    $.scrollTo('#galleryMain', 500);
//                    $.scrollTo('h2.jisseki', 500);
                    $.scrollTo('#galleryMain', {
                        duration: 300,
                        onAfter: function(){
                            $.mygallery.changeGallery($gid, settings, settings.changeGalleryType);
                            $.mygallery.changePhoto($pid, settings, settings.changeGalleryType);
                        }
                    });
                    
//                    $.mygallery.changeGallery($gid, settings, settings.changeGalleryType);
//                    $.mygallery.changePhoto($pid, settings, settings.changeGalleryType);

                    $.mygallery.effectListDefault($('#galleryList img'), settings);
                    $.mygallery.effectListCurrent($(this), settings);
                    
                    $.mygallery.showInfo($(this), settings);
                    
                    $currentGID = $gid;
                    $currentPID = $pid;
                    
                    $.mygallery.setCtrl($currentPID, settings);
                }
            });
        });
    };


    $.mygallery.effectListDefault = function($elm, settings)
    {
//        $elm.css({'opacity': settings.opacityOFF});
        $elm.css({'border-color': settings.listBorderOFF});
    }

    $.mygallery.effectListCurrent = function($elm, settings)
    {
//        $elm.css({'opacity': settings.opacityON});
        $elm.css({'border-color': settings.listBorderON});
    }

    $.mygallery.effectThumbDefault = function($elm, settings)
    {
//        $elm.css({'opacity': settings.opacityOFF});
        $elm.css({
            '-moz-box-shadow': '2px 2px 2px #666',
            '-webkit-box-shadow': '2px 2px 2px #666',
            'box-shadow': '2px 2px 2px #666'
        });
    }

    $.mygallery.effectThumbCurrent = function($elm, settings)
    {
//        $elm.css({'opacity': settings.opacityON});
        $elm.css({
            '-moz-box-shadow': '2px 2px 2px #000',
            '-webkit-box-shadow': '2px 2px 2px #000',
            'box-shadow': '2px 2px 2px #000'
        });
    }


    $.mygallery.setCtrl = function($pid, settings)
    {
//        if ($('#galleryPhoto_'+$pid).parents('table').is("#galleryPhoto table:first-child")) {
//            $('#prevPhoto').hide();
//            $('#nextPhoto').show();
//        } else if ($('#galleryPhoto_'+$pid).parents('table').is("#galleryPhoto table:last-child")) {
//            $('#prevPhoto').show();
//            $('#nextPhoto').hide();
//        } else {
//            $('#prevPhoto').show();
//            $('#nextPhoto').show();
//        }
    }

    $.mygallery.doCtrl = function($gid, $pid, settings)
    {
        $('#galleryPreview_'+$gid).attr('value', $pid);
        if ($currentGID != $gid) {
            $.mygallery.changeGallery($gid, settings, settings.changeGalleryType);
            $.mygallery.changePhoto($pid, settings, settings.changeGalleryType);
            
            $.mygallery.effectListDefault($('#galleryList img'), settings);
            $.mygallery.effectListCurrent($('#galleryList_'+$gid), settings);
            
            $.mygallery.showInfo($('#galleryList_'+$gid), settings);
        } else {
            $.mygallery.changePhoto($pid, settings, settings.changePhotoType);
        }
        $.mygallery.effectThumbDefault($('#galleryThumb_'+$gid+' img'), settings);
        if ($pid == $('#galleryPreview_'+$gid).attr('value'))
                $.mygallery.effectThumbCurrent($('#galleryThumbP_'+$pid), settings);
    }


    $.mygallery.showInfo = function($elm, settings)
    {
        var info = $elm.attr('longdesc').split('##');
        var info_l = info[0].split('%%');
        
        $('#galleryInfo_l_1').html(info_l[0]);
        $('#galleryInfo_l_2').html(info_l[1]);
        $('#galleryInfo_l_3').html(info_l[2]);
        $('#galleryInfo_r').html(info[1]);
    }


    $.mygallery.changeGallery = function($gid, settings, type)
    {
        switch(type) {
            case 'normal':
                $('#galleryThumbs .carouselGallery:visible').hide();
                $('#galleryThumbs #galleryThumb_'+$gid).show();
                break;
            case 'slide':
                $('#galleryThumbs .carouselGallery:visible')
                        .css('z-index', 1)
                        .slideUp(settings.changeGallerySec);
                $('#galleryThumbs #galleryThumb_'+$gid)
                        .css('z-index', 10)
                        .slideDown(settings.changeGallerySec);
                break;
            case 'fade':
                $('#galleryThumbs .carouselGallery:visible').fadeOut(settings.changeGallerySec);
                $('#galleryThumbs #galleryThumb_'+$gid).fadeIn(settings.changeGallerySec);
                break;
            default:
                $('#galleryThumbs .carouselGallery:visible').hide();
                $('#galleryThumbs #galleryThumb_'+$gid).show();
                break;
        }
    };

    $.mygallery.changePhoto = function($pid, settings, type)
    {
        switch(type) {
            case 'normal':
                $('#galleryPhoto table:visible').hide();
                $('#galleryPhoto img#galleryPhoto_'+$pid).parents('table').show();
                break;
            case 'slide':
//                $tar = $('#galleryPhoto table:visible');
//                $tar.css('z-index', 1)
//                .slideUp(settings.changePhotoSec, function(){$('img', $tar).hide();});
//                
//                $('#galleryPhoto img#galleryPhoto_'+$pid).show();
//                $('#galleryPhoto img#galleryPhoto_'+$pid).parents('table')
//                        .css('z-index', 10)
//                        .slideDown(settings.changePhotoSec);
                $('#galleryPhoto table:visible')
                        .css('z-index', 10)
                        .slideUp(settings.changePhotoSec);
                $('#galleryPhoto img#galleryPhoto_'+$pid).parents('table')
                        .css('z-index', 1)
                        .slideDown(settings.changePhotoSec);
                break;
            case 'fade':
                $('#galleryPhoto table:visible').fadeOut(settings.changePhotoSec);
                $('#galleryPhoto img#galleryPhoto_'+$pid).parents('table')
                        .fadeIn(settings.changePhotoSec);
                break;
            default:
                $('#galleryPhoto table:visible').hide();
                $('#galleryPhoto img#galleryPhoto_'+$pid).parents('table').show();
                break;
        }
    };

})(jQuery);


