  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-3098678-2']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

function resize_footer(){
	$("#footer").width($(window).width());
	$("#footer").css("top",$(window).height()-100);

	$("#phrases").css("left",($(window).width()-520)/2);
	$("#phrases").css("top",($(window).height()-490)/2-30);
}

var pick_key = 0;
function pick_phrase( id ){
	if( pick_key!=0 ){
		return false;
	}
	pick_key = 1;
	$("#phrases")
		.css({
			opacity: "1"
		})
		.animate({opacity: "0"},{queue: true, duration: 1000, easing: "linear", complete: function(){
			$("#phrase_id").val( id );
			$("#phrase_form").submit();
		}});
}

function send_gift(){
	var gift = $("#gift_name").val();
	if( gift=="" || gift == null ){
		return false;
	}

	var phrase_id = $("#phrase_id").val();

	$.ajax({
			type: "POST",
			url: "index.php",
			data: {
				"mod":"phrase",
				"act":"gift",
				"phrase_id": phrase_id,
				"gift_name": gift
			},
			success: function(res){
				$("#box_gift").html(res);
			}
		});
}

function back_home(){
	$("#phrases")
		.css({
			opacity: "1"
		})
		.animate({opacity: "0"},{queue: true, duration: 200, easing: "linear", complete: function(){
			$("#back_form").submit();
		}});
}

function go_haiku(){
	if( pick_key!=0 ){
		return false;
	}
	pick_key = 1;
	$("#phrases")
		.css({
			opacity: "1"
		})
		.animate({opacity: "0"},{queue: true, duration: 1000, easing: "linear", complete: function(){
			$("#haiku_form").submit();
		}});
}

var ku_step = 0;
var ku_lock = 0;
function update_kus(){
	var type_id=1;
	if( ku_step==0 || ku_step==2 ){
		type_id=2;
	}

	$.ajax({
			type: "POST",
			url: "index.php",
			data: {
				"mod":"ku",
				"act":"list",
				"type_id": type_id
			},
			success: function(res){
				$("#kus").html(res);
				ku_lock = 0;
			}
		});
}

function confirm_ku(){
	$("#kus").html("");
	var a1 = $("<p>").html("OK");
	var li1 = $("<li>").append(a1);
	li1.click( function(){
		if( ku_step<10 ){
			$("#haiku").val($("#ku_gray").children("p").html());
			$("#add_form").submit();
		} else {
			$("#tanka").val($("#ku_tanka").children("p").html());
			$("#finish_form").submit();
		}
	});

	var a2 = $("<p>").html("RETRY");
	var li2 = $("<li>").append(a2);
	li2.click( function(){
		if( ku_step<10 ){
			ku_step=0;
			update_kus();
			$("#ku_gray").children("p").html("");
		} else {
			ku_step=10;
			update_kus();
			$("#ku_tanka").children("p").html(haiku+"<br/>");
		}
	});
	$("#kus").append(li1).append(li2);

	ku_lock = 0;
}

function pick_ku( id ){
	if( ku_lock==1 ){
		return false;
	}

	ku_lock = 1;

	var str = $("#ku"+id).html();

	if( ku_step%10>0 ){
		str = "　"+str;
	}
	if( ku_step<10 ){
		$("#ku_gray").children("p").append(str);
	} else {
		$("#ku_tanka").children("p").append(str);
	}

	ku_step++;

	if( ku_step==3 ){
		confirm_ku();
	} else if( ku_step==12 ){
		confirm_ku();
	} else {
		update_kus();
	}
}

function go_tanka(){
	if( pick_key!=0 ){
		return false;
	}
	pick_key = 1;
	$("#phrases")
		.css({
			opacity: "1"
		})
		.animate({opacity: "0"},{queue: true, duration: 1000, easing: "linear", complete: function(){
			$("#tanka_form").submit();
		}});
}

function go_pool(){
	if( pick_key!=0 ){
		return false;
	}
	pick_key = 1;
	$("#phrases")
		.css({
			opacity: "1"
		})
		.animate({opacity: "0"},{queue: true, duration: 1000, easing: "linear", complete: function(){
			$("#pool_form").submit();
		}});
}


var pool_timer = 0;
var pool_id = 0;
var dip_count = -1;
var add_dip_lock = 0;

function start_pool( id ){
	pool_id = id;
	update_pool();
	setInterval( update_pool , 3000 );
}

function update_pool(){
	if( pool_timer==1 ){
		return;
	}
	pool_timer = 1;
	$.ajax({
			type: "POST",
			url: "index.php",
			data: {
				"mod":"pool",
				"act":"dips",
				"pool_id": pool_id,
				"dip_count": dip_count
			},
			success: function(res){
				if( res.substring(0,1)=="0" ){
					$("#pinger").html(parseInt(res));
				} else {
					var json = eval("("+res+")");

					if( json.status==0 ){
						$("#pool_side").html(json.finish_time+"　完結");
					}

					dip_count = json.list.length;
					var html = json.subject_word + "<br/>";
					for( var i=0 ; i<json.list.length ; i++ ){
						if( json.status==0 ){
							if( i==json.list.length-1 ){
								html = html + json.lastdip_word + "<br/>";
							}
						}
						html = html + json.list[i] + json.subject_word + "<br/>";
					}
					$("#pool_text").html(html);
					$("#new_dip").focus();
					$("#pinger").html(parseInt(json.num));
				}
				pool_timer = 0;
				//pool_timer = setTimeout( update_pool,3000 );
			}
		});
}

function add_dip( id ){
	if( add_dip_lock==1 ){
		return;
	}
	add_dip_lock = 1;
	var dip = $("#new_dip").val();
	$("#new_dip").val("");
	$.ajax({
			type: "POST",
			url: "index.php",
			data: {
				"mod":"pool",
				"act":"add",
				"new_dip": dip,
				"pool_id": id
			},
			success: function(res){
				//if( pool_timer>0 ){
				//	clearTimeout( pool_timer );
				//	pool_timer = 0;
				//}
				update_pool();
				$("#new_dip").focus();
				add_dip_lock = 0;
			}
		});
}

