//<![CDATA[
var map;
var marker    = new Array();
var points    = new Array();
var maxlat;
var minlat;
var maxlon;
var minlon;
var icon              = new GIcon();
icon.image            = "../../../images/map_icon.png";
icon.shadow           = "../../../images/map_icon_shadow.png";
icon.iconSize         = new GSize(48, 37);
icon.shadowSize       = new GSize(48, 37);
icon.iconAnchor       = new GPoint(18, 37);
icon.infoWindowAnchor = new GPoint(15, 20);

function entries_searcher(locality_id) {
	jQuery.ajax({
		type   : "GET",
		url    : "../../../map_data/entries_narrowed_by_locality/" + locality_id,
		success: create_entries
	});
}

function serialize_node(node) {
  if (typeof XMLSerializer != "undefined")
    return (new XMLSerializer()).serializeToString(node);
  else if (node.xml) return node.xml;
  else return "";
}

function format_entry_text(element, maxlen) {
  var list = element.childNodes;
  var len = 0, fulltext = "", summary = "";
  var i;

  for (i = 0; i < list.length; i++){
    switch(list[i].tagName){
    case undefined:  // text node
      var text = list[i].nodeValue;
      fulltext += text;
      text = text.replace(/\n/g, "");
      if (maxlen && len < maxlen){
        text = text.substr(0,maxlen-len);
        summary += text;
        len += text.length;
      }
      break;
    case "img":      // emoticon is expected
      var text = serialize_node(list[i]);
      fulltext += text;
      if (maxlen && len < maxlen){
        summary += text;
        len += 1;
      }
      break;
    }
  }
  return [fulltext, summary];
}

function create_entries(httpObj) {
	var XML  = httpObj.getElementsByTagName("record");
	if(XML.length != 0){
		var html = "<ul>";
		for(var i=0; i<XML.length; i++){
			//var title       = XML[i].getElementsByTagName("title")[0].firstChild.nodeValue;
			//var description = XML[i].getElementsByTagName("description")[0].firstChild.nodeValue;
			var title       = XML[i].getElementsByTagName("title")[0];
			titles          = format_entry_text(title, null);
			var noimg_title = XML[i].getElementsByTagName("noimg-title")[0].firstChild.nodeValue;
			var description = XML[i].getElementsByTagName("description")[0];
			descriptions    = format_entry_text(description, 24);
			var at          = XML[i].getElementsByTagName("at")[0].firstChild.nodeValue;
			var id          = XML[i].getElementsByTagName("id")[0].firstChild.nodeValue;
			var lat         = XML[i].getElementsByTagName("latitude")[0].firstChild.nodeValue;
			var lon         = XML[i].getElementsByTagName("longitude")[0].firstChild.nodeValue;
			var photo_id    = XML[i].getElementsByTagName("photo-id")[0].firstChild.nodeValue;
			var travel_id    = XML[i].getElementsByTagName("travel-id")[0].firstChild.nodeValue;

			points[i]  = new GLatLng(lat, lon);
			marker[id] = new GMarker(new GPoint(lon, lat), icon);

			marker[id].id          = id;
			//marker[id].title       = title;
			//marker[id].description = description;
			marker[id].title       = titles[0];
			marker[id].noimg_title = noimg_title;
			marker[id].description = descriptions[0];
			marker[id].at          = at;
			marker[id].photo_id    = photo_id;
			marker[id].travel_id    = travel_id;

			map.addOverlay(marker[id]);

			GEvent.addListener(marker[id], "click", function() {
				open_info_window(this.id);
			});

			if (maxlat == null) {
				maxlat = lat;
					minlat = lat;
					maxlon = lon;
					minlon = lon;
				}
				if (maxlat < lat) {
					maxlat = lat;
				} else if (minlat > lat) {
					minlat = lat;
				}
				if (maxlon < lon) {
					maxlon = lon;
				} else if (minlon > lon) {
					minlon = lon;
				}
		}

		var latlngbounds  = new GLatLngBounds(new GLatLng(minlat, minlon), new GLatLng(maxlat, maxlon));
		var center        = new GLatLng((parseFloat(maxlat) + parseFloat(minlat)) / 2.0, (parseFloat(maxlon) + parseFloat(minlon)) / 2.0);
		var zoom_level    = map.getBoundsZoomLevel(latlngbounds);

		map.setCenter(center, zoom_level);
	}
}

function open_info_window(id) {
	var html = '<table cellspacing="0" cellpadding="0" border="0" width="274">';
	html += '<tr valign="top">';
	//html += '<a href=""><img src="../../../images/btn_balloon_close.gif" width="14" height="14" alt="閉じる" /></a>';
	html += '<td colspan="2" align="left"><p class="date">' + marker[id].at + '</p></td>';
	html += '</tr>';
	html += '<tr valign="top">';
	html += '<td colspan="2" align="left" class="pddB5">';
	html += '<em><a href="../../../travels/entries/' + marker[id].travel_id + '#entry' + marker[id].id + '">' + marker[id].title + '</a></em>';
	html += '</td>';
	html += '<tr valign="top">';
	html += '<td align="left" width="85">';
	if (marker[id].photo_id > 0) {
		html += '<img src="../../../photos/small/' + marker[id].photo_id + '" class="phSizeS" alt="' + marker[id].noimg_title + '" />';
	} else {
		html += '<img src="../../../images/noimage_80x80.gif" class="phSizeS" alt="" />';
	}
	html += '</td>';
	html += '<td align="left" width="189">';
	html += marker[id].description.replace(/\n/g, "<br />");
	html += '</td>';
	html += '</tr>';
	html += '</table>';
	marker[id].openInfoWindowHtml(html);
}

//]]>
