Ext.ns("GoogleMaps");


//=" style="color:#0000FF;text-align:left">Vis større kart</a></small>

/* s
*/

GoogleMaps.initMap = function()
{
	GoogleMaps.shops = Ext.util.JSON.decode(Tools.companiesJSON);
 
//	GoogleMaps.shops = Ext.util.JSON.decode('[{"CountyId":9023538, "Name":"TOOLS Gan Maskinforretning AS", "Url":"http://www.ole.no", "Longitude":"59.910976", "Lattitude":"10.59082"}]');
 
 
	var myOptions = {
		zoom: 4,
		center: new google.maps.LatLng(59.910976,10.59082),
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}  
	

		

	GoogleMaps.map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
 
 
	var bounds = new google.maps.LatLngBounds();
	
 	Ext.each(GoogleMaps.shops, function(shop) {
		
		var LL2 = new google.maps.LatLng(parseFloat(shop.Longitude), parseFloat(shop.Lattitude));
		//var LL2 = new google.maps.LatLng(59.910976,10.59082);
		shop.marker = new google.maps.Marker({
			position: LL2,	
			title: shop.Name
		});	 
		shop.marker.setMap(GoogleMaps.map); 
		bounds.extend(shop.marker.position);
			
		google.maps.event.addListener(shop.marker, 'click', function() {
			document.location.href = shop.Url;
			document.location.target = "_blank";
		});
						
	});
	GoogleMaps.map.fitBounds(bounds);
		
}






GoogleMaps.initControls = function()
{						
	Ext.fly("county_ddl").on('change', this.filterByCounty );
	Ext.fly("shop_ddl").on('change', this.filterByShop );
}


GoogleMaps.filterByShop = function(e,t)
{
	var countyIdFilter = -1;
	var shopIdFilter = -1;
	var countyDDL = Ext.fly('county_ddl').dom;
	var shopDDL = Ext.fly('shop_ddl').dom;									
	
	if (countyDDL.selectedIndex > 0)
	{					
		var activeOption = countyDDL.options[countyDDL.selectedIndex];
		var countyIdFilter = parseInt(activeOption.value);
	}	
	if (shopDDL.selectedIndex > 0 )
	{					
		var activeOption = shopDDL.options[shopDDL.selectedIndex];
		var shopIdFilter = parseInt(activeOption.value);		
	} 

	var bounds = new google.maps.LatLngBounds();
	var boundsCount = 0
	var latLng = [];
	Ext.each(GoogleMaps.shops, function(shop) {
		shop.marker.setMap(null);
		if (shop.CountyId == countyIdFilter || countyIdFilter == -1) 
		{	
			//alert(shop.ShopId + ":" + shopIdFilter + ":" + shop.Name);
			if (shop.ShopId == shopIdFilter || shopIdFilter == -1) {		
				bounds.extend(shop.marker.position);
				shop.marker.setMap(GoogleMaps.map); 
				latLng[0] = shop.marker.position.lat();
				latLng[1] = shop.marker.position.lng();
				boundsCount++;
			}	
		}
	});		
	// We must add random points around shop to avoid closeup
	if (boundsCount == 1){for (i=0;i<4;i++){bounds.extend(new google.maps.LatLng(latLng[0]+(Math.random()/80), latLng[1]+(Math.random()/80)) );}}		
	GoogleMaps.map.fitBounds(bounds);	
}


