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

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

//写真サムネイルを一番上までスクロールを戻す
function photoScrollReset() {
	photo_scroll.begin();
}

//指定されたURLの写真を中央に表示する
function showPhoto(url) {
	
    var img = new Image();
    
    $(img).load(function(){
        
        // 画像追加
        $(this)
            .css('z-index', 1000)
            .css('opacity', 0.0)
            .appendTo('#view');
        
        if(img.height < img.width) {
            //横長の画像の場合
            $(this)
                .attr('width', '600')
                .removeAttr('height');
            $(this).css('left', 0);
        } else {
            //縦長の画像の場合
            $(this)
                .attr('height', '500')
                .removeAttr('width');
            $(this).css('left', (600 - $(this).width()) / 2);
        }
        
        $('#main_image').remove();
        $('#sub_image').remove();
        
        if ( $('#view .active').size() > 0 ) {
            $('#view .active')
                .fadeTo(1000, 0.0, function(){ $(this).remove() })
                .css('z-index', 999);
            
            $(this)
                .addClass('active')
                .show()
                .fadeTo(1000, 1.0, function(){
                        if (typeof callback == 'function') {
                            callback(this);
                        }
                        // 高さを調整
                        var h = $('#view .active').height() - 0;
                        $('#view').height(h);

                    })
                .css('z-index', 1000);
            
        } else {
            // 最初の画像はそのまま表示
            $(this).addClass('active').css('z-index', 1000).css('opacity', 1.0).show();
            
            if (typeof callback == 'function') {
                callback(this);
            }
            
            // 高さを調整
            var h = $('#view .active').height() - 0;
            $('#view').height(h);
            
        }
    });
    
    img.src = url;
}

//指定されたアルバムに、矢印のマーカーを表示する
function showAlbumMarker(div) {
	showMaker(div, 'album_current');
}

//指定され写真に、矢印のマーカーを表示する
function showPhotoMarker(div) {
	showMaker(div, 'photo_current');
}

//マーカーを表示する
function showMaker(div, id) {
	$('#' + id).remove();
	div.append(
		$('<img>')
			.attr('id', id)
			.attr('class', 'current_marker')
			.attr('src', current_maker_image)  // FIXME: 画像パスの相対化
	);
}

//写真のタイトルを取得する
function setPhotoTitle($div) {
    
	//idの取得
	var id = $div.attr('id');
	
	//titleの取得
	getPhotoInfo(id, function(data) {
		var title = data.photo.title._content;
		$('#photo_title span').text(title);
	});
	
}

//写真を一つ戻す
function prev(e) {
	move(-1);
}

//写真を一つ進める
function next(e) {
	move(+1);
}

//写真を指定数ずらす
function move(num) {
	//photo_scroll.move(num);
	
	var current_index = 0 ;
	
	//現在選択されているサムネイルの位置を取得
	var thumbnails = $('#photo_scroll .items div');
	for(var i=0; i<thumbnails.length; i++) {
		var div = thumbnails.eq(i);
		if(div.css('opacity') == '1') {
			current_index = i;
			break;
		}
	}
	
	//移動先のサムネイルのクリック
	thumbnails.eq(current_index + num).click();
	
    // slide_time待つ。
	if (typeof timer_lock != "undefined") {
		timer_lock = true;
		jQuery.timer(slide_time, function(timer){
	        timer_lock = false;
	        timer.stop();
	    });
	}
}

//選択されたサムネイルを選択されている状態にする
function activate(div) {
	//透明化処理
	div.fadeTo('fast', 1).addClass('current');
	div.siblings().stop().fadeTo('fast', 0.3).removeClass('current');
	
	//一番上までスクロールさせる
	var scroll = $('#photo_scroll .items');
	scroll.animate({top: -(div.position().top)}, 'slow');
}

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

var album_scroll;
var photo_scroll;

function initScroll() {
	album_scroll = $("#album_scroll").scrollable({
		vertical:true, 
		size: 3
	}).mousewheel({api: true});

	photo_scroll = $("#photo_scroll").scrollable({
		vertical:true, 
		size: 3
	}).mousewheel({api: true});
}

$(function() {
	//写真下部ナビゲーション
	$('#photo_prev').click(prev);
	$('#photo_next').click(next);
	
	$('#album_controll')
		.click(function(){
			focusOnPhotoControll();
		})
		.hover(function(){
			focusOnAlbumControll();
	});
	
	$('#photo_controll')
		.hover(function(){
			focusOnPhotoControll();
	});
	
})