hideTimer = fadeTimer = null;

//global vars
var map = false;
var mapSizeX = 400;
var mapSizeY = 300;
var points = new Array();

// mapLocation Object
function mapLocation(newpoint, newname, newabbr, newtel) {
	this.point = newpoint;
	this.name = newname;
	this.abbr = newabbr;
	this.tel = newtel;
}


function createMap() {
	// Create lat/lon objects for locations
	points[0] = new mapLocation(new YGeoPoint(49.435176, -2.574915), 'Auto Collision Repairs', 'AC', '01481 235020');
	points[1] = new mapLocation(new YGeoPoint(49.479439, -2.5133), 'Bougourd Ford Limited', 'BF', '01481 247072');
	points[2] = new mapLocation(new YGeoPoint(49.494696, -2.563983), 'Kenny Brouard Autobody Shop', 'KB', '01481 256939');
	points[3] = new mapLocation(new YGeoPoint(49.430982, -2.600257), 'Bodyline', 'BD', '01481 263122');
	points[4] = new mapLocation(new YGeoPoint(49.429915, -2.600911), 'Duques Garage - Motorcycles', 'DG', '01481 267657');

	points[9] = new mapLocation(new YGeoPoint(49.203986, -2.15589), 'Bel Royal Motors', 'BR', '01534 625880');
	points[5] = new mapLocation(new YGeoPoint(49.201601, -2.194886), 'Cunninghams Car Centre', 'CC', '01534 745824');
	points[6] = new mapLocation(new YGeoPoint(49.181065, -2.079661), 'La Motte Ford', 'LM', '01534 636633');
	points[7] = new mapLocation(new YGeoPoint(49.180175, -2.077569), 'Martin Fernando Limited', 'MF', '01534 507873');
	
	points[8] = new mapLocation(new YGeoPoint(49.726165, -2.172107), 'Island Panel Craft Ltd', 'IP', '01481 824747');
	// Create lat/lon objects for map additions
	pRd1a = new YGeoPoint(49.181353, -2.079538);
	pRd1b = new YGeoPoint(49.180708, -2.077049);
	pRd2a = new YGeoPoint(49.202190, -2.195125);
	pRd2b = new YGeoPoint(49.201252, -2.194235);
	pRd3a = new YGeoPoint(49.483899, -2.510778);
	pRd3b = new YGeoPoint(49.484925, -2.510832);
	pRd4a = new YGeoPoint(49.72665, -2.17348);
	pRd4b = new YGeoPoint(49.726165, -2.172107);
	
	// Set up map type in the constructor
	map = new YMap(document.getElementById('mapContainer'),YAHOO_MAP_REG, new YSize(mapSizeX,mapSizeY));
	// Display the map centered on a lattitude and longitude
	//map.drawZoomAndCenter(points[0].point, 2);
	map.addZoomShort();
	// Create a marker positioned at a lat/lon
	var marker = new Array();
	for (var i in points) {
		marker[i] = new YMarker(points[i].point);
		// Add a label to the marker
		mLabel = '<div style="padding-left: 3px; padding-top: 3px; color: white;">'+points[i].abbr+'</div>';
		marker[i].addLabel(mLabel);
	
		// Add auto expand
		var _txt = '<div style="width:160px;height:50px;" class="b">'+points[i].name+'</div>'+points[i].tel+'<div></div>';
		marker[i].addAutoExpand(_txt);

		// Display the marker
		map.addOverlay(marker[i]);
	}
	
	// add map additions
	var polyRdLMTE = new YPolyline([pRd1a,pRd1b],'white',9,1.0);
	map.addOverlay(polyRdLMTE);
	var polyRdCCC = new YPolyline([pRd2a,pRd2b],'#ff9955',12,1.0);
	map.addOverlay(polyRdCCC);
	var polyRdGBL = new YPolyline([pRd3a,pRd3b],'#ff9955',12,1.0);
	map.addOverlay(polyRdGBL);
	var polyRdIPC = new YPolyline([pRd4a,pRd4b],'#ff9955',12,1.0);
	map.addOverlay(polyRdIPC);
}

