
var mapLoaded = false
var siteRoot = "../";
var siteMapImagesRoot = "../images/map/";

function playPauseGallery()	{
	if ($('#playPause').attr('src').indexOf('pause') == -1)	{
		// is paused now so play
		interval = setInterval("galleryNext(false)", 5000);
		$('#playPause').attr('src','../images/galleryNav_pause.gif');
	}	else	{
		// is playing so pause
		clearInterval(interval);
		$('#playPause').attr('src','../images/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, linktext, link, color)	{

	var mIcon = new GIcon(G_DEFAULT_ICON)
	mIcon.image = "/content/images/map/"+color+"-dot.png";
	mIcon.iconSize = new GSize(32, 32);
	var mOptions = { icon: mIcon, title : title };
	var mLoc = new GLatLng(Lat, Lng);
	var mLocInfoWindow = new GLatLng(Lat, Lng+0.0005);

	points.push(mLoc)

	var mMarker = new GMarker(mLoc, mOptions);
	if(title=="YOU ARE HERE"){
		var contentHTML = '<b>' + title + '</b><br/>'+ text;
		map.openInfoWindowHtml(mLocInfoWindow, contentHTML);			
	}
	
		
	GEvent.addListener(mMarker, "click", function() {
		if(linktext!='' && title!="YOU ARE HERE"){
			var contentHTML = '<b>' + title + '</b><br />' + text + '<br /><br /><div class="more"><img src="images/info.gif" /><a href="' + link + '">&nbsp; '+linktext+'</a></div>';
		}
		else{
			var contentHTML = '<b>' + title + '</b><br />' + text;
		}
		map.openInfoWindowHtml(mLocInfoWindow, contentHTML);
	});
	

		//showDetails(title, text, link);
	

	map.addOverlay(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 showDetails(title, text, link)	{		
	$('#popupImageCaption').html(title + '&nbsp;')
	$('#DPText').html(text)
	if (link != '')	{
		$('#moreHolder').html('<a href="' + link + '">+ more</a>');
	}	else	{
		$('#moreHolder').html('');
	}
	$('#detailsPopup').css('display','block');
}

function light(popupImg)	{
	// alert(popupImg)
	$('#modalLargeImage').attr('src', popupImg)
	$('#largeImage').jqmShow();
}

function makeSmall(img)	{
	aImg = img.split('.');
	//return aImg[0] + '_s.' + aImg[1];
	return aImg[0] + '_s.gif'
}


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);
}




function galleryNext(stop)	{
	if (stop)	{	clearInterval(interval);		}

	galleryPosition = galleryPosition+1;
	if (galleryPosition >= galleryArray.length)	{	galleryPosition = 0;	}
	changeGallery(galleryPosition);
}

function galleryBack(stop)	{
	if (stop)	{	clearInterval(interval);		}

	galleryPosition = galleryPosition-1;
	if (galleryPosition < 0)	{	galleryPosition = galleryArray.length - 1;	}
	changeGallery(galleryPosition);
}

function hideAppTypes()	{
	// Hide both panels
	$('#map').hide();
	$('#gallery').hide();

	// reset the rollovers on the buttons
	$('#mbsport').attr('src','../i/mapButton_sport.png');
	$('#mbvenue').attr('src','../i/mapButton_venue.png');
	$('#mbmap').attr('src','../i/mapButton_map.png');
}

function changeGallery(arrayIndex)	{
	$('#gallery').css("background","url('" + galleryArray[arrayIndex].image + "')");
	$('#galleryDetails').html('<b>' + galleryArray[arrayIndex].title.toUpperCase() + '</b> - ' + galleryArray[arrayIndex].text);
	$('#galleryNavPageCount').text((arrayIndex + 1)+ '/' + (galleryArray.length) )
	galleryPosition = arrayIndex;
}

function initGallery(galleryType)	{

	switch (galleryType) {
		case 'sport': 
			galleryArray = sport;
			break;

		case 'venue': 
			galleryArray = venue;
			break;
	}

	changeGallery(0);

	clearInterval(interval);
	interval = setInterval("galleryNext(false)", 5000);
}

function changeAppTo(toWhich, galleryType)	{

	hideAppTypes();
	//alert('#' + toWhich)
	// show app
	$('#' + toWhich).show();
	// show button highlight
	$('#mb' + galleryType).attr('src','../i/mapButton_' +galleryType+ '_over.png');

	if (galleryType != 'map')		{
		initGallery(galleryType);
	}	else	{
		if (!mapLoaded)		{
			map = new GMap2(document.getElementById("map_canvas"));

			// Set default location 
			map.setCenter(new GLatLng(55.858358, -4.264755), 11);
			// Size control
			map.addControl(new GLargeMapControl3D());
			// The little overview map
			map.addControl(new GOverviewMapControl(new GSize(166, 100)));

			createMarkers();
			mapLoaded = true;
		}
	}

	// Fix the png's in ie6
	$('#mbsport').ifixpng(); 
	$('#mbvenue').ifixpng(); 
	$('#mbmap').ifixpng(); 
}


// Init
$(document).ready(function(){
	$('#largeImage').jqm();
});


function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
} 

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());
}
