$(document).ready(function(){
	$('#search-button').click(function(){
		search($('#keywords').val(),0,100);
	});

	var loginMember = $.cookie('member');
	if(loginMember != null && loginMember != '0'){
		var node = $('<div style="margin:10px; text-align:center; width:400px; position:relative;" />');
		node.append($('<div style="position:absolute; top:5px; left:330px; width:250px; text-align:left;" />')
						.append($('<span class="style1" />')
									.append($('<img src="image/arrow4.png" alt="*" />'))
									.append('地図上の酒蔵詳細情報から、出発地・目的地に住所を設定できます。<br/>')
									.append($('<img src="image/arrow4.png" alt="*" />'))
									.append('そのほか、酒蔵名／住所／駅名が設定できます。')
								)
					);
		node.append($('<form name="routeSearch" style="margin:10px; padding:0; text-align:left;" />')
						.append('出発地：<input type="text" name="from" id="from" value="" maxlength="255" style="width:250px; margin:5px 0 0 0; padding:0; font-size:100%;" /><br />')
						.append('目的地：<input type="text" name="to" id="to" value="" maxlength="255" style="width:250px; margin:5px 0 5px 0; padding:0; font-size:100%;" /><br />')
						.append('<input type="button" id="btn" value="ルート案内" onclick="dispRoute()" style="font-size:100%;" />')
						.append('<input type="button" id="btn2" value="クリア" onclick="clearRoute()" style="font-size:100%;" />')
						.append('<input type="button" id="btn_print" value="印刷" onclick="print()" style="font-size:100%;" />')
					);
		$('#route-box').append(node);
		document.getElementById("btn_print").style.visibility = "hidden";
	} else {
		var node = $('<div style="margin:10px; text-align:center;" />');
		node.append('ルート検索は');
		node.append($('<a />').attr('href','mypage.cgi').append('酒蔵マップ会員'));
		node.append('限定機能です。');
		$('#route-box').append(node);			
	}
});

function search(keywords,offset,limit){
	$.get('search.cgi',{'s':keywords,'n':offset,'m':limit},function(data,stat){
		$('#search').empty();
		tags = data.getElementsByTagName('search');
		for(var i = 0; i < tags.length; i++){
			var news = {
				'id' : tags[i].getAttribute('id'),
				'name' : tags[i].getAttribute('name'),
				'url' : tags[i].getAttribute('url'),
				'date' : tags[i].getAttribute('date') };
			var memo = '';
			for(var j = 0; j < tags[i].childNodes.length; j++){
				var cn = tags[i].childNodes[j];
				if (cn.nodeType == 3) {
					memo += cn.nodeValue;
				}
			}
			if(memo.length > 100){
				news['memo'] = memo.substring(0,100) + '...';
			}else{
				news['memo'] = memo;
			}
			var node = $('<div class="news_link" />');
			node.append($('<img src="./image/arrow.png" alt="*" />')).append('&nbsp;');
			node.append($('<a />').attr('href','javascript:viewIdMarker('+news['id']+',10);').append(news['name']));
			//node.append('&nbsp[').append($('<a />').attr('href',news['url']).append('HP')).append(']');
			node.append('&nbsp[').append($('<a />').attr('href','javascript:jump('+ news['id'] +',\''+ news['url'] +'\');').append('HP')).append(']');
			node.append($('<div class="news_comment">').append(news['memo']));
			node.append($('<div class="news_date">').append('('+news['date']+')'));
			$('#search').append(node);
		}
	},'xml');
}