function showMap(e, mode, mapNum, zoom) {
	if (mode==0) {
		// make sure nothing else is happening...
		clearTimeout(hideTimer);
		clearTimeout(fadeTimer);
		// init maps if they haven't been called before
		if (!map) { createMap(); }
		document.getElementById('mapHover').style.display = 'inline';
		document.getElementById('mapHover').style.opacity = '1';
		document.getElementById('mapHover').style.filter= '';
		
		if (!zoom || zoom < 1 || zoom > 16) { zoom = 2; }
		// Display the map centered on a latitude and longitude
		map.drawZoomAndCenter(points[mapNum].point, zoom);
		document.getElementById('mapFullLink').innerHTML = '<a href="http://maps.yahoo.com/#lon='+points[mapNum].point.Lon+'&lat='+points[mapNum].point.Lat+'&mag=1" target="_blank">Open Full Map In New Window...</a>';
	}
	
	var posX = 0;
	var posY = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posX = e.pageX;
		posY = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posX = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posY = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	// posx and posy contain the mouse position relative to the document
	// add a little padding
	posX+=2;
	posY+=8;
	
	var clientX, clientY, scrollX, scrollY;
	if (self.innerHeight) // all except Explorer
	{
		clientX = self.innerWidth;
		clientY = self.innerHeight;
		scrollX = self.pageXOffset;
		scrollY = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		clientX = document.documentElement.clientWidth;
		clientY = document.documentElement.clientHeight;
		scrollX = document.documentElement.scrollLeft;
		scrollY = document.documentElement.scrollTop;
	}
	else if (document.body) // other Explorers
	{
		clientX = document.body.clientWidth;
		clientY = document.body.clientHeight;
		scrollX = document.body.scrollLeft;
		scrollY = document.body.scrollTop;
	}

	// make sure map doesn't go off bottom or right side of screen
	if (posY + mapSizeY + 20 > clientY + scrollY) {
		document.getElementById('mapHover').style.top = (posY-(mapSizeY+30))+'px';
	} else { 
		document.getElementById('mapHover').style.top = posY+'px';
	}
	if (posX + mapSizeX + 10 > clientX + scrollX) {
		document.getElementById('mapHover').style.left = (posX-(mapSizeX))+'px';
	} else {
		document.getElementById('mapHover').style.left = posX+'px';
	}
}

// hide the map, with a flashy fade effect!
function fadeMap(fadeLevel) {
	if (fadeLevel==null) { fadeLevel = 100; }
	fadeLevel = fadeLevel - 5;
	
	if (fadeLevel>1) {	
		fadeTimer = setTimeout('fadeMap('+fadeLevel+')', 50);
		document.getElementById('mapHover').style.opacity = (fadeLevel / 100);
		document.getElementById('mapHover').style.filter='alpha(opacity='+fadeLevel+')';
	} else {
		document.getElementById('mapHover').style.display = 'none';
		document.getElementById('mapHover').style.opacity = '1';
		document.getElementById('mapHover').style.filter= '';
		clearTimeout(fadeTimer);
	}
}

function hideMap(e, fadeLevel) {
	if (!e) var e = window.event;
	var tg = (window.event) ? e.srcElement : e.target;
	if (tg.id == 'mapLink') { hideTimer = setTimeout('fadeMap(100)', 500); return; }
	if (tg.nodeName != 'DIV') return;
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
	while (reltg != tg && reltg.nodeName != 'BODY')
		reltg= reltg.parentNode; 
	if (reltg== tg) return;
	// Mouseout took place when mouse actually left layer
	// Handle event	
	if (fadeLevel==null) { fadeLevel = 100; }
	if (tg.id=='mapHover')	
	hideTimer = setTimeout('fadeMap(100)', 500);//fadeMap(fadeLevel);
}
