/**
 * Print basic map Example: 
 * var map = new GMap(20.573653,-100.374012);
 * map.preview('preview');
 * map.show('mapa');
 * @author Rbrt
 * @version 1.2.51
 * @returns Map object
 */
// Import in HTML document: 
// <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
function GenericMap(lat, long) {
    // ------ public Methods -----------
    this.getMap = function() { return this.map; };
    this.setInitialPosition = function(lat, long) { this.initialPosition = new google.maps.LatLng(lat, long); this.lat = lat; this.long = long;};// map center
    this.setZoom = function(z) { this.zoom = z; };
    this.setIcon = function(i) { this.icon = i; };
    this.setTitle = function(str) { this.title = str; };
    this.setInfo = function(str) { this.info = str; };
    this.setMapId = function(id) { this.MAPTYPE_ID = id; };
    this.setOptions = function(options) { this.mapOptions = options; };
    this.setKey = function(ky) { this.key = ky; };
    this.setLanguage = function(lan) { this.language = lan; };
    
    this.show = function(divId){ this.getFullMap(divId); };   //for full map    
    this.preview = function(divId, tag){ this.getPreviewMap(divId, tag); };   //for preview and static image map
        //NOTE: for 'preview' is necessary set the Key 

    
    
    // ------------ Do NOT modify -----------
    this.map;
    this.initialPosition = new google.maps.LatLng(lat, long);
        this.lat = lat;
        this.long = long;
    this.zoom = 12;
    this.icon = "http://mrant.net/wp3/wp-content/uploads/2011/05/Google_Maps_Marker.png";
    this.title = "Place";
    this.info = "";
    this.key;
    this.language = 'es';
    this.MAPTYPE_ID = 'mapIdCustom';
    this.mapOptions = { zoom: this.zoom,
                                    center: this.initialPosition,
                                    mapTypeId: google.maps.MapTypeId.ROADMAP
                                };
    this.enableBugCorrection = true;
    
    this.getFullMap = function(divId){ 
                                    try {
                                        $('#'+divId).html(' ');
                                        var map = new google.maps.Map(document.getElementById(divId), this.mapOptions);
                                        var pos = this.initialPosition;
                                        var correction = this.enableBugCorrection;
                                        google.maps.event.addListener(map, 'tilesloaded', function() {
                                            google.maps.event.trigger(map, 'resize');
                                            if (correction) {
                                                map.setCenter(pos);
                                                correction = false;
                                            }
                                        });
                                        this.map = map;
                                        this.enableBugCorrection = correction;
                                        addMarker(this.initialPosition, this.map, this.icon, this.title, this.info);
                                    }catch (e) { }
                                };
    this.getPreviewMap = function(divId, tagType) { 
                                        var div = getJQObject(divId);
                                        if (div) {                                          
                                            var w = div.width();
                                            var h = div.height();
                                            var img = "<img src='http://maps.google.com/maps/api/staticmap?center="+this.lat+","+this.long+"&amp;";
                                            img += "markers=icon:"+this.icon+"|"+this.lat+","+this.long+"&amp;";
                                            img += "size="+w+"x"+h+"&amp;zoom="+this.zoom+"&amp;sensor=false&amp;";
                                            img += "key="+this.key+"&amp;hl="+this.language+"&amp;maptype=roadmap;' alt='"+this.title+"' />";
                                            
                                            var ob = (tagType) ? $('#'+divId+' '+tagType) : div;
                                            try{ ob.html(img); }
                                            catch(e) {  }
                                        } else console.error("div map '"+divId+"' not found");
                                    };
        
        function addMarker(pos, map, image, titulo, info) {			
			//Marcador
			var marker = new google.maps.Marker({
				position: pos,
				map: map,
				title: titulo,
				icon: image
			});
			if (info.length>0) addInfo(map, info, marker);
		}//function
		
		function addInfo(map, info, marker) {			
			var infowindow = new google.maps.InfoWindow({
				maxWidth:300,
				content: info
			});
			google.maps.event.addListener(marker, 'click', function() {
				infowindow.open(map, marker);
			}); 
		}//function
        
}//class

//look for a valid object, using the id or the class.
//returns JQuery object or false
function getJQObject(name) {
    if (typeof name == 'string') {  
        if (testObject(name, 'id')) return $(name);
        if (testObject(name, 'class')) return $(name);
        if (testObject('#'+name, 'id')) return $('#'+name);
    }
    if (typeof name == 'object') {
        if (name.length) return name;
    }
    return false;
    
        function testObject(name, type) {
            try { if ($(name).attr(type)) return true; }
            catch(e) {}
            return false;
        }//function
}//function
