//<![CDATA[

var map;
var geocoder = new GClientGeocoder();

window.onload = function(){
	
	gMapLoad();
	
	getObject('stateDropdown').selectedIndex = 0;
	getObject('stateDropdown').onchange = function(){
		
		state = this.options[this.selectedIndex].value;
		state_name = this.options[this.selectedIndex].text;
		zoom = ( state.toLowerCase() == "alaska" )? 3 : 6 ;
		processAddressArray( state );
		zoomCenterToAddress( state_name ); 
		
	}
	
	getObject('reset_button').onclick = function(){
		getObject('stateDropdown').selectedIndex = 0;
		map.setCenter(new GLatLng(40,-93), 3);
		processAddressArray( null );
	}
	
}


// Creates a marker at the given point with the given number label
function createMarker(point, label) {
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(label);
	});
	return marker;
}


var bigPoint;
var zoom = 6;
function zoomCenterToAddress(address) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
        map.setCenter(point, zoom);
        /*
        var marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(address);
        bigPoint = point;
        map.removerOverlay(marker);
        */
      }
      
    }
  );
//alert(typeof(bigPoint));
}

var locationsArray = new Array();

function gMapLoad(argLocationSelected) {
	
	try{
		if (GBrowserIsCompatible()) {
			
			map = new GMap2(document.getElementById("gMap"));
			map.addControl(new GLargeMapControl());
			
			map.setCenter(new GLatLng(40,-93), 3);
			
			processAddressArray( null );
//			for (var i = 0; i < loc_array.length; i++) {
//
//				var lat = parseFloat( loc_array[i]['loc_latitude'] );
//				var lng = parseFloat( loc_array[i]['loc_longitude'] );
//								
//				var point = new GLatLng(lat,lng);
//				
//				var bubble_text = ( loc_array[i]['loc_name'] != "" )? "<strong>"+loc_array[i]['loc_name']+"</strong><br />" : "" ;
//					bubble_text += ( loc_array[i]['typeName'] != "" )? "<small>"+loc_array[i]['typeName']+"</small><br />" : "" ;
//					bubble_text += ( loc_array[i]['loc_address'] != "" )? loc_array[i]['loc_address'] : "" ;
//					bubble_text += ( loc_array[i]['loc_address2'] != "" )? " "+loc_array[i]['loc_address2']+"<br />" : "<br />" ;
//					bubble_text += loc_array[i]['loc_city']+", "+loc_array[i]['stateAbbrv']+" "+loc_array[i]['loc_zip'];
//					
//				var html = trim("<span>"+bubble_text );
//				
//				if (loc_array[i]['loc_contact_info']) {
//					html += trim("<br /><br /><strong>Contact:</strong><br />" + loc_array[i]['loc_contact_info']);
//				}
//				
//				html += "</span>";
//				
//				var marker = createMarker(point,html);
//				map.addOverlay(marker);
//				
//			}
			
			if (stateAbbrevToZoom) {
				zoomCenterToAddress(stateAbbrevToZoom);
			}
			
		}
	}catch(e){ throwException( 'locations.js : gMapLoad()', e.lineNumber, e, e.message ); }
}



function processAddressArray( state ){
	
	locationsArray = null;
	map.clearOverlays();
	
	if( state != null ){
		
		//alert( loc_array[0]['stateAbbrv'] );
		filterLocationArray( state );
		
	}else{
		
		locationsArray = loc_array;
		
	}
	
	if( locationsArray != null ){
		for (var i = 0; i < locationsArray.length; i++) {
	
			var lat = parseFloat( locationsArray[i]['loc_latitude'] );
			var lng = parseFloat( locationsArray[i]['loc_longitude'] );
							
			var point = new GLatLng(lat,lng);
			
			var bubble_text = "";
				bubble_text += ( locationsArray[i]['typeName'] != "" )? "<strong>"+locationsArray[i]['typeName']+"</strong><br />" : "" ;
				bubble_text += ( locationsArray[i]['loc_name'] != "" )? "<strong>"+locationsArray[i]['loc_name']+"</strong><br />" : "" ;
				bubble_text += ( locationsArray[i]['loc_address'] != "" )? locationsArray[i]['loc_address'] + " " + locationsArray[i]['loc_address2']+"<br />": "" ;
				bubble_text += locationsArray[i]['loc_city']+", "+locationsArray[i]['stateAbbrv']+" "+locationsArray[i]['loc_zip'];
				
			var html = trim("<span>"+bubble_text );
			
			if (locationsArray[i]['loc_contact_info']) {
				html += trim("<br /><br /><strong>Contact:</strong> " + locationsArray[i]['loc_contact_info']);
			}
			
			html += "</span>";
			
			var marker = createMarker(point,html);
			map.addOverlay(marker);
			
		}
	}
	
}



function filterLocationArray( state ){
	
	var a = 0;
	locationsArray = new Array();
	 
	for( i = 0; i < loc_array.length; i++ ){
		
		if( loc_array[i]['stateAbbrv'].toUpperCase() == state.toUpperCase() ){
			
			locationsArray[a] = loc_array[i];
			a++;
			
		}
				
	}

}






//### throwException( file_name, line_num, ex, msg )
//##
//#	  function that gives file LINE NUMBER when a JS Exception is thrown
function throwException( file_name, line_num, ex, msg ){
	
	var msg = "Error in FILE: " +file_name+ " LINE: " +line_num+ "\n\nTYPE: " +ex+ "\n\n" +msg+ "\n\n" +ex.stack;
	
	if( getObject('error_div') ){
		getObject('error_div').innerHTML = "<pre class=\"error\">"+msg+"</pre>";
	}else{
		alert( msg );
	}

}


function getObject( oID ){
	try{
		return document.getElementById(oID);
	}catch(e){ alert( e ); } 
}

//]]