﻿/* Start config variables - please customize location of image ordner and translations of button texts */
var PROVIDERURL = "/worldwide-provider.php";
var IMAGEPATH   = "/images";
//buttons wird aus laenderspez. js datei gezogen
//var buttons = {
//    'vcard': 'save VCard',
//    'print': 'print'
//};
/* End config variables */
function initApp() {
    var officeMap = new OfficeApp.Map();
}
var OfficeApp = {};
var map = null;
OfficeApp.Map = function() {
    map = new GMap2(document.getElementById("offices-map"));
    var mapControl = new GLargeMapControl();
    map.addControl(mapControl);
    map.setCenter(new GLatLng(40, 5), 1);
    var officeMarkers = new OfficeApp.OfficeMarkers(map);
    officeMarkers.setMarkers();
    var worldImage = new WorldImage();
    map.worldImage = worldImage;
    map.addOverlay(worldImage);
    GEvent.addListener(map, 'dragstart', function() {
        if (map.infoBox) {
            map.removeOverlay(map.infoBox);
        }
        map.worldImage.redraw;
    });
    GEvent.addListener(map, 'zoomend', function() {
        if (map.infoBox) {
            map.removeOverlay(map.infoBox);
        }
        map.worldImage.redraw;
    });
}
G_NORMAL_MAP.getMinimumResolution = function(){return 1};
G_NORMAL_MAP.getMaximumResolution = function(){return 14};
OfficeApp.OfficeMarkers = function(map) {
    this.map = map;
    mgr = new GMarkerManager(map);
}
OfficeApp.OfficeMarkers.prototype = {
    setMarkers: function(){
        GDownloadUrl(PROVIDERURL + '?method=getdealersww', function(data, a) {
            var xml = GXml.parse(data);
            if (xml) {
                var zoomMin, zoomMax;
                var markers = xml.documentElement.getElementsByTagName("marker");
                for (var i = 0; i < markers.length; i++) {
                    var latlng = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                             parseFloat(markers[i].getAttribute("lng")));
                    var dotIcon   = new GIcon(G_DEFAULT_ICON);
                    dotIcon.image = "/images/pferd_zeiger_schatten.png";
                    dotIcon.iconSize = new GSize(12, 12);
                    dotIcon.shadow = null;
                    dotIcon.iconAnchor = new GPoint(5, 0);
                    var dotMarker = new GMarker(latlng, {
                        title: markers[i].getAttribute("title"),
                        icon: dotIcon,
                        content: content
                    });
                    GEvent.addListener(dotMarker, 'click', function() {
                        if (map.infoBox) {
                            map.removeOverlay(map.infoBox);
                        }
                        map.setCenter(this.getPoint(), 3);
                        map.worldImage.show;
                        mgr.refresh();
                    });

                    var flagIcon = new GIcon(G_DEFAULT_ICON);
                    flagIcon.image = "/images/marker/24/" + markers[i].getAttribute("land_clean") + ".png";
                    flagIcon.iconSize = new GSize(24, 24);
                    flagIcon.shadow = null;
                    flagIcon.iconAnchor = new GPoint(5, 1);
                    var flagMarker = new GMarker(latlng, {
                        title: markers[i].getAttribute("title"),
                        icon: flagIcon
                    });
                    flagMarker.content = {
                        "id": markers[i].getAttribute("id"),
                        "title": markers[i].getAttribute("title"),
                        "country": markers[i].getAttribute("land"),
                        "country_clean": markers[i].getAttribute("land_clean"),
                        "name": markers[i].getAttribute("name"),
                        "address": markers[i].getAttribute("address"),
                        "plz": markers[i].getAttribute("plz"),
                        "ort": markers[i].getAttribute("ort"),
                        "phone": markers[i].getAttribute("phone"),
                        "fax": markers[i].getAttribute("fax"),
                        "email": markers[i].getAttribute("email"),
                        "homepage": markers[i].getAttribute("homepage")
                        
                    };
                    GEvent.addListener(flagMarker, 'click', function() {
                        if (map.infoBox) {
                            map.removeOverlay(map.infoBox);
                        }
                        var infoBox = new OfficeInfobox(this.content);
                        map.infoBox = infoBox;
                        map.addOverlay(infoBox);
                     });
                     mgr.addMarker(dotMarker, 1, 1);
                     mgr.addMarker(flagMarker, 2, 15);
                }
            }
            mgr.refresh();
        });
    }
}

