function el_id(elm_id){
    if(document.getElementById(elm_id)) return document.getElementById(elm_id);
    else return 0;
}

var map = 0;  
var marker_index = 1;
var address_index = 0;
var addresses = new Array();
var latlongObjs = new Array();
var pushpinSkin = new Array();
var pushpinDetails = new Array();
var largeMapDrawn = false;
var indexes = new Array();
var markers = new Array();
var theMap = 0;
var pprint = 0;

var blogo_prefix = 'http://img.411.ca/';

var inputStartText = "Starting address...";
var inputEndText = "Ending address...";

var directionsPair = new Array();

function CreateMap() {
    map = new VEMap('tinyMapDiv');
}

function LoadMap(mapMode) {
    map = new VEMap('tinyMapDiv');
    if (token != '')
    {
	    map.SetClientToken(token);
    }
    map.SetDashboardSize(VEDashboardSize.Tiny);

    map.LoadMap(new VELatLong(lat_coords, long_coords), 15, VEMapStyle.Road, false);
    map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
    map.SetCenter(new VELatLong(lat_coords, long_coords)); 
    //param: lat-long object, 1=paid/0=reg, title/name, url

    AddPushPin(new VELatLong(push_pin_coords[0].lat, push_pin_coords[0].long), 1, push_pin_coords[0].addr, "#");

    map.SetCenter(new VELatLong(lat_coords, long_coords));
    map.SetZoomLevel(14);
    // must call last (After adding pins)
    DrawPushPins(map);

	if (pprint == 1)
	{
		window.print();
	}

}

function AddPushPin(latLongObj, colourType, description, url) {
    address_index++;
    pushpinSkin.push(colourType);
    latlongObjs.push(latLongObj);
    pushpinDetails.push(new Array(description, url));
}


function lage_map(){
    if(!largeMapDrawn) {
    
        el_id('fullMapDiv').style.display='block';
        el_id('fullMapDiv').style.visibility='visible';
        el_id('resz').style.display='block';
        el_id('resz').style.visibility='visible'; 
        el_id('tinyMapDiv').style.display='none';        
        el_id('tinyExpand').style.display='none';                
        el_id('tinyDiv').style.display='none';                
        
        var map2 = new VEMap('fullMapDiv');
	if (token != '')
	{
	        map2.SetClientToken(token);
	}
        map2.LoadMap(new VELatLong(lat_coords, long_coords), 10, VEMapStyle.Road, false);   
        map2.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
        map2.SetCenter(new VELatLong(lat_coords, long_coords)); 
        map2.SetZoomLevel(14);
    	map.SetDashboardSize(VEDashboardSize.Full);
        DrawPushPins(map2);     
                
        largeMapDrawn = true;
    }
    else
    {
        largeMapDrawn = false;
    }
}

function UnloadMap() {
    if (map != null) {
       map.Dispose();
    }
}


   
function DrawPushPins (map) {   
    var pin = new VEShape(VEShapeType.Pushpin, new VELatLong(lat_coords, long_coords));               
                                           
    if(pushpinSkin[i] == 0) {                        
        var icon = "<div class='profile-pushpin' id='profile-pushpin-1'><table border='0' cellpadding='0' cellspacing='0' width=20><tr><td width='5'><img src='/res/profilew_marker_top_left.gif' /></td><td  width=10 background='/res/profilew_marker_top_bg.gif'></td><td width='5'><img src='/res/profilew_marker_top_right.gif' /></td></tr><tr><td background='/res/profilew_marker_left.gif'></td><td nowrap bgcolor='#006991'><div id='profilew-pushpin-address'  class='pinaddr' style='height:2px; color:#fff'>&nbsp;</div><div style='width:15px; text-align: right; font-size: 11px; font-weight: bold; padding-bottom:1px;'>&nbsp;</div></td><td background='/res/profilew_marker_right.gif'></td></tr><tr><td colspan='3'><table border='0' cellpadding='0' cellspacing='0' width='100%'><tr><td width='10'><img src='/res/profilew_marker_bottom_left.gif' /></td><td width=15 background='/res/profilew_marker_bottom_bg.gif'></td><td width='5'><img src='/res/profilew_marker_bottom_right.gif' /></td></tr></table></td></tr></table></div>";
    } else {
        var icon = "<div class='profile-pushpin' id='profile-pushpin-1'><table border='0' cellpadding='0' cellspacing='0' width=20><tr><td width='5'><img src='/res/profile_marker_top_left.gif' /></td><td width=10 background='/res/profile_marker_top_bg.gif'></td><td width='5'><img src='/res/profile_marker_top_right.gif' /></td></tr><tr><td background='/res/profile_marker_left.gif'></td><td nowrap bgcolor='#006991'><div id='profile-pushpin-address' class='pinaddr' style='height:2px; color:#fff'>&nbsp;</div><div style='width:15px; text-align: right; font-size: 11px; font-weight: bold; padding-bottom:1px;'>&nbsp;</div></td><td background='/res/profile_marker_right.gif'></td></tr><tr><td colspan='3'><table border='0' cellpadding='0' cellspacing='0' width='100%'><tr><td width='10'><img src='/res/profile_marker_bottom_left.gif' /></td><td  width=15 background='/res/profile_marker_bottom_bg.gif'></td><td width='5'><img src='/res/profile_marker_bottom_right.gif' /></td></tr></table></td></tr></table></div>";
    }    
    var icon = "<div class='pushpin-container-big' id='profile-pushpin-1'><div class='pushpin-text'>&nbsp;</div></div>";
    map.ClearInfoBoxStyles();
    pin.SetDescription('');
    pin.SetCustomIcon(icon);
    map.AddShape(pin);
//    var oh = el_id('profile-pushpin-1').offsetHeight;
//    el_id('profile-pushpin-1').style.top = '-' + oh + 'px';
   
    //map.SetMapView(latlongObjs);    
    map.SetZoomLevel(14);
}

