
var nokiaMap = {
	lat: -71.08,
	long: -155.31,
	radius: 218,
	lastLat: -71.08,
	lastLong: -155.31,
	lastRadius: 218,
	maxReturned: 100,
	zoomLevel: 3909018,
	days: 1,
	artistId: "",
	groupId: "",
	useLocalData: false,
	player: null,
	working: false,
	ticks: null,
	firstTime: true,

	refreshMarkers: function() {
		if (this.radius != this.lastRadius || this.getMovement() > ((this.radius * 2) / 3)) {
			var tmpTicks;
			this.ticks = tmpTicks = (new Date()).getTime();
			if (!this.working) {
				nokiaMap.startWorking(tmpTicks, 2);
			}
		}
	},
	getMovement: function() {
		var mapCenter = player.map.getMapCenterPosition();
		var x = 69.1 * (mapCenter.latitude - this.lastLat);
		var y = 69.1 * (mapCenter.longitude - this.lastLong) * Math.cos(this.lastLat / 57.3);
		return Math.sqrt(x * x + y * y);
	},
	startWorking: function(tmpTicks, countdown) {
		if (tmpTicks == this.ticks) {
			if (countdown == 0) {
				$('#refresh').html("Refreshing...");
				this.working = true;
				this.refreshMap();
			} else {
				$('#refresh').html("Refreshing in " + countdown + "... <a href='#' onclick='nokiaMap.refreshMap(); return false;'>Refresh Now</a>");
				setTimeout('nokiaMap.startWorking(' + tmpTicks + ', ' + (countdown - 1) + ')', 1000);
			}
		}
	},
	refreshMap: function() {
		try {
			var mapCenter = player.map.getMapCenterPosition();
			this.lastLat = mapCenter.latitude;
			this.lastLong = mapCenter.longitude;

			this.lastRadius = this.radius;

			var mapBounds = player.map.getBoundingBox();
			if (this.firstTime) {
				mapBounds[0].latitude=-71.08;
				mapBounds[0].longitude=-155.31;
				mapBounds[1].latitude=90;
				mapBounds[1].longitude=157.195;
				this.radius=218;
				this.firstTime=false;
			}
			var southWest = mapBounds[0];
			var northEast = mapBounds[1];
			
			RefreshMap(southWest.latitude, northEast.latitude, southWest.longitude, northEast.longitude);
		} catch (err) { }

		iinit();
	},
	singleMarker: function(slat,slong,desc) {
		var mapObj = nokiaMap.layer.getMapObjects(true);
		nokiaMap.layer.removeMapObjects(mapObj);
		var myMarker = nokiaMap.layer.addMapObjects({
			type: "marker",
			longitude: slong,
			latitude: slat,
			clickable: true,
			infoTitle: "Real time web",
			infoDescription: ""
		});
		myMarker[0].setHtmlContent(unescape(desc));
		myMarker[0].setHasOwnInfoBubble(true);
		//var ev = new nokia.maps.oviapp.utils.Event("mapObjectSelected",null,true);
		//myMarker[0].fireEvent(ev);
		$('#refresh').html(unescape(desc));
	},
	createEmMarkers: function(slat,slong,desc) {
		var myMarker = nokiaMap.layer.addMapObjects({
			type: "marker",
			longitude: slong,
			latitude: slat,
			clickable: true,
			infoTitle: "Top Downloads",
			infoDescription: ""
		});
		myMarker[0].setHtmlContent(desc);
	},
		load: function() {
			player = nokiaMapLoader.getPlayer({ jsPlugin: "supported", uiLanguage: "en", token: "ce76cd9432ce1c0ebcef32ca8abc821d", playerDomId: "map_content", console: false, onInitDone: function() {

				try {
					// Create layer
					nokiaMap.layer = player.map.createLayer({ name: "testLayer" });

					// Set zoom scale and center the map on the middle marker:
					player.map.moveTo({
						latitude: nokiaMap.lat,
						longitude: nokiaMap.long,
						animationMode: "teleport",
						scale: 3909018
					});
					player.map.addEventHandler(nokia.maps.pfw.MapModel.EVENT_MOVE_DONE, function() {
						nokiaMap.setDistance();
						nokiaMap.refreshMarkers();
					});

					nokiaMap.setDistance();
					nokiaMap.refreshMap();
				}
				catch (err) {
					alert(err);
				}
			}
			});
		},
		setDistance: function() {
			var mapBounds = player.map.getBoundingBox();
			var southWest = mapBounds[0];
			var northEast = mapBounds[1];

			// vertical miles
			var y = 69.1 * Math.abs(northEast.latitude - southWest.latitude);
			this.radius = parseInt((y / 2) * 3);
		}
	}

$(function() {
	setTimeout("nokiaMap.load()", 500);
})


