﻿


var Projeto = {
    map: null,
    gxml: null,
    initialize: function() {
        jQuery(function() {

            Projeto.overLayKMLMataAtlantica = new GGeoXml("http://www.pactomataatlantica.org.br/kml/limite_MA.kml");
            Projeto.config();
        });
    },


    getCoords: function() {
        if (Projeto.coordsIsLoaded) return;
        Projeto.coordsIsLoaded = true;

        jQuery.getJSON("services/getCoords.aspx?t=ex", function(retorno) {
            if (retorno.code != '-1') {
                retorno.each(function(e) {

                    //var markerOpts = { draggable: true };
                    var latlng = new GLatLng(e.lat.replace(",", "."), e.lng.replace(",", "."));

                    //var marker = new GMarker(latlng, { draggable: true, boucy: true, clickable: true });


                    var icon = new GIcon(G_DEFAULT_ICON);
                    icon.iconAnchor = new GPoint(10, 17);
                    icon.iconSize = new GSize(10, 17);
                    icon.image = 'http://www.pactomataatlantica.org.br/img/marker.png';


                    //var marker = new GMarker(latlng, { draggable: false, clickable: true });

                    var marker = new GMarker(latlng, icon);

                    marker.draggable = false;
                    marker.clickable = true;

                    var extras = e.extras.split("|");
                    var instituicao = extras[4];
                    var dataInicio = extras[5];
                    var id = extras[6];
                    var img = ((extras.length >= 7) ? ("http://www.pactomataatlantica.org.br/media/" + extras[7]) : "");


                    marker.bindInfoWindowHtml("<div><p><img src=" + img + " alt=\"\"></p> " +
                        "<p style=\"font-size:9px;color:#000;\"><strong style=\"font-size:9px;\">Nome do Projeto:&nbsp;&nbsp;</strong>" + extras[0] + "</p>" +
                        "<p style=\"font-size:9px;color:#000;\"><strong style=\"font-size:9px;\">Área:&nbsp;&nbsp;</strong>" + extras[1] + " (ha)</p>" +
                        "<p><a style=\"font-size:9px;color:#000;\" href=\"visualizacao-cadastro-executor.aspx?pId=" + id + "\">Mais Informações</a></p>" +
                        "</div>");


                    Projeto.map.addOverlay(marker);


                });
            }
        });



        jQuery.getJSON("services/getCoords.aspx?t=vi", function(retorno) {
            if (retorno.code != '-1') {
                retorno.each(function(e) {
                    var latlng = new GLatLng(e.lat.replace(",", "."), e.lng.replace(",", "."));
                    var icon = new GIcon(G_DEFAULT_ICON);
                    icon.iconAnchor = new GPoint(10, 17);
                    icon.iconSize = new GSize(10, 17);
                    icon.image = 'http://www.pactomataatlantica.org.br/img/markerAzul.png';

                    var marker = new GMarker(latlng, icon);

                    marker.draggable = false;
                    marker.clickable = true;
           
                    var extras = e.extras.split("|");
                    var id = extras[5];

                    marker.bindInfoWindowHtml("<div>" +
                    "<p style=\"font-size:9px;color:#000;\"><strong style=\"font-size:9px;\">Nome do Produtor:&nbsp;&nbsp;</strong>" + extras[0] + "</p>" +
                    "<p style=\"font-size:9px;color:#000;\"><strong style=\"font-size:9px;\">Área Ocupada:&nbsp;&nbsp;</strong>" + extras[1] + " (ha)</p>" +
                    "<p><a style=\"font-size:9px;color:#000;\" href=\"visualizacao-cadastro-produtor.aspx?pId=" + id + "\">Mais Informações</a></p>" +
                    "</div>");

                    Projeto.map.addOverlay(marker);
                });
            }
        });

    },

    coordsIsLoaded: false,
    init3D: false,

    baseUrl: 'http://www.pactomataatlantica.org.br/kml/',

    overLayKMLMataAtlantica: null,

    overlayAL: new GGeoXml("http://www.pactomataatlantica.org.br/kml/AL/doc.kml"),
    overlayALIsLoaded: false,

    overlayBA: new GGeoXml("http://www.pactomataatlantica.org.br/kml/BA/doc.kml"),
    overlayBAIsLoaded: false,

    overlayES: new GGeoXml("http://www.pactomataatlantica.org.br/kml/ES/doc.kml"),
    overlayESIsLoaded: false,

    overlayMG: new GGeoXml("http://www.pactomataatlantica.org.br/kml/MG/doc.kml"),
    overlayMGIsLoaded: false,

    overlayMS: new GGeoXml("http://www.pactomataatlantica.org.br/kml/MS/doc.kml"),
    overlayMSIsLoaded: false,

    overlayPE: new GGeoXml("http://www.pactomataatlantica.org.br/kml/PE/doc.kml"),
    overlayPEIsLoaded: false,

    overlayPR: new GGeoXml("http://www.pactomataatlantica.org.br/kml/PR/doc.kml"),
    overlayPRIsLoaded: false,

    overlayRJ: new GGeoXml("http://www.pactomataatlantica.org.br/kml/RJ/doc.kml"),
    overlayRJIsLoaded: false,


    overlayRS: new GGeoXml("http://www.pactomataatlantica.org.br/kml/RS/doc.kml"),
    overlayRSIsLoaded: false,

    overlaySC: new GGeoXml("http://www.pactomataatlantica.org.br/kml/SC/doc.kml"),
    overlaySCIsLoaded: false,

    overlaySP: new GGeoXml("http://www.pactomataatlantica.org.br/kml/SP/doc.kml"),
    overlaySPIsLoaded: false,

    overLayKMLMataAtlantica: new GGeoXml("http://www.pactomataatlantica.org.br/kml/limite_MA.kml"),
    overlayMataAtlanticaIsLoaded: false,


    config: function() {

        Projeto.map = new GMap2(document.getElementById("map3d"), { size: new GSize(1000, 364) });
        Projeto.map.setCenter(new GLatLng(0, 0), 0);

        var kml = Projeto.baseUrl + "limite_MA.kml";
        Projeto.showKMLMataAtlantica();

        Projeto.map.enableDoubleClickZoom();

        var customUI = Projeto.map.getDefaultUI();
        Projeto.map.setUI(customUI);
        Projeto.map.setMapType(G_SATELLITE_MAP); //Earth


        Projeto.getCoords();

        //Centraliza o mapa no bioma da mata atlântica.
        Projeto.map.setCenter(Projeto.map.setCenter(new GLatLng(-18.28134, -46.357705), 3));

        jQuery("#hiddenKML").click(function(e) {
            Projeto.showKMLMataAtlantica();
            e.preventDefault();
        });
    },

    showKMLEarth: function(url) {

        jQuery("#map3d").show();
        var d = document.documentElement;
        var b = document.body;
        var who = d.offsetHeight ? d : b;
        jQuery("#map3d").css('height', Math.max(who.scrollHeight, who.offsetHeight) - 308);

        url = Projeto.baseUrl + url;

        if (!Projeto.init3D) {
            Projeto.init3D = true;
            init3D(true, url);
        }
        else
            fetchKML(url, 'mapa');

        return false;
    },


    showKMLUF: function(uf) {
        if (GBrowserIsCompatible()) {

            uf = uf.toString().toUpperCase();

            var overlayIsLoaded = eval("Projeto.overlay" + uf + "IsLoaded");

            var overlay = eval("Projeto.overlay" + uf);

            if (overlayIsLoaded) {
                eval("Projeto.overlay" + uf + "IsLoaded=false;");
                Projeto.map.removeOverlay(overlay);
                return;
            }
            else {
                eval("Projeto.overlay" + uf + "IsLoaded=true;");

                Projeto.map.addOverlay(overlay);
            }

            Projeto.gxml = overlay;

            GEvent.addListener(Projeto.gxml, "load", function() {
                if (Projeto.gxml.hasLoaded()) {
                    Projeto.map.setCenter(Projeto.gxml.getDefaultCenter(), Projeto.map.getBoundsZoomLevel(Projeto.gxml.getDefaultBounds()));
                }

            });


        }

    },

    showKMLMataAtlantica: function() {
        if (GBrowserIsCompatible()) {


            if (Projeto.overlayMataAtlanticaIsLoaded) {
                Projeto.overlayMataAtlanticaIsLoaded = false;
                Projeto.map.removeOverlay(Projeto.overLayKMLMataAtlantica);

                jQuery('#hiddenKML').attr('class', '');
                jQuery('#hiddenKML').html((PARAMS.getUrlVar("lang").replace("#", "") == "en") ? "Expand KML Map" : "Exibir Mapa do Bioma Lei da Mata Atlântica");
            }
            else {
                Projeto.overlayMataAtlanticaIsLoaded = true;
                Projeto.map.addOverlay(Projeto.overLayKMLMataAtlantica);

                jQuery("#hiddenKML").html((PARAMS.getUrlVar("lang").replace("#", "") == "en") ? "Hide KML Map" : "Esconder Mapa do Bioma Lei da Mata Atlântica");
                jQuery("#hiddenKML").attr('class', 'expand');
            }


        }
    }
}


