/* 
	woap-listing-map.js
	===============================================================
	Wellington on a Plate
	CT: Frontend Functions
	
	Last Updated: See SVN	
		
	Note that jQuery uses the jQuery shortcut (rather than $)
	to prevent conflicts with Prototype which is used by the backend.
	
  ===============================================================
*/

// Print button control

function PWTGMapPrintButton() {}
PWTGMapPrintButton.prototype = new GControl();
PWTGMapPrintButton.prototype.initialize = function(map) {
    var button = document.createElement('input');
    button.type = 'image';
    button.id = 'pwt_gmap_print_button';
    button.src = '/resources/ui/images/maps/button-print.png';
    button.value = 'Print';
    GEvent.addDomListener(button, 'click', function() {
				tb_remove();	
        window.print();
    });
    map.getContainer().appendChild(button);
    return button;
};
PWTGMapPrintButton.prototype.getDefaultPosition = function() {		
	return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(216, 19));	
};

// Centre button control

function PWTGMapCentreButton() {}
PWTGMapCentreButton.prototype = new GControl();
PWTGMapCentreButton.prototype.initialize = function(map) {
    var button = document.createElement('input');
    button.type = 'image';
    button.id = 'pwt_gmap_centre_button';
    button.src = '/resources/ui/images/maps/button-centre-map.png';
    button.value = 'Centre map on event';
    GEvent.addDomListener(button, 'click', function() {
        PWT_Events_Map.centre_on_event();
    });
    map.getContainer().appendChild(button);
    return button;
};
PWTGMapCentreButton.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(15, 19));
}


// Street view control

function PWTGMapPegman() {}
PWTGMapPegman.prototype = new GControl();
PWTGMapPegman.prototype.initialize = function(map) {
    var pegman = document.createElement('div');
    pegman.id = 'pwt_gmap_pegman';
    var img = document.createElement('img');
    img.src = '/resources/ui/images/maps/pegman.gif';
    pegman.appendChild(img);
    GEvent.addDomListener(pegman, 'click', function() {
        PWT_Events_Map.toggle_street_view();
    });
    map.getContainer().appendChild(pegman);
    return pegman;
}
PWTGMapPegman.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(30, 80));
}

// Sets up and handles events for the PWT Events googlemap.

