function dumpAll(obj) {
	dump("------- "+obj+" --------\n");
	for (v in obj) {
		dump(v+":"+obj[v]+"\n");
	}
}

$(document).ready(function () {

	var photo_dialog = new Ficia.UI.Dialog.Photo({
		user_id: user_id,
		viewer_user_id: viewer_user_id,
		photo: photo_list
	});

	var menuicon = new Ficia.Widget.MenuIcon({
		view: "member"
	});
	photo_list.menuicon_manager = album_list.menuicon_manager = menuicon;

	installAutoPager();
	installSlidebarDisplayControl();
	installFitPhotoMethods();
	installResizeMethods();

	installAccountMethods();
	installLogoutMethods();
	installLoginMethods();
	installCommentCheckboxMethods();
	installGoodCheckboxMethods();

	installOpenCloseListMethods();

	//toolbar in slideshow
	installBackToAlbumMethods();
	installSlideControlButtonsMethods();

	//balloon
	installBalloonMethods();

	// wait画像をクリックするとshow_photoの処理をやり直す
	$("#photo_image_loading_wait").click(function(e){
		e.preventDefault();
		photo_list.show_photo(photo_list.slide_photo_selected_index);
	});

	// account form
	$('#account-profile').click(function(e){
		e.preventDefault();
		$.dialog.close();
		$('#account-form2').dialog_open();
	});

	//album list を取り込む
	album_list.open_album_list("public");
	album_list.open_album_list("share");

	// for caption
	$('#photo-caption-content').click(function(e){
		e.preventDefault();
		$('#photo-caption-content').hide();
	});

	// for pubic album / private album switch
	$("#open-album2").click(function(e){
		e.preventDefault();
		$("#share_albums").hide();
		$("#public_albums").show();
		if ($("#open-share2:visible").length > 0) {
			$("#open-album2").addClass("enablemenu").removeClass("disablemenu");
			$("#open-share2").addClass("disablemenu").removeClass("enablemenu");
		} else {
			$("#open-album2").removeClass("disablemenu enablemenu");
		}
	}).addClass("active enablemenu disablemenu");
	$("#open-share2").click(function(e){
		e.preventDefault();
		$("#public_albums").hide();
		$("#share_albums").show();
		$("#open-album2").addClass("disablemenu").removeClass("enablemenu");
		$("#open-share2").addClass("enablemenu").removeClass("disablemenu");
	}).addClass("active enablemenu disablemenu");


	$("#open-album2").click();
});

$(window).resize(function () {
	if ($("#photolist_contents").css('display') == 'none') return;
	photo_list.resize();
});


function installAutoPager() {
	var photolist_viewer = $("#photolist_viewer_area");

	var load_timer_id  = null;
	var dummy_timer_id = null;
	photolist_viewer.scroll(function(){
		if (!photo_list.is_paging) return;
		// if (photo_list.album != null || photo_list.photocount == photo_list.total) {
		// 	return;
		// }
		if (photo_list.photocount == photo_list.total) {
			return;
		}


		if (load_timer_id) clearTimeout(load_timer_id);
		load_timer_id = setTimeout(function(){
			if (load_timer_id) clearTimeout(load_timer_id);
			photo_list.load_current_page();
		}, 300);


		if (dummy_timer_id) clearTimeout(dummy_timer_id);
		dummy_timer_id = setTimeout(function(){
			if (dummy_timer_id) clearTimeout(dummy_timer_id);
			photo_list.dummy_image_loading_on_scroll();
		}, 10);
	});
}

function installSlidebarDisplayControl() {
	return; // 休止中
	var is_open = true;
	var is_running = false;
	var running_wait = [false, false, false];
	var end_of_fadeto = function(i) {
		running_wait[i] = true;
		if (running_wait[0] && running_wait[1] && running_wait[2]) {
			is_running = false;
		}
	};
	var mouseover = function() {
		if (is_open || is_running) return;
		is_open = true;
		is_running = true;
		running_wait = [false, false, false];
		$("#slideshow_viewer").css({
			width: $("#slide_display").innerWidth() - 110
		});
		photo_list.show_slidebar();

		$("#slide_display").fadeTo("fast", 1, function(){end_of_fadeto(0)});
		$("#slideshow").fadeTo("fast", 1, function(){end_of_fadeto(1)});
		$("#slideshow_viewer").fadeTo("fast", 1, function(){end_of_fadeto(2)});
	};
	var mouseout = function(is_force) {
		// if ((!is_open || is_running) || is_force !== true) return;
		is_open = false;
		is_running = true;
		running_wait = [false, false, false];
		$("#slide_display").fadeTo("fast", 0, function(){end_of_fadeto(0)});
		$("#slideshow").fadeTo("fast", 0, function(){end_of_fadeto(1)});
		$("#slideshow_viewer").fadeTo("fast", 0, function(){end_of_fadeto(2)});
	};
	
	$("#slide_display").hover(mouseover, mouseout);
	mouseout();
}

