/*
The details will consist of  title, Address, short description, lat, long, hyperlink. I will prebuild this list for you so for the time being create your own test data. Once you are happy with what you have created then we can talk about implementing it on existing pages and testing/adjusting.
*/
var Dots=[];
function PD(aTitle,aAddress,aShortDescription, aLat,aLon,aHyperlink,aEvalScript,aTag, nCentreID,bubbleType){
  Dots.push(new Dot(aTitle,aAddress,aShortDescription, aLat,aLon,aHyperlink,aEvalScript,aTag,nCentreID,bubbleType));
}
function Dot(aTitle,aAddress,aShortDescription, aLat,aLon,aHyperlink,aEvalScript,aTag,nCentreID,bubbleType){
    this.title=aTitle;
    this.address=aAddress;
    this.description=aShortDescription;
    this.lat=aLat;
    this.lon=aLon;
    this.evalScript = aEvalScript;
    this.centre=nCentreID;
    this.link=aHyperlink;
    this.tag=aTag;
    this.bubbleType = bubbleType;
}

var map=false;
var theHint=false;

function initMap(mapdiv,legenddiv, apppath, iconPath)
{
    map = new GMap2(document.getElementById(mapdiv));
    iconPath = iconPath || (apppath + "/images/icon.png");
    
    io.ApplicationManager.on("updated", function updateGoogleMap() {
        if(map.getInfoWindow().isHidden()){
            return;
        }
        map.updateCurrentTab(function(tab) {
            var dot = Dots[map.currentIndex];
            dot.added = io.ApplicationManager.hasCentre(dot.centre);
            tab.contentElem.innerHTML = getInfoTabHTML(map.currentIndex, map.sourceType);
        });
    }, map);
    
	map.addControl(new GSmallZoomControl());
	map.addControl(new GMapTypeControl());
	map.apppath = apppath;
    var latmin = 10000; var latmax=-10000;
    var lonmin = 10000; var lonmax=-10000;
	for(i=0;i<Dots.length;i++){
        if(Dots[i].lat>latmax){latmax=Dots[i].lat;}
        if(Dots[i].lat<latmin){latmin=Dots[i].lat;}
        if(Dots[i].lon>lonmax){lonmax=Dots[i].lon;}
        if(Dots[i].lon<lonmin){lonmin=Dots[i].lon;}
	}
    var bounds = new GLatLngBounds(
    	   new GLatLng(latmin,lonmin),
    	   new GLatLng(latmax,lonmax));
    map.setCenter(new GLatLng(latmin+(latmax-latmin)/2,lonmin+(lonmax-lonmin)/2),
	       map.getBoundsZoomLevel(bounds)-1);
	       
	manager = new GMarkerManager(map);
        
    batch = [];

	for(i=0;i<Dots.length;i++)
	{
        var P = new GLatLng(Dots[i].lat,Dots[i].lon);

		if(typeof(getCustomIconOpts) == "function")
		{
			opts = getCustomIconOpts(apppath, i, Dots);
		}
		else
		{
			var icon = new GIcon();
			icon.image = iconPath;
			icon.shadow = apppath + "/images/shadow50.png";
			icon.iconSize = new GSize(19.0, 27.0);
			icon.shadowSize = new GSize(33.0, 27.0);
			icon.iconAnchor = new GPoint(9.0, 13.0);
			icon.infoWindowAnchor = new GPoint(9.0, 13.0);

			opts = 
			{
				icon: icon,
				dbID: i,
				dot : Dots[i],
				labelText: Dots[i].tag,
				labelClass: "resultsGoogleTabNo",
				labelOffset: new GSize(-8,-11)
			};
		}

		var marker = new MMMarker(P,opts);
		map.addOverlay(marker);
		
		GEvent.addListener(marker, "click", function() { 
          MMMmouseclick(this.dot);
        }); 
        
        batch.push(marker);
	}
//	manager.addMarkers(batch, Dots.length);
//  manager.refresh();
}

