/*
DO NOT CHANGE THIS FILE copyright MyWeather LLC, 2008
*/
function TrafficMap(divID, host, lat, lon, zoom, msgHandler, dataUrl)
{
    //this.domain -------------------------------------------------------------------
    //this is how the map knows where to look for myweather data (incidents, cameras, traffic, icons...)
    this.domain = dataUrl; 
    //-------------------------------------------------------------------------------
    
    //this.contentBaseDirectoryAbsolute ----------------------------------------------
    //this is how the map knows to use the html pages directory as a base for relative url calls.
    //javascript automatically takes care of this for us when we use the app from different domains    
    this.contentBaseDirectoryAbsolute = dataUrl; // GetCurrentDirectoryAbsolute();
    //--------------------------------------------------------------------------------
           
    this.datasetDir = this.domain + "/data/" + host + "/dataset/";    
    this.incidentDataUrl = this.datasetDir + "traffic-incidents.xml";
    this.cameraDataUrl = this.datasetDir + "traffic-cameras.xml";
    this.sensorDataUrl = this.datasetDir + "traffic-sensors.xml";
    
    this.incidents = true;    
    this.cameras = true;
    this.sensors = true;
    
    this.localZoom = zoom;
    this.localLat = lat;
    this.localLon = lon;
    //this.directoryUrl = "../data/"+ host + "/overlay/";
    this.directoryUrl = this.domain + "data/"+ host + "/overlay/";
    this.host = host;
    this.minZoom = 4;
    this.maxZoom = 16;
    this.handler = msgHandler;
        
    var width = 650;
    var height = 381;
  var mapID = 'InteractiveWeatherFlash';
  
  document.trafficmap = this;   
    
  //document.getElementById(divID).innerHTML = this.WriteMarkup('flash/InteractiveMap.swf', width, height, mapID);
  document.getElementById(divID).innerHTML = this.WriteMarkup(this.domain + '/app/flash/InteractiveMap_v6.swf?cb=12345', width, height, mapID);
  
    this.map = navigator.appName.indexOf('Microsoft') != -1 ? window[mapID] : document[mapID];
}

TrafficMap.prototype.WriteMarkup = function WriteFlash(movie, width, height, id)
{
  var s= new String; 
  s+= '<div style="position:relative;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"  codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0" id="' + id + '" width="' + width + ' " height="' + height + '">\n';
  s+= '<param name="allowScriptAccess" value="always" />\n';
  s+= '<param name="allowNetworking" value="all" />\n';
  s+= '<param name="movie" value="' + movie + '" />\n';
  s+= '<param name="wmode" value="opaque" />\n';
  //s+= '<param name="menu" value="false" />\n';
  s+= '<param name="quality" value="high" />\n';
  s+= '<param name="bgcolor" value="#FFFFFF" />\n';
  s+= '<param name="base" value="' + this.contentBaseDirectoryAbsolute + '" />';
  s+= '<embed src="' + movie + '" name="' + id + '" base="' + this.contentBaseDirectoryAbsolute + '" quality="high" wmode="opaque" scale="exactfit" bgcolor="#FFFFFF" width="' + width + '" height="' + height + '" allowScriptAccess="always" allownetworking="all" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" />\n';
  s+= '</object>\n';
  s+= '<div id="mapPanel" style="position:absolute; top:30px; left:15px; background-color:transparent;">';
  s+= '<div id="zoomIn" onclick="document.trafficmap.ZoomIn();" style="margin-left:6px; width:24px; height:24px; cursor:pointer; background-image:url(' + this.domain + '/app/images/zoom_in.png); background-repeat:no-repeat;"></div>';
    s+= '<div id="zoomOut" onclick="document.trafficmap.ZoomOut();" style="margin-left:6px; width:24px; height:24px; cursor:pointer; background-image:url(' + this.domain + '/app/images/zoom_out.png); background-repeat:no-repeat;"></div>';
    s+= '</div>';
  s+= '</div>';
  return s;
}

TrafficMap.prototype.LoadIconLayer = function(xmlUrl, node, layer, onPressHandler, onRollOverHandler, onRollOutHandler, visible)
{
    //alert(xmlUrl);
    //public static function LoadIconsFromXML(xmlUrl:String, nodeName:String, imageName:String, latName:String, lonName:String, visible:Boolean, onPressCallback:String, onRollOverCallback:String, onRollOutCallback:String, minZoomLevel:Number, maxZoomLevel:Number, groupName:String, perpetual:Boolean, viewDependent:Boolean, layerDepth:Number)
    this.map.LoadIconsFromXML(xmlUrl, node, "image", "lat", "lon", visible, onPressHandler.toString(), onRollOverHandler.toString(), onRollOutHandler.toString(), 1, 20, layer, true, false, 1);
}

