$(function() {
	// twitter #102kado
	$('#tofu_kado_contents').twitSearch('#102kado', 20);
	
	// twitter TofuDeveloper
	$('#staffs_twitter_contents').twitUserTimeLine('TofuDeveloper', 20);
	
	var time = new Date().getTime();
	// みんなの使用履歴
	$.getJSON(
		'/tofu/ajax_user_history.php', 
		{'kwd' : time}, 
		function(json) {
			var url = 'http://www.tofu-system.com/daiz-free/search.php?kwd=';
			for (var i = 0; i < json.length; i++) {
				var keyword = json[i].kwd;
				if (keyword.length > 13) {
					keyword = keyword.slice(0, 12) + '...';
				}
				$('#user_history_body ul').append('<li class="history_item"><a href="' + url + encodeURI(json[i].kwd) + '" target="_blank">' + keyword + '</a></li>');
			}
		}
	);
	
	// ユーザー使用履歴
	$('#search_form :submit').click(function() {
		setTimeout(
			function() {
				$('#user_count_box').loadCount();
			}, 
			5000);
	});
	$.fn.extend({
		loadCount : function() {
/*
			var self = this
			$.getJSON(
				'/tofu/ajax_user_count.php', 
				{'kwd' : new Date().getTime()}, 
				function(json) {
					var count = json.count;
					var url = 'http://www.tofu-system.com/daiz-free/search.php?kwd=';
					if (count > 0) {
						if (json.kwd != '' && json.url != '') {
							$(self).html('<div id=\"user_count_inner\"><p>本日の利用制限回数を超えました！<br />１つのIPアドレスに対して１日１回までしか利用できません。<br />もっと使いたい場合は無制限に使える<a href="https://www.tofu-system.com/item/tofu/" onclick="pageTracker._trackEvent(\'click\', \'item/tofu\', \'toppage_search\');" onkeypress="pageTracker._trackEvent(\'keypress\', \'item/tofu\', \'toppage_search\');">ToFU有料版</a>をご利用下さい。<br /></p><p style="font-size:1.2em;">あなたの本日の使用履歴：<a href="' + url + encodeURI(json.kwd) + '&target_url=' + encodeURI(json.url) + '" target="_blank">' + json.kwd + '</a></p></div><div id=\"please_introduce\"><div id=\"please_introduce_inner\"><a href=\"/please_introduce/\">ToFU担当者<br />からのお願い。</a></div>');
						} else {
							$(self).html('<div id=\"user_count_inner\"><p>本日の利用制限回数を超えました！<br />１つのIPアドレスに対して１日１回までしか利用できません。<br />もっと使いたい場合は無制限に使える<a href="https://www.tofu-system.com/item/tofu/" onclick="pageTracker._trackEvent(\'click\', \'item/tofu\', \'toppage_search\');" onkeypress="pageTracker._trackEvent(\'keypress\', \'item/tofu\', \'toppage_search\');">ToFU有料版</a>をご利用下さい。<br /></p></div><div id=\"please_introduce\"><div id=\"please_introduce_inner\"><a href=\"/please_introduce/\">ToFU担当者<br />からのお願い。</a></div></div>');
						}
					}
				}
			);
*/
		}
	});
//	$('#user_count_box').loadCount();
	
	// キーワード
	$('#kwd').focus(function() {
		$(this).css('background-color', '#ffd');
	}).
	blur(function() {
		$(this).css('background-color', '#fff');
		var text = $(this).val();
		if (text == '') {
			return;
		}
	});
	
	// URL
	$('#target_url').focus(function() {
		$('#search_form h2:eq(1) span').remove();
		$(this).css('background-color', '#ffd');
	}).
	blur(function() {
		$(this).css('background-color', '#fff');
		var text = $(this).val();
		if (text == '') {
			return;
		}
		if (!text.match(/^https?:\/\//i)) {
			$(this).css('background-color', '#f00');
			$('#search_form h2:eq(1)').append('<span>URLの入力が間違えています</span>');
			return;
		}
		if (text.match(/[^-\/_.!~*\'()a-zA-Z0-9;?:\@&=+\$,%#]+/i)) {
			$(this).css('background-color', '#f00');
			$('#search_form h2:eq(1)').append('<span>URLの入力が間違えています</span>');
			return;
		}
	});
	
	// キーワードにフォーカス
	$('#kwd').focus();
	$('#target_url').val('http://');
});

