$(function() {
	setMenuSide();
	$('#maintabs').tabs();
	$('#ranking').tabs();
	$('#keywordtab').tabs();
	$(window).resize(wresize);


	$("#dialog").dialog({
		//オプション設定
		draggable: true,
		resizable: false,
		autoOpen: false,
		modal: false,
		buttons:{
			'登録':function(){
				$.ajax({
					type: "POST",
					url: "ajaxfunction.php",
					data:
					{
						"keyword": $('#addDialogKeyword').val(),
						"type"   : $('#addDialogType').val(),
						"mode"   : "addKeyword"
					},
					success: function(msg){
						if(msg == "false"){
							alert("ユーザー登録/ログインが必要な機能です。");
						}else{
							$('#keywordtab').tabs('select',0);
							$('#aboutKeyword').html("登録が完了しました。");
						}
						$('#dialog').dialog('close');
					}
				});
			},
			'やめる':function(){
				$('#dialog').dialog('close');
			}
		}
	});

	$("#fix").dialog({
		//オプション設定
		draggable: true,
		resizable: false,
		autoOpen: false,
		modal: false,
		buttons:{
			'修正依頼':function(){
				$.ajax({
					type: "POST",
					url: "ajaxfunction.php",
					data:
					{
						"book_title"   : $('#fixTitle').val(),
						"release_date" : $('#fixDate').val(),
						"release_word" : $('#fixWord').val(),
						"book_uid"     : $('#fixid').val(),
						"price"     : $('#fixPrice').val(),
						"type"         : $('#fixType').val(),
						"mode"         : "fixBookData"
					},
					success: function(msg){
						alert(msg);
						$('#fix').dialog('close');
					}
				});
			},
			'やめる':function(){
				$('#fix').dialog('close');
			}
		}
	});

	$('#buttonopen').click(function() {
		$('#dialog').dialog('open');
	});

	$('#loginformsubmit').submit(function(){
		$('#loginform').slideUp();
		$('#login').slideDown("slow",function(){
			$.ajax({
				type: "POST",
				url: "ajaxfunction.php",
				data:
				{
					"regUserId" : $('#regUserId').val(),
					"regUserPassword" : $('#regUserPassword').val(),
					"mode"   : "userLogin"
				},
				success: function(msg){
					if(msg == "loginOk"){
						/*
						$('#login').slideUp();
						$('#loginlist').slideDown();
						*/
						window.location.reload();
					}else{
						$('#login').slideUp();
						$('#loginform').slideDown();
						$('#loginerror').html("<br />ログインに失敗しました。<br />ID,パスワードを確認してください。");
					}
				}
			});
		});
		return false;
	});

	
	$('.getBookList').click(function(){
		$('#maintabs').tabs('select',1);
		setKeywordTab($(this).attr("type"));
		$('#home').html('<img src="images/ajax-loader2.gif" />Now Loading');
		$.ajax({
			type: "POST",
			url: "ajaxfunction.php",
			data:
			{
				"date"   : $(this).attr("date"),
				"type"   : $(this).attr("type"),
				"mode"   : $(this).attr("mode")
			},
			success: function(msg){
				$('#home').html(msg);
				resetEditareaSize('#home');
				resetEditareaSize("#keywordtab");
			}
		});
	});

	$('.getBookUserList').click(function(){
		$('#maintabs').tabs('select',2);
		$('#checkList').html('<img src="images/ajax-loader2.gif" />Now Loading');

		if($('#bought').attr('checked')){
			var bought = $('#bought').val();
		}else{
			var bought = 0;
		}

		if($('#unread').attr('checked')){
			var read = $('#unread').val();
		}else{
			var read = 0;
		}
		
		var order = $("#order").val();
		
		$.ajax({
			type: "POST",
			url: "ajaxfunction.php",
			data:
			{
				"offset" : $(this).attr("offset"),
				"type"   : $(this).attr("type"),
				"mode"   : $(this).attr("mode"),
				"read"   : read,
				"bought" : bought,
				"order"  : order
			},
			success: function(msg){
				$('#checkList').html(msg);
				resetEditareaSize('#checkList');
				resetEditareaSize("#keywordtab");
			}
		});
	});

	$('#contents').show();
});

function wresize(){
	resetEditareaSize("#home");
	resetEditareaSize("#checkList");
	resetEditareaSize("#keywordtab");
}

function toggleMenuSide(){
	if($('.menu').css('float') == 'right'){
		$('.menu').css('float','left');
	}else{
		$('.menu').css('float','right');
	}
}

function setMenuSide(){
	//幅が1100以下の場合、左にメニューを持ってきます。
	var width = $(window).width();
	if(width < 1100){
		$('.menu').css('float','left');
	}else{
		$('.menu').css('float','right');
	}
}


