/*--------------------------------------------------------------------------------------------------------------------------------*/
function initInstIdxGM(instmap) {
	
	//writeToElement(instmap.mapid+'_resultdiv','initInstIdxGM','');
	// tweak the display depending if the nav index is being requested
	
	// display the map table
	showElement(instmap.mapid+'_maptable');
	
	if (instmap.indexsize>=0) {
		showElement(instmap.mapid+'_resultidxcell');
	} else {
		// no index - adjust map width
		//instmap.width	+= 350;
		instmap.height	 = (instmap.width*.75);
	};
	
	
	
	var mapid	= instmap.mapid+'_map';
	writeToElement(instmap.mapid+'_resultdiv','mapid '+mapid,'');

	//if (isDev()) {
	//	alert('devmsg: ' + instmap.mapid);
	//}

	//alert(mapid);
	//return;
	
	var mapdiv	= document.getElementById(mapid);
	//var mapdiv	= document.getElementById('instmap_map');
	mapdiv.style.width		= instmap.width + 'px';
	mapdiv.style.height		= instmap.height + 'px';
	mapdiv.style.border		= '1px solid black';

	//writeToElement(instmap.mapid+'_resultdiv','<br>'+instmap.width+' ','a');
	//writeToElement(instmap.mapid+'_resultdiv','<br>'+instmap.height+'<br>','a');

	try {
		var map 	= new google.maps.Map2(mapdiv);
		if (instmap.height>290) {	map.addControl(new GLargeMapControl());
		} else {					map.addControl(new GSmallMapControl());}
		
		map.addControl(new GMapTypeControl());
		
		// default map
		instmap.lng		= -100.634765625;
		instmap.lat		= 41.11246878918085;
		instmap.zoom	= instmap.zoom ? instmap.zoom : 3;
		instmap.latlng	= new google.maps.LatLng(instmap.lat,instmap.lng);
		
		map.setCenter(instmap.latlng, instmap.zoom);
	
		instmap.map	= map;
		
		findMapWithAddress(instmap);
	}
	catch (err) {
		hideElement(instmap.mapid+'_maptable');
	}

}
/*--------------------------------------------------------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------------------------------------------------------*/
function findMapWithAddress(instmap) {

	var addrstr				= '';
	//var addressfields		= ['state','country'];
	var addressfields		= ['city','state','country'];
	for (fieldidx in addressfields) {
		var addrfield		= addressfields[fieldidx];
		var field			= instmap.address[addrfield] ? instmap.address[addrfield] : '';
		if (field.length>0) {
			// add to the search string
			addrstr	+= addrstr.length>0 ? ', '+field : field;
		}
	}	
	if (addrstr.length>0) {
		// set the address search string in the instlocation obj
		instmap.addrstr	= addrstr;
	}

	// create a google search with the address string from above
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(instmap.addrstr,
		function(point) {
			if (!point) {
				instmap.zoom	= 3;
				//instmap.latlng	= new google.maps.LatLng(instmap.lat,instmap.lng);
				
				//map.setCenter(instmap.latlng, instmap.zoom);
			} else {
				// Found - center point and set selected zoom
				
				if (!instmap.zoom) {
					instmap.zoom	= 3;
				}

				instmap.zoom	= instmap.address['state'] ? 6 : instmap.zoom;
				instmap.zoom	= instmap.address['city'] ? 10 : instmap.zoom;
				
				instmap.map.setCenter(point, instmap.zoom);
				instmap.latlng	= point;
				
				rdiv	= document.getElementById(instmap.mapid+'_resultdiv');
				if (rdiv.innerHTML.match(/wait/i)) {
					msg	= "<br>viewing " + instmap.addrstr + "<br> ";
					writeToElement(instmap.mapid+'_resultdiv',msg,'a');
				}
			}
		}
	);
	//*/
}
/*--------------------------------------------------------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------------------------------------------------------*/
function addInstMarker(instmap,instrec) {

	// Create google's "tiny" marker icon
	var rkcicon 		= new GIcon(G_DEFAULT_ICON);
	//blueIcon.image 		= "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
	rkcicon.image 		= instrec.mapicon;

	// Set up our GMarkerOptions object
	markerOptions 		= { icon:rkcicon };

	var marker		= new GMarker(instrec.latlng, markerOptions);

	// marker popup html
	var instpageurl	= 'http://www.dragondoor.com/instructor/' + instrec.instid;
	var markerHTML	 = '<table><tr><td style="font-size:10pt;text-align:left">';
	markerHTML		+= '<img src="' + instrec.badgesrc + '" border="0">';
	markerHTML		+= '</td><td style="font-size:10pt;text-align:left">';
	markerHTML		+= '<a href="' + instpageurl + '"><b>' + instrec.name + '</b></a>';
	markerHTML		+= '<br><font style="font-size:8pt">' + instrec.desc + '</td></tr></table>';

	marker.bindInfoWindowHtml(markerHTML);

	// add the new marker to the map
	if (instmap.map) {	instmap.map.addOverlay(marker);	};

	GEvent.addListener(marker,"mouseover", function(latlng) {
		var instlocrec	= getInstRecAtLatLng(instmap,latlng);

		// html to display in the mouseover preview area (under the map)
		var markerMO	 = '<table style="margin-left:20px;"><tr><td style="font-size:10pt;text-align:left;width:200px;height:35px;">';
		markerMO		+= '<b>' + instrec.name + '</b><br>' + instrec.certname;
		markerMO		+= '</td><td style="font-size:10pt;text-align:left">';
		markerMO		+= '' + instrec.addrstr + '';
		markerMO		+= '</td></tr></table>';
		//var msg		= '<div style="margin-left:10px"><b>' + instlocrec.name + '</b>';
		//msg			+= '<br><font style="font-size:8pt">' + instlocrec.desc + '</font></div>';
		writeToElement(instmap.mapid+'_resultdiv',markerMO,'');
		
		//msg	= '<br>' + instmap.mapid+'_resultdiv';
		//writeToElement('instmap_resultdiv',msg,'a');
	});

	GEvent.addListener(marker,"mouseout", function(latlng) {
		writeToElement(instmap.mapid+'_resultdiv','<div style="height:40px">&nbsp;</div>','');
	});

	// handle clicks
	//GEvent.addListener(map,"click", function(overlay, latlng) {     
	//	if (latlng && selectlocation) {	setLocation(map,latlng);	}
	//});

	GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(markerHTML);
	});
}
/*--------------------------------------------------------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------------------------------------------------------*/
// Managing Instructor data
/*--------------------------------------------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------------------------------------------*/
function activateInstMap(mapobj) {

	if (!mapobj.map) {
		initInstIdxGM(mapobj);
		requestInstData(mapobj);
	} else {
		// toggle the map table display
		showhide(mapobj.mapid+'_maptable');
	}
}
/*--------------------------------------------------------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------------------------------------------------------*/
function toogleInstMap(mapid) {

	alert(mapid);
	// mapid should be set to the map's object name
	try {
		eval('var mapobj = ' + mapid);
		activateInstMap(mapobj);
	}
	catch (e) {
		//alert(e);
	}
}
/*--------------------------------------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------------------------------------*/
function requestInstData(mapobj) {
	
	var url	= 'http://www.dragondoor.com/cgi-bin/google_maps.pl';

	var loc		= document.location.toString();
	var mode	= loc.match(/https/i) ? 'https' : 'http';
	url			= url.replace(/https*/, mode);

	var request	= new Object();
	request.loadmode	= mapobj.loadmode;	
	request.loaddata	= mapobj.loaddata;
	request.kmldata		= 1;
	
	if (mapobj.certname) {
		request.certname		= mapobj.certname;
	};

	var status = AjaxRequest.get(
		{	'url'			: url,
			'onSuccess'		: function(req){ requestInstDataResponse(mapobj,req.responseText); },
			'onLoading'		: function(req) { writeToElement(mapobj.mapid+'_resultdiv','requesting data, please wait...'); },
			'parameters'	: request,
			'instdatareq'	: '1'
		}
	);
	return status;

/*
 			'loadmode' 		: mapobj.loadmode,
			'loaddata' 		: mapobj.loaddata

*/
}
/*--------------------------------------------------------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------------------------------------------------------*/
function requestInstDataResponse(mapobj,response) {

	var msg		= 'response received';

	try {
		// load the response JSON
		eval("var result = ("+response+")");
		
		if (result) {

			if (result.success) {
					msg	= result.success;

					if (result.resultidx) {writeToElement(mapobj.mapid+'_resultidx',result.resultidx,'');}
					if (result.instdata) {
						var rmsg	= processInstData(mapobj,result.instdata);
						
						//responsemsg		= '<pre>' + response + '<pre>';
						//writeToElement('debugmsg',responsemsg,'a');
					}

			} else if (result.error) {	msg	= result.error;
			} else {					msg = 'unexpected error - invalid response received';	}


		} else { msg = 'unexpected error - empty response received'; }

	}
	catch(err) {
			msg		= "response error: " + err;
			msg		+= "<br>- " + response;
	}

	msg	+= "<br>viewing " + mapobj.addrstr + "<br> ";
	writeToElement(mapobj.mapid+'_resultdiv',msg,'');
	//if (!msg) {	writeToElement(mapobj.mapid+'_resultdiv',response,''); };

}
/*--------------------------------------------------------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------------------------------------------------------*/
function processInstData(mapobj,instdata) {
	
	var msg		= '<br>';

	mapobj.instdata			= instdata;
	for (inst in instdata) {
		var instloc		= loadInstLocation(mapobj,instdata[inst]);
		msg		+= ' - ' + instloc + '<br>';

		//msg		+= ' - ' + instdata[inst].name + '<br>';
		//msg		+= '&nbsp;&nbsp;&nbsp;' + instdata[inst].lat + ', ' + instdata[inst].lng + '<br>';
	}
	return msg;
}
/*--------------------------------------------------------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------------------------------------------------------*/
function loadInstLocation(mapobj,instrec) {

	instrec.mapicon	= 'http://www.dragondoor.com/images/kml/teardrop_rkc_redgold.png';
	instrec.latlng	= new google.maps.LatLng(instrec.lat,instrec.lng);
	addInstMarker(mapobj,instrec);

	var msg		= '<br>';
	//	msg		+= ' - ' + instrec[inst].name + '<br>';
	//	msg		+= '&nbsp;&nbsp;&nbsp;' + instrec.lat + ', ' + instrec.lng + '<br>';
	return msg;
}
/*--------------------------------------------------------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------------------------------------------------------*/
function getInstRecAtLatLng(mapobj,latlng) {

	for (inst in mapobj.instdata) {
		if (mapobj.instdata[inst].latlng == latlng) { return mapobj.instdata[inst]
		} else {
		}
	}

	return false;
}
/*--------------------------------------------------------------------------------------------------------------------------------*/
