/*
	javascript for the map of locations
*/

var map;
var control;
var bar;

function onLoad() {
	map = new GMap2(document.getElementById('map'));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	control = new BpControl(js_empty_map,new GSize(69,7),G_ANCHOR_TOP_LEFT,'bpcontrol');
	map.addControl(control);
  control.show();
 	map.setCenter(new GLatLng(41.50857729743935, -99.84375),3);

  if(BpBrowser.type == BpBrowser.MSIE) {
    map.openInfoWindowHtml(map.getCenter(),' ');
    map.closeInfoWindow();
  }

	setupGetOverlays(map);
	setupGetMarkers(map);
	setupZoomToMarkers(map);
	setupMapArray(map);

	map.getMarkerById = function(id) {
		var m = this.getMarkers();
		for(var i = 0; i < m.length; i++)
			if(m[i].getUserData().loc_id == id)
				return m[i];
	};

	bar = new BpMarkerList(document.getElementById('sidebar'),'bar','sidebar-highlight','sidebar-lowlight');
	bar.setHtmlTemplate('<div><b><a href="javascript:void(0)" onclick="GEvent.trigger(map.getMarkerById([loc_id]),\'click\')">[loc_name]</a></b><br/>[loc_desc]</div>');

	GEvent.addListener(map,'infowindowclose',onIwClose);

  if(js_id_to_show > 0) {
    BpDownloadUrl(js_json_url+'?id='+js_id_to_show, mapArray);
  }
}

var openerMarker = null;
function onIwClose() {
	bar.lowlight(openerMarker);
	openerMarker = null;
}

function JfMarker() {
	BpMarker.apply(this,arguments);
}
JfMarker.prototype = new BpMarker(new GLatLng(0,0));

JfMarker.prototype.initialize = function(map) {
	BpMarker.prototype.initialize.call(this,map);
	this._clickListener = GEvent.addListener(this,'click',this.onClick);
	bar.add(this);
};

JfMarker.prototype.remove = function() {
	BpMarker.prototype.remove.call(this,map);
	GEvent.removeListener(this._clickListener);
	delete this._clickListener;
	bar.remove(this);
	if(openerMarker === this)
		map.closeInfoWindow();
};

JfMarker.prototype.onClick = function() {
	if(openerMarker === this) {
		map.closeInfoWindow();
	} else {
		map.closeInfoWindow();
		openerMarker = this;
		var infoTabs = [
			new GInfoWindowTab("Info", this.getInfoPaneContent()),
			new GInfoWindowTab("Directions", this.getDirPaneContent()),
			new GInfoWindowTab("Images", this.getPicPaneContent())
		];

		this.openInfoWindowTabsHtml(infoTabs);

//    if(BpBrowser.type != BpBrowser.MSIE)
//      setTimeout(function(){ bar.highlight(tis); },200);
//	  else
	    bar.highlight(this);
	}
};

JfMarker.prototype.getInfoPaneContent = function() {
	if(this._infoPaneContent)
		return this._infoPaneContent;

	var d = this.getUserData();
	this._infoPaneContent = '<div class="iw-div">' +
		"<b>" + (d.loc_name ? d.loc_name : '(no name)' ) + "</b>" +
		"<br/>" +
		( d.loc_address1 ? d.loc_address1 + "<br/>" : "" ) +
		( d.loc_address2 ? d.loc_address2 + "<br/>" : "" ) +
		( d.loc_city     ? d.loc_city     + ", "    : "" ) +
		( d.loc_state    ? d.loc_state    + " "     : "" ) +
		( d.loc_zip      ? d.loc_zip      + ""      : "" ) +
		"<br/>" +
		d.loc_desc +
		"<p/>" +
		( d.loc_phone   ? "<b>Phone</b>: "    + d.loc_phone + "<br/>" : "" ) +
		( d.loc_fax     ? "<b>Fax</b>: "   + d.loc_fax   + "<br/>" : "" ) +
		( d.loc_hours   ? "<b>Hours</b>: " + d.loc_hours  + "<br/><br/>" : "" ) +
		( d.loc_custom1 ? "<br/>"   + d.loc_custom1 : "" ) +
		( d.loc_custom2 ? "<br/>"   + d.loc_custom2 : "" ) +
		( d.loc_custom3 ? "<br/>"   + d.loc_custom3 : "" ) +
		"</div>";

	return this._infoPaneContent;
};

