﻿/*
 * Prints the map
 */
function PrintMap()
{
    ShowNotesPrintoutArea();
    RefreshNotesPrintoutArea();
    HideMapControlsThatDontAutoHide();
    
    window.print();
    
    // If this is IE we can hide the div right away
    if(window.ActiveXObject)
        HideNotesPrinoutArea();
        
    ShowMapControlsThatDontAutoHide();
}

/*
 * Lists all notes on a map
 */
function RefreshNotesPrintoutArea()
{
    ClearNotesPrintoutArea();
    
    // List path points
    for(var i=0;i<PathPoints.length;i++)
        AddNoteToPrintoutArea('addpoint_icon.gif',PathPoints[i].getLocation(),PathPoints[i].getNote());
    
    // List sprites
    for(var i=0;i<Sprites.length;i++)
        AddNoteToPrintoutArea(Sprites[i].getImageFilename(),Sprites[i].getLocation(),Sprites[i].getNote());    
}

/*
 * Adds a note to the printout area
 */
function AddNoteToPrintoutArea(ImageFilename,LatLng,Note)
{
    // If there's no note text, disregard
    if(Note==null||Note=="")
        return;
    
    // Create a new DOM element for the note printout
    var NewElement = document.createElement('div');
 
    // Set background style to alternating background color
    NewElement.className = (NotesPrintoutAlternatingBackground?'NotePrintout':"NotePrintoutAlternate");
    NotesPrintoutAlternatingBackground=!NotesPrintoutAlternatingBackground;
    
    // Add the icon
    var NewIcon = document.createElement('img');
    NewIcon.className='NotePrintoutIcon';
    NewIcon.src = "css/app/Sprites/" + ImageFilename;
    
    // Add the latitude and longitude
    var LatLngDisplay = document.createElement("div");
    LatLngDisplay.setAttribute("class",'NotePrintoutLocation');
    var NewLatitudeDisplay = document.createElement('div');
    NewLatitudeDisplay.setAttribute("class",'NotePrintoutLocationLat');
    NewLatitudeDisplay.appendChild(document.createTextNode("Latitude: " + LatLng.x));
    var NewLongitudeDisplay = document.createElement('div');
    NewLongitudeDisplay.className='NotePrintoutLocationLong';
    NewLongitudeDisplay.appendChild(document.createTextNode("Longitude: " + LatLng.y));
    LatLngDisplay.appendChild(NewLatitudeDisplay);
    LatLngDisplay.appendChild(NewLongitudeDisplay);
    
    // Add the note one line at a time
    var NoteLines = Note.split('\r\n');
    var NoteDisplay = document.createElement('div');
    NoteDisplay.setAttribute('class','NotePrintoutNote');
    for(var i=0;i<NoteLines.length;i++)
    {
        NoteDisplay.appendChild(document.createTextNode(NoteLines[i]));
        NoteDisplay.appendChild(document.createElement('br'));
    }

    // Assemble the new note element from its sections
    NewElement.appendChild(NewIcon);
    NewElement.appendChild(NoteDisplay);
    NewElement.appendChild(LatLngDisplay);
    
    // Append the new printout element to the printout area
    var NotesPrintoutContainer = $get('NotesPrintout');
    NotesPrintoutContainer.appendChild(NewElement);
}

var NotesPrintoutAlternatingBackground=true;

/*
 * Clears all notes from the printout area
 */
function ClearNotesPrintoutArea()
{
    // Clear all nodes from the notes printout area
    var NotesPrintout=$get('NotesPrintout');
    while(NotesPrintout.firstChild!=null)
        NotesPrintout.removeChild(NotesPrintout.firstChild)
        
    // Reset alternate flag for notes background
    NotesPrintoutAlternatingBackground=true;
}

/*
 * Hides the notes printout area
 */
function HideNotesPrinoutArea()
{
    $get('NotesPrintout').style['display']='none';
}

/*
 * Shows the notes printout area
 */
function ShowNotesPrintoutArea()
{
    $get('NotesPrintout').style['display']='block';
}

function HideMapControlsThatDontAutoHide()
{
    $get('DistanceDisplayOutside').style['display']='none';
    $get('FileMenu').style['display']='none';
}

function ShowMapControlsThatDontAutoHide()
{
    $get('DistanceDisplayOutside').style['display']='block';
//    $get('FileMenu').style['display']='block';
}

