﻿dojo.require("esri.map");

var map, aucklandCastleGraphic, bishopaAucklandParkAndRideGraphic;

function init() {
    var initialExtent = new esri.geometry.Extent({ "xmin": 418589, "ymin": 528010, "xmax": 422557, "ymax": 530656, "spatialReference": { "wkid": 102100} });
    map = new esri.Map("map", { extent: initialExtent, nav: true, logo: false, displayGraphicsOnPan: !dojo.isIE });
    var tiledMapServiceLayer = new esri.layers.ArcGISTiledMapServiceLayer("http://spatial.durham.gov.uk/ArcGIS/rest/services/External/Background_InterMap/MapServer");
    map.addLayer(tiledMapServiceLayer);

    //dynamically set the copyright year.
    var date = new Date;
    var year = date.getFullYear()

    dojo.byId('mapCopyright').innerHTML = dojo.string.substitute("© Crown copyright. All rights reserved. Durham County Council. LA 100049055. ${0}.", [year]);

    dojo.connect(map, "onLoad", function() {

        var aucklandCastleGeometry = new esri.geometry.Point(421363, 530202, new esri.SpatialReference({ wkid: 27700 }));
        aucklandCastleGraphic = generateGraphic(aucklandCastleGeometry, 'Event Location', 'Auckland Castle,<br>Bishop Auckland, <br> DL14 7NR', 'yellow', 'red', '/Style%20Library/en-US/Durham/images/icon-castle.gif');

        var bishopaAucklandParkAndRideGeometry = new esri.geometry.Point(419784, 528465, new esri.SpatialReference({ wkid: 27700 }));
        bishopaAucklandParkAndRideGraphic = generateGraphic(bishopaAucklandParkAndRideGeometry, 'Park & Ride', 'Bishop Auckland College,<br>DL14 6JZ', 'aqua', 'blue', '/Style%20Library/en-US/Durham/images/icon-bus.gif');

        map.graphics.add(aucklandCastleGraphic);
        map.graphics.add(bishopaAucklandParkAndRideGraphic);

        showFeatureCallout(aucklandCastleGraphic);

    });
}

function generateGraphic(pointGraphic, title, contents, color, outerColor, iconImageUrl) {

    // The InfoWindow will display data held within the attributes key/value object. 
    // The infoTemplate is used to tell the infoWindow how to display the attributes
    var infoTemplate = new esri.InfoTemplate("${aTitle}", "${aContents}");

    var attributes = { aTitle: title, aContents: contents };

    var symbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 15, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color(outerColor), 3), new dojo.Color(color));

	var graphic = new esri.Graphic(pointGraphic, symbol, attributes, infoTemplate);

    var pntSym = new esri.symbol.PictureMarkerSymbol(iconImageUrl, 17, 18);
    graphic.setSymbol(pntSym);

    return (graphic);
}

function getExtent(geometry) {

    var newExtent;
    switch (geometry.type) {
        case "point":
            newExtent = getXYAndScaleExtent(geometry.x, geometry.y, 2500);
            break;
        case "polyline":
            newExtent = geometry.getExtent();
            break;
        case "polygon":
            newExtent = geometry.getExtent();
            break;
    }

    return (newExtent);
}

function getXYAndScaleExtent(xValue, yValue, scaleValue) {

    var scale = parseInt(scaleValue);
    var x = parseInt(xValue);
    var y = parseInt(yValue);

    var dpi = 96;

    if (screen.deviceXDPI) {
        dpi = screen.deviceXDPI;
    }

    var spatialRef = new esri.SpatialReference({ wkid: 27700 });

    // Produce a scale factor using the map width and map height
    var mapWidth = (this.map.width / dpi) * 2.54 / 100;
    var mapHeight = (this.map.height / dpi) * 2.54 / 100;

    // Use the scale factor to build the extent of the map
    var xmin = x - (mapWidth * scale) / 2;
    var ymin = y - (mapHeight * scale) / 2;
    var xmax = x + (mapWidth * scale) / 2;
    var ymax = y + (mapHeight * scale) / 2;

    var newExtent = new esri.geometry.Extent();
    newExtent.xmin = parseFloat(xmin);
    newExtent.ymin = parseFloat(ymin);
    newExtent.xmax = parseFloat(xmax);
    newExtent.ymax = parseFloat(ymax);
    newExtent.spatialReference = spatialRef;

    return (newExtent);
}

function setMapExtent(newExtent, feature, callback) {

    var onExtentChangeHandle = dojo.connect(map, 'onExtentChange', function() {
        dojo.disconnect(onExtentChangeHandle);
        if (callback) {
            callback(feature);
        }
    });

    this.map.setExtent(newExtent, true);
}

function showFeatureCallout(featureGraphic) {

    map.infoWindow.setTitle(featureGraphic.getTitle());
    map.infoWindow.setContent(featureGraphic.getContent());
    map.infoWindow.resize(220, 130);
    var point = new esri.geometry.Point(featureGraphic.geometry.x, featureGraphic.geometry.y);

    map.infoWindow.show(map.toScreen(point), map.getInfoWindowAnchor(map.toScreen(point)));
}

function panToFeatureAndShowCallout(featureGraphic) {

    map.infoWindow.setTitle(featureGraphic.getTitle());
    map.infoWindow.setContent(featureGraphic.getContent());
    map.infoWindow.resize(220, 130);
    var point = new esri.geometry.Point(featureGraphic.geometry.x, featureGraphic.geometry.y);

    map.centerAt(point);
    
    map.infoWindow.show(map.toScreen(point), map.getInfoWindowAnchor(map.toScreen(point)));
}

function zoomToFeatureAndShowCallout(featureGraphic) {

    var extent = getExtent(featureGraphic.geometry);

    setMapExtent(extent, featureGraphic, function(feature) {

        map.infoWindow.setTitle(feature.getTitle());
        map.infoWindow.setContent(feature.getContent());
        map.infoWindow.resize(220, 130);
        var point = new esri.geometry.Point(feature.geometry.x, feature.geometry.y);
        map.infoWindow.show(map.toScreen(point), map.getInfoWindowAnchor(map.toScreen(point)));

    });
}

dojo.addOnLoad(init);