JfMarker.prototype.getDirPaneContent = function() {
	if(this._dirPaneContent)
		return this._dirPaneContent;

	var name = this.getUserData().loc_name;
	var point = this.getPoint();
	this._dirPaneContent = '<form action="http://maps.google.com/maps" target="_blank">' +
		'<div class="iw-div"><b>Directions to ' + name + '</b>' +
    '<br /><br />Starting Address: ' +
    '<input type="text" SIZE=20 MAXLENGTH=100 name="saddr" id="saddr" value="" /><br /><br />' +
    '<INPUT value="Get Directions" TYPE="SUBMIT">' +
    '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + '"/></form>' + 
    '</div>';

	return this._dirPaneContent;
};

JfMarker.prototype.getPicPaneContent = function() {
	if(this._picPaneContent)
		return this._picPaneContent;

	var name = this.getUserData().loc_name;
	var title = "<b>Images for " + name + "</b><br/>";
	var images = this.getUserData().images;
	var iarr = [];
	for(var i = 0; i < images.length; i++) {
		iarr.push('<a href="/'+js_base+'/img/loc_images/' + images[i].image_name + '" target="_blank"><img border="0" src="/'+js_base+'/thumbs?src=/img/loc_images/' + images[i].image_name + '"/></a>');
	}
	if(iarr.length == 0)
		iarr.push("<br />There are no images for this location.");
	this._picPaneContent = '<div class="iw-div">' + title + iarr.join(' ') + '</div>';

	return this._picPaneContent;
};

// end of JfMarker definition

function doSearch() {
	var m = map.getMarkers();
	for(var i = 0; i < m.length; i++)
		map.removeOverlay(m[i]);

	var query = [];
	var name     = document.getElementById('form_name').value;
	if(name)
		query.push("name=" + escape(name));
	var city     = document.getElementById('form_city').value;
	if(city)
		query.push("city=" + escape(city));
	var s = document.getElementById('form_state');
	var state    = s.options[s.selectedIndex].value;
	if(state)
		query.push("state=" + escape(state));
	var zip      = document.getElementById('form_zip').value;
	if(zip)
		query.push("zip=" + escape(zip));
	var w = document.getElementById('form_within');
	var within   = w.options[w.selectedIndex].value;
	if(within)
		query.push("within=" + escape(within));
	var keywords = document.getElementById('form_keywords').value;
	if(keywords)
		query.push("keywords=" + escape(keywords));
	
	BpDownloadUrl(js_json_url+'?'+query.join('&'), mapArray);
}

function mapArray(json) {
	json = json.replace(/<!--.+-->$/,'');
	var obj;
	try {
		obj = eval(json);
	} catch(e) {
		alert(e + RegExp.$1);
	}

	// check that we have an array, and that the first field is true, indicating success
	if(obj.length == 0 || !obj[0]) {
		alert(obj[1] ? obj[1] : 'There was an unknown server error.\nPlease try again later.');
		return;
	}

	obj = obj[1];
	if(obj.length == 0) {
		alert('There were no matches to your search.');
		return;
	}

  if(obj.length == max_count)
    control.setContent(js_showing_some.replace(/\[count\]/,max_count));
  else
    control.setContent(js_showing_all);

	for(var i = 0; i < obj.length; i++) {
		if(obj[i].Location.loc_latitude != 0 && obj[i].Location.loc_longitude != 0) {
			var marker = new JfMarker(new GLatLng(obj[i].Location.loc_latitude,obj[i].Location.loc_longitude));
			var d = obj[i].Location;
			d.images = obj[i]['Image'];
			marker.setUserData(d);
			map.addOverlay(marker);
		}
	}

	map.zoomToMarkers();
}