/*
    Mapa Inwestycji
    Copyright (C) 2006-2007 Jakub Labenski

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation version 3 of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

function Viewer()
{
}

Viewer.prototype.topMarkers = [];
Viewer.prototype.markers = [];
Viewer.prototype.shapes = [];
Viewer.prototype.bounds = new GLatLngBounds();
Viewer.prototype.showShapes = 0;

Viewer.prototype.addTopMarker = function(city_id, city, latitude, longitude, all, planned, started, finished, idea, suspended, abandoned, rebuilt, approved, demolished)
{
    var point = new GLatLng(latitude, longitude);
    this.topMarkers.push(this.createTopMarker(point, city_id, city, all, planned, started, finished, idea, suspended, abandoned, rebuilt, approved, demolished));
    this.bounds.extend(point);
}

Viewer.prototype.add = function(name, type, status, permalink, id, shape)
{

    var list = [];
    nextList(shape, 0, list, nextList, nextList, nextNumber);

    if (list.length < 1 || list[0].length < 2) {
        return;
    }

    var points = [];
    var markers = [];
    for (var i in list[0][0]) {
        var point = new GLatLng(list[0][0][i][0], list[0][0][i][1]);
        if (list[0][0][i].length < 3 || list[0][0][i][2] == 1) {
            var marker = this.createMarker(point, name, type, status, permalink, id);
            this.markers.push(marker);
            markers.push(marker);
        }
        points.push(point);
        this.bounds.extend(point);
    }

    var shapes = [];
    for (var i in list[0][1]) {
        var p = list[0][1][i];
        if (p.length > 1) {
            var poly = [];
            for (j in p) {
                poly.push(points[p[j]]);
            }
            var pl;
            if (p[0] == p[p.length - 1]) {
                pl = new GPolygon(poly, getShapeColor(status), 5, 0.66, getShapeColor(status), 0.38);
            } else {
                pl = new GPolyline(poly, getShapeColor(status), 5);
            }

            this.shapes.push(pl);
            shapes.push(pl);
        }
    }

    for (var i in markers) {
        markers[i].shape = shapes;
    }
}

Viewer.prototype.createMarker = function(point, name, type, status, permalink, id)
{

    var marker = new GMarker(point, getIcon(type, status));
    marker.status = status;
    marker.permalink = permalink;
    marker.name = name;
    marker.id = id;

    GEvent.addListener(marker, "click", function() {
        showInfoWindowHtml(marker, marker.name, marker.status, marker.permalink, marker.id);
    });

    GEvent.addListener(marker, "infowindowopen", function() {
        if (viewer.showShapes == 0) {
            for (var i in marker.shape) {
                map.addOverlay(marker.shape[i]);
            }
        }
    });

    GEvent.addListener(marker, "infowindowclose", function() {
        if (viewer.showShapes == 0) {
            for (var i in marker.shape) {
                map.removeOverlay(marker.shape[i]);
            }
        }
    });

    return marker;
}

Viewer.prototype.createTopMarker = function(point, city_id, city, all, planned, started, finished, idea, suspended, abandoned, rebuilt, approved, demolished)
{
    var marker = new GMarker(point);
    marker.all = all;
    marker.planned = planned;
    marker.started = started;
    marker.finished = finished;
    marker.idea = idea;
    marker.suspended = suspended;
    marker.abandoned = abandoned;
    marker.rebuilt = rebuilt;
    marker.approved = approved;
    marker.demolished = demolished;
    marker.city = city;
    marker.city_id = city_id;

    GEvent.addListener(marker, "click", function() {
        showTopInfoWindowHtml(marker, marker.city_id, marker.city, marker.all, marker.planned, marker.started, marker.finished, marker.idea, marker.suspended, marker.abandoned, marker.rebuilt, marker.approved, marker.demolished);
    });

    return marker;
}

Viewer.prototype.show = function(origZoom, origCoor1, origCoor2)
{
    var zoom = origZoom;
    var coor1 = origCoor1;
    var coor2 = origCoor2;

    if (zoom == -1) {
        zoom = map.getBoundsZoomLevel(this.bounds);
    }

    if(origZoom == -1){
        map.setCenter(this.bounds.getCenter(), zoom);
    } else {
        map.setCenter(new GLatLng(coor1, coor2), zoom);
    }

    for (var i in this.topMarkers) {
        map.addOverlay(this.topMarkers[i]);
    }

    for (var i in this.markers) {
        map.addOverlay(this.markers[i]);
    }

    if (this.showShapes) {
        for (var i in this.shapes) {
            map.addOverlay(this.shapes[i]);
        }
    }
}

Viewer.prototype.shapeStateChanged = function()
{
    if (this.showShapes) {
        this.showShapes = 0;
        for (var i in this.shapes) {
            map.removeOverlay(this.shapes[i]);
        }
    } else {
        this.showShapes = 1;
        for (var i in this.shapes) {
            map.addOverlay(this.shapes[i]);
        }
    }

    return this.showShapes;
}

function ViewerControl() {
}

ViewerControl.prototype = new MiControl();

ViewerControl.prototype.initialize = function(map) {
    var container = document.createElement("div");

    var shapeButton = this.createButton_(container, lcShapes, 1);
    GEvent.addDomListener(shapeButton, "click", function() {
        setButtonState(shapeButton, viewer.shapeStateChanged());
    });
    shapeButton.id = 'shape_id';
    map.getContainer().appendChild(container);

    return container;
}


function loadViewer() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.addControl(new ViewerControl());

        initIcons();

        load(map);
        if(gb_shape==1){
            var evt = document.createEvent("MouseEvents");
            evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
            var cb = document.getElementById("shape_id");
            cb.dispatchEvent(evt);
        }

    }
}

var viewer = new Viewer();

