﻿function swapImgRestore() 
{ 
  var i,x,a=document.sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function preloadImages() 
{ 
  var d=document; if(d.images){ if(!d.p) d.p=new Array();
  var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
  if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}

function findObj(n, d) { 
  var p,i,x;  
  if(!d) d=document; 
  if((p=n.indexOf("?"))>0&&parent.frames.length) 
  {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
  }
  if(!(x=d[n])&&d.all) 
    x=d.all[n]; 
  for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && document.getElementById) 
    x=document.getElementById(n); 
  return x;
}

function swapImage() 
{ 
    var i,j=0,x,a=swapImage.arguments; document.sr=new Array; for(i=0;i<(a.length-2);i+=3)
    if ((x=findObj(a[i]))!=null){document.sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function Reseller(reseller_name_row_1, reseller_name_row_2, reseller_address_row_1, reseller_address_row_2, reseller_phone, reseller_fax, reseller_mail, dgLat, dgLon)
{
    this.reseller_name_row_1 = reseller_name_row_1;
    this.reseller_name_row_2 =  reseller_name_row_2;
    this.reseller_address_row_1 =  reseller_address_row_1;
    this.reseller_address_row_2 = reseller_address_row_2;
    this.reseller_phone = reseller_phone;
    this.reseller_fax = reseller_fax;
    this.reseller_mail = reseller_mail;
    this.dgLat = dgLat;
    this.dgLon = dgLon;
}
function initialize() 
{
  if (GBrowserIsCompatible()) 
  {
    var map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GScaleControl());
    map.addControl(new GOverviewMapControl());
    map.setCenter(new GLatLng(42.588907, 11.517371), 5);
    if (aResellers.length > 0)
        renderMap(map, aResellers, "http://www.akrapovic.it/images/mappointer.gif");
  }
}

function renderMap(map, aResellers, iconUri)
{
    var i, x1, x2, xm, y1, y2, ym;
    var icon = getIcona(iconUri, 23, 20, 0, 20, 20, 20);
    if (aResellers.length == 0)
    {
        map.setZoom(5);
        map.panTo(new GLatLng(42.588907, 11.517371));
        return;
    }
    gmarkers = new Array(aResellers.length);
    infoTabs = new Array(aResellers.length);

    for (i = 0; i < aResellers.length; i++)
    {
        if (i == 0)
        {
            x1 = aResellers[i].dgLat;
            x2 = aResellers[i].dgLat;
            xm = aResellers[i].dgLat;
            y1 = aResellers[i].dgLon;
            y2 = aResellers[i].dgLon;
            ym = aResellers[i].dgLon;
        }
        else
        {
            if (x1 > aResellers[i].dgLat)
                x1 = aResellers[i].dgLat;
            if (x2 < aResellers[i].dgLat)
                x2 = aResellers[i].dgLat;

            xm += aResellers[i].dgLat;

            if (y1 > aResellers[i].dgLon)
                y1 = aResellers[i].dgLon;
            if (y2 < aResellers[i].dgLon)
                y2 = aResellers[i].dgLon;

            ym += aResellers[i].dgLon;
        }

        point = new GLatLng(aResellers[i].dgLat, aResellers[i].dgLon);
        html =  "<table border='0' width='300' class='googleBaloon'>";
        html += "<tr><td><b>" + aResellers[i].reseller_name_row_1 + "</b></td></tr>";
        html += "<tr><td>" + aResellers[i].reseller_name_row_2 + "</td></tr>";
        html += "<tr><td>" + aResellers[i].reseller_address_row_1 + "</td></tr>";
        html += "<tr><td>" + aResellers[i].reseller_address_row_2 + "</td></tr>";
        html += "<tr><td>Tel. " + aResellers[i].reseller_phone + "</td></tr>";
        html += "<tr><td>Fax " + aResellers[i].reseller_fax + "</td></tr>";
        html += "<tr><td>e-mail: <a href=\"mailto:" + aResellers[i].reseller_mail + "\">" + aResellers[i].reseller_mail + "</a></td></tr>";
        html += "</table>";

        gmarkers[i] = createMarker(point, icon, html);
        infoTabs[i] = html;
        map.addOverlay(gmarkers[i]);
    }
    xm /= aResellers.length;
    ym /= aResellers.length;

    var centromappa = new GLatLng(xm, ym);
    var point_min = new GLatLng(x1, y1);
    var point_max = new GLatLng(x2, y2);
    var rectBounds = new GLatLngBounds(point_min, point_max);
    var zoom = map.getBoundsZoomLevel(rectBounds);
    map.setZoom(zoom - 1);
    map.panTo(centromappa);
}

function createMarker(point, icon, html)
{
    var marker = new GMarker(point, icon);
    GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
    return marker;
}
function writeLink(reseller_ID)
{
    for (i = 0; i < aResellers.length; i++)
    {
        if (aResellers[i].reseller_ID = reseller_ID)
        {
            document.write("<a title='Clicca qui per visualizzare il rivenditore sulla mappa.' href=\"#map\" onclick=\"openBaloon(" + i + ");\"><img alt='' src='" + icon + "' border='0'/></a>");
            break;
        }
    }
}

function openBaloon(i)
{
    gmarkers[i].openInfoWindowHtml(infoTabs[i]);
}

function getIcona(uri, w, h, anchorX, anchorY, infoWindowX, infoWindowY)
{
    if (!uri) return null;
    var icon = new GIcon();
    icon.image = uri;
    icon.iconSize = new GSize(w, h);
    icon.iconAnchor = new GPoint(anchorX, anchorY);
    icon.infoWindowAnchor = new GPoint(infoWindowX, infoWindowY);
    return icon;
}