Projeto.initialize();





//var Projeto = {
//    map: null,
//    gxml: null,
//    initialize: function() {
//        jQuery(function() {

//            Projeto.overLayKMLMataAtlantica = new GGeoXml("http://www.pactomataatlantica.org.br/kml/limite_MA.kml");
//            Projeto.config();
//        });
//    },


//    getCoords: function() {
//        if (Projeto.coordsIsLoaded) return;
//        Projeto.coordsIsLoaded = true;
//        jQuery.getJSON("services/getCoords.aspx", function(retorno) {
//            if (retorno.code != '-1') {
//                retorno.each(function(e) {

//                    //var markerOpts = { draggable: true };
//                    var latlng = new GLatLng(e.lat.replace(",", "."), e.lng.replace(",", "."));

//                    //var marker = new GMarker(latlng, { draggable: true, boucy: true, clickable: true });

//                    var marker = new GMarker(latlng, { draggable: false, clickable: true });

//                    marker.enableDragging();

//                    var extras = e.extras.split("|");
//                    var instituicao = extras[4];
//                    var dataInicio = extras[5];
//                    var id = extras[6];
//                    var img = ((extras.length >= 7) ? ("http://www.pactomataatlantica.org.br/media/" + extras[7]) : "");


//                    marker.bindInfoWindowHtml("<div><p><img src=" + img + " alt=\"\"></p> " +
//                        "<p style=\"font-size:9px;color:#000;\"><strong style=\"font-size:9px;\">Nome do Projeto:&nbsp;&nbsp;</strong>" + extras[0] + "</p>" +
//                        "<p style=\"font-size:9px;color:#000;\"><strong style=\"font-size:9px;\">Área:&nbsp;&nbsp;</strong>" + extras[1] + " (ha)</p>" +
//                        "<p><a style=\"font-size:9px;color:#000;\" href=\"http://www.pactomataatlantica.org.br/imprimir-executor.aspx?pId=" + id + "\">Mais Informações</a></p>" +
//                        "</div>");


