// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
//
var map;
var mgr;
var allmarkers=[];

var countrylocations;
var statelocations;
var citylocations;

function mapload(){
	if (GBrowserIsCompatible()) {
        	map = new GMap2($("map"));
		map.addControl(new GSmallMapControl());
	//	map.addControl(new GOverviewMapControl()); 
        	map.setCenter(new GLatLng(35,-93), 3);
      		mgr = new GMarkerManager(map, {trackMarkers:true});
		new Ajax.Request('/hymotion/community/get_country_level',
		{
			method:'get',
			onSuccess: function(transport){
				var response = transport.responseText || "no response text";
				countrylocations = response.evalJSON();
				getStates();
			},
			onFailure: function(){alert('something went wrong...')}
		});	
	}
	$("details").hide();
}
function getStates(){
		new Ajax.Request('/hymotion/community/get_state_level',
		{
			method:'get',
			onSuccess: function(transport){
				var response = transport.responseText || "no response text";
				statelocations = response.evalJSON();
				getzip();
			},
			onFailure: function(){alert('something went wrong...')}
		});
}
function getzip(){
		new Ajax.Request('/hymotion/community/get_all_posts',
		{
			method:'get',
			onSuccess: function(transport){
				var response = transport.responseText || "no response text";
				citylocations = response.evalJSON();
				window.setTimeout(setupMarkers,0);
			},
			onFailure: function(){alert('something went wrong...')}
		});
	mgr.refresh();
}
function setupMarkers(){
	var markers = [];
	for(var i = 0; i < countrylocations.length;i++){
		var posn = new GLatLng(countrylocations[i].latitude, countrylocations[i].longitude);
		var marker = new createMarker(posn, countrylocations[i].country,"country");
		markers.push(marker);
	}	
	mgr.addMarkers(markers,0,2);
	mgr.refresh();
	var smarkers = [];
	for(var i=0; i<statelocations.length;i++){
		var posn = new GLatLng(statelocations[i].latitude, statelocations[i].longitude);
		var marker = new createMarker(posn,statelocations[i].state,"state");
		smarkers.push(marker);	
	}
	mgr.addMarkers(smarkers,3,4);
	mgr.refresh();
	cmarkers = [];                                         
	for(var i =0; i<citylocations.length;i++){
		var posn = new GLatLng(citylocations[i].latitude,citylocations[i].longitude);
		var marker = new createMarker(posn,citylocations[i].zipcode,"zipcode");
		cmarkers.push(marker);
	}
	mgr.addMarkers(cmarkers,5,17);
	mgr.refresh();

}
function createMarker(posn, title, type){
	var custIcon = new GIcon(G_DEFAULT_ICON);
        custIcon.image = "/hymotion/images/mapicon.png";
	custIcon.transparent="/hymotion/images/mapicon.png";
	custIcon.imageMap = [0,0,47,0,47,56,0,56];
	custIcon.iconSize = new GSize(44, 45);
	custIcon.iconAnchor = new GPoint(23, 56);
	custIcon.infoWindowAnchor = new GPoint(23, 0);
	
	//custIcon.infoShadowAnchor = new GPoint(18, 25);
	
	var marker = new GMarker(posn, {icon:custIcon, title: title, type: type});
	//var marker = new GMarker(posn, {title: title, type: type});
	GEvent.addListener(marker, "click", function() {
				new Ajax.Request('/hymotion/community/blips?'+type+'='+title,
				{
				method:'get',
				onSuccess: function(transport){
					var response = transport.responseText || "no response text";
					var data = response.evalJSON();
					marker.openInfoWindowHtml(generateCustomHtml(data));
				},
				onFailure: function(){alert('something went wrong...');}
				});
	});
	return marker;
}

var inBubble;

function generateCustomHtml(data){
	result = "";
	result +="<div class=\"bubble\">";
	result +="<ul>";
	for(var i = 0; i < data.length; i++){
		result += "<li><a href=\"#\" onclick=\"details("+data[i].id+");return false;\"><img src=\"/hymotion/post/picture/"+data[i].id+"/thumb/"+data[i].picturepath+"\"/>"
		       + "<span class=\"displayname\">"+data[i].display_name+"</span><br/>"
		       + data[i].city+", "+data[i].state
		       + "</a><div class=\"clear\"></div></li>";
	}
	result +="</ul>";
	result +="</div>";
	inBubble = data;
	return result;
}


