ShowToelichting = true;
var x;
var y;

var InitialX;
var InitialY;

// breedte van de rechtermarge
var rechtermarge=300

var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd		= new Function();
		o.root.onDrag			= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode)
		{
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		}
		else
		{
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode)
		{
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		}
		else
		{
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup = Drag.end;

		return false;
	},

	drag : function(e)
	{
		ShowToelichting = false;
		Toelichting = document.getElementById("toelichting");
		Toelichting.style.visibility = 'hidden';

		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;
		
		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);

		BigMap = document.getElementById("bigmap");
		Magnifier = document.getElementById("magnifier");

		if (o == BigMap)
		{
			Magnifier = document.getElementById("magnifier");
			Magnifier.style.top = -Math.round(ny/(1689/136)) + 'px';
			Magnifier.style.left = -Math.round(nx/(2011/160)) + 'px';
		}
		else if (o == Magnifier)
		{
			BigMap = document.getElementById("bigmap");
			if (-Math.round(nx*(1689/136)) < -(1689 - document.documentElement.clientWidth + rechtermarge + 20))
			{
				BigMap.style.left = -(1689 - document.documentElement.clientWidth + rechtermarge + 20) + 'px';
			}
			else
			{
				BigMap.style.left = -Math.round(nx*(1689/136)) + 'px';
			}

			if (-Math.round(ny*(2011/160)) < -(2011 - document.documentElement.clientHeight + 20))
			{
				BigMap.style.top = -(2011 - document.documentElement.clientHeight + 20) + 'px';
			}
			else
			{
				BigMap.style.top = -Math.round(ny*(2011/160)) + 'px';
			}
		}

		return false;
	},

	end : function()
	{	
		ShowToelichting = true;
		document.onmousemove = getXY;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};

function getXY(e)
{
	if (window.Event)
	{
		x = e.pageX;
		y = e.pageY;
	}
	else
	{
		// IE 4
		if (document.all && !document.getElementById)
		{
		}
		// Opera
		else if (window.opera)
		{
		}
		// IE 6+ & Mozilla
		else if (document.getElementById && document.compatMode)
		{
			x = event.clientX + document.documentElement.scrollLeft;
			y = event.clientY + document.documentElement.scrollTop;
		}
		// IE 5+
		else if (document.all)
		{
			x = event.clientX + document.body.scrollLeft;
			y = event.clientY + document.body.scrollTop;
		}
	}
}

function ShowSimpleTekst(b, c)
{
	if (ShowToelichting)
	{
		// all except Explorer
		if (self.innerHeight)
		{
			docHeight = self.innerHeight;
			docWidth = self.innerWidth ;
		}
		// Explorer 6 Strict Mode
		else if (document.documentElement && document.documentElement.clientHeight)
		{
			docHeight = document.documentElement.clientHeight;
			docWidth = document.documentElement.clientWidth;
		}
		// other Explorers
		else if (document.body)
		{
			docHeight = document.body.clientHeight;
			docWidth = document.body.clientWidth;
		}

		Toelichting = document.getElementById("toelichting");

		var hoofdtext = (b != -1) ? b : "";
		var detailtext = (c != -1) ? c : "";

		tekst = "<b>" + hoofdtext + "</b><br />" + detailtext + "<br />";
		// Bij het laden van de pagina heeft Toelichting.style.left geen waarde
	// alert(Toelichting.style.top+'/'+Toelichting.style.left+'/'+Toelichting.style.visibility);
				
		if (Toelichting.style.top&&Toelichting.style.left&&Toelichting.style.visibility&&x&&y){
			Toelichting.innerHTML = tekst;
			Toelichting.style.top = (y + Toelichting.offsetHeight < docHeight - 15) ? (y + 15) + 'px' : y - (y + Toelichting.offsetHeight - docHeight) + 'px';
			Toelichting.style.left = (x + Toelichting.offsetWidth < docWidth - 15) ? (x + 15) + 'px' : x - (x + Toelichting.offsetWidth - docWidth) + 'px';
			Toelichting.style.visibility = 'visible'; 
			}
	}
}