function fitPhotoImage() {
	var target = $("#photo_image");
	var wOfTarget = target.attr("owidth");
	var hOfTarget = target.attr("oheight");
	if (!(wOfTarget && hOfTarget)) return;

	var container = $("#photodetail_contents");
	var wOfContainer = container.width();
	var hOfContainer = container.height();
	var hmax = hOfContainer-100;
	var wrate = wOfTarget / wOfContainer;
	var hrate = hOfTarget / hmax;
	if (wrate > hrate) {
		if (wOfTarget > wOfContainer) {
			hOfTarget = wOfContainer / wOfTarget * hOfTarget;
			wOfTarget = wOfContainer;
		}
	} else {
		if (hOfTarget > hmax) {
			wOfTarget = hmax / hOfTarget * wOfTarget;
			hOfTarget = hmax;
		}
	}
	var xOfTarget = (wOfContainer - wOfTarget) / 2;
	var yOfTarget = (hOfContainer - hOfTarget) / 2;
	target.width(wOfTarget);
	target.height(hOfTarget);
	$("#photo_image_container").css({'top':yOfTarget+'px','left':xOfTarget+'px'});

	// for wait image
	var photodetail_contents = $("#photodetail_contents");
	xOfTarget = (photodetail_contents.width()  / 2) - 32;
	yOfTarget = (photodetail_contents.height() / 2) - 32;
	$("#photo_image_loading_wait").css({'top':yOfTarget+'px','left':xOfTarget+'px'});
}

function installFitPhotoMethods() {
	$("#photo_image").load(function(){
		var target = $("#photo_image");
		if (target.attr("osrc") != target.attr("src")) {
			target.attr("owidth", target.width());
			target.attr("oheight", target.height());
			target.attr("osrc", target.attr("src"));
		}
		fitPhotoImage();
		photo_list.is_photo_loading = false;
		target.show();
	});
}

function installResizeMethods() {
	var fit = function() {
		var hOfContainer = 0;
		if (document.documentElement) {
			hOfContainer = document.documentElement.clientHeight;
		} else if (document.body) {
			hOfContainer = document.body.clientHeight;
		} else {
			hOfContainer = window.innerHeight;
		}
		hOfContainer -= 10;
		$(".left_viewer2").height(hOfContainer-163+20);
		$(".left").height(hOfContainer-108-13);
		$(".contents_area").height(hOfContainer-88);
		$("#photolist_viewer_area").height(hOfContainer-138);
	};
	$(window).resize(function(){
		fit();
		if ($("#photodetail_contents").css('display') == 'none') return;
		fitPhotoImage();
	});
	fit();
}

function installAccountMethods() {
	$("#account").click(function (e) {
		e.preventDefault();
		var nickname = $("#account-form-nickname").val(user_nickname);
		$("#account-card-input").hide();
		$.ajax({
			url : "/account/is_payer",
			type : "post",
			dataType: "json",
			cache: false,
			success : function(data){
				if (data.is_payer) $("#account-card-input").show();
			}
		});
		$("#account-form").dialog_open();
	});
	$("#account-form-cancel").click(function (e) {
		e.preventDefault();
		$.dialog.close();
	});
	$("#account-form-submit").click(function (e) {
		e.preventDefault();
		var nickname = $("#account-form-nickname").val();
		var data = {}; 
		data.nickname = nickname;

		var password = $("#account-form-password").val();
		var password2 = $("#account-form-password2").val();
		if (password != password2) {
			alert("２つのパスワードが違っています");
			return;
		}
		data.password = password;

		if (data.nickname.length === 0 && data.password.length === 0) {
			//変更点なし
			$.dialog.close();
			return;
		}
		var url = "/user/"+viewer_user_id+"/attribute";
		//var data = { nickname: nickname,password:password }; 
		$.ajax({
			url : url, 
			type : "put",
			data: data,
			dataType: "json",
			cache: false,
			error: function (xml, status, e) {
				alert("Server Error:" + status);
			},
			success : function(data){
				if (data.msg == "ng") {
					alert("アカウント情報の更新に失敗しました["+url+"]");
				}
				user_nickname = nickname;
				$.dialog.close();
			}
		});
	});
}

