(function($) {
    
    $.fn.uoGoogleMaps = function(options) {
    
        options	= $.extend({}, $.fn.uoGoogleMaps.defaults, options);

        var $_ = $(this);
        var $infoWindowHtml = $_.find(':nth-child(1)');
        $_.css({width : options.width, height : options.height});        
        
        var geocoder = new GClientGeocoder();
                
        function _loadPreview(address) {
            _doGeoCoding(address, null, _loadPreviewCallback);
        }
        
        function _loadMap(address, targetAddress) { 
            _doGeoCoding(address, targetAddress, _loadMapCallback);
        }
    
        function _loadMapCallback(address, targetAddress, point) {            
            var map = new GMap2($_.get(0));
            var route = new GDirections(map, $(options.route).get(0));
            var marker = new GMarker(point);
            
            var $heading = $infoWindowHtml.find('h1');
            var $text = $infoWindowHtml.find('p');
            var $input = $infoWindowHtml.find('input');
            var $button = $infoWindowHtml.find('button');
            
            if(options.toolTipClass) {
                $infoWindowHtml.addClass(options.toolTipClass);
            }
            else {
                $heading.css({ 'font-weight' : 'bold', 'font-size' : '11px', margin : 0, padding : 0, 'line-height' : 'normal'});
                $text.css({'font-size' : '10px', margin : '0 0 5px 0', padding : 0});
                $input.css({'font-size' : '10px', padding : '1px 2px 2px 2px', width : '180px'});
                $button.css({'font-size' : '10px', margin : '10px 0 0 0'});
            }
            
            if(!options.enableRoute) {
                $input.remove();
                $button.remove();
            }
            
            //$heading.html(options.title);
            $text.html(address)
               
            if(!targetAddress)
                marker.openInfoWindowHtml($infoWindowHtml.html());
               
            if(options.blowup)
                marker.showMapBlowup(point);
                        
            map.addOverlay(marker);
            map.setMapType(G_NORMAL_MAP);
            map.setCenter(point, options.zoom);
            map.addControl(new GLargeMapControl());
            map.addControl(new GMapTypeControl());
            route.loadFromWaypoints(point)
            
            if(targetAddress)
                route.load('from: ' + address + ' to: '+ targetAddress, { 'locale' : 'de_de' });
            
            
            $button.live('click', function() {
                  _computeRoute($('#uoGoogleMapsTarget').val());
                  document.location.hash = options.jumpAnchor;
                  return false;
            });
        }
    
        function _loadPreviewCallback(address, targetAddress, point) {
            $_.append('<img src="http://maps.google.com/staticmap?center='+ point.lat() +','+ point.lng() +'&markers='+ point.lat() +','+ point.lng() 
                +',midyellow&zoom='+ options.zoom +'&size='+ options.width +'x'+ options.height 
                +'&key='+ options.key +'" alt="'
                + address +'" title="'+ address +'" />')
        }
    
        function _doGeoCoding(address, targetAddress, callback) {
             geocoder.getLatLng(address, function(point) {
                    if (!point)
                        alert(address + " not found");
                    else 
                        callback(address, targetAddress, point);
            });
        }
    
        function _computeRoute(targetAddress) {
            _doGeoCoding(options.address, targetAddress, _loadMapCallback);
        }
        
        this.computeRoute = _computeRoute;
        this.preview = _loadPreview;
        this.map = _loadMap;
         
        if(options.preview) {
            _loadPreview(options.address);
            $infoWindowHtml.remove();
        }
        else {
            _loadMap(options.address);
        }
        
        return this;
    }
    
    $.fn.uoGoogleMaps.defaults = {
        width: 640,
        height: 480,
        preview: true,
        blowup: false,
        enableRoute: true,
        zoom: 15,
        route: '#route',
        address: '',
        jumpAnchor: 'uoGoogleMaps', 
        title: 'Route berechnen',
        toolTipClass: '',
        key: ''
    }
    
})(jQuery);