function resetEditareaSize(target) {
	if(target){
		var edit = $(target);
		var top = edit.offset().top;
		var height = $(window).height();
		var fix = height - top - 55;
		edit.css('height', fix + 'px');
		//$('#keywordtab').css('height', fix - 55 + 'px');
	}
}

function ajax_load(e){
	$('#maintabs').tabs('select',1);
	$('#home').html('<img src="images/ajax-loader2.gif" />Now Loading');
	setKeywordTab($(this).attr("type"));
	$.ajax({
		type: "POST",
		url: "ajaxfunction.php",
		data:
		{
			"date"   : $(e).attr("date"),
			"type"   : $(e).attr("type"),
			"mode"   : $(e).attr("mode")
		},
		success: function(msg){
			$('#home').html(msg);
			resetEditareaSize('#home');
			resetEditareaSize("#keywordtab");
		}
	});
	return false;
}

function ajax_load2(e){
	$('#maintabs').tabs('select',2);
	setKeywordTab($(this).attr("type"));

		if($('#bought').attr('checked')){
			var bought = $('#bought').val();
		}else{
			var bought = 0;
		}

		if($('#unread').attr('checked')){
			var read = $('#unread').val();
		}else{
			var read = 0;
		}
		
		var order = $("#order").val();

	$.ajax({
		type: "POST",
		url: "ajaxfunction.php",
		data:
		{
			"offset"   : $(e).attr("offset"),
			"type"   : $(e).attr("type"),
			"mode"   : $(e).attr("mode"),
			"read"   : read,
			"bought" : bought,
			"order"  : order
		},
		success: function(msg){
			$('#checkList').html(msg);
			resetEditareaSize('#checkList');
			resetEditareaSize("#keywordtab");
		}
	});
	return false;
}


function ajax_searchKeyword(){
	if($("#keyword").val() == ""){
		return false;
	}
	$('#maintabs').tabs('select',1);
	$('#home').html('<img src="images/ajax-loader2.gif" />Now Loading');
	$.ajax({
		type: "POST",
		url: "ajaxfunction.php",
		data:
		{
			"keyword": $("#keyword").val(),
			"type"   : $(".searchtype:checked").val(),
			"mode"   : "searchKeyword"
		},
		success: function(msg){
			$('#home').html(msg);
			resetEditareaSize('#home');
			resetEditareaSize("#keywordtab");
		}
	});
	return false;
}

function ajax_searchUserKeyword(keyword,type){
	if(keyword == ""){
		return false;
	}else{
		keyword = decodeURIComponent(keyword);
	}
	$('#home').html('<img src="images/ajax-loader2.gif" />Now Loading');
	$('#maintabs').tabs('select',1);
	$.ajax({
		type: "POST",
		url: "ajaxfunction.php",
		data:
		{
			"keyword": keyword,
			"type"   : type,
			"mode"   : "searchKeyword"
		},
		success: function(msg){
			$('#home').html(msg);
			resetEditareaSize('#home');
			resetEditareaSize("#keywordtab");
		}
	});
	return false;
}

function ajax_AllKeyword(type){
	$('#home').html('<img src="images/ajax-loader2.gif" />Now Loading');
	$('#maintabs').tabs('select',1);
	$.ajax({
		type: "POST",
		url: "ajaxfunction.php",
		data:
		{
			"type"   : type,
			"mode"   : "searchAllKeyword"
		},
		success: function(msg){
			$('#home').html(msg);
			resetEditareaSize('#home');
			resetEditareaSize("#keywordtab");
		}
	});
	return false;
}


function ajax_toggleUserKeyword(keyword,type,e){
	$.ajax({
		type: "POST",
		url: "ajaxfunction.php",
		data:
		{
			"keyword": keyword,
			"type"   : type,
			"mode"   : "toggleKeyword"
		},
		success: function(msg){
			$(e).html(msg);
		}
	});
	return false;
}

function ajax_addUserKeyword(){
	if($("#keyword").val() == ""){
		return false;
	}
	$('#maintabs').tabs('select',1);
	$('#keywordtab').tabs('select',0);
	var booktype = $(".searchtype:checked").val();
	$.ajax({
		type: "POST",
		url: "ajaxfunction.php",
		data:
		{
			"keyword": $("#keyword").val(),
			"type"   : booktype,
			"mode"   : "addKeyword"
		},
		success: function(msg){
			if(msg != "false"){
				$('#aboutKeyword').html("新しいキーワードを追加しました。");
			}else{
				$('#aboutKeyword').html("キーワードが登録できません。");
			}
			$("#keyword").val("");
		}
	});
	return false;
}

function ajax_addUserKeyword_cmd(title,type){

	$('#addDialogKeyword').val(title);
	$('#addDialogType').val(type);
	$('#dialog').dialog('open');
	return false;
}

