
var mapLoaded = false
var siteRoot = "../";
var siteMapImagesRoot = "../i/map/";

// Set the map up
function initialize() {
	if (GBrowserIsCompatible()) {
		// Create google map object
		map = new GMap2(document.getElementById("map_canvas"));
		// Set default location 
		map.setCenter(new GLatLng(55.863148 ,-4.25541), 11);
		// Size control
		map.addControl(new GLargeMapControl3D());
		// The little overview map
		map.addControl(new GOverviewMapControl(new GSize(166, 100)));

		// Add markers to the map
		createMarkers();

		// scale to fit - Points array is built in the createMarkers() function
		//fitMap(map, points);
		
		// above seems to get it almost right but not quite
		map.setZoom(15) 

		// Alerts the geoCoOrds of a click
		//GEvent.addListener(map, 'click', function(overlay, point) {
		//	alert(point.y + "  " + point.x);
		//});


	}
}


function playPauseGallery()	{
	if ($('#playPause').attr('src').indexOf('pause') == -1)	{
		// is paused now so play
		interval = setInterval("galleryNext(false)", 5000);
		$('#playPause').attr('src','../i/galleryNav_pause.gif');
	}	else	{
		// is playing so pause
		clearInterval(interval);
		$('#playPause').attr('src','../i/galleryNav_play.gif');
	}
}

function toggleGroup(type) {
	for (var i = 0; i < markers[type].length; i++) {
		var marker = markers[type][i];
		if (marker.isHidden()) {
		  marker.show();
		} else {
		  marker.hide();
		}
	} 
}

function changeMapDisplayType(toWhich)	{
	// reset styles
	$('#G_NORMAL_MAP').attr('class','');
	$('#G_SATELLITE_MAP').attr('class','');
	$('#G_PHYSICAL_MAP').attr('class','');

	// set active
	$('#'+toWhich).attr('class','on');

	// change map
	map.setMapType(eval(toWhich));
}


function createMarker(Lat, Lng, title, text, group, link, smallImg, largeImg, hasPopup)	{

	var mOptions = {  title : title };		//icon: iconImages[group],
	var mLoc = new GLatLng(Lat, Lng);
	var mMarker = new GMarker(mLoc, mOptions);

	// Attach click function if needed
	//alert((hasPopup == true));
	if (hasPopup == true)	{
		GEvent.addListener(mMarker, "click", function() {
			showDetails(title, text, group,link, smallImg, largeImg, Lat, Lng);
		});
	}

	// Add this set of geoLocs to our list
	//points.push(mLoc)
	
	// Add it to the map
	map.addOverlay(mMarker);

	// Add this item into our group arrays
	//aMark = group.split(',')
	//for (var m=0 ; m < aMark.length ; m++ )	{			
	//	markers[aMark[m]].push(mMarker);
	//}
}



function createIcon(imgPath, sizeX, sizeY, aPointX, aPointY)	{
	var mIcon = new GIcon(G_DEFAULT_ICON);
	mIcon.image = imgPath;
	mIcon.shadow = '';
	mIcon.iconSize = new GSize(sizeX, sizeY);
	mIcon.iconAnchor  = new GPoint(aPointX, aPointY);
	return mIcon;
}


// My functions
function moveMapTo(Lat, Lng)	{
	if (GBrowserIsCompatible()) {
		map.panTo(new GLatLng(Lat, Lng));
	}
}

function light(popupImg)	{
	$('#modalLargeImage').attr('src', popupImg)
	$('#largeImage').jqmShow();
}

function showDetails(title, text, group, link, smallImg, largeImg, Lat, Lng)	{
	// Zoom in - set the pin off to the right so it's not covered by the info box
	//map.setCenter(new GLatLng(Lat,Lng - 0.0700), 12);
	map.setCenter(new GLatLng(Lat + 0.0700 ,Lng), 12);

	$('#detailsPopup').css('display','block');
	$('#gmapTitle').html(title)
	$('#gmapPara').html(text)

	if (smallImg.length == 0 || smallImg == null)		{
		$('#gmapImage').attr('src', '../content/images/shim.gif');	
	}	else	{
		$('#gmapImage').attr('src', '../content/' + smallImg);	
	}

	$('#moreHolder').html('<a href="javascript:void(0)" onclick="light(\'i/' +largeImg+ '\')" >+ Large image</a>');
}

function resetMapScale()	{
	$('#detailsPopup').hide(); 
	fitMap(map, points);
	map.setZoom(1) 
}

function makeSmall(img)	{
	aImg = img.split('.')
	return aImg[0] + '_s.' + aImg[1]
}



function hideMarker(which)	{
	var marker = markers[which][0];
	if (marker.isHidden()) {
		$('#m' + which).attr('src','../i/gpin_' +which+ '_s.gif');
	}	else	{
		$('#m' + which).attr('src','../i/gpin_' +which+ '_s_on.gif');
	}

	toggleGroup(which);
}



// Init and clean up on page change
$(document).ready(function(){
	initialize();
	//$('#markerMenu').ifixpng()
});
$(window).unload( function () { GUnload(); } );


function fitMap(map, points) {
	var bounds = new GLatLngBounds();
	for (var i=0; i< points.length; i++) {
		bounds.extend(points[i]);
	}

	//alert(bounds.getSouthWest() + " - " + bounds.getNorthEast());

	map.setZoom(map.getBoundsZoomLevel(bounds) - 1);
	map.setCenter(bounds.getCenter());
}