function details(id){
	for(var i = 0; i <inBubble.length; i++){
		if(id==inBubble[i].id){
			$('details').show();
			$('details').replace('<div id=\"details\">'
			+"<div class=\"column span-5\"><span class=\"hymotiongreen caps\"> "+inBubble[i].display_name+"</span><br/>"
			+"<img src=\"/hymotion/post/picture/"+inBubble[i].id+"/medium/"+inBubble[i].picturepath+"\"/>"
			+"</div>"
			+"<div class=\"column span-16 last\">"+inBubble[i].what_say_you
			+"<ul><li class=\"odd\"><div>Location</div>"+inBubble[i].city+", "+inBubble[i].state+"</li>"
			+"<li class=\"even\"><div>Car make, model, year</div>"+inBubble[i].make+", "+inBubble[i].model+", "+inBubble[i].year+"</li>"
			+"<li class=\"odd\"><div>Daily commute</div>"+inBubble[i].daily_commute+"</li>"
			+"<li class=\"even\"><div>Miles Per Gallon</div>"+inBubble[i].miles_per_gallon+"</li>"
			+"</ul>"
			+"</div>"
			+"<div class=\"clear\"></div></div>");	
		}
	}
}
function zoomState(){
	var sw;
	var ne;
        /*$('details').show();
	$('details').replace('<div id=\"details\">case \''+$('state').value+'\':<br/> '
	+ 'sw=new GLatLng' + map.getBounds().getSouthWest()+'; ne = new GLatLng'+map.getBounds().getNorthEast()+';<br/>break;');
*/
	switch($('state').value){
		case 'AL':
			sw=new GLatLng(29.11377539511439, -92.7685546875); ne = new GLatLng(35.96022296929667, -80.4638671875);		
			break;
		case 'AK':
			sw=new GLatLng(58.17070248348609, -175.166015625); ne = new GLatLng(71.63599288330606, -125.94726562500001);
			break;
		case 'AZ':
			sw=new GLatLng(30.977609093348686, -117.421875); ne = new GLatLng(37.68382032669382, -105.1171875);
			break;
		case 'AR':
			sw=new GLatLng(31.409912194070973, -97.998046875); ne = new GLatLng(38.08268954483802, -85.693359375);
			break;
		case 'CA':
			sw=new GLatLng(30.14512718337613, -127.13378906250001); ne = new GLatLng(43.13306116240612, -102.5244140625);
			break;
		case 'CO':
			sw=new GLatLng(35.55010533588551, -111.6650390625); ne = new GLatLng(41.88592102814744, -99.3603515625);
			break;
		case 'CT':
			sw=new GLatLng(40.75557964275588, -74.24560546875); ne = new GLatLng(42.27730877423709, -71.16943359375);
			break;
		case 'DE':
			sw=new GLatLng(37.361425501905146, -78.321533203125); ne = new GLatLng(40.52215098562377, -72.169189453125);
			break;
		case 'DC':
			sw=new GLatLng(37.142803443716836, -80.101318359375); ne = new GLatLng(40.3130432088809, -73.948974609375);
			break;
		case 'FL':
			sw=new GLatLng(24.86650252692691, -89.4287109375); ne = new GLatLng(32.008075959291055, -77.1240234375);
			break;
		case 'GA':
			sw=new GLatLng(29.286398892934763, -89.296875); ne = new GLatLng(36.12012758978146, -76.9921875);
			break;
		case 'HI':
			sw=new GLatLng(18.656654486539995, -160.499267578125); ne = new GLatLng(22.46180203533398, -154.346923828125);
			break;
		case 'ID':
			sw=new GLatLng(39.061849134291535, -127.17773437499999); ne = new GLatLng(50.54136296522161, -102.568359375);
			break;
		case 'IL':
			sw=new GLatLng(36.79169061907076, -95.009765625); ne = new GLatLng(43.02071359427862, -82.705078125);
			break;
		case 'IN':
			sw=new GLatLng(36.84446074079564, -92.26318359375); ne = new GLatLng(43.068887774169625, -79.95849609375);
			break;
		case 'IA':
			sw=new GLatLng(38.99357205820944, -99.99755859375); ne = new GLatLng(45.02695045318543, -87.69287109375);
			break;
		case 'KS':
			sw=new GLatLng(35.28150065789119, -104.39208984375); ne = new GLatLng(41.64007838467891, -92.08740234375);
			break;
		case 'KY':
			sw=new GLatLng(34.488447837809304, -91.4501953125); ne = new GLatLng(40.91351257612757, -79.1455078125);
			break;
		case 'LA':
			sw=new GLatLng(27.955591004642528, -98.67919921875); ne = new GLatLng(34.88593094075315, -86.37451171875);
			break;
		case 'ME':
			sw=new GLatLng(42.212245162885814, -75.12451171875); ne = new GLatLng(47.945786463687185, -62.81982421875);
			break;
		case 'MD':
			sw=new GLatLng(37.509725842937485, -80.057373046875); ne = new GLatLng(40.66397287638688, -73.905029296875);
			break;
		case 'MA':
			sw=new GLatLng(40.6723059714534, -75.047607421875); ne = new GLatLng(43.683763524273346, -68.895263671875);
			break;
		case 'MI':
			sw=new GLatLng(40.38002840251183, -90.17578125); ne = new GLatLng(46.28622391806705, -77.87109375);
			break;
		case 'MN':
			sw=new GLatLng(40.58058466412761, -105.5126953125); ne = new GLatLng(51.781435604431195, -80.9033203125);
			break;
		case 'MS':
			sw=new GLatLng(29.24806324379655, -95.361328125); ne = new GLatLng(36.08462129606931, -83.056640625);
			break;
		case 'MO':
			sw=new GLatLng(35.40696093270201, -98.7451171875); ne = new GLatLng(41.75492216766298, -86.4404296875);
			break;
		case 'MT':
			sw=new GLatLng(40.58058466412761, -122.12402343749999); ne = new GLatLng(51.781435604431195, -97.5146484375);
			break;
		case 'NE':
			sw=new GLatLng(38.70265930723801, -106.23779296875); ne = new GLatLng(44.762336674810996, -93.93310546875);
			break;
		case 'NV':
			sw=new GLatLng(35.79999392988527, -122.80517578125); ne = new GLatLng(42.114523952464246, -110.50048828125);
			break;
		case 'NH':
			sw=new GLatLng(42.512601715736665, -74.77294921875); ne = new GLatLng(45.43700828867389, -68.62060546875);
			break;
		case 'NJ':
			sw=new GLatLng(37.86618078529668, -77.926025390625); ne = new GLatLng(41.004775422229464, -71.773681640625);
			break;
		case 'NM':
			sw=new GLatLng(30.902224705171417, -111.708984375); ne = new GLatLng(37.614231415424165, -99.404296875);
			break;
		case 'NY':
			sw=new GLatLng(40.212440718286466, -80.88134765625); ne = new GLatLng(46.13417004624326, -68.57666015625);
			break;
		case 'NC':
			sw=new GLatLng(31.9148675032762, -87.1435546875); ne = new GLatLng(38.54816542304656, -74.8388671875);
			break;
		case 'ND':
			sw=new GLatLng(44.49650533109345, -106.80908203125); ne = new GLatLng(50.00773901463685, -94.50439453125);
			break;
		case 'OH':
			sw=new GLatLng(37.35269280367273, -87.978515625); ne = new GLatLng(43.5326204268101, -75.673828125);
			break;
		case 'OK':
			sw=new GLatLng(31.877557643340015, -104.12841796875); ne = new GLatLng(38.51378825951165, -91.82373046875);
			break;
		case 'OR':
			sw=new GLatLng(41.21172151054787, -126.123046875); ne = new GLatLng(47.040182144806664, -113.818359375);
			break;
		case 'PA':
			sw=new GLatLng(37.63163475580643, -83.43017578125); ne = new GLatLng(43.78695837311561, -71.12548828125);
			break;
		case 'RI':
			sw=new GLatLng(41.3500103516271, -72.2186279296875); ne = new GLatLng(42.108411365705855, -70.6805419921875);
			break;
		case 'SC':
			sw=new GLatLng(32.01739159980399, -83.814697265625); ne = new GLatLng(35.398005947151056, -77.662353515625);
			break;
		case 'SD':
			sw=new GLatLng(41.73852846935917, -106.36962890625); ne = new GLatLng(47.517200697839414, -94.06494140625);
			break;
		case 'TN':
			sw=new GLatLng(32.287132632616355, -92.900390625); ne = new GLatLng(38.89103282648846, -80.595703125);
			break;
		case 'TX':
			sw=new GLatLng(24.5271348225978, -112.060546875); ne = new GLatLng(38.341656192795924, -87.451171875);
			break;
		case 'UT':
			sw=new GLatLng(36.29741818650808, -117.44384765625); ne = new GLatLng(42.56926437219384, -105.13916015625);
			break;
		case 'VT':
			sw=new GLatLng(42.36666166373274, -75.706787109375); ne = new GLatLng(45.29807513870794, -69.554443359375);
			break;
		case 'VA':
			sw=new GLatLng(34.397844946449844, -85.4736328125); ne = new GLatLng(40.83043687764923, -73.1689453125);
			break;
		case 'WA':
			sw=new GLatLng(44.99588261816546, -126.58447265624999); ne = new GLatLng(50.45750402042055, -114.27978515625);
			break;
		case 'WV':
			sw=new GLatLng(35.79999392988527, -86.41845703125); ne = new GLatLng(42.114523952464246, -74.11376953125);
			break;	
		case 'WI':
			sw=new GLatLng(41.62365539068639, -95.80078125); ne = new GLatLng(47.41322033016902, -83.49609375);
			break;
		case 'WY':
			sw=new GLatLng(39.99395569397331, -113.8623046875); ne = new GLatLng(45.93587062119052, -101.5576171875);
			break;
		default:
			sw=new GLatLng(10.141931686131018, -145.1953125); ne = new GLatLng(59.88893689676585, -46.7578125);
			break;

	}
	bound = new GLatLngBounds(sw,ne);
	map.setZoom(map.getBoundsZoomLevel(bound));
	map.panTo(bound.getCenter());
	
}
