if (typeof triodos === 'undefined' || typeof triodos.utils === 'undefined' || typeof triodos.mggg === 'undefined') {
    throw new Error('triodos.utils and triodos.mggg are required.');
}

// induce private local scope
(function () {

    var lang;
    var country;
    var mapTypeId;
    triodos.mggg.currentBranch = $j('#currentBranch').html();

    $j(document).ready(function () {
        if($j('#map').length > 0){
            importData();
        }
    });

    function importData() {
        var pathMgggPage = translate('mgggbundle', 'project.overview.path');

        var postCode = urlParam('postCode');
        var locale = urlParam('locale');

        var lang;
        var country;
        if (locale.length == 5) {
            lang = locale.substring(0,2);
            country = locale.substring(3, 5);
            mapTypeId = $j("#map #type").html();

            if(mapTypeId === 'SATELLITE' || mapTypeId === 'G_SATELLITE_MAP'){
                mapTypeId = google.maps.MapTypeId.SATELLITE;
            }
            else if (mapTypeId === 'TERRAIN' || mapTypeId === 'G_PHYSICAL_MAP') {
                mapTypeId = google.maps.MapTypeId.TERRAIN;
            }
            else if (mapTypeId === 'HYBRID' || mapTypeId === 'G_HYBRID_MAP'){
                mapTypeId = google.maps.MapTypeId.HYBRID;
            }
            else {
                mapTypeId =  google.maps.MapTypeId.ROADMAP;
            }

            var geocoder = new google.maps.Geocoder();
            var searchOptions = {
                address: postCode,
                language: lang,
                region: country
            };
            geocoder.geocode(searchOptions, function(results, status) {
                if (status === google.maps.GeocoderStatus.OK && results.length > 0) {
                    var lat = triodos.utils.urlencode(results[0].geometry.location.lat());
                    var lng = triodos.utils.urlencode(results[0].geometry.location.lng());
                    var t = results[0].types[0];
                    var acc = 500;
                    if (!acc) {
                        acc = 5;
                    }

                    $j.post(
                        '/LatLongProjectSearch',
                        {
                            hostPrefix: $j("meta[name=hostPrefix]").attr("content"),
                            lang: lang,
                            country: country,
                            varSubSelect: "projectsNear",
                            latitude: lat,
                            longitude: lng,
                            accuracy: acc,
                            numberItems: 5
                        },
                        function(data){
                            var localData = [];
                            var length = data.length;
                            for(var i=0; i < length; i++){
                                localData.push({
                                    nam: data[i].tit,
                                    sub: data[i].sub,
                                    hid: data[i].hid,
                                    adr: data[i].adr,
                                    hno: data[i].hos,
                                    slo: data[i].slo,
                                    dis: data[i].dis,
                                    loc: data[i].loc,
                                    pro: data[i].pro,
                                    cou: data[i].cou,
                                    lat: data[i].lat,
                                    lng: data[i].lng,
                                    sec: data[i].sec,
                                    hpg: data[i].hsp === '1',
                                    vis: data[i].vis,
                                    bch: data[i].brk,
                                    url: pathMgggPage + '?projectId=' + data[i].key + '&locationId=' + data[i].loi,
                                    highlighted: true
                                });
                                if(i < 3){
                                    var address = jQuery.trim(getListValue(localData[i].cou)+getListValue(localData[i].pro)+getListValue(localData[i].loc)+
                                        getListValue(localData[i].dis)+getListValue(localData[i].slo)+getListValue(localData[i].adr)+getListValue(localData[i].hno)+
                                        getListValue(localData[i].hid)+localData[i].sub);

                                    if(address.substr(address.length-1, address.length) == ','){
                                        address = address.substr(0, address.length-1);
                                    }
                                     
                                    $j('#project'+i).html('<h3><a href=\"'+localData[i].url+'\">'+localData[i].nam+'</a></h3><span class=\"industry\">'+
                                        localData[i].sec+'</span><p>'+address+'</p>');
                                }
                            }

                            var mapEl = $j('#map');
                            if (mapEl) {
                                mapEl = mapEl[0];
                            }
                            else {
                                return;
                            }

                            var bounds = triodos.utils.getBounds(localData);

                            var mapOptions = {
                                el: mapEl,
                                lat: lat,
                                lng: lng,
                                zoom: $j('meta[name=branchZoomLevel]').attr('content'),
                                mapTypeId: mapTypeId,
                                bounds: bounds
                            };
                            var res = triodos.utils.initMap(mapOptions);
                            var map = res[0];
                            var manager = res[1];

                            // activate view links
                            $j('#branchView').click(function () {
                                triodos.mggg.displayMapView(map, 'branch');
                            });
                            $j('#europeView').click(function () {
                                triodos.mggg.displayMapView(map, 'europe');
                            });
                            $j('#worldView').click(function () {
                                triodos.mggg.displayMapView(map, 'world');
                            });

                            triodos.utils.callWhenMapReady(map, function () {
                                var placementOptions = {
                                    map: map,
                                    manager: manager,
                                    markerFactory: triodos.mggg.markerFactory,
                                    data: localData
                                };
                                triodos.utils.placeMarkers(placementOptions);
                            });

                            triodos.mggg.trackZoom(map);
                        },
                        'json'
                        );
                }
            });
        }
    }

    function getListValue(value){
        if(value != '' && value != 'undefined'){
            return value + ', ';
        }
        else if(value == 'undefined' || value == null) {
            return '';
        }
        else {
            return value;
        }
    }

// end local scope
})();