TrafficMap.prototype.LoadIncidents = function(visible)
{
    this.LoadIconLayer(this.incidentDataUrl ,'incident','TrafficMapIncidentLayer','document.trafficmap.TrafficMapIncident_onPress', 'document.trafficmap.TrafficMapIncident_onRollOver', 'document.trafficmap.TrafficMapIncident_onRollOut', visible);      
}

TrafficMap.prototype.LoadCameras = function(visible)
{
    this.LoadIconLayer(this.cameraDataUrl ,'camera','TrafficMapCameraLayer','document.trafficmap.TrafficMapCamera_onPress', 'document.trafficmap.TrafficMapCamera_onRollOver', 'document.trafficmap.TrafficMapCamera_onRollOut', visible);      
}

TrafficMap.prototype.LoadSensors = function(visible)
{
    this.LoadIconLayer(this.sensorDataUrl ,'sensor','TrafficMapSensorLayer','document.trafficmap.TrafficMapSensor_onPress', 'document.trafficmap.TrafficMapSensor_onRollOver', 'document.trafficmap.TrafficMapSensor_onRollOut', visible);      
}

TrafficMap.prototype.ZoomIn = function()
{this.map.ZoomIn();}

TrafficMap.prototype.ZoomOut = function()
{this.map.ZoomOut();}

TrafficMap.prototype.SetView = function(zoom, lat, lon)
{this.map.SetView(zoom, lat, lon);}

TrafficMap.prototype.HideTraffic = function()
{this.map.RoutecastHideTraffic();}

TrafficMap.prototype.ShowTraffic = function()
{this.map.RoutecastShowTraffic();}

TrafficMap.prototype.HideIconLayer = function(layer)
{this.map.HideGroup(layer);}

TrafficMap.prototype.ShowIconLayer = function(layer)
{this.map.ShowGroup(layer);}

TrafficMap.prototype.HideIncidents = function(types)
{
    if (types == null)
    {this.map.HideGroup("TrafficMapIncidentLayer");}
    else
    {this.map.HideGroup("TrafficMapIncidentLayer", "type", types);}
}

TrafficMap.prototype.ShowIncidents = function(types)
{
    if (types == null)
    {this.map.ShowGroup("TrafficMapIncidentLayer");}
    else
    {this.map.ShowGroup("TrafficMapIncidentLayer", "type", types);}        
}

TrafficMap.prototype.HideCameras = function()
{this.HideIconLayer("TrafficMapCameraLayer");}

TrafficMap.prototype.ShowCameras = function()
{this.ShowIconLayer("TrafficMapCameraLayer");}

TrafficMap.prototype.HideSensors = function()
{this.HideIconLayer("TrafficMapSensorLayer");}

TrafficMap.prototype.ShowSensors = function()
{this.ShowIconLayer("TrafficMapSensorLayer");}

TrafficMap.prototype.TrafficMapIncident_onPress = function(icon)
{
    icon.iconType = "TrafficMap.Incident";
    this.handler("onPress", icon);   
}

TrafficMap.prototype.TrafficMapIncident_onRollOver = function(icon)
{
    icon.iconType = "TrafficMap.Incident";
    this.handler("onRollOver", icon);  
}

TrafficMap.prototype.TrafficMapIncident_onRollOut = function(icon)
{
    icon.iconType = "TrafficMap.Incident";
    this.handler("onRollOut", icon);  
}

TrafficMap.prototype.TrafficMapCamera_onPress = function(icon)
{
    icon.iconType = "TrafficMap.Camera";
    this.handler("onPress", icon);   
}

TrafficMap.prototype.TrafficMapCamera_onRollOver = function(icon)
{
    icon.iconType = "TrafficMap.Camera";
    this.handler("onRollOver", icon);  
}

TrafficMap.prototype.TrafficMapCamera_onRollOut = function(icon)
{
    icon.iconType = "TrafficMap.Camera";
    this.handler("onRollOut", icon);  
}

TrafficMap.prototype.TrafficMapSensor_onPress = function(icon)
{
    icon.iconType = "TrafficMap.Sensor";
    this.handler("onPress", icon);   
}

