//photosetの情報を保存しておく配列
var photoset = Array();
var api;

/**
 * 切替時間 (ms)
 */
var slide_time = 5 * 1000;

/**
 * 初回時の切替時間 (ms)
 */
var first_slide_time = slide_time * 2;

/**
 * タイマー処理のロック用
 */
var timer_lock = false;

/**オートスライド処理*/
function setAutoSlide() {
	jQuery.timer(first_slide_time, function(timer){
		
		if (timer_lock) {
		    return false;
		}
		
        // intervalの変更
        if (timer.interval != slide_time) {
            timer.reset(slide_time);
        }
		
		$('#photo_next').click();
		focusOnPhotoControll();
	});
}

//サムネイルがクリックされたときの処理を行う
function thumbnailClick(e) {
	activate($(this));
	
	//写真を表示する
	var url = $(this).children('img').attr('id');
	showPhoto(url);
	
	setPhotoTitle($(this));
	showPhotoMarker($(this));
	
	// first_slide_time待ってからタイマーロッックを解除
    if (typeof timer_lock != "undefined") {
        timer_lock = true;
        jQuery.timer(first_slide_time, function(timer){
            timer_lock = false;
            timer.stop();
        });
    }
}

//アルバムにフォーカスを合わせる
function forcusAlbum(album) {
	activate(album);
	
	//フォトセットから写真一覧を取得し、サムネイルを表示する
	var index = album.attr('id');
	var set = photoset[index].photoset;
	
	//既に表示されているサムネイルを削除
	$('#photo_scroll .items').empty();
	
	//サムネイルを取得、設定
	for(var i=0; i<set.photo.length; i++) {
		//写真のURL
		var url = getPhotoUrl(set.photo[i]);
		//サムネイル画像のURL
		var resizeUrl = getResizeImageUrl('65', url);
		
		var div = $('<div>').append(
				$('<img>')
					.attr('src', resizeUrl)
					.attr('id', url)
					.css('width', 65)
		)
		.attr('class', 'photo')
		.attr('id', set.photo[i]['id'])
		.css('opacity', 0.3)
		.click(thumbnailClick);
		setThumbnailHover(div);
		
		$('#photo_scroll .items').append(div);
	}
	
	showAlbumMarker(album);
	photoScrollReset();
	
    // 一番最初の写真をクリック
    if ($('#photo_scroll .items').size() > 0) {
        $('#photo_scroll .items div:first').click();
    }
}

//アルバムがクリックされたときの処理を行う
function clickAlbum() {
	forcusAlbum($(this));
}

//ギャラリーに表示するアルバムをセットする
function setGalleryAlbum(album) {
	if(!album)return;
	
	var div = $('<div>')
		.attr('class', 'album')
		.css('opacity', 0.3)
		.click(clickAlbum);
	
	setThumbnailHover(div);
	
	$('#album_scroll div.items:first').append(div);
	
	//Photosetの情報を取得して
	getPhotosFromPhotoSet(album.id, function(data){
		
		//アルバムの表紙絵を設定する
		var img = $('<img>')
			.css('width', '65px')
			.css('height', '40px')
			.attr('src', getResizeImageUrl('65', getPhotoUrl(getPrimaryPhoto(data))));
		div
			.attr('id', photoset.length)
			.append(img)
			.append($('<span>').text(album.title));

		// loading画像の消去
		if ($('#album_scroll .loading').size() > 0) {
		    $('#album_scroll .loading').remove();
		    $('#album_controll').height(20);
		    $('#album_scroll').height(0);
		}
		
		if ( $('#album_scroll').height() < 400 ) {
         	// srcrollの高さ調整
    	    var appendHeight = div.height() + 10;
    	    $('#album_controll').height($('#album_controll').height() + appendHeight);
    	    $('#album_scroll').height($('#album_scroll').height() + appendHeight);
		}
		
		//photosetの情報を保存しておく
		photoset.push(data);
		
		if (photoset.length == 1) {
    		// 最初のアルバムであればクリック
    	    forcusAlbum(div);
		}
	});
};

function setGallery(data) {
	for(var i=0; i<data.length; i++) {
		setGalleryAlbum(data[i]);
	}
};

function initGallery() {
	$.getJSON('./galleries/', setGallery);
};

$(function() {
	$('#album_next').click(
		function() {
			$("div.scrollable").scrollable({api: true}).next();
		}
	);
	
	initScroll();
	initGallery();
	setAutoSlide();
});