function ShowTekst(event, a, b, c, d)
{
	if (ShowToelichting)
	{
		// all except Explorer
		if (self.innerHeight)
		{
			docHeight = self.innerHeight;
			docWidth = self.innerWidth ;
		}
		// Explorer 6 Strict Mode
		else if (document.documentElement && document.documentElement.clientHeight)
		{
			docHeight = document.documentElement.clientHeight;
			docWidth = document.documentElement.clientWidth;
		}
		// other Explorers
		else if (document.body)
		{
			docHeight = document.body.clientHeight;
			docWidth = document.body.clientWidth;
		}

		Toelichting = document.getElementById("toelichting");

		var hoofdtext = (b != -1) ? Hoofd[b] : "";
		var detailtext = (c != -1) ? Detail[c] : "";
		var eventtext = (d != -1) ? Eventtxt[d] : "";

		tekst = "<b>" + hoofdtext + "</b><br />" + detailtext.substring(0, 1).toUpperCase() + detailtext.substring(1, detailtext.length) + "<br />" + eventtext;

		if (Toelichting.style.top&&Toelichting.style.left&&Toelichting.style.visibility&&x&&y){
			Toelichting.innerHTML = tekst;
			Toelichting.style.top = (y + Toelichting.offsetHeight < docHeight - 15) ? (y + 15) + 'px' : y - (y + Toelichting.offsetHeight - docHeight) + 'px';
			Toelichting.style.left = (x + Toelichting.offsetWidth < docWidth - 15) ? (x + 15) + 'px' : x - (x + Toelichting.offsetWidth - docWidth) + 'px';
			Toelichting.style.visibility = 'visible';
			}
	}
}

function HideTekst(){
		Toelichting = document.getElementById("toelichting");
		if (Toelichting.style.visibility&&Toelichting.style.top&&Toelichting.style.left){
		Toelichting.style.visibility = 'hidden';
		Toelichting.style.top = '-500px';
		Toelichting.style.left = '-500px';
		}
	}

function H()
{
}

function mouseMove(o, color)
{
	Magnifier = document.getElementById("magnifier");
	if (o == Magnifier)
		o.style.borderColor = "#ff0000";
}

function mouseOut(o, color)
{
	Magnifier = document.getElementById("magnifier");
	if (o == Magnifier)
		o.style.borderColor = "#000000";
}

function size()
{
	// all except Explorer
	if (self.innerHeight)
	{
		MapHeight = self.innerHeight - 20;
		MapWidth = self.innerWidth - rechtermarge - 20;
	}
	// Explorer 6 Strict Mode
	else if (document.documentElement && document.documentElement.clientHeight)
	{
		MapHeight = document.documentElement.clientHeight - 20;
		MapWidth = document.documentElement.clientWidth - rechtermarge - 20;
	}
	// other Explorers
	else if (document.body)
	{
		MapHeight = document.body.clientHeight;
		MapWidth = document.body.clientWidth - rechtermarge;
	}

	MapBody = document.getElementById("map-body");
	BigMap = document.getElementById("bigmap");
	Magnifier = document.getElementById("magnifier");
	
	MapBody.style.height = MapHeight + 'px';
	MapBody.style.width = MapWidth + 'px';

	MagHeight = Math.round(MapHeight/2011 * 160);
	MagWidth = Math.round(MapWidth/1689 * 136);
	Magnifier.style.width = MagWidth + 'px';
	Magnifier.style.height = MagHeight + 'px';
	
	minX = -(1689 - MapWidth + rechtermarge + 20);
	minX = -(1689 - MapWidth);
	minY = -(2011 - MapHeight);
	Drag.init(BigMap, null, minX, 0, minY, 0);
	maxY = 160 - MagHeight;
	maxX = 136 - MagWidth;
	Drag.init(Magnifier, null, 0, maxX, 0, maxY);
}

function init()
{
// try to get cookie, set default if not present.
	var Cookie_InitialX = getCookie("InitialX");
	if (Cookie_InitialX == null)
	{
		InitialX = -400;
	}
	else
	{
		InitialX = Cookie_InitialX;
	}
	
	var Cookie_InitialY = getCookie("InitialY");
	if (Cookie_InitialY == null)
	{
		InitialY = -600;
	}
	else
	{
		InitialY = Cookie_InitialY;
	}

	if (window.Event)
	{
		document.captureEvents(Event.MOUSEMOVE);
	}
	document.onmousemove = getXY;

	size();

	BigMap = document.getElementById("bigmap");
	Magnifier = document.getElementById("magnifier");
	
	BigMap.style.left = InitialX + 'px';
	BigMap.style.top = InitialY + 'px';

	Magnifier.style.left = -Math.round(InitialX/(2011/160)) + 'px';
	Magnifier.style.top = -Math.round(InitialY/(1689/136)) + 'px';
	
	window.onresize=size;
}

function getCookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

function setCookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}

function StoreXY()
{	
	BigMapLeft = document.getElementById("bigmap").style.left;
	BigMapTop = document.getElementById("bigmap").style.top;
	
    if (BigMapLeft != null)
    {
		if (BigMapLeft.indexOf("px") != -1)
		{
			BigMapLeft = BigMapLeft.substring(0,BigMapLeft.length-2)
		}
    }
    
    if (BigMapTop != null)
    {
		if (BigMapTop.indexOf("px") != -1)
		{
			BigMapTop = BigMapTop.substring(0,BigMapTop.length-2)
		}
    }

	var todays_date = new Date();
    var expires_date =
    new Date(todays_date.getTime() +
    (52 * 86400000)); // 52 weeks from now

	setCookie("InitialX",BigMapLeft,expires_date);
	setCookie("InitialY",BigMapTop,expires_date);
}

