/************************************
 * Copyright (c) Motoki, 2007       *
 * e-mail: hotspot@narod.ru         *
 *    web: http://hotspot.narod.ru/ *
 *                                  *
 *    Exclusively for Fargate.ru    *
 ************************************/

/* A More Accessible Map */

/* 
 * Copyright (c) Seth Duffey
 * http://alistapart.com/articles/cssmaps
 *
 */
	
var mapMaker = {

	offsetX: -16, // tooltip X offset
	offsetY: 16,  // tooltip Y offset
	element: false,
	DLs:     false,
	DTs:     false,
	on:      false,
	pins:    null,
	pin:     0,
	marker:  null,
	info:    null,
	timeOut: 10,
	speed:   10,

	/* constructor - sets events */
	init: function()
	{
		var currentLocation = 0;
		mapMaker.DLs = document.getElementsByTagName('dl');
		mapMaker.DTs = document.getElementsByTagName('dt');

		// only loop thru items once
		if( mapMaker.on == false )
		{
			//loop through each DL on page
			var i = 0;
			while (mapMaker.DLs.length > i)
			{
				//only affect DLs with a class of 'map'
				if (mapMaker.DLs[i].className.indexOf('map') > -1)
				{
					//change map DL class, this way map is text only without javascript enabled
					//mapMaker.DLs[i].className = 'map on';

					//remove any white space
					stripWhitespace(mapMaker.DLs[i]);
				};
				i++;
			};

			i = 0;
			while (mapMaker.DTs.length > i)
			{
				if (mapMaker.DTs[i].parentNode.className.indexOf('map') > -1)
				{
					stripWhitespace(mapMaker.DTs[i]);

					//current Location
					currentLocation = mapMaker.DTs[i].firstChild;

					// add events to links
					addEvent(currentLocation, 'mouseover', mapMaker.showTooltip); //displays tooltip on mouse over
					addEvent(currentLocation, 'mouseout', mapMaker.hideTooltip); //hide tooltip on mouse out
					addEvent(currentLocation, 'focus', mapMaker.showTooltip); //display tooltip on focus, for added keyboard accessibility
					addEvent(currentLocation, 'blur', mapMaker.hideTooltip); //hide tooltip on focus, for added keyboard accessibility
				}
				i++;
			}
			mapMaker.on = true;
			mapMaker.pins = getPins();			
			mapMaker.marker = new Marker();
			mapMaker.info = document.getElementById('info');
			mapMaker.pin = getCurrentPinIndex();
		}
	},

	/* SHOW TOOLTIP */
	showTooltip: function()
	{
		var evt = this;
		var i = 0;

		//Find DD to display - based on currently hovered anchor move to parent DT then next sibling DD
		var objid = evt.parentNode.nextSibling;
		mapMaker.element = objid; //set for the hideTooltip

		//get width and height of background map
		var mapWidth  = objid.parentNode.offsetWidth;
		var mapHeight = objid.parentNode.offsetHeight;

		//get width and height of the DD
		var toopTipWidth = objid.offsetWidth;
		var toopTipHeight = objid.offsetHeight;

		//figure out where tooltip should be places based on point location
		var newX = evt.offsetLeft + mapMaker.offsetX;
		var newY = evt.offsetTop + mapMaker.offsetY;

		//check if tooltip fits map width 
		if ((newX + toopTipWidth) > mapWidth)
		{
			objid.style.left = newX - toopTipWidth - 24 + 'px';
		}
		else
		{
			objid.style.left = newX + 'px';
		}
		
		//check if tooltip fits map height 
		if ((newY + toopTipHeight) > mapHeight)
		{
			objid.style.top = newY - toopTipHeight - 14 + 'px';
		}
		else
		{
			objid.style.top = newY + 'px';
		};
	},

	/* HIDE TOOLTIP */
	hideTooltip: function()
	{
		mapMaker.element.style.left = '-9999px';
	},

	setPin: function(pin)
	{
		mapMaker.info.innerHTML = pin.label;
		mapMaker.marker.setLocation(pin.x, pin.y);
	},

	moveToPin: function()
	{
		var pin = mapMaker.pins[mapMaker.pin];
		mapMaker.info.innerHTML = pin.label;
		mapMaker.marker.moveTo(pin.x, pin.y);
	},

	moveToFirstPin: function()
	{
		if (mapMaker.pins.length > 0)
		{
			mapMaker.pin = 0;
			mapMaker.moveToPin();
		}
		return false;
	},

	moveToPrevPin: function()
	{
		if (mapMaker.pin > 0)
		{
			mapMaker.pin--;
			mapMaker.moveToPin();
		}
		return false;
	},

	moveToNextPin: function()
	{
		if (mapMaker.pin < mapMaker.pins.length - 1)
		{
			mapMaker.pin++;
			mapMaker.moveToPin();
		}
		return false;
	},

	moveToLastPin: function()
	{
		if (mapMaker.pins.length > 0)
		{
			mapMaker.pin = mapMaker.pins.length - 1;
			mapMaker.moveToPin(mapMaker.pins[mapMaker.pin]);
		}
		return false;
	}
};

/* LOAD SCRIPT */
	/* for Mozilla */
		if (document.addEventListener) {
			document.addEventListener("DOMContentLoaded", mapMaker.init, null);
		};
		
	/* for Internet Explorer */
		/*@cc_on @*/
		/*@if (@_win32)
			document.write("<script defer src=ie_onload.js><"+"/script>");
		/*@end @*/
		
	/* for other browsers */
		addEvent(window, 'load', mapMaker.init);

function Pin(id)
{
	this.id = id;
	this.trigger = document.getElementById('location_' + this.id);
	this.x = parseInt(this.trigger.style.left);
	this.y = parseInt(this.trigger.style.top);
	this.label = this.trigger.innerHTML;
}

function Marker()
{
	this.id = 'marker';
	this.trigger = document.getElementById('location_' + this.id);
	this.iterator = null;

	this.setLocation = function(x, y)
	{
		this.trigger.style.left = Math.round(x - 4) + "px";
		this.trigger.style.top = Math.round(y - 4) + "px";
	}

	this.moveTo = function(x, y)
	{
		if (this.iterator)
		{
			window.clearTimeout(this.iterator);
		}
		this.iterator = window.setTimeout("mapMaker.marker.onMoving(" + x + ", " + y + ")", mapMaker.timeOut);
	}

	this.onMoving = function(x, y)
	{

		var cx = parseInt(this.trigger.style.left) + 4;
		var cy = parseInt(this.trigger.style.top) + 4;
		var dx = x - cx;
		var dy = y - cy;
		if (Math.abs(dx) > mapMaker.speed ||
			Math.abs(dy) > mapMaker.speed)
		{
			var d = Math.sqrt(dx * dx + dy * dy);
			var nx = cx + dx * mapMaker.speed / d;
			var ny = cy + dy * mapMaker.speed / d;
			this.setLocation(nx, ny);
			this.iterator = window.setTimeout("mapMaker.marker.onMoving(" + x + ", " + y + ")", mapMaker.timeOut);
		}
		else
		{
			this.setLocation(x, y);
		}
	}
}