/**
* Displays map on property page (single object)
* v. 12-Jul-2011
*/

window.addEvent('load', function(){

    // Select object after/before which the map container will injected
    var container = $E(oneContainer);

    if (container) {
        // Defining containers for map and stuff
        var c_hpmapscanvas = new Element('div', {id: 'hpmapscanvas'});

        // Map container
        var c_map_canvas = new Element('div', {
            id: 'map_canvas',
            styles: {
                width: mapwidth,
                height: mapheight,
                margin: '10px 0px 0px 0px'
            }
        });
        c_map_canvas.inject(c_hpmapscanvas);

        // Street view container
        if (streetview == 2){
            var c_pano_info = new Element ('div', {
                id: 'panoinfo',
                styles: {
                    width: mapwidth,
                    height: '10px',
                    margin: '5px 0px 0px 0px'
                }
            });
            c_pano_info.inject(c_hpmapscanvas);

            var c_pano = new Element ('div', {
                id: 'pano',
                styles: {
                    width: mapwidth,
                    height: svheight,
                    margin: '5px 0px 0px 0px'
                }
            });
            c_pano.inject(c_hpmapscanvas);
        }

        c_hpmapscanvas.inject(container, mapPosition); // adding container into hot properties' page

    } else { // terminate
        return;
    }


    var myOptions = {
        zoom: parseInt(myZoom),
        scrollwheel: false,
        streetViewControl: streetview // if more than 0 - add Pegman control
    };

    var myMap = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    var infowindow = new google.maps.InfoWindow();

    switch (maptype) {
        case "satellite":
            myMap.setMapTypeId(google.maps.MapTypeId.SATELLITE);
            break;
            case "hybrid":
            myMap.setMapTypeId(google.maps.MapTypeId.HYBRID);
        break;
            case "terrain":
            myMap.setMapTypeId(google.maps.MapTypeId.TERRAIN);
        break;
            default:
            myMap.setMapTypeId(google.maps.MapTypeId.ROADMAP);
        break;
    }

    switch (geocorcheck) {
        case 1: // coordinates only
            var myLatLon = new google.maps.LatLng(mylat, mylon);
            myMap.setCenter(myLatLon);
            setMarker(myLatLon);
            if (streetview == 2) {setStreetView(myLatLon)};
            break;

        case 3: // lat/lon -> address
            if (mylat && mylon) {
                var myLatLon = new google.maps.LatLng(mylat, mylon);
                myMap.setCenter(myLatLon);
                setMarker(myLatLon);
                if (streetview == 2) {setStreetView(myLatLon)};
            } else {
                myGeocoder();
            }
            break;

        default: // address only & address first
            myGeocoder();
            break;
    }		


    // Peforms geocoding
    function myGeocoder() {
        geocoder = new google.maps.Geocoder();
        if (geocoder) {
            geocoder.geocode( { 'address': myaddress}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    myMap.setCenter(results[0].geometry.location);
                    setMarker(results[0].geometry.location);
                    if (streetview == 2) {setStreetView(results[0].geometry.location)};
                } else if (geocorcheck == 2 && (mylat && mylon)) { // address -> lat/lon
                    var myLatLon = new google.maps.LatLng(mylat, mylon);
                    myMap.setCenter(myLatLon);
                    setMarker(myLatLon);
                    if (streetview == 2) {setStreetView(myLatLon)};
                } else if (errhandler == 1) { // no map
                    $('hpmapscanvas').remove();							
                    return; // terminate			
                } else if (errhandler == 2) { // default lat/lon
                    var myLatLon = new google.maps.LatLng(deflat, deflon);
                    myMap.setCenter(myLatLon);
                    setMarker(myLatLon);
                    if (streetview == 2) {setStreetView(myLatLon)};
                } else { // pop-up and error message
                    alert(mesNoGeocode + status);
                }
            });
        }
    }


    // Marker and bubble pop-up window
    function setMarker(latlon){
        if (icon) { // use custom icon
            var marker = new google.maps.Marker ({
                map: myMap,
                position: latlon,
                icon: icon
            });
        } else {
            var marker = new google.maps.Marker ({
                map: myMap,
                position: latlon
            });
        }        
        if (bubble) {
            infowindow.setContent(myaddress);
            infowindow.open(myMap, marker);
        }				
    }


    // Build separate StreetView window below the map
    function setStreetView(latlon){
        var sv = new google.maps.StreetViewService();				
        sv.getPanoramaByLocation(latlon, 50, function processSVData(data, status){
            if (status == google.maps.StreetViewStatus.OK) {
                var panoramaOptions = {
                    position: latlon,
                    pov: {
                        heading: parseInt(myHeading),
                        pitch: parseInt(myPitch),
                        zoom: 1
                    }
                };
                var panorama = new google.maps.StreetViewPanorama(document.getElementById("pano"), panoramaOptions);		
                myMap.setStreetView(panorama);
            } else {
                $('panoinfo').setHTML(mesNoStreetview);
                $('pano').setText('');
                $('pano').setStyles({
                    height: '10px'
                });
            }				
        });
    }
}
);
