﻿////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////
function isMobile() {
    if (navigator.userAgent.match(/Android/i) ||
    navigator.userAgent.match(/webOS/i) ||
    navigator.userAgent.match(/iPhone/i) ||
    navigator.userAgent.match(/iPod/i) ||
    navigator.userAgent.match(/Blackberry/i)) {
        return true;
    } else {
        return false;
    }
}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

var geocoder;
var map;
function googleMapsInitialize() {
    if (GBrowserIsCompatible()) {
        var mapCanvas = document.getElementById("map_canvas");
        map = new GMap2(mapCanvas);
        var latitude = mapCanvas.getAttribute("latitude");
        var longitude = mapCanvas.getAttribute("longitude");

        var mapType;
        var currentMapType = getCookie("TRANCHE_MAGAZINE_MAP_TYPE");
        if (currentMapType != "") {
            switch (currentMapType) {
                case "MAP": { mapType = G_NORMAL_MAP; break; }
                case "SATELLITE": { mapType = G_SATELLITE_MAP; break; }
                case "HYBRID": { mapType = G_HYBRID_MAP; break; }
                case "TERRAIN": { mapType = G_PHYSICAL_MAP; break; }
                default: mapType = G_SATELLITE_MAP;
            }
        }
        else
            mapType = G_SATELLITE_MAP;

        map.setCenter(new GLatLng(latitude, longitude), 13, mapType);
        var point = new GLatLng(latitude, longitude);
        map.setUIToDefault();
        //map.addOverlay(new GMarker(point));
        var mm = new GMarkerManager(map); 
        var marker = new GMarker(point);
        mm.addMarker(marker, 0, 17); 
        
        if (isMobile()) {
            map.disableDragging();
        }
        geocoder = new GClientGeocoder();
        //geocoder.getLocations(point, showAddress);
    }
}
function showAddress(response) {
    map.clearOverlays();
    if (!response || response.Status.code != 200) {
        alert("Status Code:" + response.Status.code);
    }
    else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
        marker = new GMarker(point);
        map.addOverlay(marker);
        GEvent.addListener(map, 'click', function(marker, point) {
            if (marker) {
                marker.openInfoWindowHtml
          ("<table width='180'><tr><td>" +
          place.address + "</td></tr></table>");
            }
        });
    }
}

function terminate() {
    GUnload();
    if (changePage) {
        changePage = false;
    } else {
        setCookie("TRANCHE_MAGAZINE_MAP_TYPE", "SATELLITE", 1);
    }
}
