$(function() {
	
	if (!$.searchbox) $.searchbox = {};
	
	$.searchbox.keywords = {};
	$.searchbox.category = [];
	$.searchbox.load_time = 5000;
	$.searchbox.load_timer = null;
	$.searchbox.load_counter = 0;
	
	$.searchbox.submitKeyword = function(type, word) {
		$.searchbox.changeSearchEntry(type);
		$('.searchArea input').attr('value', word);
		$('select[name=searchType] option:selected').val(type);
		$('.searchArea form').submit();
	};

	$.searchbox.changeSearchEntry = function(type) {
		if (!type) type = $('select[name=searchType] option:selected').val();
		$('.searchArea form').attr('action', {'all':'/pubSearchAll.do','event':'/pubSearchEvent.do','tdw':'/pubSearchTdwBoothDetail.do','member':'/pubSearchMemberDetail.do','product':'/pubSearchProduct.do','blog':'/pubSearchBlogEntry.do'}[type]);
	};


	$.searchbox.changeSearchEntry();
	$.getJSON('/script/pickupKeyword.jss', function(json) {

		if (!json) return;
		
		// aタグ挿入＋配列に変換
		var cnt = 0;
		var type = $('select[name=searchType] option:selected').val();
		$.map(['event','tdw','member','product','blog'], function(t) {
			var keywords = json[t];
			
			if (keywords) {
				var categoryName = {'event':'イベント','tdw':'TDW','member':'会員','product':'商品・作品','blog':'ブログ'};
				$.searchbox.keywords[t]
					 = categoryName[t] + ' ： ' + $.map(keywords.split(','), function(word) {
						return '<a href=\"#\" onclick=\"$.searchbox.submitKeyword(' + "'" + t +"','" + word + "'" + ')\">' + word + '</a>';
					}).join('');
				$.searchbox.category.push(t);

				if (t == type) $.searchbox.load_counter = cnt;
				cnt++;
			}
		});
		
		if ($.searchbox.category.length == 0) return; 


		clearTimeout($.searchbox.load_timer);
		
		var hover = false;
		$('#pickupKeyword dd').hover(function() {hover = true;}, function() {hover = false;});
		
		// キーワードloading
		(function load() {
			
			// マウスオーバー時は無視する
			if (hover) {
				$.searchbox.load_timer = setTimeout(arguments.callee, $.searchbox.load_time);
				return;
			}
			
			$('#pickupKeyword').show().find('dd').html($.searchbox.keywords[$.searchbox.category[$.searchbox.load_counter]]);
			
			$.searchbox.load_counter++;
			if ($.searchbox.load_counter >= $.searchbox.category.length) $.searchbox.load_counter = 0;
			$.searchbox.load_timer = setTimeout(arguments.callee, $.searchbox.load_time);
		})();
		
				
	});

});

