


/**** Add application wide javascripts below this point  ******/
function default_search_text() {
	
  var searcher = $('search_code');
  searcher.value="";
}


function app_setup() {
	if($('search_code')){
  	default_search = $('search_code').value;
  	Event.observe('search_code', 'focus', default_search_text);
	}
	
	if($('map_canvas')) {
	  mapLoader();
	}
}


function mapLoader()
{
	
	//Create Ajax request object
	var xmlhttp;
		if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}else{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				var results = eval('('+xmlhttp.responseText+')');
				//document.getElementById("myDiv").innerHTML=results[0].broker_name;
				//document.getElementById("myDiv").innerHTML=results.length;
				
				set_up_map(results);
				
			}
	}
	
	//Post the broker search results to the request object
	xmlhttp.open("POST","/search/search-brokers-json?postcode="+search_postcode,true);
	xmlhttp.send();
}

function set_up_map(resultset){
	
	var map_center_lat=resultset.origin.latitude;
	var map_center_long=resultset.origin.longitude;
	var latlng = new google.maps.LatLng(map_center_lat, map_center_long);
	var bounds = new google.maps.LatLngBounds();
	
	for (var bounds_i = 0; bounds_i < resultset.brokers.length; bounds_i++) {
		var b = new google.maps.LatLng(resultset.brokers[bounds_i].latitude, resultset.brokers[bounds_i].longitude);
		bounds.extend(b);
	}
	
	var myOptions = {
      zoom: 7,
			center: latlng,
			streetViewControl: true,
			mapTypeControl: true,
			panControl: true,
			zoomControlOptions: {
        style: google.maps.ZoomControlStyle.SMALL
      },
      mapTypeId: google.maps.MapTypeId.ROADMAP
	};
		
	var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	map.fitBounds(bounds);
	
	var contentString = '';
	var infowindow = new google.maps.InfoWindow({content: contentString, maxWidth: 200});
	
	drop();
	
	function drop() {
		for (var drop_i = 0; drop_i < resultset.brokers.length; drop_i++) {
      addMarkerMethod(resultset.brokers[drop_i], drop_i);
    }
	}
	
	function addMarkerMethod(abroker, index_item){
    var geocoder = new google.maps.Geocoder(); 
    geocoder.geocode( { 'address': abroker.postcode}, function(markerresults, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        marker = new google.maps.Marker({
          map:map,
          draggable:false,
          animation: google.maps.Animation.DROP,
          position: markerresults[0].geometry.location,
          title:abroker.broker_name,
          index_ref: index_item,
          icon : '/images/pins/marker-'+(index_item+1)+'.png',
          url : abroker.url
        });
        google.maps.event.addListener(marker, 'mouseover', function() {
          openInfoWindow(this);
        });
        google.maps.event.addListener(marker, 'mouseout', function() {
            infowindow.close();
        });
        google.maps.event.addListener(marker, 'click', function() {
          window.location=this.url;
        });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });   
	}
	
	function openInfoWindow(marker){
		contentString ='<div class="map-infowindow"><h2>'+resultset.brokers[marker.index_ref].broker_name+'</h2><p>'+resultset.brokers[marker.index_ref].address+'</p></div>';
		infowindow.setOptions({content: contentString	});
		infowindow.open(map,marker);
	}
	
}

Event.observe(window, "load", app_setup);
