﻿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 = "http://static2.foodbrasil.com.br/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);            
            $('#ctl00_FormCPH_mapaGeral_ddlBairro').val(ref);
            applySearchGeral(true, false);
            isViewPortSearchEnabledGeral = true;
        });
    }

    return newMarker;
}
