/**
 * Filename     :   map.js
 * Copyright    :   Transport for London 2005
 *
 * Author       :   Phil Maskell
 *
 **/
var origZIndex;
var currentPopup;
var popupId = 0;


AutoSizeFramedCloud = OpenLayers.Class(OpenLayers.Popup.FramedCloud, {
    'autoSize': true
});

function plotActivityGeometry(data) {
    if(data == "ERROR") {
        window.location.href = "error";
    } else if(data.indexOf('$') > 0) {
        clearLayer("activityCanvas");
        var points = data.split('|');
        if(points[0] == "1") {
            document.getElementById("messageContainer").innerHTML = "<div class=\"mapMessage positive alert\" id=\"mapResults\"><span>There is <strong>"+points[0]+" result</strong> currently displayed on the map.</span></div>";
        } else {
            document.getElementById("messageContainer").innerHTML = "<div class=\"mapMessage positive alert\" id=\"mapResults\"><span>There are <strong>"+points[0]+" results</strong> currently displayed on the map.</span></div>";
        }
        plotGeometry(data, "activityCanvas", new OpenLayers.Size(34,34));
    } else {
        document.getElementById("activityCanvas").innerHTML = "";
        if(currentPopup!= null) {
            map.removePopup(currentPopup);
            clearLayer("highlightMarker");
            currentPopup = null;
        }
        if(data == "0") {
            document.getElementById("messageContainer").innerHTML = "<div class=\"mapMessage negative alert\" id=\"mapResults\"><span>There are <strong>"+data+" results</strong>. Please zoom out or widen your search criteria.</span></div>";
        } else {
            document.getElementById("messageContainer").innerHTML = "<div class=\"mapMessage negative alert\" id=\"mapResults\"><span>There are <strong>"+data+" results</strong>, which are too many to display. Please zoom in or narrow your search until there are fewer than 500 results.";
        }
    }
}

function plotGeometry(data, layer, size) {
    var geometry = data.split('|');

    var plotCanvas = document.getElementById(layer);
    var pointsInnerHtml = new Array();

    var offset = new OpenLayers.Pixel(-(size.w/2), -(size.h/2));

    clearLayer("highlightMarker");
    for (i = 1; i < geometry.length; i++) {
        var pointPoly = geometry[i].split('$');
        if (pointPoly.length > 2) {
            var point = pointPoly[0].split(',');

            var pixel = map.getPixelFromLonLat(new OpenLayers.LonLat(point[0], point[1]));
            var x = pixel.x;
            var y = pixel.y;

            var left = x + offset.x;
            var top = y + offset.y;

            pointsInnerHtml.push("<img id=\"");
            pointsInnerHtml.push(pointPoly[2]);
            pointsInnerHtml.push("\" alt=\" \" title=\"Click symbol for more information\" class=\"mS\" src=\"");
            pointsInnerHtml.push(pointPoly[1]);
            pointsInnerHtml.push("\" style=\"position:absolute;left:");
            pointsInnerHtml.push(left);
            pointsInnerHtml.push("px;top:");
            pointsInnerHtml.push(top);
            pointsInnerHtml.push("px;z-index:10;\" onmouseover='javascript:mouseover(this, \"");
            pointsInnerHtml.push(pointPoly[3]);
            pointsInnerHtml.push("\")' onmouseout='javascript:mouseout(this, \"");
            pointsInnerHtml.push(pointPoly[1]);
            pointsInnerHtml.push("\")' onclick='javascript:mouseclick(\"");
            pointsInnerHtml.push(pointPoly[2]);
            pointsInnerHtml.push("\")' />");
        }
    }
    if(pointsInnerHtml.length > 0) {
        var innerHtml = pointsInnerHtml.join("");
        plotCanvas.style.display = 'none';
        plotCanvas.innerHTML = innerHtml;
        plotCanvas.style.display = '';
    }
}

function mouseover(img, url) {
    img.src = url;
    origZIndex = img.style.zIndex;
    img.style.zIndex = 1000;
}

function mouseout(img, url) {
    img.src = url;
    img.style.zIndex = origZIndex;
}

function mouseclick(id) {
    if(currentPopup != null && popupId != 0) {
        map.removePopup(currentPopup);
        clearLayer("highlightMarker");
        currentPopup = null;
        if(popupId != id) {
            popupId = id;
            AjaxFunctions.getInfo(id, mouseinfo);
        } else {
            popupId = 0;
        }
    } else {
        popupId = id;
        AjaxFunctions.getInfo(id, mouseinfo);
    }
}

function viewOnMap(x, y, z, worksRecId) {
    init(x, y, z);
    if (browser.isLessThanIE6) {
        //nowt
    } else {
        mouseclick(worksRecId);
    }
}

function mouseinfo(data) {
    if(data == "ERROR") {
        window.location.href = "error";
    } else {
        if (!browser.isKonqueror) {
            overview.minimizeControl();
        }
        var tmp = data.split('|');
        var ll = new OpenLayers.LonLat(tmp[0],tmp[1]);
        popupClass = AutoSizeFramedCloud;
        popupContentHTML = '<div id=\"infoMessage\">'+tmp[3]+'</div>';
        clearLayer("highlightMarker");
        addMarker(ll, tmp[2], popupClass, popupContentHTML, true);
    }
}

/**
 * Function: addMarker
 * Add a new marker to the markers layer given the following lonlat,
 *     popupClass, and popup contents HTML. Also allow specifying
 *     whether or not to give the popup a close box.
 *
 * Parameters:
 * ll - {<OpenLayers.LonLat>} Where to place the marker
 * popupClass - {<OpenLayers.Class>} Which class of popup to bring up
 *     when the marker is clicked.
 * popupContentHTML - {String} What to put in the popup
 * closeBox - {Boolean} Should popup have a close box?
 * overflow - {Boolean} Let the popup overflow scrollbars?
 */
function addMarker(ll, image, popupClass, popupContentHTML, closeBox, overflow) {
    var feature = new OpenLayers.Feature(highlightMarker, ll);
    feature.closeBox = closeBox;
    feature.popupClass = popupClass;
    feature.data.popupContentHTML = popupContentHTML;
    feature.data.overflow = (overflow) ? "auto" : "hidden";

    var marker = feature.createMarker();

    var size = new OpenLayers.Size(34,34);
    var offset = new OpenLayers.Pixel(-((size.w/2)-1), -(size.h/2));
    var icon = new OpenLayers.Icon(image, size, offset);

    marker.icon = icon;

    highlightMarker.addMarker(marker);

    if(currentPopup!= null) {
        map.removePopup(currentPopup);
    }
    currentPopup = feature.createPopup(closeBox);
    map.addPopup(currentPopup);
    currentPopup.show();
}