//                    Projeto.map.addOverlay(marker);


//                });
//            }
//        });


//    },

//    coordsIsLoaded: false,
//    init3D: false,

//    baseUrl: 'http://www.pactomataatlantica.org.br/kml/',

//    overLayKMLMataAtlantica: null /*new GGeoXml("http://www.pactomataatlantica.org.br/kml/limite_MA.kml")*/,

//    config: function() {

//        var kml = Projeto.baseUrl + "limite_MA.kml";
//        Projeto.showKML(kml);
//        Projeto.getCoords();

//        jQuery("#hiddenKML").click(function(e) {
//            Projeto.showKmlMA(true);
//            e.preventDefault();
//        });
//    },

//    showKmlMA: function(init) {

//        if (jQuery('#hiddenKML').attr('class') == "expand" && init) {
//            
//            Projeto.coordsIsLoaded = false;
//            
//            Projeto.overLayKMLMataAtlantica = null;
//            Projeto.initMap();
//            if (null != Projeto.gxml)
//                Projeto.map.addOverlay(Projeto.gxml);


//            jQuery('#hiddenKML').attr('class', '');
//            jQuery('#hiddenKML').html((PARAMS.getUrlVar("lang").replace("#", "") == "en") ? "Expand KML Map" : "Exibir Mapa do Bioma Lei da Mata Atlântica");