var PWT_Events_Map = {
    zoom: 14,
    width: 502,
    height: 330,
    info_window_width: 483,

    markers_added: false,

    map: false,
    marker: false,
    accom_markers: [],
    controls: [],

    lat: false,
    lng: false,
    venue: false,
        
    init: function (data) {
        // Settings
        this.lat = parseFloat(data.venue.latitude);
        this.lng = parseFloat(data.venue.longitude);
        this.venue = data.venue;
        this.init_large_map(); // Lightbox
					
    },

    
    init_large_map: function() {
        // Large map
        this.map = new GMap2(document.getElementById('map2'));
        this.map.setCenter( new GLatLng(this.lat, this.lng), this.zoom );
        var ui = this.map.getDefaultUI();
        ui.zoom.scrollwheel = true;
        ui.zoom.doubleclick = true;
        ui.maptypes.normal = true;
        ui.maptypes.satellite = false;
        ui.maptypes.hybrid = false;
        ui.maptypes.physical = false;
        ui.controls.largemapcontrol3d = false;
        ui.controls.smallzoomcontrol3d = false;
        ui.controls.maptypecontrol = false;
        ui.controls.menumaptypecontrol = false;
        ui.controls.scalecontrol = false;
        this.map.setUI(ui);

        // Controls
        this.controls[0] = new GLargeMapControl3D(); 
        this.controls[1] = new PWTGMapPrintButton();
        this.controls[2] = new PWTGMapCentreButton();
        this.controls[3] = new GScaleControl();
        for( var i = 0; i < this.controls.length; i++ ) {
            this.map.addControl( this.controls[i] );
        }
				this.map.addOverlay(
            new GMarker(
                new GLatLng(this.lat, this.lng),
                { 
                    title: this.venue.revtitle,
                    icon: G_DEFAULT_ICON,
                    clickable: false
                }
            )
        );
        GEvent.addListener(this.map, 'dblclick', function() {
            PWT_Events_Map.centre_on_event();
        });
    },

    init_large_map_markers: function() {
        // Add marker now that the map has been popped up - needs to be popped
        // up so that infowindows can be sized correctly
				this.marker = new GMarker(
                new GLatLng(this.lat, this.lng),
                { 
                    title: this.venue.revtitle,
                    icon: G_DEFAULT_ICON
                }
        );
				this.map.addOverlay(this.marker);
        this.marker.bindInfoWindow(
            this.get_safe_html( this.get_popup_html(this.venue) )
        );
        GEvent.addListener(this.marker, 'infowindowopen', function(e) {
            jQuery('a.accom-zoom-in').click(function() {
                PWT_Events_Map.map.zoomIn();
                return false;
            });
            jQuery('a.accom-zoom-out').click(function() {
                PWT_Events_Map.map.zoomOut();
                return false;
            });
        });        

        // Alter "powered by Google" link to link to map view
        jQuery('#TB_window #logocontrol a').attr('href', jQuery('#TB_window #logocontrol a').attr('href') + '&amp;t=m');
    },

    get_popup_html: function(data) {
        var windowHTML = '<p class="text-heading">';
				windowHTML += '<strong>'+data.revtitle+'</strong>';
				windowHTML += '</p>';
        if ( data.image ) {
            windowHTML += data.image;
        }
        windowHTML += data.description;
        if ( data.accom_type || data.address || data.price_range ) {
            windowHTML += '<ul>';
            if ( data.accom_type ) {
                windowHTML += '<li><strong>Type:</strong> ' + data.accom_type + '</li>';
            }
            if ( data.address ) {
                windowHTML += '<li><strong>Location:</strong> ' + data.address + '</li>';
            }
            if ( data.price_range ) {
                windowHTML += '<li><strong>Rates:</strong> ' + data.price_range + '</li>';
            }
            windowHTML += '</ul>';
        }
        if ( data.bookit_url ) {
            windowHTML += '<p><a href="' + data.bookit_url + '" class="accom-book-now">Book Now</a></p>';
        }
        if ( data.link ) {
            windowHTML += '<p>'+data.link+'</p>';
        }
        windowHTML += '<ul class="zoom"><li><a href="#" class="accom-zoom-in">Zoom in</a></li><li><a href="#" class="accom-zoom-out">Zoom out</a></li><li><a target="_blank" href="http://maps.google.co.nz/maps?q='+this.lat+','+this.lng+'&zoom=3" title="This link will open in a new window/tab. ">View on Google Maps</a></li></ul>';
        return windowHTML;
    },

    show_large_map: function() {
        var t = 'Map'; // this.title || this.name || null
        var a = '#TB_inline?height=402&amp;width=802&amp;inlineId=map2&amp;showTitle=0&amp;modalcss=pwt-google-map';
        var g = '';
        tb_show(t,a,g);
				jQuery('#map').blur();
        PWT_Events_Map.map.checkResize();
        if ( PWT_Events_Map.marker == false ) {
            PWT_Events_Map.init_large_map_markers();
        }
        PWT_Events_Map.centre_on_event();
    },

    popup: function() {
				PWT_Events_Map.show_large_map();
        GEvent.trigger(PWT_Events_Map.marker, 'click'); 
        return false;
    },

    centre_on_event: function() {
        this.center(this.lat, this.lng);
        GEvent.trigger(this.marker, 'click');
    },


    center: function(lat, lng) {
        var point = this.map.fromLatLngToContainerPixel( new GLatLng(lat,lng) );
        var latlng = this.map.fromContainerPixelToLatLng( 
            new GPoint(
                point.x + 60,
                point.y - 120
            )
        );
        this.map.setCenter(latlng, this.zoom);
    },

    get_safe_html: function(html) {
        // Have to determine size before inserting into bubble
        var div = jQuery('<div></div>');
        div.css('width', this.info_window_width);
        div.html(html);
        jQuery('div.pwt-google-map').append(div);
        var width = div.width();
        var height = div.height();
        div.remove();
        html = '<div style="height: '+height+'px; width: '+this.info_window_width+'px;">'+html+'</div>';     
        return html;
    }

};

// Code to run on page load.



