﻿
// default constructor
function CMap(strName)
{
    // check parameters
    if (!strName)
        strName = "cMap";

    // build options list
    var options = {"centerX" : 2530000,
                   "centerY" : 5990000,
                   "zoomLevel" : 11,
                   "useScalebar" : true};
 
    // create map instance
    var baseMap = new GSMap(strName, options);
	
    // add controls
    baseMap.addControl(GSMap.COMPACT_ZOOM_CONTROL);
    
    // create default layer
    baseMap.createLayer("Default");
    
    // select created layer
    var baseLayer = baseMap.getLayer("Default");
    
    // store map and layer
    this.map = baseMap;
    this.layer = baseLayer;
    this.selectedFeature;
	
    // set the 'class functions'
    this.zoomMap = zoomMap;
    this.resetLayerZoom = resetLayerZoom;
    this.clearMap = clearMap;
    this.addMapPoint = addMapPoint;
    this.loadMapPoints = loadMapPoints;
    this.loadMapPointsWithText = loadMapPointsWithText;
    this.deleteFeature = deleteFeature;
	this.zoomMapToPoint = zoomMapToPoint;
	
	// accessors
    this.getMap = getMap;
    this.getLayer = getLayer;
    
    // event handlers
    this.onFeatureClicked = onFeatureClicked;
}

function zoomMapToPoint(x, y)
{
  var coordinate = new GSPoint(x, y);
  this.map.centerAndZoom(coordinate, 0);
  this.map.updateSize();
}

// zooms map to the specified level
function zoomMap(intLevel)
{
    // check parameters
    if (!intLevel)
        intLevel = 11;    
    
    if(intLevel == 11)
    {
        // center the map on NZ and zoom all the way out
        this.map.centerOnNewZealand();        
    }
    else
    {
        // change the zoom level of the map
        this.map.zoom(intLevel)
    }
}

// zooms the map to display all features on the layer
function resetLayerZoom()
{	            
    this.map.centerOnLayer("Default");
}

function clearMap()
{
    // clear all added features and reset map zoom
    this.layer.clear();
    this.zoomMap(11);
}

// add map point on specified coordinates
function addMapPoint(strFeatName, strFeatDescription, intXCoord, intYCoord)
{
    // check parameters
    if (!((strFeatName) && (strFeatDescription) && (intXCoord) && (intYCoord)))
        return;

    // create coordinate object based on entered values
    var coordinate = new GSPoint(parseInt(intXCoord), parseInt(intYCoord));
    
    // create the html-based information window
    var infoHtml = "<b>" + strFeatName + "</b><br />" +
                   "<em>" + strFeatDescription + "</em>";
    
    // create the parameter object used to create the feature object
    var params = {"name" : strFeatName,
                  "coordinate" : coordinate,
                  "infoHtml" : infoHtml};

    // create the feature object and add the event handler
    var feature = new GSPointFeature(params);
    feature.addEventHandler('click', onFeatureClicked);

    // add the feature to the current layer
    this.layer.addFeature(feature);
    
    // center map to show all features on current layer
    this.map.centerOnLayer("Default");
}

// loads an array of items { name, description, xCoord, yCoord } into the map starting at item index (intStart)
function loadMapPoints(arrItems, intStart)
{
    // check parameters
    if (!arrItems)
        return
    else if (!intStart)
        intStart = 0;

    var infoHtml;
    var params;
    
    if (intStart < 0)
        intStart = 0;

    // array to store created features
    var arrFeatures = [];
    
    for (var i = intStart; i < arrItems.length; i++)
    {
        // create coordinate object based on entered values
        var coordinate = new GSPoint(arrItems[i].xCoord, arrItems[i].yCoord);
        
        // create the html-based information window
        infoHtml = "<b>" + arrItems[i].name + "</b><br />" +
                   "<a href='" + arrItems[i].description + "'>View Details</a>";
        
        // create the parameter object used to create the feature object
        params = {"name" : arrItems[i].name,
                  "coordinate" : coordinate,
                  "infoHtml" : infoHtml};

        var icon = new GSIcon();
        icon.imageSrc = arrItems[i].iconSrc;
        icon.imageSize = new GSDimension(19,25);

        // create the feature object and add the event handler
        var feature = new GSPointFeature(params);   
        feature.setIcon(icon);     
        feature.addEventHandler('click', onFeatureClicked);
        
        // add created feature to array
        arrFeatures[arrFeatures.length] = feature;

        // clear memory
        delete coordinate;
        delete feature;
    }
    
    // add array of features to map
    this.layer.addFeatures(arrFeatures);
    
    // center map to show all features on current layer
    this.map.centerOnLayer("Default");    
}

function loadMapPointsWithText(arrItems, intStart)
{
    // check parameters
    if (!arrItems)
        return
    else if (!intStart)
        intStart = 0;

    var infoHtml;
    var params;
    
    if (intStart < 0)
        intStart = 0;

    // array to store created features
    var arrFeatures = [];
    
    for (var i = intStart; i < arrItems.length; i++)
    {
        // create coordinate object based on entered values
        var coordinate = new GSPoint(arrItems[i].xCoord, arrItems[i].yCoord);
        
        // create the html-based information window
        infoHtml = "<b>" + arrItems[i].name + "</b><br />" +
                   "<em>" + arrItems[i].description + "</em>";
        
        // create the parameter object used to create the feature object
        params = {"name" : arrItems[i].name,
                  "coordinate" : coordinate,
                  "infoHtml" : infoHtml};

        // create the feature object and add the event handler
        var feature = new GSPointFeature(params);        
        feature.addEventHandler('click', onFeatureClicked);
        
        // add created feature to array
        arrFeatures[arrFeatures.length] = feature;

        // clear memory
        delete coordinate;
        delete feature;
    }
    
    // add array of features to map
    this.layer.addFeatures(arrFeatures);
    
    // center map to show all features on current layer
    this.map.centerOnLayer("Default");    
}

// deletes selected feature (stored when clicking on a feature)
function deleteFeature(targetFeature)
{
    // check parameters
    if (!targetFeature)
        targetFeature = this.selectedFeature;
    
    // check if feature has been defined
    if (targetFeature)
    {
        // remove the attached event handler
        targetFeature.removeEventHandler(onFeatureClicked);
        
        // remove the feature from the default layer
        this.layer.removeFeature(targetFeature);
    }
}

function getMap()
{
    return this.map;
}

function getLayer()
{
    return this.layer;
}

// event handlers
function onFeatureClicked(e)
{
    // display infoHtml window
    this.showInfoWindow();
    
    // store the currently selected feature
    map.selectedFeature = this;
	
    // stop event from being handled further
    GSUtil.cancelEvent(e);
}