TrafficMap.prototype.TrafficMapSensor_onRollOver = function(icon)
{
    icon.iconType = "TrafficMap.Sensor";
    this.handler("onRollOver", icon);  
}

TrafficMap.prototype.TrafficMapSensor_onRollOut = function(icon)
{
    icon.iconType = "TrafficMap.Sensor";
    this.handler("onRollOut", icon);  
}


function GetDefaultIRParams(paramSchema)
{
    // called from actionscript (this is where you configure the map at startup)
    
    // actionscript passes in an array which you can loop through to view the array schema if you would like
    // or you could just ignore the paramSchema array
        
    //alert(paramSchema.toString());    //view array schema
   
    //var trafficmap = document.trafficmap;


    var sectorServiceUrl = 'http://md.myweather.net/?service=radarservice';

    //var virtualEarthTileUrl = 'http://r3.ortho.tiles.virtualearth.net/tiles/';
    var virtualEarthTileUrl = 'http://ecn.t3.tiles.virtualearth.net/tiles/';

    var params = new Array();
    params[0] = 0; //stormcells
    params[1] = "";   //sector groups    
    params[2] = -1;   //host type
    params[3] = 'MSVE';   //use which tiles (MSVE or MYWX)
    params[4] = document.trafficmap.minZoom;  //min zoom level
    params[5] = document.trafficmap.maxZoom; //max zoom level
    params[6] = 7;  //starting zoom level
    params[7] = 7;  //prompt zoom level ("Click anywhere on the map" zoom level)
    params[8] = document.trafficmap.localZoom;  //local zoom
    params[9] = document.trafficmap.localLat;   //local lat    
    params[10] = document.trafficmap.localLon;  //local lon
    params[11] = false;  //track mouse latlon and report back to javascript function CatchIRLatLon(lat, lon)
    params[12] = false;   //draw latlon lines
    params[13] = '';   //MYWX tile url (only used if params[3] = MYWX)     
    params[14] = "";    //myweather representation ("logo", "copyright", or "") 
    params[15] = false;  //watchboxes on or off
    params[16] = "";    //watchbox data url
    params[17] = false;   //lightning on or off
    params[18] = '';   //lightning data url (only used if param 17 = true)
    params[19] = true;    //routecast on or off
    params[20] = document.trafficmap.directoryUrl;  //routecast url, string, only used if param 19 = true
    params[21] = document.trafficmap.host;    //host, string
    params[22] = 0;    //MSVE tile style, number, (road = 0, aerial = 1, hybrid = 2)
    params[23] = document.trafficmap.handler.toString();  //message handler function name, string, flash periodically sends messages to this function
    params[24] = false; //drawMyLocation, boolean
    params[25] = 0; //myLocationLat
    params[26] = 0; //myLocationLon
    params[27] = 0; //myLocationzoom
    params[28] = ""; //liveLocalRadarUrl
    params[29] = false; //start with liveLocalRadar
    params[30] = false; //start with watchboxes on
    params[31] = false; //will we use county overlays?
    params[32] = false; //start with county overlays on?      
    params[33] = false; //will we be showing city forecasts?                
    params[34] = ""; //data url for city forecasts?      
    params[35] = false; //start with city forecasts visible on?  
    params[36] = ""; //futurecast url    
    params[37] = ""; //msve token
    params[38] = sectorServiceUrl;
    params[39] = virtualEarthTileUrl;
    params[40] = "";
    params[41] = 0xFFFFFF;
    params[42] = "xml/veAttribution.xml";   //ve attribution xml data url
    params[43] = "";
    
    //alert(params.toString());

    return params;

}

function InitialIRLoadComplete()
{
    //this function is called from the flash movie just before the "InitialIRLoadComplete" message is sent to the client page message handler
    var trafficmap = document.trafficmap;
    
    //load incidents onto the map
    if (trafficmap.incidents)
    {trafficmap.LoadIncidents(false);}
    
    //load cameras onto the map
    if (trafficmap.cameras)
    {trafficmap.LoadCameras(false);}
    
    //load speed sensors onto the map
    if (trafficmap.sensors)
    {trafficmap.LoadSensors(false);}
    
}

function GetCurrentDirectoryAbsolute()
{
    var dir = window.location.href;
    var dirs = dir.split('/');
    var page = dirs[dirs.length-1];
    dir = dir.replace(page, "");
    return dir;
}

