var locations = '';
var registry = new Array();
var products = '';

$(document).ready(function(){
	populateProducts();

});

function plotProduct(plot, options)
{
	blockUI();
	var product = options.product;
	var temp = eval("products.products."+product);
	if( temp != null)
	{
		var count = 0;
		var debug = '';
		for(var i = 0; i < temp.length; i++ )
		{
			
			var id = temp[i];
			var tempid = registry[id];
			var location = locations.locations.location[tempid];
			if( plot){
				map.addOverlay(location.marker);
				count++;
				debug = ' points added to map.';
			} else {
				map.removeOverlay(location.marker);
				count++;
				debug = ' points removed from map.';
			}
		}
		$("#MapConsole").append("<p>" + count + debug + "</p>");
	}
	else
	{
		alert("Product: " + product + " does not exist");
	}
	unblockUI();
}

function createProductMarker(location)
{

	var myicon = new GIcon(baseIcon);
	//TODO will need a dynamic way to change color of marker based on products at site
	myicon.image = "/images/markers/" + "Green" + ".png";
	var point = new GLatLng(location.lat, location.lng);
	var options = { icon:myicon };
	location.marker = new GMarker(point, options);
	addProductMarkerClickEvent(location.marker, location.id);
}

function populateLocations()
{
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) 
	{	
		receiveReq.open("GET", '/map/get-plotting-data', true);		
		receiveReq.onreadystatechange = function(){
			if (receiveReq.readyState == 4) {
				locations = eval("(" + receiveReq.responseText + ")");
				for(i=0;i < locations.locations.location.length; i++) {	
					var temp = locations.locations.location[i];				
					findProductPoint(temp);
					registry[temp.id] = i;
				}
			}
		};		
		receiveReq.send(null);	
	}	
}

function populateProducts()
{
	blockUI();
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) 
	{	
		receiveReq.open("GET", '/map/get-product-sites/', true);		
		receiveReq.onreadystatechange = function(){
			if (receiveReq.readyState == 4) {
				products = eval("(" + receiveReq.responseText + ")");
				populateLocations(); //nested on purpose to prevent server from working too fast lol
				unblockUI();
			}
		};		
		receiveReq.send(null);
	}	
}

function findProductPoint(location) {
	if( location.lat && location.lng ){
		createProductMarker(location);
	}
	else{
		if( geocoder){
			geocoder.getLatLng( location.address, function(point){
				if( !point ) {
					alert("could not find " + location.address );
				} else {
					location.lat = point.lat();
					location.lng = point.lng();
					createProductMarker(location);
				}
			});
		}
	}
}

function addProductMarkerClickEvent( marker, id )
{
	GEvent.addListener(marker, "click", function() {
		if (receiveReq.readyState == 4 || receiveReq.readyState == 0) 
		{	
			receiveReq.open("GET", '/map/get-extended-data/companyid/'+ id, true);		
			receiveReq.onreadystatechange = function(){
				if (receiveReq.readyState == 4) {
					marker.openInfoWindowHtml(receiveReq.responseText);
				}
			};		
			receiveReq.send(null);	
		}
	});
}