var map_pos = null;
var maps = [];
var maps2 = [];

$(document).ready(function() {

	$("img").each(function() {
		if(map_pos == null) return;
		if($(this).attr('id').substr(0,11) == 'pin_module_') {
			
			var map_id = $(this).attr('id').split('_');
            map_id = map_id[map_id.length-1]; 
			jObj = $('<div id="gmap_' + map_id + '" class="google_map_placeholder"></div>');
			
			$(this).before(jObj);
			$(this).remove();
			
			var obj = { 
				o: document.getElementById('gmap_' + map_id), 
				p: map_pos
			};
			maps.push(obj);
			
		}
	});
	
	$("div.gmap_placeholder").each(function() {
		var pp = $(this).attr('id').split('_');
		var map_pos = [ pp[1], pp[2] ];
		var map_id = Math.random() * 10000;
		jObj = $('<div id="gmap_' + map_id + '" class="google_map_placeholder"></div>');
		
		$(this).before(jObj);
		$(this).remove();
		
		var obj = { 
			o: document.getElementById('gmap_' + map_id), 
			p: map_pos
		};
		maps.push(obj);
	});
	
	loadMapsScript();
	
});

function initialize() {
	for(var i in maps) {
		var m = maps[i];
		if(m.p == null || typeof(m.p) == 'undefined') return;
		var myLatlng = new google.maps.LatLng(m.p[0], m.p[1]);
		var myOptions = {
			zoom: 8,
			center: myLatlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}
		var map = new google.maps.Map(m.o, myOptions);
	}
}

function loadMapsScript() {
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
	document.body.appendChild(script);
}


