﻿/*  map24journey.js
*   Description:
*           Javascript class to maintain a list of locations and provide
*           an easy wrapper for generating map24 local control commands.
*   By:     Paul Court
*   Date:   26/10/2006
*/

/*-- Constructor --*/
function Map24Journey(journey_id){
    this.journey_id = journey_id;
    this.points = [];
    this.icon_base = "http://www.trackinghost.co.uk/V2/images/icons/other/";
    this.icon_colours = ['blue','green','orange','red'];
    this.red_flag_icon = this.icon_base + 'redflag.png';
    this.green_flag_icon = this.icon_base + 'greenflag.png';
}

Map24Journey.prototype.AddPoint = function(loc_id, latitude, longitude, desc, bearing, colour){
    this.points.push({'id':loc_id, 'latitude':latitude, 'longitude':longitude, 'desc':desc, 'bearing':bearing, 'colour':colour});
}

Map24Journey.prototype.Get_LC_Commands = function(){
    var locations = "";
    var item_list = "";
    if(this.points.length > 0){
        for(var i = 0; i < this.points.length; i++){
            locations += this.LC_Item(
                this.LC_Location(
                    this.points[i].id, 
                    this.points[i].latitude, 
                    this.points[i].longitude, 
                    this.points[i].desc,
                    this.points[i].bearing,
                    this.points[i].colour)
            );
            item_list += this.LC_Item(this.points[i].id);
        }
    }
    var final_command = "";
    final_command += "<Commands>\n";
    final_command += locations + "\n";
    final_command += this.LC_Item(this.LC_ControlObject('SHOW', item_list)) + "\n";
    final_command += this.LC_Item(this.LC_SetMapView(item_list)) + "\n";
    final_command += "</Commands>\n";
    return final_command;
}

/* --- Some generic helper functions ---*/

// Name:    Map24Journey.Bearing_Name
// Desc:    Return a compass point name for a given bearing.
// Params:  Bearing from 0 to 360.
// Returns: String
Map24Journey.prototype.Bearing_Name = function(bearing){
    if (bearing > 337.5 || bearing < 22.5) return ("north");
    if (bearing > 22.5 && bearing < 67.5) return ("north_east");
    if (bearing > 67.5 && bearing < 112.5) return ("east");
    if (bearing > 112.5 && bearing < 157.5) return ("south_east");
    if (bearing > 157.5 && bearing < 202.5) return ("south");
    if (bearing > 202.5 && bearing < 247.5) return ("south_west");
    if (bearing > 247.5 && bearing < 292.5) return ("west");
    if (bearing > 292.5 && bearing < 337.5) return ("north_west");
    return "north";
}

// Name:    Map24Journey.Check_Icon_Colour
// Desc:    checks that a given color name is valid for an icon colour.
// Params:  A colour name as a string.
// Return:  Boolean true or false.
Map24Journey.prototype.Check_Icon_Colour = function(colour){
    var result = false;
    for(var i = 0; i < this.icon_colours.length; i++){
        if(colour == this.icon_colours[i]){ result = true; }
    }
    return result;
}

// Name:    Map24Journey.Icon_URL
// Desc:    returns an icon url for a given bearing and colour name.
// Params:  brearing from 0 to 360 degrees, and a valid colour name.
// Return:  string url to icon file.
Map24Journey.prototype.Icon_URL = function(bearing, colour){
    var icon_colour = colour;
    var final_icon = "";
    var icon_bearing = this.Bearing_Name(bearing);
    
    if(colour.indexOf("http") == 0){
        
        final_icon = colour;
    } else {
        if(!this.Check_Icon_Colour(colour)){
            icon_colour = "blue";
        }
        final_icon = this.icon_base + icon_colour + "_" + icon_bearing + ".png";
    }
    return final_icon;
}


/* --- Map24 LocalControl Wrappers --- */

// Name:    Map24Journey.LC_Coordinate
// Desc:    Returns a string representing a LocalControl coordinate block
//          for a given lat and long.
// Params:  Latitude and Longitude in normal WGS-84 decimal degrees
// Returns: String
Map24Journey.prototype.LC_Coordinate = function(latitude, longitude){    
    var map24lat = latitude * 60;
    var map24long = longitude * 60;
    var coord_block = "<Coordinate>\n\t<Longitude>"+map24long+"</Longitude>\n\t<Latitude>"+map24lat+"</Latitude>\n</Coordinate>\n";
    return coord_block;
}

