$(function(){
	$("a.showAddress").each(function(){
		var id;
		var width;
		var height;
		var i = 0;
		do{
			id = "map_"+i;
			i++;
		}while(typeof $("#"+id)[0] != "undefined");

		var adrTitle	= typeof $(this).attr("title") != "undefined" ? $(this).attr("title") : "Address";
		var toShow		= typeof $(this).attr("title") != "address" ? $ (this).attr("address") : "";
		var dimension	= $(this).attr("dimension");
		if(typeof dimension != "undefined"){
			var parts	= dimension.split(/[:,]/);
			width = parseInt(parts[1]); height = parseInt(parts[3]);
		}else{
			width = 400; height = 400;
		}
		
		$(this).before('<div class="mapAddres" style="width: '+width+'px; height: '+height+'px;" id="'+id+'"></div>').remove();
		
		if(toShow!="" && typeof toShow != "undefined"){
			adrTitle = (adrTitle!="" && typeof adrTitle != "undefined") ? adrTitle : "Address";
			if(arguments.length == 3){
				width = 400;
				height = 400;
			}else{
				if(typeof width == "undefined" || parseInt(width) <= 0){
					width = 400;
				}
				if(typeof height == "undefined" || parseInt(height) <= 0){
					height = 400;
				}
			}
			$("#"+id).jmap("searchAddress", {
					address:	toShow
				},
				function(options, point) {
					$("#"+id).jmap("init", {
						mapDimensions:		[width, height],
						mapCenter:			[point.y, point.x],
						//centerMethod:		"pan",
						mapZoom:			16,
						mapShowjMapIcon:	false
					});
					$("#"+id).jmap("addMarker", {
						pointLatLng:	[point.y, point.x],
						pointHTML:		adrTitle + "<br />" + options.address
					});
				}
			);
		}else{
			$("#"+id).html('<p class="error">Ooops! Address missing..</p>');
		}
	});
});