//            Projeto.getCoords();

//            var customUI = Projeto.map.getDefaultUI();
//            this.map.setUI(customUI);
//            Projeto.map.setMapType(G_SATELLITE_MAP); //Earth
//        }
//        else {

//            jQuery("#hiddenKML").html((PARAMS.getUrlVar("lang").replace("#", "") == "en") ? "Hide KML Map" : "Esconder Mapa do Bioma Lei da Mata Atlântica");
//            jQuery("#hiddenKML").attr('class', 'expand');

//            if (null == Projeto.map) {
//                Projeto.initMap();
//            }

//            if (null == Projeto.overLayKMLMataAtlantica) {
//                Projeto.overLayKMLMataAtlantica = new GGeoXml("http://www.pactomataatlantica.org.br/kml/limite_MA.kml");
//            }

//            Projeto.map.addOverlay(Projeto.overLayKMLMataAtlantica);
//            Projeto.getCoords();
//        }
//    },

//    showKMLEarth: function(url) {

//        jQuery("#map3d").show();
//        var d = document.documentElement;
//        var b = document.body;
//        var who = d.offsetHeight ? d : b;
//        jQuery("#map3d").css('height', Math.max(who.scrollHeight, who.offsetHeight) - 308);

//        url = Projeto.baseUrl + url;

//        if (!Projeto.init3D) {
//            Projeto.init3D = true;
//            init3D(true, url);
//        }
//        else
//            fetchKML(url, 'mapa');

//        return false;
//    },

//    initMap: function() {
//        Projeto.map = new GMap2(document.getElementById("map3d"), { size: new GSize(1000, 364) });
//        Projeto.map.setCenter(new GLatLng(0, 0), 0);

//        if (null != Projeto.overLayKMLMataAtlantica)
//            Projeto.overLayKMLMataAtlantica = new GGeoXml("http://www.pactomataatlantica.org.br/kml/limite_MA.kml");
//    },

//    overLayKMLMataAtlanticaLoaded: null,

//    showKML: function(kml) {
//        if (GBrowserIsCompatible()) {

//            Projeto.initMap();

//            Projeto.gxml = new GGeoXml(kml);

//            if (kml.toString().indexOf("limite_MA.kml") >= 0) {
//                Projeto.overLayKMLMataAtlantica = Projeto.gxml;
//                Projeto.overLayKMLMataAtlanticaLoaded = true;
//            }
//            else {
//                Projeto.overLayKMLMataAtlanticaLoaded = false;
//            }

//            GEvent.addListener(Projeto.gxml, "load", function() {
//                if (Projeto.gxml.hasLoaded()) {
//                    Projeto.map.setCenter(Projeto.gxml.getDefaultCenter(), Projeto.map.getBoundsZoomLevel(Projeto.gxml.getDefaultBounds()));
//                }

//            });

//            Projeto.map.enableDoubleClickZoom();
//            Projeto.map.clearOverlays();
//            Projeto.map.addOverlay(Projeto.gxml);

