//Get Parameters
var map0;
var TMap_lat = null,
    TMap_lon = null,
    TMap_res = null,
    TMap_panel = null,
    TMap_legend = null,
    TMap_showAddressBar = null;

function GetURLParas()
{
    var url_lat = null,
    	url_lon = null,
    	url_res = null,
    	url_panel = null,
    	url_legend = null,
    	url_showAddressBar = null;
    
    var isURL=location.href;
    var paraList=isURL.split('#');

    if (paraList.length == 1)
    {
    	TMap_lat = flashInitLatitude;
    	TMap_lon = flashInitLongititude;
    	TMap_res = flashInitRes;
    	TMap_legend = default_LegendStr;
    	return;
    }
    
    var paras=paraList[1].split('&');
    
    var i;
    for(i in paras)
    {
        var temp=paras[i].split('=');
        if(temp[0].toLowerCase() == "lat")		{ url_lat    =temp[1] };
        if(temp[0].toLowerCase() == "lon")		{ url_lon    =temp[1] };
        if(temp[0].toLowerCase() == "res")		{ url_res    =temp[1] };
        if(temp[0].toLowerCase() == "panels")		{ url_panel  =temp[1] };
        if(temp[0].toLowerCase() == "legend")		{ url_legend =temp[1] };
        if(temp[0].toLowerCase() == "addressbar") { url_showAddressBar =temp[1].toString() };
    }
    
    TMap_lat = (url_lat==null)?flashInitLatitude:url_lat;
    TMap_lon = (url_lon==null)?flashInitLongititude:url_lon;
    TMap_res = (url_res==null)?flashInitRes:url_res;
    TMap_legend = (url_legend==null)?default_LegendStr:url_legend;
    TMap_showAddressBar = url_showAddressBar;
    //alert("lat="+url_lat+", lon="+url_lon+", res="+url_res+"\npanel="+url_panel+"\nlengend="+url_legend+"\naddressbar="+url_showAddressBar);
}
		
function on_mapload()
{
    ShowPanels();
    map0.SetBorderVisibility(true);
    map0.SetOnClickListener( ShowHelp );
    map0.SetOnDoubleClickListener( LoadDBPage );
    resizeMap();  
}


function ShowPanels()
{
    //Initial Varities
    var p_nav = false,
        p_zoom = false,
        p_overview = false,
        p_legend = false,
        p_download = false;
    
    //Get visiblity
    if(TMap_panel != null)
    {
    	var panelParas=url_panel.split('+');
	    var i;
	    for(i in panelParas)
	    {
	        if(panelParas[i].toLowerCase() == "nav")		{ p_nav      = true };
	        if(panelParas[i].toLowerCase() == "zoom")		{ p_zoom     = true };
	        if(panelParas[i].toLowerCase() == "overview")	{ p_overview = true };
	        if(panelParas[i].toLowerCase() == "legend")	{ p_legend   = true };
	        if(panelParas[i].toLowerCase() == "download") { p_download = true };
	    }
    }
    // show all by default.
    else
    {
    	p_nav = true,
        p_zoom = true,
        p_overview = true,
        p_legend = true,
        p_download = true;
    }
    
    //Set visiblity
    map0.SetNavigationPanelVisibility(p_nav);
    map0.SetZoomPanelVisibility		 (p_zoom);
    map0.SetMinimapPanelVisibility	 (p_overview);
    map0.SetLegendPanelVisibility	 (p_legend);
    map0.SetDownloadPanelVisibility  (p_download);
    //
    map0.SetNavigationPanelPosition(70, 10);
    map0.SetZoomPanelPosition(10, 10);
    map0.SetLegendPanelPosition(500, 50);
    map0.SetDownloadPanelPosition(150, 10);
    //Add Legend
    //alert(p_legend + "\n" + TMap_legend);
    if( (p_legend==true)&&(TMap_legend!=null))
    {
    	AddLegend();
    }
}


function window_resize()
{
    resizeMap();
}
        

function resizeMap()
{
    var mapW = document.getElementById("tableWidthDetector").offsetWidth;
    var mapH = 300;

    document.getElementById("divTMapContainer0").style.width = mapW;
	document.getElementById("divTMapContainer0").style.height = mapH;
	
	document.getElementById("divAddressBar").style.width = mapW;
							
	if ( map0 != null)
	{
	    map0.SetSize(mapW ,mapH);
	    map0.SetLegendPanelPosition(mapW - 100 , 50);
	}
}

