/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is WCPierce Web Controls.
*
* The Initial Developer of the Original Code is William C. Pierce.
* Portions created by the Initial Developer are Copyright (C) 2005
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*   René Strauss
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */

/*
2005-09-08
+ Changed getMarkerById to getOverlayById
2005-09-10
+ Changed GMarker_ServerClick to call new server side event MarkerClick
+ Added ZoomLevel to GMap_SaveState
2005-10-05
+ Added optimizations to GMap_SaveState
+ Moved _ef and _defaultMarker to "global" variables
2005-10-12
+ Added the client counterparts to two new events, ClientLoad and MapTypeChanged
+ Updated GMap_SaveState to save the current Map Type to a new hidden field
*/

var _ef = function() { };
var _defaultMarker = new GMarker(new GPoint(0.1, 51.5));
var _gMapRegX = new RegExp(":", "gi");
_gMapRegX.compile(":", "gi");

var GMarkers = [];

GMap2.prototype.getOverlayById = function(a) {
    for (var b = 0; b < GMarkers.length; b++) {
        if (GMarkers[b].id == a)
            return GMarkers[b];
    }
    return null;
};

function addListener(a, b, c, d) {
    if (a.addEventListener) {
        a.addEventListener(b, c, d);
        return true;
    }
    else if (a.attachEvent) {
        var e = a.attachEvent("on" + b, c);
        return e;
    }
    else {
        alert("Handler could not be attached");
    }
}

function bind(a, b, c, d) {
    return window.addListener(a, b, function() { d.apply(c, arguments); });
}

function cbo_Complete(responseText, responseXML) {
    eval(responseText);
}

function cbo_Error(status, statusText, responseText) {
    alert('Error: ' + status + '\n' + statusText + '\n' + responseText);
}

function __DoCallBack(eventTarget, eventArgument) {
    var cbo = new CallBackObject();
    cbo.OnComplete = function() { cbo_Complete.apply(eventTarget, arguments) };
    cbo.OnError = cbo_Error;
    window.GMap_SaveState(eventTarget);
    cbo.DoCallBack(eventTarget.id, eventArgument);
}

function GMap_ServerClick(overlay, point) {
    if (overlay != null) {
        var point = new GPoint(0, 0);
        point.x = overlay.getPoint().x;
        point.y = overlay.getPoint().y;
    }
    var arg = 'GMap_Click|' + point.x + ',' + point.y;
    __DoCallBack(this, arg);
}

function GMap_ServerMoveStart() {
    var center = this.getCenter();
    var arg = 'GMap_MoveStart|' + center.x + ',' + center.y;
    __DoCallBack(this, arg);
}

function GMap_ServerMoveEnd() {
    var center = this.getCenter();
    var arg = 'GMap_MoveEnd|' + center.x + ',' + center.y;
    __DoCallBack(this, arg);
}

function GMap_MoveEnd() {
    var bounds = this.getBounds();
    var numOverlays = GMarkers.length;
    for (var i = 0; i < numOverlays; i++) {
        var pnt = GMarkers[i].getPoint();
        if (pnt.x < bounds.getSouthWest().x || pnt.y < bounds.getSouthWest().y ||
      pnt.x > bounds.getNorthEast().x || pnt.y > bounds.getNorthEast().y)
            this.removeOverlay(GMarkers[i]);
        numOverlays = GMarkers.length;
    }
}

function GMarker_ServerClick() {


    //var arg = 'GMarker_Click|'+this.u.x+','+this.u.y+','+this.id;
    //var arg = 'GMarker_Click|'+this.mf.x+','+this.mf.y+','+this.id;
    //var arg = 'GMarker_Click|' + this.mq.x + ',' + this.mq.y + ',' + this.id;
    var arg = 'GMarker_Click|' + this.jj.x + ',' + this.jj.y + ',' + this.id;
    // trying a as map property of gmarker
    __DoCallBack(this.f, arg);
    //  __DoCallBack(this.c, arg);
    //  __DoCallBack(this.a, arg);
    //  __DoCallBack(this.map, arg);
}

function GMap_ServerZoomEnd(oldZoomLevel, newZoomLevel) {
    if (oldZoomLevel == undefined)
        oldZoomLevel = newZoomLevel;
    var arg = 'GMap_ZoomEnd|' + oldZoomLevel + ',' + newZoomLevel;
    __DoCallBack(this, arg);
}

var MY_MIN_ZOOM_LEVEL = 0;
var MY_MAX_ZOOM_LEVEL = 10;

function GMap_MinMaxZoom() {
    if (this.getZoom() > MY_MIN_ZOOM_LEVEL)
        this.setZoom(MY_MIN_ZOOM_LEVEL);
    if (this.getZoom() < MY_MAX_ZOOM_LEVEL)
        this.setZoom(MY_MAX_ZOOM_LEVEL);
}

function GMap_ServerClientLoad(map) {
    var arg = 'GMap_ClientLoad|';
    __DoCallBack(map, arg);
}

function GMap_ServerMapTypeChanged() {
    var arg = 'GMap_MapTypeChanged|';
    __DoCallBack(this, arg);
}

function GMap_SaveState(eventTarget) {
    var evt = eventTarget ? eventTarget : this;
    var evtId = evt.id.replace(_gMapRegX, '_');
    document.getElementById(evtId + '_MapCenter').value = evt.getCenter();
    document.getElementById(evtId + '_MapBounds').value = evt.getBounds();
    document.getElementById(evtId + '_ZoomLevel').value = evt.getZoom();

    var mapType = 'G_NORMAL_MAP';
    switch (evt.getCurrentMapType()) {
        case G_HYBRID_MAP:
            mapType = 'G_HYBRID_MAP';
            break;
        case G_SATELLITE_MAP:
            mapType = 'G_SATELLITE_MAP';
            break;
        default:
            mapType = 'G_NORMAL_MAP';
            break;
    }
    document.getElementById(evtId + '_MapType').value = mapType;
}

