/*******************************************
* append options into select tag           *
*******************************************/
function appendOption(select_id, start, end, step) {
	for (var i=start; i<=end; i+=step) {
		var elem = document.createElement("option");
		elem.appendChild(document.createTextNode(i));
		elem.value = zeroPad(i, (""+end).length);
		$("#" + select_id).append(elem);
	}
}
function appendFuken(select_id) {
	var fuken = ['北海道','青森県','岩手県','宮城県','秋田県','山形県','福島県','茨城県','栃木県','群馬県',
				'埼玉県','千葉県','東京都','神奈川県','新潟県','富山県','石川県','福井県','山梨県','長野県',
				'岐阜県','静岡県','愛知県','三重県','滋賀県','京都府','大阪府','兵庫県','奈良県','和歌山県',
				'鳥取県','島根県','岡山県','広島県','山口県','徳島県','香川県','愛媛県','高知県','福岡県',
				'佐賀県','長崎県','熊本県','大分県','宮崎県','鹿児島県','沖縄県'];
	for (var i=0; i<fuken.length; i++) {
		var elem = document.createElement("option");
		elem.appendChild(document.createTextNode(fuken[i]));
		elem.value = zeroPad(i+1, 2);
		$("#" + select_id).append(elem);
	}
}
function zeroPad(n, keta) {
	return ("0000000000"+n).slice(-keta);
}
/**************************************
* Set birthday from cookie            *
**************************************/
function setDate() {
	if ($.cookie("birth_day")) {
		var ymd_hm = $.cookie("birth_day").split(" ");
		var ymd = ymd_hm[0].split("-");
		$("#iYear").val(ymd[0]);
		$("#iMonth").val(ymd[1]);
		$("#iDay").val(ymd[2]);
		var hm  = ymd_hm[1].split(":");
		$("#iHour").val(hm[0]);
		$("#iMinute").val(hm[1]);
	}
	if ($.cookie("gender")) {
		$("#iSex").val($.cookie("gender"));
	}
}
function postTo(action, value) {
	var _body=document.getElementById('fw_body');
	var myForm=document.createElement('form');
	myForm.setAttribute("action",action);
	myForm.setAttribute("method","post");
	var inps=new Object;
	inps[0]=document.createElement('input');
	inps[0].setAttribute("type","hidden");
	inps[0].setAttribute("name","feel_id");
	inps[0].setAttribute("value",value);
	for(var i in inps){
		myForm.appendChild(inps[i]);
	}
	_body.appendChild(myForm);
	myForm.submit();
}
function postFeel(feel_id) {
	var html = '<img id="msg_wrt" src="images/anm-flwrt.gif" alt="書き込み中" width="80" height="16" border="0" style="position:absolute;top:10px;left:150px;width:80px;" />';
	$("#fbtn").after(html);
	$.getJSON(
		'/api/feel_log.php',
		{	f    : 'postFeel',
			u    : getCharaplaID_FSO(),
			f_id : feel_id },
		function(data, status){
			if (data['no_user']) {
				location.href = 'init.php';
				return;
			}
			setImages(data);
			if (parseInt(data['feels_count']) == 20)
				showLevelUp();
		}
	);
	setTimeout(function(){$("#msg_wrt").remove();}, 1500);
}
function showLevelUp() {
	var html ='<div id="msg-lv2" style="position:absolute;top:48px;left:3px;width:155px;">' +
			  '<img src="images/msg-lv2.gif" width="155" height="60" alt="はじめまして！" />' +
			  '<a href="javascript:void(0);" onclick="javascript:removeLevelUp();">' +
			  '<img src="images/msg-s1.gif" alt="はじめまして！" width="155" height="441" border="0" /></a></div>';
	$("#fw_fortune,#fw_power,#planet").css({opacity:"0.5"});
	$("#fw_result").after(html);
}
function removeLevelUp() {
	$("#msg-lv2").remove();
	$("#fw_fortune,#fw_power,#planet").css({opacity:"1.0"});
}