//            var customUI = Projeto.map.getDefaultUI();
//            this.map.setUI(customUI);
//            Projeto.map.setMapType(G_SATELLITE_MAP); //Earth

//            Projeto.getCoords();

//            if (!Projeto.overLayKMLMataAtlanticaLoaded)
//                Projeto.showKmlMA(false);
//        }
//    }



//    //    showKML: function(kml) {
//    //        if (GBrowserIsCompatible()) {

//    //            if (null == Projeto.map) {
//    //                Projeto.initMap();
//    //            }

//    //            Projeto.gxml = new GGeoXml(kml);

//    //            if (kml.toString().indexOf("limite_MA.kml") >= 0)
//    //                Projeto.overLayKMLMataAtlantica = Projeto.gxml;

//    //            GEvent.addListener(Projeto.gxml, "load", function() {
//    //                if (Projeto.gxml.hasLoaded()) {
//    //                    Projeto.map.setCenter(Projeto.gxml.getDefaultCenter(), Projeto.map.getBoundsZoomLevel(Projeto.gxml.getDefaultBounds()));
//    //                }
//    //            });

//    //            Projeto.map.enableDoubleClickZoom();
//    //            Projeto.map.clearOverlays();

//    //            if (null != Projeto.overLayKMLMataAtlantica)
//    //                Projeto.map.addOverlay(Projeto.overLayKMLMataAtlantica);
//    //                            
//    //            Projeto.map.addOverlay(Projeto.gxml);

//    //    
//    //            var customUI = Projeto.map.getDefaultUI();
//    //            this.map.setUI(customUI);
//    //            Projeto.map.setMapType(G_SATELLITE_MAP); //Earth


//    //            //Projeto.getCoords();
//    //        }
//    //    }
//}






//Projeto.initialize();



////var Projeto = {
////    map: null,
////    gxml: null,
////    initialize: function() {
////        jQuery(function() {

////            Projeto.config();
////        });
////    },


////    getCoords: function() {
////        jQuery.getJSON("services/getCoords.aspx", function(retorno) {
////            if (retorno.code != '-1') {
////                retorno.each(function(e) {

////                    var markerOpts = {};
////                    var latlng = new GLatLng(e.lat.replace(",", "."), e.lng.replace(",", "."));
////                    //var latlng = new GLatLng(e.lng.replace(",", "."), e.lat.replace(",", "."));


////                    var marker = new GMarker(latlng, markerOpts);
////                    var extras = e.extras.split("|");
////                    var instituicao = extras[4];
////                    var dataInicio = extras[5];
////                    var id = extras[6];
////                    var img = ((extras.length >= 7) ? ("http://www.pactomataatlantica.org.br/media/" + extras[7]) : "");

////                    marker.bindInfoWindowHtml("<div><p><img src=" + img + " alt=\"\"></p> " +
////                    "<p style=\"color:#000;\"><strong>Nome do Projeto:&nbsp;&nbsp;</strong>" + extras[0] + "</p>" +
////                    "<p style=\"color:#000;\"><strong>Área:&nbsp;&nbsp;</strong>" + extras[1] + " (ha)</p>" +
////                    "<p style=\"color:#000;\"><strong>Início do Projeto:&nbsp;&nbsp;</strong>" + dataInicio + "</p>" +
////                    "<p style=\"color:#000;\"><strong>Instituição Executora:&nbsp;&nbsp;</strong>" + instituicao + "</p>" +
////                    //                    "<p style=\"color:#000;\"><strong>Local da Implementação:&nbsp;&nbsp;</strong>" + extras[3] + " - " + extras[2] + "</p>" +
////                    "<p><a style=\"color:#000;\" href=\"http://www.pactomataatlantica.org.br/imprimir-executor.aspx?pId=" + id + "\">Mais Informações</a>" + instituicao + "</p>" +
////                    "</div>");

////                    Projeto.map.addOverlay(marker);
////                });
////            }
////        });


////    },

////    init3D: false,