function WorldImage() {
    this.width  = 73;
    this.height = 51;
    this.left   = 425;
    this.top    = 20;
    this.div;
}
WorldImage.prototype = new GOverlay();
WorldImage.prototype.initialize = function(map){
    var div = document.createElement("div");
    div.style.width  = this.width + "px";
    div.style.height = this.height + "px";
    div.style.left = this.left +"px";
    div.style.top = this.top +"px";
    div.style.position = "absolute";
    var html = '<a href="javascript:void(0);" id="worldImg"><img src="' + '/images/world_mini.gif" width="73" height="51" alt="" /></a>';
    div.innerHTML = html;
    map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
    var worldImg = document.getElementById("worldImg");
    function switchZoom(ib, m) {
        return function() {
            GEvent.trigger(ib, "click");
            m.setCenter(new GLatLng(40, 5), 1);
        };
    }
    GEvent.addDomListener(worldImg, 'click', switchZoom(this, map));
    this.map_ = map;
    this.div = div;
}
WorldImage.prototype.remove = function() {
    this.div.parentNode.removeChild(this.div);
}
WorldImage.prototype.copy = function() {
    return new WorldImage();
}
WorldImage.prototype.redraw = function(force) {
    if (this.map_.getZoom() == 1) {
        this.div.style.visibility = "hidden";
    } else {
        this.div.style.visibility = "visible";
    }

    var c1 = this.map_.fromLatLngToDivPixel(this.map_.getBounds().getSouthWest());
    var c2 = this.map_.fromLatLngToDivPixel(this.map_.getBounds().getNorthEast());
    this.div.style.width  = this.width + "px";
    this.div.style.height = this.height + "px";
    this.div.style.left = (c1.x + this.left) +"px";
    this.div.style.top = (c2.y + this.top) +"px";
}

function OfficeInfobox(content) {
    this.content_ = content;
    this.width  = 300;
    this.height = 200;
    this.left   = 109;
    this.top    = 40;
    this.div;
}
OfficeInfobox.prototype = new GOverlay();
OfficeInfobox.prototype.initialize = function(map) {
    var div = document.createElement("div");
    div.id  = "detailLayer";
    div.style.width  = this.width + "px";
    div.style.height = this.height + "px";
    div.style.left = this.left +"px";
    div.style.top = this.top +"px";
    div.style.position = "absolute";

    var html = '<div class="officeLayer">' +
                    '<div class="h2Outer">' +
                        '<div class="headlineFlag"><img src="' + '/images/marker/24/' + this.content_['country_clean'] + '.png" /></div>' +
                        '<div class="headline">' + this.content_['title'] + '</div>' +
                        '<div class="icClose"><a href="javascript:void(0);" id="closeImg"><img src="' + '/images/ic_close.gif" /></a></div>' +
                        '<div class="clear2">&nbsp;</div>' +
                    '</div>' +
                    '<div class="officeLayerContent heightAndScroll">' +
                        '<div class="addressBox">' + this.content_.address + '<br />' + this.content_.plz + ' ' + this.content_.ort + '</div>';
        if (this.content_.phone != "" || this.content_.fax != "" || this.content_.email != "" || this.content_.homepage != "")
        {
            html += '<div class="addressBox">';
            if (this.content_.phone != "")
            {
                html += '<span class="addressLeft">Tel:</span> ' + '<a href="phone:' + this.content_.phone + '">' + this.content_.phone + '</a><br />';
            }
            if (this.content_.fax != "")
            {
                html += '<span class="addressLeft">Fax:</span> ' + this.content_.fax + '<br />';
            }
            if (this.content_.email != "")
            {
                html += '<span class="addressLeft link">E-Mail:</span> ' + '<a href="mailto:' + this.content_.email + '" class="weiterlink">' + this.content_.email + '</a><br /><div class="clearleft">&nbsp;</div>';            }
            if (this.content_.homepage != "" && this.content_.homepage != "http://")
            {
                html += '<span class="addressLeft">URL:</span> ' + '<a href="' + this.content_.homepage + '" class="weiterlink">' + this.content_.homepage + '</a><br />';
            }
            html += '<div class="clearleft">&nbsp;</div>' +'</div>';
        }
            html += '</div>' +
                    '<div class="officeLayerContent">' +
                        '<div class="addressBox boxMarginTop">' +
                            '<span class="addressLeft">&nbsp;</span> ' +
                            '<span class="addressLeft2"><a href="/worldwide-provider.php?method=getvcard&kdnr=' + this.content_.id + '" class="vcardlink">' + buttons.vcard + '</a></span>' +
                            '<span class="addressLeft"><a href="javascript:window.print();" class="printlink">' + buttons.print + '</a></span>' +
                            '<div class="clearleft">&nbsp;</div>' +
                        '</div>' +
                    '</div>' +
               '</div>';
    div.innerHTML = html;
    map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
    var closeImg = document.getElementById("closeImg");
    function removeInfoBox(ib, m) {
        return function() {
            GEvent.trigger(ib, "closeclick");
            m.removeOverlay(ib);
        };
    }
    GEvent.addDomListener(closeImg, 'click', removeInfoBox(this, map));
    this.map_ = map;
    this.div = div;
}
OfficeInfobox.prototype.remove = function() {
    this.div.parentNode.removeChild(this.div);
}
OfficeInfobox.prototype.copy = function() {
    return new OfficeInfobox(this.content_);
}
OfficeInfobox.prototype.redraw = function(force) {
    if (!force) return;
    var c1 = this.map_.fromLatLngToDivPixel(this.map_.getBounds().getSouthWest());
    var c2 = this.map_.fromLatLngToDivPixel(this.map_.getBounds().getNorthEast());
    this.div.style.width  = this.width + "px";
    this.div.style.height = this.height + "px";
    this.div.style.left = (c1.x + this.left) +"px";
    this.div.style.top = (c2.y + this.top) +"px";
}
