if(!mapness){
	var mapness = {};
}

mapness.mapmini = {
	queue : [],
	queuePointer: 0,
	countryListPointer: 0,
	
	queueCountries : function(countryList, mapHolder){
		this.queue.push({"countries":countryList,"mapHolder":mapHolder});
	},
	
	executeQueue : function(pointer){
		if(!pointer){
			pointer = 0;
		}
		if(pointer < this.queue.length){
			this.queue[pointer].map = new GMap2(document.getElementById(this.queue[pointer].mapHolder));
			this.queue[pointer].map.setCenter(new GLatLng(27, 0), 1);
			this.queue[pointer].map.setMapType(G_PHYSICAL_MAP);
			this.outlineCountries();
		}
	},
	
	executeElement : function(pointer){
		this.queuePointer = pointer;
		this.queue[pointer].map = new GMap2(document.getElementById(this.queue[pointer].mapHolder));
		this.queue[pointer].map.setCenter(new GLatLng(27, 0), 1);
		this.queue[pointer].map.setMapType(G_PHYSICAL_MAP);
		this.outlineCountries();
	},
	
	outlineCountries : function(url, bounds,listPointer){
		var request = new RequestO("post", "/xhr/outline.php", { 
			"countries" : this.queue[this.queuePointer].countries
		},{
			"loading": function(){
			},
			"complete": function(data) {
				var bounds = new GLatLngBounds();
				var mapInstance = mapness.mapmini.queue[mapness.mapmini.queuePointer].map;
				var iStart = 0;
				var arePoints = false;
				var polylineLevels = "";
				//File structure is [encoded Levels<br>encoded Polylines<br>...]
				do {
						var iEnd = data.indexOf("<br>", iStart);
						if (false == arePoints) {//Levels
							polylineLevels = data.substring(iStart, iEnd);
							arePoints = true;
						}else {//Points
							var polygon = new GPolygon.fromEncoded({
								polylines: [{
								color: "#1c9208",
								weight: 1,
								opacity: 1,
								points: data.substring(iStart, iEnd),
								levels: polylineLevels,
								zoomFactor: 2,
								numLevels: 18
							}],
							fill: true,
							color: " #32cc19",
							opacity: .6,
							outline: true
						});
						var tempBounds = polygon.getBounds();
						bounds.extend(tempBounds.getSouthWest());
						bounds.extend(tempBounds.getNorthEast());
						mapInstance.addOverlay(polygon);
						arePoints = false;
					}
					iStart = iEnd + 4;//jump above '<br>' to the next polylines
				}
				while (data.length > iStart);
				mapInstance.setCenter(bounds.getCenter(), mapInstance.getBoundsZoomLevel(bounds));
				mapness.mapmini.destroyLoader(mapness.mapmini.queuePointer);
				mapness.mapmini.queuePointer++;
				mapness.mapmini.executeQueue(mapness.mapmini.queuePointer);
			}
		});
	},
	
	buildMinimapLoader : function(){
		$(".mapHolders").each(function(){
			$(this).before('<div class="overlayLoader"><div><img src="/images/ajax-loader2.gif"><span class="note">Loading..</span></div></div>');
		});
	},
		
	destroyLoader: function(pointer){
		$("#"+this.queue[pointer].mapHolder).prev().remove();
	}
		
}