// Name:    Map24Journey.LC_MapObjectID
// Desc:    Wraps the given string in map24 obejct id tags.
Map24Journey.prototype.LC_MapObjectID = function(object_id){
    return "<MapObjectID>" + object_id + "</MapObjectID>\n";
}

// Name:    Map24Journey.LC_Description
// Desc:    Wraps the given string in map24 description tags.
Map24Journey.prototype.LC_Description = function(desc){
    return "<Description>" + desc + "</Description>\n";
}

// Name:    Map24Journey.LC_Item
// Desc:    Wraps the given string in map24 item tags.
Map24Journey.prototype.LC_Item = function(item){
    return "<item>" + item + "</item>\n";
}

// Name:    Map24Journey.LC_Command
// Desc:    Wraps the given string in map24 command tags
Map24Journey.prototype.LC_Command = function(content){
    return "<Commands>" + url + "</Commands>\n";
}

// Name:    Map24Journey.LC_Icon
// Desc:    Creates the correct map24 icon tag for a given image url
Map24Journey.prototype.LC_Icon = function(url){
    return "<LogoURL>" + url + "</LogoURL>\n";
}

// Name:    Map24Journey.LC_Location
// Desc:    Returns a string representing a LocalControl location block
//          for the given parameters.
// Params:  Location Id, Latitude, Longitude, description, bearing, icon_colour
// Returns: String
Map24Journey.prototype.LC_Location = function(loc_id, latitude, longitude, desc, bearing, colour){    
    var location_block = "";
    location_block += "<DeclareMap24Location>\n";
    location_block += this.LC_MapObjectID(loc_id);
    location_block += this.LC_Coordinate(latitude, longitude);
    location_block += this.LC_Description(desc);
    location_block += this.LC_Icon(this.Icon_URL(bearing, colour));
    location_block += "</DeclareMap24Location>\n";
    return location_block;
}
Map24Journey.prototype.LC_LocationS = function(loc_id, latitude, longitude, desc){    
    var location_block = "";
    location_block += "<DeclareMap24Location>\n";
    location_block += this.LC_MapObjectID(loc_id);
    location_block += this.LC_Coordinate(latitude, longitude);
    location_block += this.LC_Description(desc);
    location_block += this.LC_Icon(this.green_flag_icon);
    location_block += "</DeclareMap24Location>\n";
    return location_block;
}
Map24Journey.prototype.LC_LocationE = function(loc_id, latitude, longitude, desc){    
    var location_block = "";
    location_block += "<DeclareMap24Location>\n";
    location_block += this.LC_MapObjectID(loc_id);
    location_block += this.LC_Coordinate(latitude, longitude);
    location_block += this.LC_Description(desc);
    location_block += this.LC_Icon(this.red_flag_icon);
    location_block += "</DeclareMap24Location>\n";
    return location_block;
}

// Name:    Map24Journey.LC_ControlObject
// Desc:    Returns a string representing a control block.
// Params:  Control name (SHOW, HIDE, ENABLE, DISABLE), item list.
// Returns: String
Map24Journey.prototype.LC_ControlObject = function(control, items){
    var control_block = "";
    control_block += "<ControlMapObject>\n";
    control_block += "<Control>" + control + "</Control>\n";
    control_block += "<MapObjectIDs>" + items + "</MapObjectIDs>\n";
    control_block += "</ControlMapObject>\n";
    return control_block;
}

// Name:    Map24Journey.LC_SetMapView
// Desc:    Returns a string representing a SetMapView block.
// Params:  Mab object id list.
// Returns: String
Map24Journey.prototype.LC_SetMapView = function(items){
    var control_block = "";
    control_block += "<SetMapView>\n";
    // control_block += "<Control>" + control + "</Control>\n";
    control_block += "<MapObjectIDs>" + items + "</MapObjectIDs>\n";
    control_block += "</SetMapView>\n";
    return control_block;
}
