/* ------------------------------------------------------------------------
	Class: magimap
	Use: Google Maps Plugin
	Company: Magico.ie
	Author: Martin Poucher
	Version: 1.0
	Created: 2nd March 2009
------------------------------------------------------------------------- */

var map;
var settings;
var gmarkers = [];
var bounds;
var magnification;
var mapCentreLatitude;
var mapCentreLongitude;

(function($){
    $.fn.magimap = function(options) {
        settings = $.extend({
            magnification: 6,
            locations: null,
            width: "408px",
            height: "400px",
            mapCentreLatitude: "53.278353",
            mapCentreLongitude: "-9.052734",
            enableContainerToggle: true,
            autoDisplayPopup: false,
            displayMapControls: true,
            displayTypeControls: false,
            displayZoomControls: true,
            linksPlaceHolder: ""
        },options);
        
        mapCentreLatitude = settings.mapCentreLatitude;
        mapCentreLongitude = settings.mapCentreLongitude;
        magnification = settings.magnification;
        
        var googleMapPlaceholder = $(this);
        $(this).css({display:"block", width:settings.width, height:settings.height});
             
        if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById(googleMapPlaceholder.attr("id")));
            map.setCenter(new GLatLng(settings.mapCentreLatitude, settings.mapCentreLongitude), settings.magnification);
            var locationLinks = "";
            
            if (settings.displayMapControls) map.addControl(new GSmallMapControl());
            if (settings.displayTypeControls) map.addControl(new GMapTypeControl());
            if (settings.displayZoomControls) {
                var boxStyleOpts = {
                    opacity: .2,
                    border: "2px solid red"
                };
                var otherOpts = {
                          buttonHTML: "<img src='http://gmaps-utility-library.googlecode.com/svn/trunk/dragzoom/release/examples/zoom-button.gif' />",
                          buttonZoomingHTML: "<img src='http://gmaps-utility-library.googlecode.com/svn/trunk/dragzoom/release/examples/zoom-button-activated.gif' />",
                          buttonStartingStyle: {width: '24px', height: '24px'},
                          stickyZoomEnabled: true,
                          overlayRemoveTime: 1000
                      };

                map.addControl(new DragZoomControl(boxStyleOpts, otherOpts, {}), 
                                                 new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(13, 120)));
            }
            
            for (var i = 0; i < settings.locations.length; i++) {
                //gmarkers[i] = createMarker(i);
                var pointOfInterest = settings.locations[i];
                gmarkers[i] = new GMarker(new GLatLng(pointOfInterest.latitude, pointOfInterest.longitude));
                gmarkers[i].html = "<h3>" + pointOfInterest.name + "</h3>\
                                    <p>" + pointOfInterest.banner + "</p>\
                                    " + (pointOfInterest.url != "" && pointOfInterest.url != undefined ? 
                                        "<p><a href=\"" + pointOfInterest.url + "\">View Details</a>" : 
                                        "");
                GEvent.addListener(gmarkers[i], "click", function() {
                    this.openInfoWindowHtml(this.html);
                });
                
                map.addOverlay(gmarkers[i]);
                locationLinks += " <a href='javascript:highlightLocation(" + i + ");'>" + settings.locations[i].name + "</a>,";
            }

            if (settings.linksPlaceHolder != "" && settings.locations.length > 0) {
                $(settings.linksPlaceHolder).html(locationLinks.substring(0, locationLinks.length - 1));
            }
            
            if (settings.autoDisplayPopup) gmarkers[0].openInfoWindowHtml(gmarkers[0].html);
        }
        return $(this);
    };
})(jQuery);
        
function highlightLocation(index) {
    gmarkers[index].openInfoWindowHtml(gmarkers[index].html);
}  

function zoomShowAll() {
    bounds = new GLatLngBounds();
    map.setCenter(new GLatLng(0,0),0);
    
    for (var i = 0; i < settings.locations.length; i++) {
        bounds.extend(new GLatLng(settings.locations[i].latitude, settings.locations[i].longitude));
    }

    map.setZoom(map.getBoundsZoomLevel(bounds));
    var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
    var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
    map.setCenter(new GLatLng(clat,clng));
}    

function centreMap()
{
    map.setCenter(new GLatLng(mapCentreLatitude, mapCentreLongitude), magnification);  
}      
