/**
 *	@author Dariusz Pobożniak | http://pobozniak.pl
 */

$(document).ready(function() {
	mapShow();
})

function mapShow() {
    if ($('.handimap').length > 0) {
    	$('.handimap').each(function() {
    		var $parent = $(this).parent();
    		var $thisid = $(this).attr('id');
    		
    		//alert($(this).attr('id'));
			var center = $parent.find('[id*=map_center]').html().split(',');
			lat = parseFloat(center[0]);
			lng = parseFloat(center[1]);
			zoom = parseInt($parent.find('[id*=map_zoom]').html()); 
			switch($parent.find('[id*=map_type]').html()) {
				case 'Mapa' :       var maptype = G_NORMAL_MAP;    break;
				case 'Satelitarna': var maptype = G_SATELLITE_MAP; break;
				case 'Hybrydowa':   var maptype = G_HYBRID_MAP;    break;
				default:	        var maptype = G_NORMAL_MAP;    break;
			}
			$(this).googleMap(lat,lng,zoom, {
				controls: ["GSmallMapControl", "GMapTypeControl"],
				markers: $(".geo")
			});
			$.googleMap.maps[$thisid].setMapType(maptype);
		})	
    }
}

/* jQuery googleMap Copyright Dylan Verheul <dylan@dyve.net>
 * Licensed like jQuery, see http://docs.jquery.com/License
 */

$.googleMap = {
    maps: {},
    marker: function(m) {
        if (!m) {
            return null;
        } else if (m.lat == null && m.lng == null) {
            return $.googleMap.marker($.googleMap.readFromGeo(m));
        } else {
            var marker = new GMarker(new GLatLng(m.lat, m.lng));
            if (m.txt) {
                GEvent.addListener(marker, "click", function() {
                    marker.openInfoWindowHtml(m.txt);
                  });
            }
            return marker;
        }
    },
    readFromGeo: function(elem) {
        var latElem = $(".latitude", elem)[0];
        var lngElem = $(".longitude", elem)[0];
        var txtElem = $(".pin_txt", elem)[0];
        if (latElem && lngElem) {
            return { lat:parseFloat($(latElem).html()), lng:parseFloat($(lngElem).html()), txt:$(txtElem).html() }
        } else {
            return null;
        }
    },
    mapNum: 1
};

$.fn.googleMap = function(lat, lng, zoom, options) {

    // If we aren't supported, we're done
    if (!window.GBrowserIsCompatible || !GBrowserIsCompatible()) return this;

    // Default values make for easy debugging
    if (lat == null) lat = 52.025459;
    if (lng == null) lng = 19.204102;
    if (!zoom) zoom = 6;
    
    // Sanitize options
    if (!options || typeof options != 'object')    options = {};
    options.mapOptions = options.mapOptions || {};
    options.markers = options.markers || [];
    options.controls = options.controls || {};

    // Map all our elements
    return this.each(function() {
        // Make sure we have a valid id
        if (!this.id) this.id = "gMap" + $.googleMap.mapNum++;
        // Create a map and a shortcut to it at the same time
        var map = $.googleMap.maps[this.id] = new GMap2(this, options.mapOptions);
        // Center and zoom the map
		map.setCenter(new GLatLng(lat, lng), zoom);
          
        var MapTypes = map.getMapTypes();
		MapTypes[0].getName= function()  { return 'Mapa'; }
		MapTypes[1].getName = function() { return 'Satelitarna'; }
		MapTypes[2].getName = function() { return 'Hybrydowa'; }  
        
		// Add controls to our map
		for (var i = 0; i < options.controls.length; i++) {
		   var c = options.controls[i];
		   eval("map.addControl(new " + c + "());");
		}
		// If we have markers, put them on the map
		var marker = null;
		for (var i = 0; i < options.markers.length; i++) {
		   if (marker = $.googleMap.marker(options.markers[i])) map.addOverlay(marker);
		}
    });

};