function GetRouteMap(startLocation, endLocation) {

    if ((startLocation == '') || (endLocation == ''))
    {
        alert('Please provide both addresses!');
        return false;
    }
    map = new VEMap('tinyMapDiv');
    map.SetDashboardSize(VEDashboardSize.Tiny);
    map.LoadMap();
    map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
    directionsPair = new Array(startLocation, endLocation);
    locations = new Array(startLocation, endLocation); // + ', Canada'
    var options = new VERouteOptions;
    options.DrawRoute      = true;
    options.SetBestMapView = true;
    options.RouteCallback  = ShowTurns;
    options.DistanceUnit   = VERouteDistanceUnit.Kilometer;
    options.ShowDisambiguation = true;

    map.LoadMap(new VELatLong(lat_coords, long_coords), 15, VEMapStyle.Road, false);

    map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
    map.GetDirections(locations, options);

}

function ShowTurns(route) {

	if (lang == 'fr')
	{
		m_from = 'Départ';
		m_drive = 'Distance';
		m_about = 'Durée';
		m_for = 'pour';
		m_to = 'Arrivée';
	}
	else
	{
		m_from = 'From';
		m_drive = 'Drive';
		m_about = 'About';
		m_for = 'for';
		m_to = 'To';
	}

 /*   var turns = "<h3>Turn-by-Turn Directions</h3>(rounding errors are possible)";
    turns += "<p><b>Distance:</b> " + route.Distance.toFixed(1) + " miles";
    turns += "<br/><b>Time:</b> " + GetTime(route.Time) + "</p>";
    */
    var directionsHTML = "<h2>"+m_from+":</h2>";
    //directionsHTML += "<p>" + locations[0] + "<br/>" + "Second line of location" + "</p>";
    directionsHTML += "<p>" + directionsPair[0] + "</p>";
    directionsHTML += "<h2>"+m_drive+": <span class='less-h2'>" +route.Distance.toFixed(1)+ " km /</span> "+m_about+": <span class='less-h2'>"+GetTime(route.Time)+"</span></h2><p></p>";
    // Unroll route and populate DIV
    var legs          = route.RouteLegs;
    var leg           = null;
    var turnNum       = 0;  // The turn #
    directionsHTML += "<ol>";
    // Get intermediate legs
    for(var i = 0; i < legs.length; i++) {
        // Get this leg so we don't have to derefernce multiple times
        leg = legs[i];  // Leg is a VERouteLeg object
        var legNum = i + 1;
        /*  turns += "<br/><b>Distance for leg " + legNum + ":</b> " + leg.Distance.toFixed(1) + " miles" +
               "<br/><b>Time for leg "     + legNum + ":</b> " + GetTime(leg.Time) + "<br/><br/>";
        */
        // Unroll each intermediate leg
        var turn        = null;  // The itinerary leg
        var legDistance = null;  // The distance for this leg
        for(var j = 0; j < leg.Itinerary.Items.length; j ++) {
            turn = leg.Itinerary.Items[j];  // turn is a VERouteItineraryItem object
            directionsHTML += "<li>"+turn.Text;
            legDistance = turn.Distance;
            // So we don't show 0.0 for the arrival
            if(legDistance > 0) {
                // Round distances to 1/10ths
                directionsHTML += " - "+legDistance.toFixed(1)+" km";
                // Append time if found
                if(turn.Time != null) {
                    directionsHTML += " "+m_for+" "+ GetTime(turn.Time);
                }
                directionsHTML += "</li>";
            }
        }
    }
    directionsHTML += "</ol>";
    directionsHTML += "<h2>"+m_to+":</h2>";
    //directionsHTML += "<p>"+locations[1]+"<br/>"+"ending location line 2"+"</p>";
    directionsHTML += "<p>"+directionsPair[1]+"</p>";
    // Populate DIV with directions
    // SetDirections(directionsHTML);
    document.getElementById("directions").innerHTML = directionsHTML;
    if (pprint == 1)
    {
    	window.print();
    }
}

function SetDirections(s) {
    var d = el_id("directions");
    d.innerHTML = s;    
}

// time is an integer representing seconds
// returns a formatted string
function GetTime(time) {
    if(time == null) {
       return '';
    }
    if(time > 60) {
                                     // if time == 100
       var seconds = time % 60;       // seconds == 40
       var minutes = time - seconds;  // minutes == 60
       minutes     = minutes / 60;    // minutes == 1
       if(minutes > 60) {
                                            // if minutes == 100
          var minLeft = minutes % 60;        // minLeft    == 40
          var hours   = minutes - minLeft;   // hours      == 60
          hours       = hours / 60;          // hours      == 1
          return(hours + " hour(s), " + minLeft + " minute(s), " + seconds + " second(s)");
       }
       else
       {
          return(minutes + " minutes, " + seconds + " seconds");
       }
    }
    else
    {
       return(time + " seconds");
    }
}

function ClearAll() {
    map.DeleteRoute();
    SetDirections("");
    map.LoadMap(latlongObjs[0], 15); // pick the location for this profile
    map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
    DrawPushPins(map);
}
