var map;
var gmm;
var geocoder;
var cS = null;

var CSerializer = function ()
{
	this.queue = [];
	this.busy = 0;
	this.concurrent = 1;
	
	var t = this;
	var func = function ()
	{
		t.OnTimer ();
	};
	this.timer = setInterval(func, 100);
};

CSerializer.prototype = {};

CSerializer.prototype.SetBusy = function (val) { this.busy = val ? this.busy + 1 : this.busy - 1;  };
CSerializer.prototype.OnTimer = function ()
{
	if (this.busy > 0) return;

	if (this.queue.length == 0)
	{
		clearInterval (this.timer);
		return;
	}
	for (var i = this.concurrent; i > 0; i--)
	{
		if (this.queue.length == 0) break;
		var item = this.queue.shift ();
		if (item[5] > 3) break;
		CreateAddressBalloonInternal(item[0], item[1], item[2], item[3], item[4], item[5]);
	}
};

CSerializer.prototype.AddToQueue = function (address, title, idx, center, useCallback1, retryCount)
{
	this.queue.push ([address, title, idx, center, useCallback1, retryCount]);
};
 
function loadGMap ()
{
	if (GBrowserIsCompatible()) 
	{
		map = new GMap2(document.getElementById("map"));
        
        zoomControlLarge = new GLargeMapControl();
		map.addControl(zoomControlLarge);
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(52.3961, 3.7866), 1);
		map.enableDoubleClickZoom();
		map.enableContinuousZoom();
		map.enableScrollWheelZoom();
		map.enableDragging();

		gmm = new GMarkerManager(map);
		geocoder = new GClientGeocoder();
	}
}

function CreateIcon (idx)
{
	var icon = new GIcon();
	if (idx >= 0 && idx < 5) 
	{
		icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
		icon.iconSize = new GSize(12, 20);
	} 
	else if (idx >=5 && idx < 10) 
	{
		icon.image = "http://labs.google.com/ridefinder/images/mm_20_blue.png";
		icon.iconSize = new GSize(12, 20);
	}
	else if (idx >= 10) 
	{
		icon.image = "http://labs.google.com/ridefinder/images/mm_20_yellow.png";
		icon.iconSize = new GSize(12, 20);
	}
	
	icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	icon.shadowSize = new GSize(22, 20);
	icon.iconAnchor = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);
	return icon;
}

function InitSerializer ()
{
	cS = new CSerializer();
}


function CreateAddressBalloon(address, title, idx, center, useCallback1, retryCount)
{ 
	cS.AddToQueue (address, title, idx, center, useCallback1, retryCount);
}

function CreateAddressBalloonInternal(address, title, idx, center, useCallback1, retryCount)
{
	var a = address;
	var i = idx;
	var c = center;
	var t = title;

	if (!geocoder) return;
	
	var callback1 = function (result)
	{
		cS.SetBusy (false);
		if (result.Status.code == 620)
		{ //debugger;
			CreateAddressBalloon(a, t, i, c, useCallback1, retryCount + 1);
			return;
		}		
		if (result.Status.code != 200) return;
		var point = result.Placemark[0].Point.coordinates;
		if (!point) return;
		CreateBalloon (point, t, i, c);		
	}

	/*var callback1 = function (point)
	{
		cS.SetBusy (false);
		if (!point) return;
		CreateBalloon (point, t, i, c);
	}
	*/
/*
	var callback2 = function (point)
	{
		if (!point)
		{
			var newAddress = SplitAddress(a, t, i, c);
			if (!newAddress || newAddress == '') return;
			CreateAddressBalloonInternal (newAddress, t, i, c, true);
			return;
		}
		cS.SetBusy (false);
		CreateBalloon (point, t, i, c);
	};


	if (useCallback1) geocoder.getLatLng(a, callback1);
	else
	{
		cS.SetBusy (true);
		geocoder.getLatLng(a, callback2);
	}
*/

	var newAddress = SplitAddress(a, t, i, c);
	if (!newAddress || newAddress == '') return;
	//alert(idx + newAddress);
	cS.SetBusy (true);
	//geocoder.getLatLng(newAddress, callback1);	
	geocoder.getLocations(newAddress, callback1);
}

function CreateBalloon (point, title, idx, center)
{
		//Test (idx);
	//if(center) map.setCenter(point);
	if(center) map.setCenter(new GLatLng(point[1],point[0]), 1);	
	//if(center) mapLarge.setCenter(point);
	var icon = CreateIcon (idx);

	//var marker = new GMarker (point, icon);	
	var marker = new GMarker (new GLatLng(point[1],point[0]), icon);	
	GEvent.addListener(marker, "click", function()
	{
		marker.openInfoWindowHtml(title, {maxWidth: 300});
	});
	map.addOverlay (marker);
	//mapLarge.addOverlay (marker);
}

function SplitAddress(address, title, idx, center) 
{
	var hsubs = address.split(" | ");
	var aff = null;
	for (i = 0; i < hsubs.length; i++)
	{
		if (hsubs[i].indexOf(',') >= 0 )
		{ 
			aff = hsubs[i]; break; 
		}
	} 
	if (!aff) return null;

	var subs = aff.split(",");
	var cntr = subs.pop();
	var zipc = subs.pop();
	var city = subs.pop();
	var readaddr = '';

	if ( cntr.match(/united states|usa/i)) 
	{
	    readaddr = city + " " + zipc + " " + cntr;
	}
	else 
	{
		zipc = zipc.replace(/\d/g, '');
	    readaddr = zipc + " " + cntr;
	}

	return readaddr;
}

/*function Test (i)
{
	var d = document.getElementById ('testDiv');
	d.innerText += i + ", ";
}*/