﻿// JScript File
var mapIcons   = new Array();
mapIconsAdd ('home', false, undefined, 'assets/img/mapIcons/iconMyHome.png', 35, 22, 'assets/img/mapIcons/iconMyHomeShadow.png', 54, 22, 18, 34, 5, 1)
mapIconsAdd ('store', false, undefined, 'assets/img/mapIcons/iconStoreNormal.png', 22, 28, 'assets/img/mapIcons/iconStoreShadow.png', 49, 28, 8, 27, 5, 1)
mapIconsAdd ('store', false, 'available', 'assets/img/mapIcons/iconStoreAvailable.png', 22, 28, 'assets/img/mapIcons/iconStoreShadow.png', 49, 28, 8, 27, 5, 1)
mapIconsAdd ('store', false, 'exhausted', 'assets/img/mapIcons/iconStoreLimited.png', 22, 28, 'assets/img/mapIcons/iconStoreShadow.png', 49, 28, 8, 27, 5, 1)
mapIconsAdd ('store', false, 'unavailable', 'assets/img/mapIcons/iconStoreUnavailable.png', 22, 28, 'assets/img/mapIcons/iconStoreShadow.png', 49, 28, 8, 27, 5, 1)
mapIconsAdd ('store', true, undefined, 'assets/img/mapIcons/iconMyStoreNormal.png', 29, 40, 'assets/img/mapIcons/iconMyStoreShadow.png', 56, 40, 16, 39, 5, 1)
mapIconsAdd ('store', true, 'available', 'assets/img/mapIcons/iconMyStoreAvailable.png', 29, 40, 'assets/img/mapIcons/iconMyStoreShadow.png', 56, 40, 16, 39, 5, 1)
mapIconsAdd ('store', true, 'exhausted', 'assets/img/mapIcons/iconMyStoreLimited.png', 29, 40, 'assets/img/mapIcons/iconMyStoreShadow.png', 56, 40, 16, 39, 5, 1)
mapIconsAdd ('store', true, 'unavailable', 'assets/img/mapIcons/iconMyStoreUnavailable.png', 29, 40, 'assets/img/mapIcons/iconMyStoreShadow.png', 56, 40, 16, 39, 5, 1)
var mapStores = new Array ();
var mapGeocoder = null;
var map = null;
var mapPoints = new Array();