function MMMmouseclick(dot)
{
	if(dot.bubbleType == 1)
		showBubble1(dot.tag);
	if(dot.bubbleType == 2)
		showBubble2(dot.tag);
	if(dot.bubbleType == 3)
		showBubble3(dot.tag);
	if(dot.bubbleType == 4)
		showBubble4(dot.tag);
}

function getInfoTabHTML(ix, type){
	var w1 =
	type == 0 ?
	('<table width="200"><tr><td><b>'+Dots[ix].address+'</b></td></tr>'+
			'<tr><td><a href="' + Dots[ix].link + '"">' + strings[0] + '</a></td></tr>'+
			(
			    Dots[ix].added?
			    '<tr><td>' + strings[3] + '</td></tr>'
			    :
			    '<tr><td><a href="javascript:void(0);" onclick="eval(' + Dots[ix].evalScript + ')">' + strings[1] + '</a></td></tr>'
			) +
			'</table>')
	:		
	('<table width="200"><tr><td><b>'+Dots[ix].address+'</b></td></tr>'+
			(
			    Dots[ix].added?
			    '<tr><td>' + strings[3] + '</td></tr>'
			    :
			    '<tr><td><a href="javascript:void(0);" onclick="eval(' + Dots[ix].evalScript + ')">' + strings[1] + '</a></td></tr>'
			) +
			'</table>');
    return w1;
}

function showBubble1(tag)
{
	for(ix=0;ix<Dots.length;ix++)
	{
	    if(Dots[ix].tag==tag)
	    {
	        Dots[ix].added = io.ApplicationManager.hasCentre(Dots[ix].centre);

            var tab = new GInfoWindowTab(strings[2], getInfoTabHTML(ix, 0));	        
            map.currentIndex = ix;
            map.sourceType = 0;
            
			var infoTabs = [ tab ];
			var K=allMMMmarkers[ix].latlng;
			map.openInfoWindowTabsHtml(K,infoTabs);
		}
	}
}

function showBubble2(tag)
{
	for(ix=0;ix<Dots.length;ix++)
	{
	    if(Dots[ix].tag==tag)
	    {
	    	//	2006-20-10 BEGIN PSERGEI: decreased table width
			var w1='<table width="200"><tr><td><b>'+Dots[ix].address+'</b></td></tr>'+
					'<tr><td><a href="' + Dots[ix].link + '"">' + strings[0] + '</a></td></tr>'+
					'</table>';
			//	END PSERGEI
			var infoTabs = [
				new GInfoWindowTab(strings[2], w1)
			];
			var K=allMMMmarkers[ix].latlng;
			map.openInfoWindowTabsHtml(K,infoTabs);
		}
	}
}

function showBubble3(tag)
{
	for(ix=0;ix<Dots.length;ix++)
	{
	    if(Dots[ix].tag==tag)
	    {
	        Dots[ix].added = io.ApplicationManager.hasCentre(Dots[ix].centre);
	        
            var tab = new GInfoWindowTab(strings[2], getInfoTabHTML(ix, 1));	        
            map.currentIndex = ix;
            map.sourceType = 1;

			var infoTabs = [ tab ];
			var K=allMMMmarkers[ix].latlng;
			map.openInfoWindowTabsHtml(K,infoTabs);
		}
	}
}

function showBubble4(tag)
{
	for(ix=0;ix<Dots.length;ix++)
	{
	    if(Dots[ix].tag==tag)
	    {
	    	//	2006-20-10 BEGIN PSERGEI: decreased table width
			var w1='<table width="200"><tr><td><b>'+Dots[ix].address+'</b></td></tr>'+
					'</table>';
			//	END PSERGEI
			var infoTabs = [
				new GInfoWindowTab(strings[2], w1)
			];
			var K=allMMMmarkers[ix].latlng;
			map.openInfoWindowTabsHtml(K,infoTabs);
		}
	}
}
