var gm = function(el) {
    this._el = document.getElementById(el); 
    if (!this._el) 
        alert('Map container ' + el + ' can not be found.'); 
    
    this._localSearch = new GlocalSearch(); 
    this._localRef = []; 
}
gm.prototype = { 
    // initialise map with center point
    init : function (lat, lng, z) {
		if (!GBrowserIsCompatible()) return false;     
        this._map = new GMap2(this._el);
        
        this._map.addControl(new GSmallMapControl());
        
		this._map.setCenter(new GLatLng(lat,lng),z); 
		this._map.savePosition(); 
		
		this._localSearch.setCenterPoint(this._map);
    	this._localSearch.setSearchCompleteCallback(this,
        function() { 
            var results = this._localSearch.results; 
            if (!results || results.length == 0) 
                return false; 
            
            var res = results[0], url = res.url.replace(/\+/g,' ').match(/q=(.[^\&]+)$/);
            // we dont have a reference and we fail
            if (!url || !url[1]) return false; 
            var ref = this._localRef[unescape(url[1])]; 
            
            this.plot(new GLatLng(res.lat, res.lng), ref.html, ref.icon); 
            
            // no need for reference anymore. 
           //  delete this._localRef[unescape(url[1])];
            
        });
        		
        return true; 
    }, 
    
    // create icon to plot
    icon : function(url, w, h) {
        var res = new GIcon(); 
        res.iconSize = new GSize(w, h);
        res.image = url; 
		res.infoWindowAnchor = new GPoint(9, 2);
        res.iconAnchor = new GPoint(9,h);
        return res; 
    }, 
    
    // returns map instance 
    map : function() { 
        return this._map; 
    }, 
    
    // plot market ar certain location with html and icon as optional 
    plot : function(pt, html, icon) 
    {
        var options = {}; 
        
        if (icon)  {
            options.icon = icon; } 
            
        var marker = new GMarker(pt, options); 
		
		this._map.addOverlay(marker);
        
        // add html if there is any.. 
        if (!html) 
            return; 

        GEvent.addListener(marker, "click", function(){ 
            marker.openInfoWindowHtml(html);
            //location.href = html; 
        });
    }, 
    
    // using glocalsearch 
    search : function(place, html, icon) 
    {
        // have to reference 
        this._localRef[place] = { 
            html: html, icon: icon };
        
	
        
        this._localSearch.execute(place); 
    }, 
    
    // poly from points
    polyFromPtString : function (c,o, input, alpha)
    {
        var result = [], coords = input.replace('','').split(',');
        for (var i = 0; i < coords.length; i += 2) 
        {            
            var x = parseFloat(coords[i+1]), y = parseFloat(coords[i]); 
            if (isNaN(x)) 
                continue; 

            result.push( new GLatLng(x, y) ); 
        }

        this._map.addOverlay(new GPolygon(result, c, 3, alpha, o, alpha));            
    
    },
    
    // encoded poly line
    poly: function(c, e, p) 
    {
        this._map.addOverlay(GPolygon.fromEncoded({ 
            polylines: [
                { 
                    color: c, 
                    weight: 5, 
                    points: e, 
                    levels: p, 
                    opacity: 0.6, 
                    zoomFactor: 32, 
                    numLevels:1 
                }
            ], 
            fill: true, color: c, opacity :0.3, outline: true
        })); 
    }
}
