// array of user inputted icons
var marker = {};


   
// Create a base icon for all of our markers that specifies the
var placeIcon = new GIcon();
placeIcon.iconSize = new GSize(61, 53);
placeIcon.iconAnchor = new GPoint(10,35);
placeIcon.image = "http://cadistrict38.org/wp/wp-content/plugins/district_googlemaps/place_icon.png";


			
			
			
jQuery().ready(function($){


	$('#mapframe form').submit(
		function() {



			// if error message previously exists remove it
			if( $('#maperror').length > 0 ) {
				$('#maperror').animate({opacity: 'hide', height: 'hide'}, 250).remove();
//				$('#maperror').remove();
			}



				// users address
		    	address = $('.boundary_address').val();

		    	// Create new geocoding object
		    	geocoder = new GClientGeocoder();

				// Retrieve location information, pass it to addToMap()
		    	geocoder.getLocations(address, 
				// This function adds the point to the map
				function(response) {
   
		// attempt to add marker to map   
   		try {


						// if name is not filled in throw error
						if( address == '') {
							throw('blank address');
						} // end if

				
					// Retrieve the object
					place = response.Placemark[0];

					// Retrieve the latitude and longitude
					point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);

					// Center the map on this point
					map.setCenter(point, 13);

				
	
					// Set up our GMarkerOptions object
					markerOptions = { icon:placeIcon };


					// markeroptions identifies custom icon to use on marker
					marker = new GMarker(point, markerOptions);


					// add marker to map
					map.addOverlay(marker);


		}
		catch(err) {
			
			errorMsg = '';
			
			if(err == 'blank address') {
				errorMsg = 'An address must be entered for the boundaries map to work.  Please enter your address and try again.';
			}
			else if(err == 'TypeError: response.Placemark is undefined') {
				errorMsg = 'Your address could not be found on the map, please try again.';
			}
			else {
				errorMsg = 'You entered your address properly and still something broke, sorry about that.  Please <a href="#">contact us</a> about this problem.';
			}
			
			
			$('#mapframe form').append('<div id="maperror"><h1>An error has occurred.</h1><p>' + errorMsg + '</p></div>');
		}			
									$('#maperror').animate({opacity: 'show', height: 'show'}, 250);




				} // end addtopmap
				
			); // end function call
			
			
			

			
			// keeps form from submitting
			return false;


		} // end function
	); // end submit
	
	
	
	
	



	// toggle advanced options
	$('.showadvanced_map').toggle(
		function() {
			$('.maps_chk').animate({height: 'show'}, 350);
			$('.showadvanced_map').html('hide');
		}, // end function
		function() {
			$('.maps_chk').animate({height: 'hide'}, 350);
			$('.showadvanced_map').html('advanced');
		} // end function
	); // end toggle


	// if a check box is clicked determine which one it was 
	// and which state it is now in to adjust programing
	$('.league_chk').click(
		function() {

			switch( $(this).val() ) {

				case 'bixby':
					if( $(this).attr('checked') == true ) {
						map.addOverlay(bixbypolygon);
						map.addOverlay(bixbymarker);
					} // end if
					else {
						map.removeOverlay(bixbypolygon);
						map.removeOverlay(bixbymarker);
					} // end else
				break;


				case 'gb':
					if( $(this).attr('checked') == true ) {
						map.addOverlay(gbpolygon);
						map.addOverlay(gbmarker);
					} // end if
					else {
						map.removeOverlay(gbpolygon);
						map.removeOverlay(gbmarker);
					} // end else
				break;
	

				case 'hgll':
					if( $(this).attr('checked') == true ) {
						map.addOverlay(hgllpolygon);
						map.addOverlay(hgllmarker);
					} // end if
					else {
						map.removeOverlay(hgllpolygon);
						map.removeOverlay(hgllmarker);
					} // end else
				break;


				case 'lkwd':
					if( $(this).attr('checked') == true ) {
						map.addOverlay(lkwdpolygon);
						map.addOverlay(lkwdmarker);
					} // end if
					else {
						map.removeOverlay(lkwdpolygon);
						map.removeOverlay(lkwdmarker);
					} // end else
				break;


				case 'lv':
					if( $(this).attr('checked') == true ) {
						map.addOverlay(lvpolygon);
						map.addOverlay(lvmarker);
					} // end if
					else {
						map.removeOverlay(lvpolygon);
						map.removeOverlay(lvmarker);
					} // end else
				break;


				case 'lb':
					if( $(this).attr('checked') == true ) {
						map.addOverlay(lbpolygon);
						map.addOverlay(lbllmarker);
					} // end if
					else {
						map.removeOverlay(lbpolygon);
						map.removeOverlay(lbllmarker);
					} // end else
				break;


				case 'plaza':
					if( $(this).attr('checked') == true ) {
						map.addOverlay(plazapolygon);
						map.addOverlay(plazamarker);
					} // end if
					else {
						map.removeOverlay(plazapolygon);
						map.removeOverlay(plazamarker);
					} // end else
				break;


				case 'wlb':
					if( $(this).attr('checked') == true ) {
						map.addOverlay(wlbpolygon);
						map.addOverlay(wlbmarker);
					} // end if
					else {
						map.removeOverlay(wlbpolygon);
						map.removeOverlay(wlbmarker);
					} // end else
				break;
			} // end switch
		} // end function
	); // end click








	// if uncheck all was selected then remove all overlays and make every check box state false
	$('.unchk_map').click(
		function() {
		
			// turns checkboxes to false		
			$('.league_chk').attr('checked', false)
				
			// bixby north
			map.removeOverlay(bixbymarker);
			map.removeOverlay(bixbypolygon);

			// greater bellflower
			map.removeOverlay(gbmarker);
			map.removeOverlay(gbpolygon);
					
			// hawaiian gardens
			map.removeOverlay(hgllmarker);
			map.removeOverlay(hgllpolygon);
					
			// lakewood
			map.removeOverlay(lkwdmarker);
			map.removeOverlay(lkwdpolygon);
					
			// lakewood village
			map.removeOverlay(lvmarker);
			map.removeOverlay(lvpolygon);
					
			// long beach
			map.removeOverlay(lbllmarker);
			map.removeOverlay(lbpolygon);
					
			// plaza
			map.removeOverlay(plazamarker);
			map.removeOverlay(plazapolygon);
					
			// west long beach
			map.removeOverlay(wlbmarker);
			map.removeOverlay(wlbpolygon);
		
		} // end function 
	); // end click





	// turns on all overlays and sets checkboxes to true
	$('.chk_map').click(
		function() {
		
			// set all checkboxes to true
			$('.league_chk').attr('checked', true)
				
				
			// bixby north
			map.addOverlay(bixbymarker);
			map.addOverlay(bixbypolygon);

			// greater bellflower
			map.addOverlay(gbmarker);
			map.addOverlay(gbpolygon);
					
			// hawaiian gardens
			map.addOverlay(hgllmarker);
			map.addOverlay(hgllpolygon);
					
			// lakewood
			map.addOverlay(lkwdmarker);
			map.addOverlay(lkwdpolygon);
					
			// lakewood village
			map.addOverlay(lvmarker);
			map.addOverlay(lvpolygon);
					
			// long beach
			map.addOverlay(lbllmarker);
			map.addOverlay(lbpolygon);
					
			// plaza
			map.addOverlay(plazamarker);
			map.addOverlay(plazapolygon);
					
			// west long beach
			map.addOverlay(wlbmarker);
			map.addOverlay(wlbpolygon);
		
		} // end function
	); // end click






	
	

}); // end jQuery// end jQuery