// Call this function when the page has been loaded      
function initialize() {        
	tboGoogleMap = new google.maps.Map2(document.getElementById("the_map"));       
	tboGoogleMap.setCenter(new google.maps.LatLng(27.957297,-82.462521), 10);  
	tboGoogleMap.addControl(new google.maps.LargeMapControl());
       tboGoogleMap.addControl(new google.maps.MapTypeControl());
	GEvent.addListener(tboGoogleMap,"movestart",function() {});
	GEvent.addListener(tboGoogleMap, "moveend", mapListener);
	passFeed();
	
	
    $('#jqmLegend').jqm({
       overlay: 0,
       onShow: function(h) {
           /* callback executed when a trigger click. Show notice */
           h.w.css('opacity',0.90).fadeIn('slow');
       },
       onHide: function(h) {
           /* callback executed on window hide. Hide notice, overlay. */
           h.w.fadeOut("slow",function() { if(h.o) h.o.remove(); });
       }
   });
}

function mapListener(){

	if ( tboGoogleMap.getZoom() < mapZoomMin){
		tboGoogleMap.setZoom(mapZoomMin);
	}
	if (tboGoogleMap.getZoom() > mapZoomMax){
		tboGoogleMap.setZoom(mapZoomMax);
	}
}

function passFeed(whatFeedAmI){

	  $('#loading_text').empty().html('Loading Points');
	  loadJson(georss);
	  if(jsonpath != ""){$.getJSON(jsonpath, loadJson);}
	  if(kmlpath != ""){loadKml(kmlpath);}
}

function geoCallback(){
	geoXml.gotoDefaultViewport(tboGoogleMap);
}

function loadKml(kml){
	geoXml = new GGeoXml(kml, geoCallback);
	tboGoogleMap.setCenter(geoXml.getDefaultCenter());
	tboGoogleMap.addOverlay(geoXml);
	$('#loading_text').empty().html('');
}

// Create our marker icon	  
function makeIcon (myicon) {
	if (!myicon){		
		myicon ="http://media.tbo.com/assets/_shared/map/blue.png";
	}
	var baseIcon = new google.maps.Icon();
	baseIcon.iconSize= new google.maps.Size(30,30);
	baseIcon.iconAnchor= new google.maps.Point(30,30);	
	baseIcon.infoWindowAnchor= new google.maps.Point(30,0);          
	var Newicon =  new google.maps.Icon(baseIcon, myicon, null, "");
	return(Newicon);
}



function loadJson(json){
	if(json.error){
		$('#loading_text').empty().html('No data avalible was passed');
		return;
	}
	 if(kmlpath != ""){
		tboGoogleMap.setCenter(new GLatLng(json.Center[0].lat, json.Center[0].lng), parseInt(json.Center[0].zoom));	
	}
	$.each(json.points, function(i,item){	
				
		var _icon = makeIcon (item.icon);
		var point = new GLatLng(item.latitude , item.longitude); 
		var marker = new GMarker(point, {icon:_icon, title:item.title});
		var infoTabs = [];							

		// Our info window content
		var tempTitle = '<div class="title">'+item.title +'</div><div class="street">'+item.body+ '</div>'
		var tempLink ="";
		
		if(item.Links.length != 0){
			tempLink = '<div class="zoom"><a href="'+ item.Links[0].url+'">'+ item.Links[0].name+'</a></div>'
		}
		tempBody ='<div id="info-map">'+tempTitle +' '+ tempLink +'</div>';
		infoTabs.push( new GInfoWindowTab(item.title, tempBody));

		GEvent.addListener(marker, "click", function() {
		  marker.openInfoWindowTabsHtml(infoTabs);
		});//end listener
		tboGoogleMap.addOverlay(marker);
	});//end each
	$('#loading_text').empty().html('');
}
