function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

var map;
var map;
var defaultMarker;
var changeMarker;

var defaultIcon = new GIcon();
defaultIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
defaultIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
defaultIcon.iconSize = new GSize(12, 20);
defaultIcon.shadowSize = new GSize(22, 20);
defaultIcon.iconAnchor = new GPoint(6, 20);
defaultIcon.infoWindowAnchor = new GPoint(5, 1);

var changeIcon = new GIcon();
changeIcon.image = "http://labs.google.com/ridefinder/images/mm_20_green.png";
changeIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
changeIcon.iconSize = new GSize(12, 20);
changeIcon.shadowSize = new GSize(22, 20);
changeIcon.iconAnchor = new GPoint(6, 20);
changeIcon.infoWindowAnchor = new GPoint(5, 1);


var centerLatitude = 0;
var centerLongitude = 0;
var startZoom = 3;


TownAndCountryProjection = new GProjection();

TownAndCountryProjection.mapResolutions = [256,512,1024,2048]

TownAndCountryProjection.fromLatLngToPixel = function(latlng,zoom) {
	var lng = parseInt(Math.floor(
		(this.mapResolutions[zoom] / 360)
		* (latlng.lng() + 180))
	);
	var lat = parseInt(Math.floor(
		Math.abs((this.mapResolutions[zoom] / 180)
		* (latlng.lat() - 90)))
	);
	var point = new GPoint(lng,lat);
	return point;
}

TownAndCountryProjection.fromPixelToLatLng = function(pixel,zoom,unbounded) {
	var lat = 90 - (pixel.y / (this.mapResolutions[zoom] / 180));
	var lng = (pixel.x / (this.mapResolutions[zoom] / 360)) - 180;
	var latlng = new GLatLng(lat,lng);
	return latlng;
}

TownAndCountryProjection.tileCheckRange = function(tile,zoom,tileSize){
	var rez = this.mapResolutions[zoom];
	if(tile.y < 0 || tile.y * tileSize >= rez){ return false; }
	if(tile.x < 0 || tile.x * tileSize >= rez){
		var e = Math.floor( rez / tileSize );
		tile.x = tile.x % e;
		if(tile.x < 0){ tile.x += e; }
	}
	return true;
}

TownAndCountryProjection.getWrapWidth = function(zoom){
	return this.mapResolutions[zoom];
}


copyrights = new GCopyrightCollection('Map Imagery:');
var visibleEarth = new GCopyright(
	'TownAndCountry',
	new GLatLngBounds(new GLatLng(-90,-180),new GLatLng(90,180)),
	0,
	'<a href="http://townandcountrydiscoveries.ca/section/view/?fnode=72">Town & Country</a>'
);
copyrights.addCopyright(visibleEarth);

//tile layer for land_ocean_ice
var TownAndCountryTiles = new GTileLayer(copyrights,0,5);
TownAndCountryTiles.getTileUrl = function(tile,zoom){
	if(zoom > 5) return 'images/tiles/no_tiles_at_zoom_level.png';
	else return 'images/tiles/tile.' + zoom + '.' +
	(tile.x + tile.y * Math.pow(2,zoom)) + '.png';
};
TownAndCountryTiles.isPng = function() { return true; }
TownAndCountryTiles.getOpacity = function() { return 1.0; }

var TownAndCountry = new GMapType(
	[TownAndCountryTiles],
	TownAndCountryProjection,
	'Countryside',
	{
		shortName:'CS',
		tileSize:256,
		maxResolution:3,
		minResolution:1
	}
);



function addMarker(i) {
	var mData = data[i];
	//console.log(mData);



	var marker = new GMarker(
		new GLatLng(
			parseFloat(mData.lat),
			parseFloat(mData.lng)
		),{
			title:unescape(mData.name)

		}
	);

	GEvent.addListener(marker, "click", function() {
	 	info.innerHTML =
	 		'<div id="tcInfoWindow">' +
				'<h3>' + (mData.name) + '</h3>' +
				'<div>' +
					'<p class="addr">' + (mData.address) + '</p>' +
					'<p class="city">' + (mData.city) + '</p>' +
					'<p class="phone">' + (mData.phone1) + '</p>' +
					'<p class="website"><a href="' + (mData.url) + '" target="_blank">View Web Site</a></p>' +
					'<p class="description">' + (mData.description) + '</p>' +
		 		'</div>' +
				'<img src="/map/ads/' + (mData.image) + '" alt="' + (mData.name) + '" />' +
	 		'<span style="display:block; clear:both"></span></div>'
	 	;
	 	document.location = '#info';
	  });

	map.addOverlay(marker);

}

function initGeocodeMap(lat,lng) {

	if(!$('locMap')) return;

	map = new GMap2($('locMap'),{
		'size':new GSize(500,500),
		'mapTypes':[TownAndCountry]
	});
	map.addMapType(TownAndCountry);
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());

	if(lat && lng) {
		map.setCenter(new GLatLng(0,0),1);
		var currentGCP = new GLatLng(parseFloat(lat),parseFloat(lng));
		$('lat').value = lat ;
		$('lng').value = lng ;
	} else {
		map.setCenter(new GLatLng(0,0),3);
		var currentGCP = new GLatLng(0,0);
		$('lat').value = 0 ;
		$('lng').value = 0 ;
	}

	defaultMarker = new GMarker(currentGCP,{
			draggable: true,
			icon:defaultIcon
	});
	map.addOverlay(defaultMarker);
	$("mapMessage").innerHTML = 'Drag marker to change location.';
	map.panTo(currentGCP);


	GEvent.addListener(defaultMarker, "dragstart", function() {

	});

	GEvent.addListener(defaultMarker, "dragend", function() {
		$('lat').value = defaultMarker.getPoint().lat() ;
		$('lng').value = defaultMarker.getPoint().lng() ;
	});

}

function updatemap(lng,lat) {
	var point = new GLatLng(lat,lng);
	map.removeOverlay(changeMarker);
	changeMarker = new GMarker(point, changeIcon);
	map.addOverlay(changeMarker);
	map.recenterOrPanToLatLng(point);
	var latLngStr = 'lng ' + (Math.round(point.y*100)/100) + ', lat ' + (Math.round(point.x*100)/100) + '';
}

var info;

function initMap() {

	if(!$('map')) return;
	info = $('info');

	map = new GMap2($('map'),{
		'size':new GSize(500,500),
		'mapTypes':[TownAndCountry]
	});

	map.addMapType(TownAndCountry);
	map.addControl(new GLargeMapControl());
	map.setCenter(new GLatLng(0,0),1);

	for(i in data) {
		if(!data.hasOwnProperty(i)) continue;

		addMarker(i);
	}

}

addLoadEvent(initMap);