////    baseUrl: 'http://www.pactomataatlantica.org.br/kml/',

////    overLayKMLMataAtlantica: null /*new GGeoXml("http://www.pactomataatlantica.org.br/kml/limite_MA.kml")*/,

////    config: function() {

////        var kml = Projeto.baseUrl + "limite_MA.kml";
////        Projeto.showKML(kml);
////        Projeto.getCoords();

////        jQuery("#hiddenKML").click(function(e) {

////            if (jQuery('#hiddenKML').attr('class') == "expand") {

////                jQuery('#hiddenKML').attr('class', '');
////                jQuery('#hiddenKML').html((PARAMS.getUrlVar("lang").replace("#", "") == "en") ? "Expand KML Map" : "Exibir Mapa do Bioma Lei da Mata Atlântica");

////                Projeto.map.removeOverlay(Projeto.overLayKMLMataAtlantica);
////                Projeto.overLayKMLMataAtlantica = null;
////                Projeto.getCoords();
////            }
////            else {
////                jQuery("#hiddenKML").html((PARAMS.getUrlVar("lang").replace("#", "") == "en") ? "Hide KML Map" : "Esconder Mapa do Bioma Lei da Mata Atlântica");
////                jQuery("#hiddenKML").attr('class', 'expand');

////                if (null == Projeto.map) {
////                    Projeto.initMap();
////                }

////                Projeto.overLayKMLMataAtlantica = new GGeoXml("http://www.pactomataatlantica.org.br/kml/limite_MA.kml");
////                Projeto.map.addOverlay(Projeto.overLayKMLMataAtlantica);
////            }

////            e.preventDefault();
////        });
////    },

////    showKMLEarth: function(url) {
////        jQuery("#map3d").show();
////        var d = document.documentElement;
////        var b = document.body;
////        var who = d.offsetHeight ? d : b;
////        jQuery("#map3d").css('height', Math.max(who.scrollHeight, who.offsetHeight) - 308);

////        //$('map3d').setStyle('height', Math.max(who.scrollHeight, who.offsetHeight) - 308);

////        url = Projeto.baseUrl + url;

////        if (!Projeto.init3D) {
////            Projeto.init3D = true;
////            init3D(true, url);
////        }
////        else
////            fetchKML(url, 'mapa');

////        return false;
////    },

////    initMap: function() {

////        Projeto.map = new GMap2(document.getElementById("map3d"), { size: new GSize(1000, 364) });
////        Projeto.map.setCenter(new GLatLng(0, 0), 0);

////    },

////    showKML: function(kml) {
////        if (GBrowserIsCompatible()) {

////            if (null == Projeto.map) {
////                Projeto.initMap();
////            }

////            Projeto.gxml = new GGeoXml(kml);

////            if (kml.toString().indexOf("limite_MA.kml") >= 0)
////                Projeto.overLayKMLMataAtlantica = Projeto.gxml;

////            GEvent.addListener(Projeto.gxml, "load", function() {
////                if (Projeto.gxml.hasLoaded()) {
////                    Projeto.map.setCenter(Projeto.gxml.getDefaultCenter(), Projeto.map.getBoundsZoomLevel(Projeto.gxml.getDefaultBounds()));
////                }
////            });

////            Projeto.map.enableDoubleClickZoom();
////            Projeto.map.clearOverlays();
////            Projeto.map.addOverlay(Projeto.gxml);

////            if (null != Projeto.overLayKMLMataAtlantica)
////                Projeto.map.addOverlay(Projeto.overLayKMLMataAtlantica);

////            //var kml = Projeto.baseUrl + "limite_MA.kml";
////            //Projeto.map.addOverlay(Projeto.gxml);

////            var customUI = Projeto.map.getDefaultUI();
////            this.map.setUI(customUI);
////            Projeto.map.setMapType(G_SATELLITE_3D_MAP); //Earth


////            Projeto.getCoords();

////        }
////    }
////}
////Projeto.initialize();
