/**
 *  Googlemap plugin
 *
 *  @param: Object Array. Arguments need to be in object notation.
 *  Returns: jQuery.
 */

(function($) {

  $.fn.locationPopup = function(settings){

    var locationPin = $(this);
    var pos = locationPin.offset();
    var geocoder;
    var map;

    settings = jQuery.extend({
          speedIn: "normal",
          speedOut: "normal",
      width: 500,
      height: 300
    }, settings );

    return this.each(function(){


      $(this).click(function(e){

        if (GBrowserIsCompatible()) {

          //console.log(locationPin.attr('rel'));

          // Search for address
          geocoder = new GClientGeocoder();
          geocoder.getLatLng(

            locationPin.attr('rel'),
            function(point) {

              if (!point) {

                alert(address + " not found");

              } else {
                map = new GMap2(document.getElementById("map"));
                //map.setCenter(new GLatLng(37.4419, -122.1419), 7);
                map.setCenter(point, 15);

                // Add the marker
                var marker = new GMarker(point);
                map.addOverlay(marker);

                // Add an info window
                //marker.openInfoWindowHtml(locationPin.attr('rel'));

                // Add controls
                var c = new GMapTypeControl();
                map.addControl(c);
                map.addControl(new GLargeMapControl());

              }
            }
          );

        }

        $('#mapdialog').css({'left': e.pageX-settings.width+500, 'top': e.pageY-settings.height+300});
        $("#mapdialog .handle").show();
        $('#mapdialog').fadeIn('slow',function(){
          if(map) {
            map.checkResize();
          }
        });

        $("#mapdialog .close").live('click',function(){
          $("#mapdialog").fadeOut();
        });

      });

    });

  };

})(jQuery);

