var is_full_display = false;
if (typeof(Ficia) == 'undefined') Ficia = {};
Ficia.KeyBind = function(args){
};

(function(){
var is_editfield = false;
var editfield_enable  = function(){ is_editfield = true  };
var editfield_disable = function(){ is_editfield = false };

Ficia.appendIgnoreKeybindDom = function(dom){
	dom.focus(editfield_enable).blur(editfield_disable);
};

$(document).ready(function(){
	Ficia.appendIgnoreKeybindDom($('input'));
	Ficia.appendIgnoreKeybindDom($('textarea'));

	$(document).keydown(function(e){
		if (e.metaKey) return;
		if (is_editfield) return;
		if ($ && $.dialog && $.dialog.target) return; // modal dialog が開いてる時は無視
		if ($("#photodetail_contents").css('display') != 'none' && photo_list) {
			switch (e.keyCode) {
				case 37:
				case 72: {
					photo_list.show_backphoto();
					break;
				}
				case 39:
				case 76: {
					photo_list.show_nextphoto();
					break;
				}
				case 38:
				case 85: {
					photo_list.end_photoviewer();
					break;
				}
				case 70: {
					if (is_full_display) {
						is_full_display = false;
					} else {
						is_full_display = true;
						$('#btn_close_left_a').click();
					}
					fitPhotoImage();
					break;
				}
				case 73: {
					// open info
					e.preventDefault();
					$("#preview-photo-info").click();
					break;
				}
				case 67: {
					// open comment
					e.preventDefault();
					$("#comment-onephoto").click();
					break;
				}
				default: { break; }
			}
			if (e.shiftKey) {
				switch (e.keyCode) {
					case 80: {
						// print
						$("#print-onephoto").click();
						break;
					}
					case 68: {
						// delete
						$("#trush-onephoto").click();
						break;
					}
					default: { break; }
				}
			}
		} else if ($("#photolist_contents").css('display') != 'none' && photo_list && photo_list.current_focus_id !== null) {
			var move = function(id, last_id){
				if (!photo_list.photo_list[id]) return;
				if (!photo_list.photo_list[id].img) return;

				if (e.shiftKey) {
					// shift key 経由の時はハンドリングを自前で行っておく
					if (photo_list.key_select_actions_shift_on === false) 
						photo_list.key_select_actions_shift_on = last_id;

					photo_list.select_actions = [];
					photo_list.addSelectAction({
						first_id: photo_list.key_select_actions_shift_on,
						last_id:  id,
						type:     photo_list.SELECT_ACTION_CLICK_WITH_SHIFT
					});
					photo_list.mark_showing_photos_if_selected();
					photo_list.current_focus_id = id;
				} else {
					photo_list.key_select_actions_shift_on = false;
					photo_list.photo_list[id].img.click();
				}


				setTimeout(function(){photo_list.centering(id);}, 100);
//				photo_list.resize();
			};
			switch (e.keyCode) {
				case 13: {
					// goto preview
					photo_list.start_photoviewer();
					photo_list.show_photo(photo_list.current_focus_id);
					break;
				}
				case 37:
				case 72: {
					var id = photo_list.current_focus_id;
					move(id - 1, id);
					break;
				}
				case 39:
				case 76: {
					var id = photo_list.current_focus_id;
					move(id + 1, id);
					break;
				}
				case 38:
				case 75: {
					var id = photo_list.current_focus_id;
					move(id - photo_list.thumnail_cols, id);
					break;
				}
				case 40:
				case 74: {
					var id = photo_list.current_focus_id;
					move(id + photo_list.thumnail_cols, id);
					break;
				}
				case 73: {
					// open info
					e.preventDefault();
					$("#photo-info").click();
					break;
				}
				default: { break; }
			}
			if (e.shiftKey) {
				switch (e.keyCode) {
					case 80: {
						// print
						$("#print-photos").click();
						break;
					}
					case 68: {
						// delete
						$("#trush-photos").click();
						break;
					}
					default: { break; }
				}
			}
		}
	});
});
}());