var map;
var localSearch = new GlocalSearch();

var icon = new GIcon();
icon.image = "/Images/MapPointer.png";
icon.shadow = "";
icon.iconSize = new GSize(20, 34);
icon.shadowSize = new GSize(37, 34);
icon.iconAnchor = new GPoint(10, 34);


var sParentPanelId = null;
var allAddresses = null;
var xandy = null;
var marker2 = Array();
var html = Array();

var icon_0 = new GIcon();
icon_0.iconSize = new GSize(34,35);
icon_0.shadowSize = new GSize(34,35);
icon_0.iconAnchor = new GPoint(9,33);
icon_0.infoWindowAnchor = new GPoint(19,0);
icon_0.printImage = 'http://google.webassist.com/google/markers/traditionalflat/pacifica.gif';
icon_0.mozPrintImage = 'http://google.webassist.com/google/markers/traditionalflat/pacifica_mozprint.png';
icon_0.printShadow = 'http://google.webassist.com/google/markers/traditionalflat/shadow.gif';
icon_0.transparent = 'http://google.webassist.com/google/markers/traditionalflat/pacifica_transparent.png';

var Zoom = 11;
var NumberOfRows = 0;

function GM_SetPanel(p,z,n,arrAddr)
{
    sParentPanelId = p;
    Zoom = z;
    NumberOfRows = n;
	allAddresses = arrAddr;
	mapLoad();
}

var nCurrentIndex = 0;

function usePointFromPostcode(postcode, nIndex2, callbackFunction) {
	localSearch.setSearchCompleteCallback(null, 
		function() {
			
			if (localSearch.results[0])
			{		
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				callbackFunction(point, nCurrentIndex);
				++nCurrentIndex;
			}else{
				alert("Postcode not found!");
			}
		});	
	localSearch.execute(postcode + ", UK");
}



function placeMarkerAtPoint(point, nIndex)
{
	map.setCenter(new GLatLng(point.lat(),point.lng()), Zoom, G_NORMAL_MAP);
	var marker = new GMarker(point,icon);

	html[nIndex] = '<p><strong>' + allAddresses[nIndex][0] + '</strong><br/>' + allAddresses[nIndex][1] + '<br/>' + allAddresses[nIndex][2] + '<br/>' + allAddresses[nIndex][3] + '<br/>' + allAddresses[nIndex][4] + '<br/>' + allAddresses[nIndex][5] + '<br/><a href="mailto:'+ allAddresses[nIndex][6] +'">' + allAddresses[nIndex][6] + '</a><br/><a onclick="return GetDirections(\'#stMapsEnterPostCode_' + nIndex + '\');" href="javascript:;">Get Directions</a><div class="" id="stMapsEnterPostCode_' + nIndex + '" style="display:none;top:0;position:absolute;background:#fff;width:100%;height:100%;"><p>Please enter your postcode</p><input type="text" name="txtPostcode_'+nIndex+'" id="txtPostcode_'+nIndex+'" value="" /><br/><input type="button" name="btnPostcode" id="btnPostcode" value="Search" onclick="return GetTheDirections(\'#txtPostcode_'+nIndex+'\', \''+allAddresses[nIndex][4]+'\');" /></div></p>';
	
	marker2[nIndex] = new GMarker(point, icon_0);
	map.addOverlay(marker2[nIndex]);
	
	GEvent.addListener(marker2[nIndex], "click", function() {
		marker2[nIndex].openInfoWindowHtml(html[nIndex]);
    });	
	
	map.addOverlay(marker);
}

function GetDirections(boxele)
{
	$(boxele).show();
}

var directions = null;
var directionsPanel;

function GetTheDirections(boxele, postcode)
{
	var value = $(boxele).val();
	if (value.length > 0)
	{
		//alert(value);
		//alert('to');
		//alert(postcode);
		
		
		
		directionsPanel = document.getElementById("stMap_Directions");
		$(directionsPanel).html('');
		
		directions = new GDirections(map, directionsPanel);
		directions.load("from: " + value + " to: "+postcode+" ");
		
	}
}


function mapLoad() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById(sParentPanelId));
		map.setUIToDefault();
		for(var i = 0; i < NumberOfRows; i++)
		{
			usePointFromPostcode(allAddresses[i][4], i, placeMarkerAtPoint);
		}
	}
}

$(document).ready(function() {
	InitGoogleMaps();

});