function ajax_fixbookdata(uid,type){

	$('#fixType').val(type);
	$.ajax({
		type: "POST",
		dataType: "json",
		url: "ajaxfunction.php",
		data:
		{
			"book_uid"  : uid,
			"type" : type,
			"mode" : "getBookDetailData"
		},
		success: function(msg){
			var rJSON = msg;
			$('#fixid').val(rJSON.book_uid);
			$('#fixDate').val(rJSON.release_date);
			$('#fixWord').val(rJSON.release_word);
			$('#fixTitle').val(rJSON.book_name);
			$('#fixPrice').val(rJSON.price);
			$('#fix').dialog('open');
		}
	});
	return false;
}


function ajax_deleteUserKeyword(uid,type,e){
	$.ajax({
		type: "POST",
		url: "ajaxfunction.php",
		data:
		{
			"id": uid,
			"type"   : type,
			"mode"   : "deleteKeyword"
		},
		success: function(msg){
			$(e).parent().remove();
		}
	});
	return false;
}

function ajax_addBookManage(e){
	$.ajax({
		type: "POST",
		url: "ajaxfunction.php",
		data:
		{
			"book_uid"   : $(e).attr("book_uid"),
			"type"   : $(e).attr("type"),
			"kind"   : $(e).attr("kind"),
			"mode"   : "addBookManage"
		},
		success: function(msg){
			$(e).html(msg);
		}
	});
	return false;
}

function ajax_addBookCheck(e){
	$.ajax({
		type: "POST",
		url: "ajaxfunction.php",
		data:
		{
			"book_uid"   : $(e).attr("book_uid"),
			"type"   : $(e).attr("type"),
			"mode"   : "addBookCheck"
		},
		success: function(msg){
			$(e).html(msg);
		}
	});
	return false;
}

function ajax_addBookBought(e){
	$.ajax({
		type: "POST",
		url: "ajaxfunction.php",
		data:
		{
			"book_uid"   : $(e).attr("book_uid"),
			"type"   : $(e).attr("type"),
			"mode"   : "addBookBought"
		},
		success: function(msg){
			$(e).html(msg);
		}
	});
	return false;
}

function ajax_addBookRead(e){
	$.ajax({
		type: "POST",
		url: "ajaxfunction.php",
		data:
		{
			"book_uid"   : $(e).attr("book_uid"),
			"type"   : $(e).attr("type"),
			"mode"   : "addBookRead"
		},
		success: function(msg){
			$(e).html(msg);
		}
	});
	return false;
}

function ajax_getAmazon(e){
	if($(e).attr("finish") == "finish"){
		$(e).parent().parent().next().toggle();
	}else{
		$(e).css("cursor","progress");
		$.ajax({
			type: "POST",
			url: "ajaxfunction.php",
			data:
			{
				"name"   : $(e).attr("name"),
				"mode"   : "getAmazon"
			},
			success: function(msg){
				$(e).attr("finish","finish");
				$(e).css("cursor","pointer");
				$(e).parent().parent().after('<tr><td colspan="8">' + msg + '</td></tr>');
			}
		});
	}
	return false;
}

function ajax_countFlag(){
	$('#getstatus').hide();
	$.ajax({
		type: "POST",
		url: "ajaxfunction.php",
		data:
		{
			"mode"   : "countFlag"
		},
		success: function(msg){
			$('#status').html(msg);
			$('#getstatus').show();
		}
	});
}

function setKeywordTab(type){
	if(type == "novel"){
		$('#keywordtab').tabs('select',2);
	}else if(type == "book"){
		$('#keywordtab').tabs('select',1);
	}
}

function updateStatus(e){
	$("#statusResult").html("更新中...");
	if( $(e)[0].checked ){
		value = 1;
	}else{
		value = 0;
	}
	
	$.ajax({
		type: "POST",
		url: "ajaxfunction.php",
		data:
		{
			"mode"   : "updateStatus",
			"value"  : value,
			"status" : $(e).attr("option")
		},
		success: function(msg){
			$("#statusResult").html(msg);
		}
	});
}

function updateUserEmail(e){
	$("#statusResult").html("更新中...");
	value = $('#UpdateregUserEmail').val();
	
	$.ajax({
		type: "POST",
		url: "ajaxfunction.php",
		data:
		{
			"mode"   : "updateUserEmail",
			"value"  : value
		},
		success: function(msg){
			$("#statusResult").html(msg);
		}
	});
}

function updateTwitterID(e){
	$("#statusResult").html("更新中...");
	value = $('#UpdateuserOptionTwitterID').val();
	
	$.ajax({
		type: "POST",
		url: "ajaxfunction.php",
		data:
		{
			"mode"   : "updateTwitterID",
			"value"  : value
		},
		success: function(msg){
			$("#statusResult").html(msg);
		}
	});
}
function searchFromTable(target){
	word = new RegExp("とある","i");
	$(target + ' tr.book').each(function(){
		str  = $(this).children(".book_name").children("span").html();
		alert(str.match(word));
	});
}