﻿var countryCode = location.pathname.substring(1, location.pathname.substring(1,location.pathname.length).indexOf("/")+1);
$.ajax({
  url: '/scripts/approach.xml',
  async: false,
  success: function(data){ 
                $(data).find(countryCode).each(function(){
                    addressHeadline = $(this).attr("addressHeadline");
                    addressAdr = $(this).attr("addressAdr"); 
                    addressPhone =  $(this).attr("addressPhone");  
                    addressFax = $(this).attr("addressFax");  
                    addressEmail = $(this).attr("addressEmail");  
                    addressUrl = $(this).attr("addressUrl");  
                    vcardPATH = $(this).attr("vcardPATH");  
                    latitude =  $(this).attr("latitude"); 
                    longitude = $(this).attr("longitude");  
            });
  }
}); 
// Create marker icon
var blueIcon = new GIcon(G_DEFAULT_ICON);
/* Start config variables - please customize location of image ordner and translations of button texts */
blueIcon.image = "http://www.pferd.com/images/pferd_zeiger_schatten.png";
blueIcon.shadow = "";
blueIcon.iconSize = new GSize(38,36);
var buttons = {
    'vcard': 'VCard speichern'
};
/* End config variables */
          
function load() {
    if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("offices-map"));
    var point = new GLatLng(parseFloat(latitude),parseFloat(longitude));    
    map.setCenter(point, 9);
    map.addControl(new GSmallMapControl());
    var marker = new GMarker(point, blueIcon);
    map.addOverlay(marker);
        
        GEvent.addListener(marker, 'click', function() {
        marker.hide();
        if (map.infoBox) {
            map.removeOverlay(map.infoBox);
        }
        var infoBox = new OfficeInfobox(this.content);
        map.infoBox = infoBox;
        map.addOverlay(infoBox);
    });
    }
}
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="headline">' +  addressHeadline + '</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">' + addressAdr + '</div>' +
                '<div class="addressBox">' +
                '<span class="addressLeft">Tel:</span> <a href="phone:' + addressPhone + '">' + addressPhone + '</a><br />' +
                        '<span class="addressLeft">Fax:</span> ' + addressFax  + '<br />' +
                '<span class="addressLeft link">E-Mail:</span> <a href="mailto:' + addressEmail + '" class="weiterlink">' + addressEmail + '</a><div class="clearleft">&nbsp;</div>' +
                '<span class="addressLeft link">URL:</span> <a href="' + addressUrl + '" target="_blank" class="weiterlink">' + addressUrl + '</a>' +
                '<div class="clearleft">&nbsp;</div>' +'</div>' +
            '</div>' +
                    '<div class="officeLayerContent">' +
                        '<div class="addressBox boxMarginTop">' + 
                            '<span class="addressLeft">&nbsp;</span> ' +
                            '<span class="addressLeft2"><a href="' + vcardPATH + '" class="vcardlink">' + buttons.vcard + '</a></span>' +
                            '<div class="clearleft">&nbsp;</div>' +
                        '</div>' +
                    '</div>' +
               '</div>';
    div.innerHTML = html;
    map.getPane(G_MAP_MARKER_PANE).appendChild(div);
    var closeImg = document.getElementById("closeImg");
    function removeInfoBox(ib, m) {
        return function() {
            GEvent.trigger(ib, "closeclick");
            m.removeOverlay(ib);
            load();
        };
    }
    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";
}