GoogleMaps.filterByCounty = function(e,t)
{			
	
		var countyIdFilter = -1;
		var shopIdFilter = -1;
						
		var countyDDL = Ext.fly('county_ddl').dom;
		var shopDDL = Ext.fly('county_ddl').dom;									
		if (countyDDL.selectedIndex > 0)
		{					
			var activeOption = countyDDL.options[countyDDL.selectedIndex];
			var countyIdFilter = parseInt(activeOption.value);
		}
										
		var shopEl = Ext.get("shop_ddl");
		var shopOptions	 = shopEl.dom.options;				
		shopOptions.length = 0;
		
		var optn = document.createElement("option");
		optn.text = "Velg butikk...";
		optn.value = 0;		
		shopOptions.add(optn);

		var bounds = new google.maps.LatLngBounds();
		var boundsCount = 0
		var latLng = [];
		Ext.each(GoogleMaps.shops, function(shop) {
			if (shop.CountyId == countyIdFilter || countyIdFilter == -1) {
				var optn = document.createElement("option");
				optn.text = shop.Name;
				optn.value = parseInt(shop.ShopId);		
				shopOptions.add(optn);
				shop.marker.setMap(GoogleMaps.map); 
				bounds.extend(shop.marker.position);
				latLng[0] = shop.marker.position.lat();
				latLng[1] = shop.marker.position.lng();
				boundsCount++;
			}
			else
				shop.marker.setMap(null);
		});			
		// We must add random points around shop to avoid closeup
		if (boundsCount == 1){for (i=0;i<4;i++){bounds.extend(new google.maps.LatLng(latLng[0]+(Math.random()/80), latLng[1]+(Math.random()/80)) );}}		
		GoogleMaps.map.fitBounds(bounds);																						
}



GoogleMaps.filterMap = function(countyIdFilter, shopIdFilter )
{

}



  
/* Ext.each(GoogleMaps.shops, function(shop) {
	
		//var L2 = new google.maps.LatLng( + l,  + 2.0);	
						
		var marker = new google.maps.Marker({
			position:  new google.maps.LatLng(61.93293,12.188473),
			map: GoogleMaps.map 
		});				
   });

*/
/*
	var latlng = new google.maps.LatLng(60.93293,10.188473);   
	var myOptions = {
      zoom: 3,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
	GoogleMaps.map = new google.maps.Map(Ext.get("map_canvas").dom, myOptions);	
*/   
   
	
   
   /*
	var L2 = new google.maps.LatLng(59.93293,9.188473);  
	var marker = new google.maps.Marker({
			position: L2, 
			map: GoogleMaps.map 
		});   
	GoogleMaps.attach(marker, 1);   	
    */
      	


/*
		marker.setTitle(shop.Name);

		var mapShop = {
			countyId : shop.CountyId,
			name: shop.Name,
			url: shop.Url,
			location: LatLng
		};
	
		
		GoogleMaps.attach(marker, 1);
*/









// Create a LatLngBounds object
/*
var bounds = new google.maps.LatLngBounds();

for (var i = 0; i &lt; myPositions.lenght; i++) {
  // Insert code to add marker to map here

  // Extend the LatLngBound object
  bounds.extend(myPositions[i]);
}
map.fitBounds(bounds);
*/



// Clear the markers
/*
for(var i=0; i<markers.length; i++){
  markers[i].set_map(null);

}

It may be a good idea memorywise to null the marker objects. Also the
array can be reset for future use. So the full function might look
like:

function clearMarkers(){
  for(var i=0; i<markers.length; i++){
    markers[i].set_map(null);
    markers[i] = null;
  }
  markers.length = 0; 



*/




		


Ext.onReady(function() 
{	
	if (Ext.fly('map_canvas') != null)
	{
		GoogleMaps.initMap();
		GoogleMaps.initControls();		
	}
	else if (Ext.fly('dealer_map_canvas') != null) 
	{
	
		var myOptions = {
		zoom: 4,
		center: new google.maps.LatLng(parseFloat(GoogleMaps.ll[0]), parseFloat(GoogleMaps.ll[1])),
		mapTypeId: google.maps.MapTypeId.ROADMAP
		} 

		GoogleMaps.map = new google.maps.Map(document.getElementById("dealer_map_canvas"), myOptions);

		var bounds = new google.maps.LatLngBounds();
		var LL2 = new google.maps.LatLng(parseFloat(GoogleMaps.ll[0]), parseFloat(GoogleMaps.ll[1]));
		  
		var marker = new google.maps.Marker({
			position: LL2,	
			map: GoogleMaps.map,
			title: ""
		});	
	
		marker.setMap(GoogleMaps.map);
		
		bounds.extend(marker.position);
		
		for (i=0;i<4;i++){bounds.extend(new google.maps.LatLng(GoogleMaps.ll[0]+(Math.random()/400), GoogleMaps.ll[1]+(Math.random()/400)) );}	
		GoogleMaps.map.fitBounds(bounds);	
		
		GoogleMaps.map.fitBounds(bounds);
		 

		
	}
});
