/*******************************************
* 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 zeroPad(n, keta) {
	return ("0000000000"+n).slice(-keta);
}
/**************************************
* Set birthday to cookie              *
**************************************/
function cookieBirthday(birth_day) {
	if ($.cookie("birth_day") != birth_day) {
		$.cookie("birth_day", birth_day, {expires:365, path:'/'});
	}
}

function congenial() {
	$.getJSON(
		'/lab/rel_val/valentine.php',
		{	f : 'congenial',
			iYear   : $("#iYear").val(),
			iMonth  : $("#iMonth").val(),
			iDay    : $("#iDay").val(),
			iHour   : $("#iHour").val(),
			iMinute : $("#iMinute").val(),
			jYear   : $("#jYear").val(),
			jMonth  : $("#jMonth").val(),
			jDay    : $("#jDay").val(),
			jHour   : $("#jHour").val(),
			jMinute : $("#jMinute").val()
		},
		function(data, status){
			if (data['failed']) {
				return;
			}
			setResult(data);
			var my_bd  = data['my']['birthday'].replace(/-/g, '/');
			var you_bd = data['you']['birthday'].replace(/-/g, '/');
			koikana(new Date(Date.parse(my_bd)), new Date(Date.parse(you_bd)));
			cookieBirthday(data['my']['birthday']);
		}
	);
}
function setResult(json) {
	$('#dat_p1').html('<strong>あなた</strong> ' + json['my']['birthday']);
	$('#dat_p2').html('<strong>あの人</strong> ' + json['you']['birthday']);

	$('#val_sum img').attr('src', '/lab/rel_val/images/anim-calc.gif');
	$('#sct_rel img').attr('src', '/lab/rel_val/images/anim-calc.gif');
	setTimeout(function(){
			$('#val_sum img').attr('src', json['cpt_img']['img']);
			$('#val_sum span').text(json['cpt_img']['desc']);
			$('#sct_rel img').attr('src', json['rel_img']['img']);
			$('#sct_rel span').text(json['rel_img']['desc']);
		}, 2000);

	$('#char_p1').html('<strong>あなた</strong><br/>『' + json['my']['chara'] + '』');
	$('#char_p2').html('<strong>あの人</strong><br/>『' + json['you']['chara'] + '』');

	$('#rel_msg').html(json['message'][0] + '<br/><br/><br/><strong>☆ポイント</strong><br/>' + json['message'][1] + '<br/><br/>');

	var kind = ['love','heal','talk','action','way','special'];
	var img  = ['ve-ma','mo-mo','me-me','ma-ma','su-su','mo-su'];
	for (var i=0; i<kind.length; i++) {
		if (json['suggestion'][kind[i]]) {
			var suggest = json['suggestion'][kind[i]].split(':');
			$('#orc' + (i+1) + ' span').html(suggest[1] + '<br/><br/>');
			if (json[img[i]] && json[img[i]]['in_orb']) {
				$('#orc' + (i+1) + ' span').attr('style', 'font-weight:bold;');
				$('#orc' + (i+1) + ' img').attr('src', '/lab/rel_val/images/' + img[i] + '-' + suggest[0] + '-tas.gif');
			} else {
				$('#orc' + (i+1) + ' img').attr('src', '/lab/rel_val//images/' + img[i] + '-' + suggest[0] + '.gif');
			}
		} else {
			$('#orc' + (i+1) + ' img').attr('src', '/lab/rel_val//images/' + img[i] + '-non.gif');
			$('#orc' + (i+1) + ' span').html('<br />');
			$('#orc' + (i+1) + ' span').removeAttr('style');
		}
	}
	var my_bd  = json['my']['birthday'].replace(/-/g, '').replace(/ /g, '').replace(/:/g, '');
	var you_bd = json['you']['birthday'].replace(/-/g, '').replace(/ /g, '').replace(/:/g, '');
	var url = 'http://charapla.net/lab/rel_val/index.php?bd1=' + my_bd + '&bd2=' + you_bd;
//	$('#result_url').val(url);
	$.ajax({
		url: "http://api.bit.ly/shorten",
		data: {
			version : '2.0.1',
			format  : 'json',
			login   : 'charapla',
			apiKey  : 'R_87601f27340a598065f9fef6bdf5a9fe',
			longUrl : url
		},
		dataType: "jsonp",
		cache   : false,
		success : function(data, status) {
			$('#result_url').val(data.results[url]['shortUrl']);
		}
	});
	$('#result').show();
}