function last_weather(charapla_id) {
	$.getJSON(
		'/api/feel_log.php',
		{	f : 'lastFeel',
			u :  charapla_id	},
		function(data, status){
			if (data['no_user']) {
				location.href = 'init.php';
				return;
			}
			setImages(data);
		}
	);
}
function setImages(json) {
	var blood_type = ["Ａ","Ｂ","Ｏ","ＡＢ"];
	if (json['birthday'] && json['blood']) {
		$("#fw_usrbd span").text(json['birthday'].split(' ')[0] + "　" +blood_type[json['blood']]);
	}
	if (json['expire']) {
		$("#tl-fbn").show();
		$("#fw_fortune,#fw_power,#planet").css({opacity:"0"});
		var msg_top = "200px;";
		if (json['feels_count'] >= 20)
			msg_top = "165px;";
		$("#fw_result").after('<div class="msgImg"><img id="def_msg" src="images/msg-default_s14.gif" /></div>');
	} else {
		if (json['feel_id']) {
			$("#fbtn a").each(function(){
				if ($(this).text() == json['feel_id'])
					$(this).css({opacity:"1.0"});
				else
					$(this).css({opacity:"0.5"});
				// $(this).removeAttr("onclick");
			});
			$("#sbtn a").each(function(){
				if ($(this).text() == json['feel_id'])
					$(this).css({opacity:"1.0"});
				else
					$(this).css({opacity:"0.5"});
				// $(this).removeAttr("onclick");
			});
		}
		$("#tl-fbn").hide();
		$("#fw_fortune,#fw_power,#planet").css({opacity:"1.0"});
		$("#def_msg").remove();
	}
	if (json['feels_count']) {
		if (json['feels_count'] < 19)
			$("#sbtn").remove();
		else if (json['feels_count'] > 19)
			$("#sbtn").show();
	}
	if (json['total']) {
		$("#fw_fortune span").html('&nbsp;' + parseInt(json['total']['fortune']) + '<span style="font-size:12px;">%</span>');
		$("#fw_power span").html('&nbsp;' + parseInt(json['total']['power']));
	}
	if (json['fortune_updown'])
		$("#fw_fortune_updown").html('<img src="images/' + udbtnImg(parseInt(json['fortune_updown'])) + '" width="16" height="16"/>');
	if (json['power_updown'])
		$("#fw_power_updown").html('<img src="images/' + udbtnImg(parseInt(json['power_updown'])) + '" width="16" height="16"/>');
	var ID_MAP = ["sun","mon","mer","dee","ars"];
	for (var i=0; i<ID_MAP.length; i++) {
		var html = '&nbsp;';
		html += '<img src="images/' + piconImg(json[ID_MAP[i]]['power']) +
				'" class="picon" />' +
				'<img src="images/' + ficonImg(json[ID_MAP[i]]['fortune']) +
				'" class="ficon" />';
		$("#fw_" + ID_MAP[i]).html(html);
	}
}
function piconImg(point) {
	if (point <= 19)
		return "picon-00.gif";
	else if (point > 19 && point < 36)
		return "picon-01.gif";
	else if (point >= 36 && point < 50)
		return "picon-02.gif";
	else
		return "picon-03.gif";
}
function ficonImg(point) {
	if (point <= -15)
		return "ficon-00.gif";
	else if (point > -15 && point < -10)
		return "ficon-01.gif";
	else if (point > -10 && point < 0)
		return "ficon-02.gif";
	else if (point >= 0 && point < 7)
		return "ficon-03.gif";
	else if (point >= 7 && point < 20)
		return "ficon-04.gif";
	else if (point >= 20 && point < 38)
		return "ficon-05.gif";
	else
		return "ficon-06.gif";
}
function udbtnImg(up_down) {
	if (up_down < 0)
		return "btn-rdwn.gif";
	else if (up_down > 0)
		return "btn-rup.gif";
	else
		return "btn-rsame.gif";
}

function getCharaplaID_FSO() {
	var swf = (navigator.appName.indexOf("Microsoft") != -1) ? window['charapla_getid'] : document['charapla_getid'];
	if (typeof swf.getCharaplaID_FSO == 'function')
		return swf.getCharaplaID_FSO();
	else
		return $.cookie("charapla-id");
}

function editProf() {
	$("#fw_usrbd,#tl-fbn,#fbtn,#sbtn,#def_msg").hide();
	$("#fw_input").show();

	$.getJSON(
		'/api/feel_log.php',
		{	f : 'getUser',
			u : getCharaplaID_FSO() },
		function(data, status){
			if (data['no_user']) {
				location.href = 'init.php';
				return;
			}
			setProf(data);
		}
	);
}
function setProf(json) {
	if (json['birthday']) {
		var ymd_hm = json['birthday'].split(" ");
		var ymd = ymd_hm[0].split("-");
		$("#iYear").val(ymd[0]);
		$("#iMonth").val(ymd[1]);
		$("#iDay").val(ymd[2]);
		var hm  = ymd_hm[1].split(":");
		$("#iHour").val(hm[0]);
		$("#iMinute").val(hm[1]);
	}
	if (json['gender']) {
		$("#iSex").val(json['gender']);
	}
	if (json['blood']) {
		$("#iBlood").val(json['blood']);
	}
	if (json['current_place']) {
		$("#iPlace").val(zeroPad(json['current_place'],2));
	}
}
function updateProf(update) {
	if (update) {
		$.getJSON(
			'/api/feel_log.php',
			{	f : 'updateUser',
				u : getCharaplaID_FSO(),
				iYear   : $("#iYear").val(),
				iMonth  : $("#iMonth").val(),
				iDay    : $("#iDay").val(),
				iHour   : $("#iHour").val(),
				iMinute : $("#iMinute").val(),
				iSex    : $("#iSex").val(),
				iBlood  : $("#iBlood").val(),
				iPlace  : $("#iPlace").val()
			},
			function(data, status){
				if (data['no_user']) {
					location.href = 'init.php';
					return;
				}
				if (data['failed']) {
					return;
				}
				last_weather(getCharaplaID_FSO());
			}
		);
	}
	$("#fw_input").hide();
	$("#fw_usrbd,#tl-fbn,#fbtn,#sbtn,#def_msg").show();
}