function installLogoutMethods() {
//    $("#logout").click(function (e) {
//        alert("まだログアウト未実装")
//        window.location.replace("../admin/");
//    });
}

function installLoginMethods() {
	$("#login").click(function (e) {
		e.preventDefault();
		$("#login-form").dialog_open();
	});
	$("#login-form-cancel").click(function (e) {
		e.preventDefault();
		$.dialog.close();
	});
	$("#login-form-submit").click(function (e) {
		e.preventDefault();
		$("#login-form-form").submit();
	});
}

//toolbar in slideshow
function installBackToAlbumMethods() {
	$("#back-album").click(function (e) {
		e.preventDefault();
		photo_list.end_photoviewer();
	});
	$("#photo_image").click(function (e) {
		e.preventDefault();
		photo_list.end_photoviewer();
	});
}

function installSlideControlButtonsMethods() {
	$("#btn_back").click(function (e) {
		e.preventDefault();
		photo_list.show_backphoto();
	});
	$("#btn_next").click(function (e) {
		e.preventDefault();
		photo_list.show_nextphoto();
	});
	return; // 休止中
	
	var len = Math.floor($("#slide_display").width() / 2.5);
	var intervaltimer;
	var slideshowviewer = $("#slideshow_photo_list");

	var btn_slide_l = $("#btn_slide_l");
	btn_slide_l.click(function(e) {
		e.preventDefault();
		photo_list.show_slidebar(1);
	});

	var btn_slide_r = $("#btn_slide_r");
	btn_slide_r.click(function(e) {
		e.preventDefault();
		photo_list.show_slidebar(-1);
	});
}

//--------------


//-------------
function installOpenCloseListMethods() {
	$("#btn_close_left_a").click(function (e) {
		e.preventDefault();
		$("#left").addClass("left_close");
		$("#contents_area").addClass("contents_area_wide");
		//$("#contents").addClass("contents_wide");
		$("#btn_close_left").hide();
		$("#btn_open_left").show();
	});
	$("#btn_open_left_a").click(function (e) {
		e.preventDefault();
		$("#left").removeClass("left_close");
		$("#contents_area").removeClass("contents_area_wide");
		//$("#contents").removeClass("contents_wide");
		$("#btn_close_left").show();
		$("#btn_open_left").hide();
	});
}

function getScrollHeight(v) {
	var sh=v.attr('scrollHeight');
	if (sh) {
		return sh;
	}
	var ch=v.attr('clientHeight');
	if (ch) {
		return ch;
	}
}

function installBalloonMethods() {
	balloon_install($('#create-album'), 'アルバム作成', 'l');
	balloon_install($('#share-album'), 'アルバム共有設', 'l');
	balloon_install($('#delete-album'), 'アルバム削除', 'l');
	balloon_install($('#info-album'), 'アルバム情報', 'l');


	balloon_install($('#upload-photo'), '写真をアップロードする', 'r');
	balloon_install($('#tag-photo'), '写真のタグを管理する', 'r');
	balloon_install($('#select-all'), '写真を全選択', 'r');
	balloon_install($('#trush-photos'), function(){
		if (photo_list.album === null) {
			return '写真をFiciaから削除';
		} else {
			return '写真をアルバムから削除';
		}
	}, 'r');

	balloon_install($('#good-onephoto'), 'グッド！', 'r');
	balloon_install($('#comment-onephoto'), 'コメント', 'r');
	balloon_install($('#tag-onephoto'), 'この写真のタグを管理する', 'r');
	balloon_install($('#photo-download'), 'オリジナル画像をダウンロードする', 'r');
	balloon_install($('#trush-onephoto'), function(){
		if (photo_list.album === null) {
			return '写真をFiciaから削除';
		} else {
			return '写真をアルバムから削除';
		}
	}, 'r');
}
