// JavaScript Document

var current_location = -1;
var map;
var tile_layers;
var custommap;
var marker;

var zoom = 4;
var image_dir = 2;

function Wrapper(content)
{
	this.content = content;
}
Wrapper.prototype = new GOverlay();

Wrapper.prototype.initialize = function(map) {
	if (document.getElementById("map_overlay"))
	{
		var div = document.getElementById("map_overlay");
	}
	else
	{
		var div = document.createElement("DIV");
		map.getPane(G_MAP_MAP_PANE).appendChild(div);
	}

	this.map_ = map;
	this.div_ = div;
	this.div_.id = "map_overlay";
}

Wrapper.prototype.remove = function() {
	this.div_.parentNode.removeChild(this.div_);
}

Wrapper.prototype.copy = function() {
	return new Wrapper(this.content);
}

Wrapper.prototype.redraw = function(force) {
	// Only need to redraw if the coordinate system has changed
	if (!force) return;

	var icon = new GIcon();
	icon.image = "/mimages/search/maps/pointers/white_down.gif";
	icon.iconSize = new GSize(10, 10);
	icon.iconAnchor = new GPoint(10, 10);
	icon.infoWindowAnchor = new GPoint(15, 15);

	// get the size of the map and the coordinates of the current marker all in pixels
	var size = map.getSize();
	var center = this.map_.fromLatLngToDivPixel(coords);

	// dimensions of the overlay
	var width = 138;
	var height = 56;

	// for the x coordinates, we need to make sure the overlay does not fall off the map
	var pos_left = (center.x - (width/2));
	if (pos_left < 0) pos_left = 5;
	if ((pos_left + width) > size.width) pos_left = size.width - width - 5;

	// for the y coords, we only need to make sure the top does not get lost
	var pos_top = (center.y - (height)) - 11;
	if (pos_top < 0)
	{
		pos_top = center.y - 1;
		icon.image = "/mimages/search/maps/pointers/orange_up.gif";
	}

	this.div_.innerHTML = this.content;

	// now set the style values and content
	this.div_.style.width = width + "px";
	this.div_.style.height = height + "px";
	this.div_.style.left = pos_left + "px";
	this.div_.style.top = pos_top + "px";

	if (typeof(marker) != "undefined") map.removeOverlay(marker);

	marker = new GMarker(coords, {icon: icon, inert : true });
	map.addOverlay(marker);
}



function initMap()
{
	count = 0;
	for(i in marker_points) count++;
	start_point = Math.floor(Math.random()*count)
	current_location = start_point;

	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("map"));
		map.disableDragging();

		var copyright = new GCopyright(1, new GLatLngBounds(new GLatLng(53.8136257,-3.0981445),new GLatLng(53.8654855,-2.9663944) ), 14, "2008 Accepted.co.uk");
		var copyrightCollection = new GCopyrightCollection('Accepted');
		copyrightCollection.addCopyright(copyright);

		tilelayers = [new GTileLayer(copyrightCollection, zoom, zoom)];
		tilelayers[0].getTileUrl = function(a, b) {
			var f = "/mimages/search/maps/" + image_dir + "/Tile_"+a.x+"_"+a.y+"_" + zoom + ".jpg";
			return f;
		};

		custommap = new GMapType(tilelayers, new GMercatorProjection(zoom+1), "Accepted");
		map.addMapType(custommap);

		map.setCenter(new GLatLng(54.711929, -7.283936), zoom, custommap);

		updateMap();
	}
}
addOnLoad(initMap);

function updateMap()
{
	current_location++;

	if (typeof(marker_points[current_location]) == "undefined") current_location = 0;

	details = marker_points[current_location];

	coords = new GLatLng(details.lat, details.long);

	var html = "";
	html += "<h3>&pound;" + details.loan_amount + " - " + details.town + "</h3>";

/*	rand = Math.floor(Math.random()*2);
	switch(rand)
	{
		case 0:*/
			score = details.neighbourhood_score;
			score = ((score - (score % 100)) / 200);
			html += '<p>Neighbourhood Rating</p>';
			html += '<img src="/mimages/search/stars/blue/on_white/' + score + '.gif" alt="' + score + ' Stars" />';
/*		break;
		case 1:
			html += '<p class="first">Credit Score</p>';
			html += '<p>' + details.credit_score + '</p>';
		break;
	}*/

	// this is causing the flickering in firefox, commenting this out and modifying wrapper.initialize() seems to sort it
//	map.clearOverlays();

	wrapper = new Wrapper(html);
	map.addOverlay(wrapper);

	window.setTimeout("updateMap()", 3000);
}