function LoadDBPage( latitude, longitute )
{        
    var strURL = "http://vn4.cs.fiu.edu/cgi-bin/gnis.cgi?Lat=" + latitude + "&Long="+ longitute;
    if(typeof(tfFlashPassTrough)!='undefined')
    {
    	strURL += tfFlashPassTrough;
    }
	//if( typeof(tfaction)!='undefined' ) { strURL += ("&tfaction="+tfaction); }
	//if( typeof(vid)!='undefined' ) { strURL += ("&vid="+vid); }
    window.open(strURL, "_self");
}

function AddLegend()
{
    //map0.AddLegend("Miami4sale:MLS_listings_for_sale_and_rent_in_Miami_area@re1n-_2%2Bre2n-_2%2Brntn-_2%2Brinn-_3%2Brldn-_3;--owner:Property_owner@flpropertiesowner-_1;--year_built:Year_property_built_or_renovated@flpropertiesyear-_1;--size:Sizes_of_property_interior_and_lot,_Florida_zoomin_only@flpropertiessize-_1;--appraisal:Property_value,_Florida_zoomin_only@flpropertiesvalue-_1;FL_parcels:Florida_property_data_layers,_shown_at_closeup_zoomins_only:@flproplines-_1.t00;income@blkgrpy.d0v%2Bbg_mhinc;population@blk_pop;food@dining;services@nypages-_2;~places@anno_mreal%2Bgnis-_3%2Bhotels-_3;road_lines@street.l_;~cities:Cities,_towns@city%2Bincorp.00v;~roads:Roads,_and_streets@street._n");
    //map0.AddLegend("~photos:TerraFly_Architecture_Photography_Collection@geoimages-_3");
    map0.AddLegend(TMap_legend);
}

function PressEnter(e)
{
    var keynum;
    if(window.event) // IE
    {
        keynum = e.keyCode;
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which;
    }
    if(keynum == 13) // press enter
    {
        Button_GoMap_onclick();
    }
}

function Button_GoMap_onclick()
{
    var address =   document.getElementById("Text_Street").value;
    TGetLatLngByAddress ( address,  geoCode );
    
}

function Button_GoDB_onclick()
{
    var address =   document.getElementById("Text_Street").value;
    TGetLatLngByAddress ( address,  geoCodeDB );
}

function Button_Help_onclick(disp_Lat, disp_Lon)
{
    ShowHelp(disp_Lat, disp_Lon);
}         



function ShowHelp(lat, lon)
{
//    var ContentHelp = "<b>Double Click</b>: Local Data Reports and Queries"+

//                  "\n<b>Drag</b>: Browse the map"+

//                  "\nButtons on map:<b>Full Screen</b>, <b>Legend</b>, <b>Zoom</b>, <b>Download</b>"+

//                  "\nAddress bar examples:"+

//                  "\n\t1 Flagler St, Miami, FL\n\tMiami\n\tMiami, FL\n\t33139\n\t25.77 -80.19 (coordinates)";
    var ContentHelp="asdf";
    map0.ShowInfoWindow(lat, lon, 300, 200, ContentHelp);
}
       
function geoCode(Lat, Lon, GeocodeLevel, ErrorMsg)
{
    ErrorMsg = Trim( ErrorMsg);
    if(  ErrorMsg != "" ) { alert( ErrorMsg ); }
    if(  Lat != 0.0 || Lon != 0.0  ) map0.PanTo(Lat, Lon);        
}

function geoCodeDB(Lat, Lon, GeocodeLevel, ErrorMsg)
{
    ErrorMsg = Trim( ErrorMsg);
    if( ErrorMsg != "" ) { alert( ErrorMsg ); }    
    if( Lat != 0.0 || Lon != 0.0 ) { LoadDBPage( Lat, Lon ); }        
}

function Trim(sString) 
{
    while (sString.substring(0,1) == ' ')
    {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
        sString = sString.substring(0,sString.length-1);
    }   
    return sString;
}

function InitTFMap()
{
    // Get Size
    var mapW = ((document.getElementById("tableWidthDetector").offsetWidth)>0)?(document.getElementById("tableWidthDetector").offsetWidth):600;
    var mapH = 300;
	
	// Get Parameters from URL
	GetURLParas();
	
	// Set Map Container Size
    document.getElementById("divTMapContainer0").style.width = mapW;
	document.getElementById("divTMapContainer0").style.Height = mapH;
	
	// Set Address Bar Properties
	if(TMap_showAddressBar == "hidden")
	{
		document.getElementById("divABContainer").style.display = "none";
	}
	else
	{
		document.getElementById("divAddressBar").style.width = mapW;
	}
	
	// Create Map
	CreateTMap();
}

function CreateTMap()
{
	var TMap_level = TGetLevelByResolution( TMap_res );
	map0 = new TMap(document.getElementById("divTMapContainer0"), TMap_lat, TMap_lon, TMap_level, on_mapload, true);
}