﻿function getXmlFromString(strXML) {
    var xmlDoc;
    try {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(strXML);
        return xmlDoc;
    }
    catch (e) {
        try {
            var parser = new DOMParser();
            xmlDoc = parser.parseFromString(strXML, "text/xml");
            return xmlDoc;
        }
        catch (e) {
            return null;
        }
    }
}

function CreateIcon(iconImg) {
    var markerIcon = new GIcon(G_DEFAULT_ICON);
    markerIcon.image = iconImg;
    markerIcon.iconSize = new GSize(26, 31);

    return markerIcon;
}

function CreateCityMarker(titulo, latitude, longitude, descricao) {
    
    //Coods POA: -30.0277041,-51.2287346
    
    var markerIcon = new GIcon(G_DEFAULT_ICON);
    markerIcon.image = "/img/icoPremium.gif";
    markerIcon.iconSize = new GSize(26, 31);

    var newMarker = new GMarker(new GLatLng(latitude, longitude), { draggable: false, icon: markerIcon, title: titulo });

    GEvent.addListener(newMarker, "click", function() {
        newMarker.openInfoWindowHtml(descricao);
    });

    return newMarker;
}

function liberaViewPort()
{
    isViewPortSearchEnabled = true;
}

function liberaViewPortGeral() {
    isViewPortSearchEnabledGeral = true;
}

function CreateMarker(titulo, latitude, longitude, tipoConta, iconImage, ref, tipo) {
    var markerIcon = CreateIcon(iconImage);

    var newMarker = new GMarker(new GLatLng(latitude, longitude), { draggable: false, icon: markerIcon, title: titulo });

    if(tipo !="BAIRRO") {
        GEvent.addListener(newMarker, "click", function() {            
            isViewPortSearchEnabled = false;
            newMarker.openInfoWindowHtml("Carregando...");
            $.post(GcidadeUrl+'/Service/GetMapInfo.aspx',
                            { Latitude: latitude, Longitude: longitude, Id: ref },
                            function(data) {
                                newMarker.openInfoWindowHtml(data);
                                setTimeout('liberaViewPort()','3000');
                            }
                        );
        });
    } else {
        var geocoder = new GClientGeocoder();        
        GEvent.addListener(newMarker, "click", function() {  
            isViewPortSearchEnabled = false;
            map.setCenter(new GLatLng(latitude, longitude), 15);
            try {   
            document.getElementById('ctl00_ContentPlaceHolder1_MapaPrincipal1_ddlBairro').value = ref; }
            catch(err) { //Handle errors here 
            }
            applySearch(true, false);
            isViewPortSearchEnabled = true;
        });        
    }

    return newMarker;
}

function CreateMarkerGeral(titulo, latitude, longitude, tipoConta, iconImage, ref, tipo) {
    var markerIcon = CreateIcon(iconImage);

    var newMarker = new GMarker(new GLatLng(latitude, longitude), { draggable: false, icon: markerIcon, title: titulo });

    if (tipo != "BAIRRO") {
        GEvent.addListener(newMarker, "click", function() {
            isViewPortSearchEnabledGeral = false;
            newMarker.openInfoWindowHtml("Carregando...");
            $.post(GcidadeUrl+'/Service/GetMapInfo.aspx',
                            { Latitude: latitude, Longitude: longitude, Id: ref },
                            function(data) {
                                newMarker.openInfoWindowHtml(data);
                                setTimeout('liberaViewPortGeral()', '3000');
                            }
                        );
        });
    } else {
        var geocoder = new GClientGeocoder();
        GEvent.addListener(newMarker, "click", function() {
            isViewPortSearchEnabledGeral = false;
            mapGeral.setCenter(new GLatLng(latitude, longitude), 15);
            document.getElementById('ctl00_mapaGeral_ddlBairro').value = ref;            
            applySearchGeral(true, false);
            isViewPortSearchEnabledGeral = true;
        });
    }

    return newMarker;
}

//function InitializeSingleMap(xmlInput, isFullMap) {

//    // Verifica se browser do usuario suporta exibicao dos mapas
//    if (GBrowserIsCompatible()) {

//        // Definicao/Inicializacao das variaveis/funcoes
//        var hasCenter = false;
//        var markersXML = xmlInput;
//        var mapContainer = document.getElementById("innerMap");
//        var arrMarkers = new Array();
//        var arrMarkersPremium = new Array();
//        var markerManager;

//        var map = new GMap2(mapContainer);

//        // Processa o XML com os marcadores dos estabalecimentos usando jQuery
//        //
//        // <r>
//        //  <c n="xxx" lt="999" lg="999" t="P/L" />
//        //  <m n="xxx" lt="999" lg="999" t="P/L" />
//        //  .
//        //  .
//        // </r>

//        // Pega o marcador centras
//        $(markersXML).find("c").each(function() {

//            var element = $(this);

//            var Nome = element.attr("n");
//            var Latitude = element.attr("lt");
//            var Longitude = element.attr("lg");
//            var TipoConta = element.attr("t");
//            var IconImage;

//            if (TipoConta == "P")
//                IconImage = "/img/icoPremium.gif";
//            else if (TipoConta == "L")
//                IconImage = "/img/icoLite.gif";
//            else // Se for C: cidade
//                IconImage = "/img/icoPremium.gif";

//            map.setCenter(new GLatLng(Latitude, Longitude), 17);

//            map.setUIToDefault();

//            markerManager = new MarkerManager(map, { borderPadding: 0 });

//            if (isFullMap)
//                markerManager.addMarker(CreateCityMarker("Porto Alegre", "-30.0277041", "-51.2287346", "Porto Alegre, RS <br /> Brasil"), 0, 17);
//            else
//                markerManager.addMarker(CreateMarker(Nome, Latitude, Longitude, TipoConta, IconImage), 11, 17);
//        });

//        // pega os demais marcadores
//        $(markersXML).find("m").each(function() {
//            var element = $(this);

//            var Nome = element.attr("n");
//            var Latitude = element.attr("lt");
//            var Longitude = element.attr("lg");
//            var TipoConta = element.attr("t");

//            var IconImage;

//            if (isFullMap) {
//                if (TipoConta == "P")
//                    IconImage = "/img/icoPremium.gif";
//                else
//                    IconImage = "/img/icoLite.gif";
//            }
//            else {
//                IconImage = "/img/icoSemDestaque.gif";
//            }

//            if (TipoConta === "P")
//                arrMarkersPremium.push(CreateMarker(Nome, Latitude, Longitude, TipoConta, IconImage));
//            else
//                arrMarkers.push(CreateMarker(Nome, Latitude, Longitude, TipoConta, IconImage));
//        });

//        // Estabelecimentos lite
//        markerManager.addMarkers(arrMarkers, 15, 17);

//        // Estabelecimentos premium
//        markerManager.addMarkers(arrMarkersPremium, 12, 17);

//        // Cria os marcadores das cidades com servicos da food
//        markerManager.addMarker(CreateCityMarker("Porto Alegre", "-30.0277041", "-51.2287346", "Porto Alegre, RS <br /> Brasil"), 0, 10);

//        markerManager.refresh();

//    } else {
//        mapContainer.innerHTML = '<h2>O navegador de internet que você está utilizando não suporta a visualização de mapas.</h2>';
//    }
//}