// JavaScript Document

var map = null;
var directions = null;
default_zoom_level = 9;
var afterinit = null;

var last_postcode = null;
var last_destination = null;

var objectCollection = new Array();
var inPrintWindow = false;
var afterinit = null;

var useZoomControl  = true;
var useScaleControl = false;
function initialize() 
{
		if (GBrowserIsCompatible()) 
		{	
			map = new GMap2(document.getElementById("map_canvas"));		
			directions = new GDirections(map, document.getElementById("route"));
			point = new GLatLng(52.220779, 5.179826);
			map.setCenter(point, default_zoom_level);	   	
			
			if( useZoomControl )
			{
				zoomControl = new GSmallZoomControl();
				map.addControl(zoomControl);				
			}
			
			if( useScaleControl )
			{
				mapScaleControl = new GScaleControl();
				map.addControl(mapScaleControl);
			}
			
			if(afterinit != null)
			{
				qs = new Querystring();
				id=qs.get("id", "empty");
				if( id != "empty")
				{
					afterinit(id);
					map.setZoom(GetZoomLevel());
					showIntroText();			
				}				
			}			
		}
		else 
		{
		}	
}

function initializePrint()
{
	if (GBrowserIsCompatible()) 
		{	
			map = new GMap2(document.getElementById("map_canvas"));		
			directions = new GDirections(map, document.getElementById("route"));
			inPrintWindow = true;

			qs = new Querystring();
		
			postcode=qs.get("p", "empty");
			destination=qs.get("d", "empty");
			
			showRoute( postcode, destination );

			document.getElementById("route").style.height = "100%";
			document.getElementById("route").style.border = "0px solid black";			

			setTimeout(googleReady, 3000);
		}
}

function googleReady()
{
//	alert('done');
window.print();

}

function showIntroText()
{
	document.getElementById("lblIntroText").innerHTML = IntroText();
}

function openPrintWindow()
{
	window_url = 'http://maps.newictea.nl/v3/print.html?p=' + last_postcode + '&d=' + last_destination;
	var oWindow = window.open(window_url, 'route', 'scrollbars=yes,width=620;' ); 
}


function dbMarkers( vestigingInfo , posx, posy, outletId )
{
		testdata = new mObject(vestigingInfo , posx, posy, outletId );
		
		gpsloc = posx + "," + posy;	
		var html = "<form action='http://maps.google.com/maps' id='testform"+outletId+"' name='testform"+outletId+"' method='get' target='_blank' onsubmit='DirectionMarkersubmit(this);return false;'><strong style='font-size:11px;'>"+vestigingInfo+"<br /><br /></strong><p style='font-size:11px; display:inline;'>Postcode vertrekpunt:</p><br /> <input type='text' class='inputbox' maxlength='6' size='10' name='saddr' id='saddr' value='' />&nbsp;<input value='Toon Route' class='button' type='submit' style='margin-top: 2px;'><input type='hidden' name='daddr' value='"+gpsloc+"'/></form>";	
		point = new GLatLng(posx, posy);
		testdata.point = point
		
		map.addOverlay(createMarker( point, html ));
		map.setCenter(point, GetZoomLevel());	
		objectCollection[objectCollection.length] = testdata;
		
}

function createMarker(point, html)
{
		var marker = new GMarker(point);
		GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); });
		return marker;
}

function DirectionMarkersubmit(itemd)
{	
		postcode = itemd['saddr'].value;
		dest = itemd['daddr'].value;
		
		last_postcode = postcode;
		last_destination = dest;
		
		showRoute(postcode, dest);
		return false;
}	

function showRoute(data, dest)
{		
		document.getElementById("route").style.height = "275px";
		document.getElementById("route").style.borderBottom = "1px solid black";


		if(!inPrintWindow)
		{
			document.getElementById("route").innerHTML = "<div id='print' style='text-align:right;background-color:#D5DDF3;height:20px;padding:4px;'><a href='javascript:openPrintWindow();'><img src='Images/print.jpg' width='16px' height='16px' style='margin-right:5px;' border='0' />Print</a></div>";
		}

		from = "from: " + data + ", Netherlands";
		directions.load(from + " to: " + dest );
}

function Querystring(qs) { // optionally pass a querystring to parse
	this.params = {};
	
	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;

	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &
	

	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);
		
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;
		
		this.params[name] = value;
	}
}

Querystring.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}

function mObject(_vestigingInfo , _posx, _posy, _outletId )
{
	this.vestigingInfo = _vestigingInfo;
	this.posx = _posx;
	this.posy = _posy;
	this.outletId = _outletId;
	this.point = null;
}

