(function($){

    $.gallery = {
        slideTime: 5000, // 切替時間 (ms)
        firstSlideTime: 10000, // 初回時の切替時間 (ms)
        resizeImageURI: './image_filter/resize.php',
        currentMarkerImage: './img/common/current_marker.gif'
    };


    $.gallery.setSlideTime = function(time) {
        $.gallery.slideTime = time;
    };

    $.gallery.setFirstSlideTime = function(time) {
        $.gallery.firstSlideTime = time;
    };

    $.gallery.setResizeImageURI = function(path) {
        $.gallery.resizeImageURI = path;
    };

    $.gallery.setCurrentMakerImage = function(path) {
        $.gallery.currentMarkerImage = path;
    };

    /**
     * photoオブジェクトからリサイズ画像URIの作成
     */
    $.gallery.getResizeImageUrl = function(size, url){
        return $.gallery.resizeImageURI + '?width=' + size + '&src=' + encodeURI(url);
    };


    //写真一覧にフォーカスを移す
    $.gallery.focusOnPhotoControll = function() {
        $('#scroll').animate({
            left: '-50px'
        }, 'slow');
    };

    //アルバム一覧にフォーカスを移す
    $.gallery.focusOnAlbumControll = function() {
        $('#scroll').animate({
            left: '0px'
        }, 'slow');
    };

    //写真を一つ戻す
    $.fn.prevPhoto = function() {
        $(this).movePhoto(-1);
    };

    //写真を一つ進める
    $.fn.nextPhoto = function() {
        $(this).movePhoto(+1);
    };

    //写真を指定数ずらす
    $.fn.movePhoto = function(num) {

        var current_index = 0 ;
        var thumbnails = $('.items div', this);

        //現在選択されているサムネイルの位置を取得
        current_index = $('.items .current', this).index();

        //移動先のサムネイルのクリック
        thumbnails.eq(current_index + num).click();

        // slide_time待つ。
        $(this).lockAutoSlide();
    };

    /**
     * オートスライド
     */
    $.fn.autoSlide = function(time, firstTime){

        var context = this;
        $(this).data('autoSlideTime', time);

        if ($(this).data('autoSlide')) {
            return $(this).data('autoSlide');
        }

        var autoSlideTimer = $.timer(firstTime, function(timer){

            // intervalの変更
            if (timer.interval != time) {
                timer.reset(time);
            }

            $('#photo_next').click();
            $.gallery.focusOnPhotoControll();

        });

        $(this).data('autoSlide', autoSlideTimer);

        return autoSlideTimer;
    };

    /**
     * 指定時間オートスライドを停止する
     */
    $.fn.lockAutoSlide = function(lockTime){

        if (lockTime == null) {
            lockTime = $(this).data('autoSlide').interval;
        }
        $(this).data('autoSlide').reset(lockTime);

    };

    /**
     * 写真のタイトルを取得する
     */
    $.fn.setPhotoTitle = function(id, callback) {
        callback = callback || function(){};
        //titleの取得
        var target = this;
        $.flickr.getPhotoInfo(id, function(data) {
            $(target).text(data.photo.title._content);
            callback();
        });

        return this;
    };

    /**
     * 指定されたURLの写真を中央に表示する
     */
    $.fn.showPhoto = function(url, callback){

        var img = new Image();
        var target = this;

        $(img).load(function(){

            // 画像追加
            $(img)
            .css('z-index', 1000)
            .css('opacity', 0.0)
            .appendTo(target);

            if(img.height < img.width) {
                //横長の画像の場合
                $(img)
                .attr('width', '600')
                .removeAttr('height');
                $(img).css('left', 0);

            } else {
                //縦長の画像の場合
                $(img)
                .attr('height', '500')
                .removeAttr('width');
                $(img).css('left', (600 - $(img).width()) / 2);
            }

            $('#main_image').remove();
            $('#sub_image').remove();

            if ( $('.active', target).size() > 0 ) {
                // 表示中の画像をフェードアウト
                $('.active', target)
                .fadeTo(1000, 0.0, function(){
                    $(this).remove()
                })
                .css('z-index', 999);

                if (typeof callback == 'function') {
                    callback(this);
                }

                // この画像をプライマリに
                $(img)
                .addClass('active')
                .show()
                .fadeTo(1000, 1.0, function(){
                    // 高さを調整
                    var h = $('.active', target).height() - 0;
                    $(target).height(h);

                })
                .css('z-index', 1000);

            } else {

                // 最初の画像はそのまま表示
                $(img).addClass('active').show().css('opacity', 1.0).css('z-index', 1000);

                if (typeof callback == 'function') {
                    callback(img);
                }

                // 高さを調整
                var h = $('.active', target).height() - 0;
                $(target).height(h);

            }
        });

        img.src = url;
    };


    /**
     * サムネイル画像をマウスオーバーしたときのイベントを設定する
     */
    $.fn.setThumbnailHover = function(){
        return $(this).hover(
            function() {
                $(this).css('opacity', '1.0');
            },
            function() {
                if(!$(this).hasClass('current')) {
                    $(this).css('opacity', '0.3');
                }
            });
    };

    /**
     * 選択されたサムネイルを選択されている状態にする
     */
    $.fn.activateThumbnail = function(target){

        // 透明化処理
        $(this).fadeTo('fast', 1).addClass('current');
        $(this).siblings().stop().fadeTo('fast', 0.3).removeClass('current');

        // 一番上までスクロールさせる
        $(target).data('scrollable').seekTo($(this).index(), 'slow');
    };

    /**
     * マーカーを表示する
     */
    $.fn.showMarker = function(){
        $('.current_marker', $(this).parent()).remove();
        return this.append(
            $('<img>')
            .addClass('current_marker')
            .attr('src', $.gallery.currentMarkerImage)
            );
    };

    //写真サムネイルを一番上までスクロールを戻す
    $.fn.resetScroll = function(){
        this.data('scrollable').begin();
    };

})(jQuery);

jQuery(document).ready(function(){

    //写真下部ナビゲーション
    $('#photo_prev').click(function(){
        $('#photo_scroll').prevPhoto();
        return false;
    });
    $('#photo_next').click(function(){
        $('#photo_scroll').nextPhoto();
        return false;
    });

    $('#album_controll')
    .click(function(){
        $.gallery.focusOnPhotoControll();
    })
    .hover(function(){
        $.gallery.focusOnAlbumControll();
    });

    $('#photo_controll')
    .hover(function(){
        $.gallery.focusOnPhotoControll();
    });

});
