/**
 * Main map functions
 */
MAPUTILS  = function () {
    
    //global map object
    MAPUTILS.prototype.map;
    MAPUTILS.prototype.counter = 0; 
 
    //default map settings
    MAPUTILS.prototype.currentLat =  52.3957;
    MAPUTILS.prototype.currentLng = -113.8513;
    MAPUTILS.prototype.currentZoom = 7;    
    MAPUTILS.prototype.markers = new Array();
    MAPUTILS.prototype.marker = new Array();
        
    MAPUTILS.prototype.initMap = function() {
        
        //check if map object has been loaded or created
        try{
            document.getElementById('googleMap').innerHTML;
        }catch(e){
            if (this.counter > 60) { 
                alert('Can\'t load map! Can\'t find map object!');
            } else { 
                setTimeout("maputil.initMap()", 1000);                 
            } 
            return false; 
        }
           
        //If gmaps main.js didn't load try to call init again after 60 tries we give up
        if (typeof GMap != "function") { 
            if (this.counter > 60) { 
                document.getElementById('googleMap').innerHTML = 'Can\'t load map! Gmap server timeout! ';
            } else { 
                setTimeout("maputil.initMap()", 1000); 
            } 
            this.counter++; 
            return false; 
        }
        
        //if browser not compatible update map tag with error msg
        if (!GBrowserIsCompatible()) { 
            document.getElementById('googleMap').innerHTML = 'Can\'t load map because your browser is too old!';
            return false;
        }
    
        //init gmaps object
        this.map = new GMap2(document.getElementById("googleMap"));
        
                    
        //set center of the map
        this.map.setCenter(new GLatLng(this.currentLat, this.currentLng), this.currentZoom);      
        
        //add controls and mouse functions
        this.map.enableDoubleClickZoom();
        this.map.enableContinuousZoom();
        this.map.enableScrollWheelZoom();
        //this.map.addControl(new GOverviewMapControl (/*new GSize(200,180)*/));                        
        
        this.map.addControl(new GSmallMapControl());    //zoom and pan       
        this.map.addControl(new GMapTypeControl()); //Map/Sat/hybrid
        
        //add zoomend event
        GEvent.addListener(maputil.map, "zoomend", function() {  
           
        });
                
        GEvent.addListener(maputil.map, "drag", function() {  
            
        });
        
        GEvent.addListener(maputil.map, "click", function() {              
            
        });
        
        var bounds = new GLatLngBounds(); 
        
        //proccess markers
        for(i=0;i<this.markers.length;i++){        
            p = this.markers[i].split("\|");
            
            //marker postiion
            var point = new GLatLng( p[1], p[2]);
            
            //extends bounds
            bounds.extend(point);
            
            //create marker
            this.marker[p[0]] = this.createMarker(point,document.getElementById('markersData'+p[0]).innerHTML);
            
            //add marker to map
            this.map.addOverlay(this.marker[p[0]]);

            
        }
        
        maputil.showMarker(1);
    }
    
    MAPUTILS.prototype.createMarker = function(point,html) { 
        var marker = new GMarker(point); 
        GEvent.addListener(marker, "click", 
            function() { 
                marker.openInfoWindowHtml(html); 
            }); 
        return marker; 
    }
    
    MAPUTILS.prototype.showMarker = function(markerId) { 
        maputil.marker[markerId].openInfoWindowHtml(document.getElementById('markersData'+markerId).innerHTML);
    }
}



//create maputil class
var maputil = new MAPUTILS;

//after page has been closed try to free up memory
window.onunload = function(){
    try{
        GUnload();
    }catch(e){}
}