jQuery(function($) {
	$("#result").hide();
	$("#loading").hide();

	// ぬこパンチ発動
	$("#nya").click(function() {
		var screen_name = $("#screen_name").val();
		if (screen_name.match(/[^A-Za-z0-9_]/)) {
			$("#error_msg").text("※使用できる文字は、半角の英数文字とアンダーバー(_)のみです。");
		}
		else if (screen_name == "") {
			$("#error_msg").text("※ユーザー名を入力してください。");
		} else {
			$("#error_msg").empty();
			$("#image").empty();
			$("#name").empty();
			$("#description").empty();
			$("#tweets").empty();
			$("#msg").hide();
			$("#result").hide();
			$("#loading").show();
			showTimeline(screen_name);
		}
	});
});

// タイムラインでぬっこぬこ
function showTimeline(screen_name) {
	$.ajax( {
		url : "http://twitter.com/statuses/user_timeline.json?count=25",
		type : "GET",
		data : {
			"screen_name" : screen_name
		},
		dataType : "jsonp",
		chace : true,
		timeout : 5000,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			$("#error_msg").html(textStatus + ":しばらく時間をおいてから試してみてください。");
		},
		success : function(json, status) {
			if (json.length == 0) {
				$("#tweets").html("つぶやきを取得できませんでした。");
			} else {
				var id = json[0].user.id;
				var name = json[0].user.name;
				var description = json[0].user.description;
				var image_url = json[0].user.profile_image_url.replace("_normal.", "_bigger.");

				var objData = new Object();
				objData["name"] = name;
				objData["description"] = description;

				for ( var i in json) {
					objData["tweets"+i] = json[i].text;
				}

				$("#image").html("<a href=\"http://twitter.com/" + screen_name + "\"><img src=\"" + image_url + "\" border=\"0\" /></a>");
				nukoJudgment(id);
				convertNukoText($.toJSON(objData));
			}
		},
		complete : function(XMLHttpRequest, status) {
			$("#loading").slideUp(1000, function(){
				$("#result").slideDown(1000);
			});
		}
	});
}

// ぬっこぬこツイート変換
function convertNukoText(json) {
	$.ajax( {
		url : "./convertNukoText.php",
		type : "POST",
		data : {"json" : json},
		dataType : "html",
		chace : true,
		timeout : 5000,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			$("#error_msg").html(textStatus + ":ツイート変換に失敗しました。");
		},
		success : function(data, status) {
			$("#nukotext").html(data);
		}
	});
}

// 判定結果
function nukoJudgment(id) {
	$.ajax( {
		url : "./nukoJudgment.php",
		type : "POST",
		data : {
			"id" : id
		},
		dataType : "json",
		chace : true,
		timeout : 5000,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			$("#error_msg").html(textStatus + ":判定に失敗しました。");
		},
		success : function(json, status) {
			var judg = "あなたを猫に例えると「" + json.per_a + "」で「" + json.per_b + "」な「" + json.name + "」です。";
			$("#neko").html("<img src=\"./images/illust_s_set/illust_s" + json.num + ".gif\" />");
			$("#judgment").html(judg);
			$("#link").html("<a href=\"http://twitter.com/home?status=" + encodeURIComponent(judg + " ねこになったー[ http://natter.neko-love.net/ ]") + "\" target=\"_blank\">診断結果をTwitterでつぶやく</a>");
		}
	});
}