function mapAddHome (point)
{
    var i = mapGetIcon ('home', false, undefined);
    if (i != null)
    {
        var icon = new GIcon();
        icon.image = i.iconPath;
        icon.shadow = i.shadowPath;
        icon.iconSize = new GSize(i.iconWidth, i.iconHeight);
        icon.shadowSize = new GSize(i.shadowWidth, i.shadowHeight);
        icon.iconAnchor = new GPoint(i.iconAnchorX, i.iconAnchorY);
        icon.infoWindowAnchor = new GPoint(i.infoAnchorX, i.infoAnchorY);
        var marker = mapCreateMarker (point, icon, 'Casa tua');
        map.addOverlay(marker);
    }
}
function mapAddStores ()
{
    for (var i=0 ; i < mapStores.length ; i++)
    {
        var s = mapStores[i];
        if (typeof (s.point) != 'undefined' && s.point.lng() != 0)
        {
            var ic = mapGetIcon ('store', (typeof (mapFavouriteStore) == 'undefined' || mapFavouriteStore != s.id) ? false : true
                , s.productStatus);
            if (ic != null)
            {
                var icon = new GIcon();
                icon.image = ic.iconPath;
                icon.shadow = ic.shadowPath;
                icon.iconSize = new GSize(ic.iconWidth, ic.iconHeight);
                icon.shadowSize = new GSize(ic.shadowWidth, ic.shadowHeight);
                icon.iconAnchor = new GPoint(ic.iconAnchorX, ic.iconAnchorY);
                icon.infoWindowAnchor = new GPoint(ic.infoAnchorX, ic.infoAnchorY);
                var marker = mapCreateMarker4store (s.point, icon, s);
                map.addOverlay(marker);
            }
        }
    }
}
function mapAppendCell (row, text)
{
    var c = row.insertCell ();
    c.innerText = text;
}
function mapBounds (point1, point2)
{
    var laF = Math.min (point1.lat(), point2.lat())
        , laT = Math.max(point1.lat(), point2.lat())
        , lnF   = Math.min (point1.lng(), point2.lng())
        , lnT   = Math.max(point1.lng(), point2.lng());
    return (new GLatLngBounds (new GLatLng (laF, lnF), new GLatLng(laT, lnT)));
}
function mapBoundsExtend(bounds, quota)
{
    var pF = bounds.getSouthWest()
        , pT = bounds.getNorthEast()
        , laF = pF.lat()
        , laT = pT.lat()
        , lnF = pF.lng()
        , lnT = pT.lng()
        , ila = (laT - laF) * quota
        , iln = (lnT - lnF) * quota;
    return (new GLatLngBounds (new GLatLng(laF - ila, lnF - iln), new GLatLng (laT + ila, lnT + iln)));
}
function mapCreateMarker(point, icon, text)
{
    var marker = new GMarker(point, icon);
    GEvent.addListener(marker, "click", function()
        {
        marker.openInfoWindowHtml('Casa tua');
        });
    return (marker);
}
function mapCreateMarker4store(point, icon, store)
{
    var marker = new GMarker(point, icon);
    GEvent.addListener(marker, "click", function()
        {
            if (typeof (store.hours) == 'undefined' && typeof (store.phones) == 'undefined' && typeof (store.email) == 'undefined')
                marker.openInfoWindowHtml(mapFirstTab (store));
            else
            {
                var ts = [ new GInfoWindowTab ("Indirizzo", mapFirstTab(store))
                    , new GInfoWindowTab ("Altro"
                        , '<div class="mapsStoreDetailsDiv">'
                            + ((typeof (store.hours) == 'undefined') ? ''
                                : '<p class="mapsStoreDetailsInfo"><strong>'+mapstoreHours+'</strong>: ' + store.hours + '</p>')
                            + ((typeof (store.phones) == 'undefined') ? ''
                                : '<p class="mapsStoreDetailsInfo"><strong>'+mapteleph+'</strong>: ' + store.phones + '</p>')
                            + ((typeof (store.email) == 'undefined') ? ''
                                : '<p class="mapsStoreDetailsInfo"><strong>' + mapemail+'</strong>: <a href="mailto:' + store.email + '">'
                                    + store.email + '</a></p>')
                            + '</div>')
                          ];
                marker.openInfoWindowTabsHtml(ts);
            }
        });
    return (marker);
}
function mapErrorAdd (text)
{
    document.getElementById('mapError').innerHTML += text + '<br>';
}
function mapFirstTab (store)
{
    return ('<p class="mapsStoreDetailsTitle">' + store.name
        + ((typeof(store.centre) == 'undefined') ? '' : ', ' + store.centre) + '</p>'
        + '<div class="mapsStoreDetailsDiv">'
        + ((typeof (store.address) == 'undefined') ? ''
             : '<p class="mapsStoreDetailsInfo"><strong>'+mapaddressCaps+'</strong>:</p><p class="mapsStoreDetailsInfo">' + store.address
                + ((typeof (store.streetNumber) == 'undefined') ? '' : ', ' + store.streetNumber) + '</p>')
        + '<p class="mapsStoreDetailsInfo">'
        + ((typeof (store.zip) == 'undefined') ? '' : store.zip)
        + ' - ' + ((typeof (store.city) == 'undefined') ? '' : ' ' + store.city) + '</p>'
        + '</div>'
        + ((typeof (store.thumbnail) == 'undefined') ? ''
            : '<div class="mapsStoreDetailsDiv"><a href="' + store.image + '" target=_blank><img alt="' + store.name + '" src="'
                + store.thumbnail + '"></a></div>')
        + '<p class="mapsStoreDetailsButtons"><a href="javascript:mapZoomto(' + store.point.lng() + ', ' + store.point.lat()
            + ')">Zoom In</a></p>'
        + ((typeof (mapUser) != 'undefined' && (typeof (mapFavouriteStore) == 'undefined' || store.id != mapFavouriteStore))
            ? '<p class="mapsStoreDetailsButtons"><a href="javascript:mapSetMyStore(' + store.id
                + ')">Imposta come Punto Vendita preferito</a></p>'
            : '')
        + '<div class="mapsStoreDetailsDiv">'
        + ((typeof (store.newStatus) == 'undefined') ? ''
            : '<p class="mapsStoreDetailsInfo"><strong>'+mapnewprod+'</strong>: ' + mapStatusTranslate (store.newStatus) + '</p>')
        + ((typeof (store.secondHandStatus) == 'undefined') ? ''
            : '<p class="mapsStoreDetailsInfo"><strong>'+mapusedprod+'</strong>: ' + mapStatusTranslate(store.secondHandStatus) + '</p>')
        + '</div>');
}
function mapGetIcon (type, favourite, productStatus)
{
    for (var i=0 ; i < mapIcons.length ; i++)
    {
        var ic = mapIcons[i];
        if (ic.type == type && ic.favourite == favourite
            && (typeof(ic.productStatus) == 'undefined' && typeof(productStatus) == 'undefined'
                || ic.productStatus == productStatus))
            return (ic);
    }
    mapErrorAdd ('icon ' + type + ', ' + favourite + ', ' + productStatus + ' non found');
    return (null);
}
function mapGetStore (id)
{
    for (var i=0 ; i < mapStores.length ; i++)
        if (mapStores[i].id == id)
            return (mapStores[i]);
    return (null);
}
function mapIcon (type, favourite, productStatus, iconPath, iconWidth, iconHeight, shadowPath, shadowWidth, shadowHeight
    , iconAnchorX, iconAnchorY, infoAnchorX, infoAnchorY)
{
    this.type   = type;
    this.favourite  = favourite;
    this.productStatus  = productStatus;
    this.iconPath   = iconPath;
    this.iconWidth  = iconWidth;
    this.iconHeight = iconHeight;
    this.shadowPath = shadowPath;
    this.shadowWidth    = shadowWidth;
    this.shadowHeight   = shadowHeight;
    this.iconAnchorX    = iconAnchorX;
    this.iconAnchorY    = iconAnchorY;
    this.infoAnchorX    = infoAnchorX;
    this.infoAnchorY    = infoAnchorY;
}
function mapIconsAdd (type, favourite, productStatus, iconPath, iconWidth, iconHeight, shadowPath, shadowWidth, shadowHeight
    , iconAnchorX, iconAnchorY, infoAnchorX, infoAnchorY)
{
    mapIcons[mapIcons.length] = new mapIcon (type, favourite, productStatus, iconPath, iconWidth, iconHeight, shadowPath, shadowWidth
        , shadowHeight, iconAnchorX, iconAnchorY, infoAnchorX, infoAnchorY);
}
function mapLoad()
{
    if (typeof (GMap2) != 'undefined')
    {
        map = new GMap2(document.getElementById("mapDiv"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        mapGeocoder = new GClientGeocoder();
        if (typeof (mapHome) != 'undefined')
        {
            if (mapGeocoder)
                mapGeocoder.getLatLng(mapHome, function(point) {
                    if (!point) {
                        mapShow(point);
                        document.getElementById('mapError').innerText = mapHomeNotFound1 + mapHome + mapHomeNotFound2;
                    }
                    else {
                        document.getElementById('mapHomePosition').value = point.lng() + ',' + point.lat();
                        mapShow(point);
                    }
                });
        }
        else
        {
            mapShow(null);
        }
    }
}
function mapSetMyStore(id)
{
    mapFavouriteStore = id;
    document.getElementById('mapFavouriteStore').value = id;
    __doPostBack(mapClientId.replace('_', '$') + '$bSetFavouriteStore','');
}
function mapShow(home)
{
    if (home == null)
    {
        if (typeof (mapHomeLat) != 'undefined' && typeof (mapHomeLng) != 'undefined')
            home = new GLatLng(mapHomeLat, mapHomeLng);
        else if (typeof (mapFavouriteStore) == 'Number')
            home = mapGetStore (mapFavouriteStore).point;
    }
    if (home)
    {
        for (var i=0 ; i < mapStores.length ; i++)
        {
            var s = mapStores[i];
            if (typeof (s.point) != 'undefined'
                && (typeof (mapProduct) == 'undefined'
                    || typeof (s.productStatus) != 'undefined' && s.productStatus != 'unavailable'))
                s.distanceFromHome = home.distanceFrom(s.point);
        }
        mapStores = mapStores.sort (mapStoreDistanceFromHomeCompare);
    }
    var firstPoint = home;
    var bounds = null;
    var store;
    for (var i=0
        ; i < mapStores.length
            && (!home || i == 0 || typeof (mapStores[0].distanceFromHome) == 'undefined'
                || i < 3 && typeof ((store = mapStores[i]).distanceFromHome) != 'undefined' && store.distanceFromHome <= 70000)
        ; i++)
    {
        store = mapStores[i];
        if (typeof (store.point) != 'undefined' && store.point.x != 0)
        {
            if (firstPoint == null)
                firstPoint = store.point;
            else if (bounds == null)
                bounds = new mapBounds (firstPoint, store.point);
            else
                bounds.extend (store.point);
        }
    }
    if (bounds != null)
    {
        bounds = mapBoundsExtend (bounds, 0.1);
        map.setCenter (bounds.getCenter(), map.getBoundsZoomLevel(bounds));
        if (home != null)
            mapAddHome(home);
        mapAddStores ();
    }
}
function mapStatusTranslate (status)
{
    return ((status == 'unavailable') ? mapnotAvai : (status == 'exhausted') ? mapfewPie
        : (status == 'available') ? mapavail : mapnotDef);
}
function mapStore (id, name, address, streetNumber, zip, city, province, centre, hours, phones, email, longitude, latitude, thumbnail
    , image, productStatus, newStatus, secondHandStatus)
    {
    this.id = id;
    this.name = name;
    this.address = address;
    this.streetNumber = streetNumber;
    this.zip    = zip;
    this.city   = city;
    this.province   = province;
    this.centre = centre;
    this.hours = hours;
    this.phones = phones;
    this.email = email;
    if (typeof (GLatLng) != 'undefined' && typeof (latitude) != 'undefined' && typeof (longitude) != 'undefined')
        this.point = new GLatLng (latitude, longitude);
    this.thumbnail  = thumbnail;
    this.image  = image;
    this.productStatus  = productStatus;
    this.newStatus  = newStatus;
    this.secondHandStatus   = secondHandStatus;
    }
function mapStore2table (s)
{
    var t = document.getElementById('mapStores');
    var r = t.insertRow ();
    mapAppendCell (r, s.id);
    mapAppendCell (r, s.name);
    mapAppendCell (r, s.address);
    mapAppendCell (r, s.streetNumber);
    mapAppendCell (r, s.zip);
    mapAppendCell (r, s.city);
    mapAppendCell (r, s.province);
    mapAppendCell (r, s.centre);
    mapAppendCell (r, s.hours);
    mapAppendCell (r, s.phones);
    mapAppendCell (r, s.email);
    mapAppendCell (r, s.thumbnail);
    mapAppendCell (r, s.image);
    mapAppendCell (r, s.productStatus);
    mapAppendCell (r, s.newStatus);
    mapAppendCell (r, s.secondHandStatus);
    mapAppendCell (r, s.point.lng());
    mapAppendCell (r, s.point.lat());
}
function mapStores2table (s)
{
    for (var i=0 ; i < mapStores.length ; i++)
    {
        var s = mapStores[i];
        mapStore2table (s);
    }
}
function mapStoreDistanceFromHomeCompare(store1, store2)
{
    var b1 = typeof (store1.distanceFromHome) == 'undefined', b2 = typeof (store2.distanceFromHome) == 'undefined';
    if (b1 || b2)
        if (b1 == b2)
            return (0);
        else if (b1)
            return (1);
        else
            return (-1);
    else if (store1.distanceFromHome < store2.distanceFromHome)
        return (-1);
    else if (store1.distanceFromHome == store2.distanceFromHome)
        return (0);
    else
        return (1);
}
function mapStoreGetLatLng (i)
{
    var s = mapStores[i];
    if (mapGeocoder && s.point.lng() == 0)
    {
        var str = s.address + ((typeof (s.streetNumber) == 'undefined') ? '' : ' ' + s.streetNumber)
            + ((typeof (s.zip) == 'undefined') ? '' : ' ' + s.zip) + ((typeof(s.city) == 'undefined') ? '' : ' ' + s.city)
            + ((typeof (s.province) == 'undefined') ? '' : ' ' + s.province + ' Italia');
        mapGeocoder.getLatLng (str, function (point)
        {
            mapErrorAdd(i + ':' + point + ':' + str);
            if (point)
            {
                s.point = point;
                var r = document.getElementById('mapStores').rows[i+1];
                r.cells[r.cells.length - 2].innerText = point.lng();
                r.cells[r.cells.length - 1].innerText = point.lat();
            }
        });
    }
}
function mapStoresGetLatLng ()
{
    for (var i=0 ; i < mapStores.length ; i++)
        mapStoreGetLatLng (i);
}
function mapStoresUpdate ()
{
    var inp = document.getElementById('mapStoresPosition');
    inp.value = "";
    for (var i=0 ; i < mapStores.length ; i++)
    {
        var s = mapStores[i];
        inp.value += s.id + "," + s.point.lng() + "," + s.point.lat() + ",";
    }
    __doPostBack(mapClientId + '$bUpdate','');
}
function mapUnload ()
{
    if (typeof (GUnload) != 'undefined')
        GUnload();
}
function mapZoomto (x, y)
{
    p = new GLatLng (y, x);
    map.panTo (p);
    map.zoomIn();
}
