﻿function showMap(ajaxUrl)
{

    // Create map and center it on Tampa
	map = new GMap2(document.getElementById('map'));
	var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
	map.addControl(new GLargeMapControl3D(), topRight);
	var CenterPoint = new GLatLng(27.98141,-82.451141);
	map.setCenter(CenterPoint, 11);
    var trafficOptions = {incidents:true};
    trafficInfo = new GTrafficOverlay(trafficOptions);
    map.addOverlay(trafficInfo);

    // Add an event listener for moved event
    GEvent.addListener(map, "moveend", function() {
        getMapBounds(map);
    });

    // Set up variables to hold our boundaries
    swLat=0.0, swLng=0.0, neLat=0.0, neLng=0.0;
    getMapBounds(map);
    
    // Load our markers
    loadMarkers(map, ajaxUrl);    
  
}

function loadMarkers(map, json)
{

    // Clear out the array if there is anything in it
    markers = [];
    
    // We pass the boundaries of the current map in case we want to limit later
    //$.getJSON(url, { swLat: swLat, swLng: swLng, neLat: neLat, neLng: neLng },
        //function(json) {
            
            $.each(json.markers, function(i,item) {
                
                // Reset the marker
                marker = null;

                // If there is no lat/long, don't bother
                if (item.latitude == 0 || item.longitude == 0) {
                    return true;
                }
                
                // Build the content of the popup box
                //info_window_html  = "<div style='height: 100px;'>";
                //info_window_html += "<b>" + item.intersection + "</b><br/>";
                //info_window_html += "<img src=\"" + item.img + "\"><br/>";
               // info_window_html += "</div>";
                
                info_window_html = "<b>" + item.intersection + "</b>";
                
                // icon
                var mapIcon = new GIcon(G_DEFAULT_ICON);
                mapIcon.shadow = "";
                mapIcon.iconSize = new GSize(32,32);
                mapIcon.shadowSize = new GSize(32,32);
                
                mapIcon.image = icon_out;
                
                // Create the marker
                point = new GLatLng(item.latitude, item.longitude);
                marker = new GMarker(point,{icon:mapIcon});
                var tooltip = new Tooltip(marker,item.intersection,4);
                marker.tooltip = tooltip; 
                
                map.addOverlay(marker);  
                map.addOverlay(tooltip); 
                
                // Add the click event for tool tips         
                GEvent.addListener(marker,'mouseover',function(){ 
                        this.tooltip.show(); 
                        this.setImage(icon_over);
                    }); 
                GEvent.addListener(marker,'mouseout',function(){ 
                        this.tooltip.hide(); 
                        this.setImage(icon_out);
                    });
                

                // Add the click event
                GEvent.addListener(marker, "click", function() {
                   showMapInfoBox(map, markers[i].marker, i);
                });

                // Markers is a global array of objects
                obj_marker = new Object();
                obj_marker.point = point;
                obj_marker.rank = item.rank;
                obj_marker.img = item.img;
                obj_marker.intersection = item.intersection;
                obj_marker.marker = marker;
                obj_marker.info_window_html = info_window_html;
                markers[i] = obj_marker;

            }); // end each
            
            // Now that it's done, load up the legend
            loadLegend(map);
            
        //} // end callback function
    //); // end getJSON
   
} // end loadMarkers

function showMapInfoBox(map, marker, i) {
    //map.openInfoWindowHtml(marker.getPoint(), 
    //markers[i].info_window_html);
    jQuery.slimbox(markers[i].img, markers[i].info_window_html);
}

function loadLegend(map)
{
    if (markers.length == 0) {
        $('#legend_padded ul').append("<li>No Intersections Found.</li>");
    }
    
    $.each(markers, function(i, objValue) {
        if(objValue) {
          // Loop through the markers and add them to the UL list
          $("<li><a href='#'>" + objValue.intersection + 
              "</a></li>").click(function() {
                  showMapInfoBox(map, objValue.marker, i);
          }).hover(function () {
          tooltipMouseover(objValue.marker, objValue.point);
      }, 
      function () {
          tooltipMouseout(objValue.marker, objValue.point);
      })
      .appendTo($('#legend_padded ul'));
        }
    });
}


function showHideLegend()
{
    if (legendIsDisplayed) {
        // Need to hide legend
        new_map_width = 660;
        new_legend_width = 5;
        legendIsDisplayed = false;
        $('#legendButton').val('Show Camera List');
        // First fade and hide the legend, then expand the map
        $('#legend').fadeOut("fast");
        $('#legend').toggle();
        $('#map').animate({'width': new_map_width}, function() {
    	    $('this').map('CheckResize');
        });
    }
    else {
        // Need to show legend
        new_map_width = 430;
        new_legend_width = 200;
        legendIsDisplayed = true;
        $('#legendButton').val('Hide Camera List');
        // First shrink the map, then show and fade in the legend
        $('#map').animate({'width': new_map_width}, function() {
    	    $('this').map('CheckResize');
            $('#legend').toggle();
            $('#legend').fadeIn("slow");
    	});
    }
}

function toggleTraffic() 
{
    if (toggleState == 1) {
        map.removeOverlay(trafficInfo);
		$('#toggle_traffic').val('Traffic on');
        toggleState = 0;
    } else {
        map.addOverlay(trafficInfo);
		$('#toggle_traffic').val('Traffic Off');
        toggleState = 1;
    }
}

function getMapBounds(map) 
{
    var bounds = map.getBounds();
    var swLat = bounds.getSouthWest().lat();
    var swLng = bounds.getSouthWest().lng();
    var neLat = bounds.getNorthEast().lat();
    var neLng = bounds.getNorthEast().lng();
}

//makrer,sidebar mouseover handler
function tooltipMouseover(marker,point){
	marker.tooltip.show();
	marker.setImage(icon_over);
    map.setCenter(point, 11);
}

//marker,sidebar mouseout handler
function tooltipMouseout(marker,point){
	marker.tooltip.hide();
	marker.setImage(icon_out);
	map.setCenter(point, 11);
}

function countyCenter(s){

var d = s.options[s.selectedIndex].value;





              
 switch(d)
{
case"Citrus":
  point = new GLatLng(28.859131, -82.472771);
  break;
case"Hardee":
  point = new GLatLng(27.49165, -81.809517);
  break;
case"Hernando":
  point = new GLatLng(28.900375, -82.374634);
  break;
case"Highlands":
  point = new GLatLng(27.339479, -81.252869);
  break;
case"Hillsborough":
  point = new GLatLng(27.90938, -82.352707);
  break;
case"Pinellas":
  point = new GLatLng(27.928129, -82.716972);
  break;
case"Polk":
  point = new GLatLng(28.0026, -81.618393);
  break;
case"Sarasota":
  point = new GLatLng(27.33888, -82.539629);
  break;
default:
  
}             
              
              
              


if (d !=""){
map.setCenter(point, 11);
}




}




