var SlideInButtonId = 'conf_tools';
var MapOverlayType = 10;
var MapOverlayId = 1;
var InfoBoxFrameNames = new Array('SourceFrame', 'ObjectFrame', 'InfoBoxId', 'PhotoDetailPreview');
var MouseInFrame = new Object();
for(var i = 0; i < InfoBoxFrameNames.length; i++)
	MouseInFrame[InfoBoxFrameNames[i]] = false;
var MapId = 'map';
var MouseInFrameLastStatus = new Object();
var MouseInFrameChanged = new Object();
MouseInFrame[MapId] = false;
MouseInFrameLastStatus[MapId] = false;
MouseInFrameChanged[MapId] = false;
function OverlayObject()
{
	var OverlayIsAlwayActive = false;
	var MapContainer = document.getElementById('map');
	var OverlayBottom = 30;
	var ActionsLeftTop = 6;
	var ActionsRightTop = 6;
	var LeftActions = new Array();
	var RightActions = new Array();
	this.CloseButton = false;
	
	if(document.getElementById('OverlayAlwaysActive') && document.getElementById('OverlayAlwaysActive').value == 'true')
		OverlayIsAlwayActive = true;
	
	this.CreateMapOverlay = function(MapOverlayType, MapOverlayId)
	{
		// wenn Overlay, schon existiert, dann kein weiteres erstellen
		if(document.getElementById('OBJECT_OVERLAY_'+MapOverlayType+'_'+MapOverlayId))
			return;

		// Overlay-Container erzeugen
		//AddSlideInButton(MapOverlayType, MapOverlayId);
		AddMapTypes();
		
		var MapOverlay = CreateOverlay(MapOverlayType, MapOverlayId);
		
		if(LeftActions.length > 0)
		{
			var ActionsLeft = document.createElement('div');
			ActionsLeft.setAttribute('class', 'OBJECT_OVERLAY_ACTION');
			ActionsLeft.style.position = 'absolute';
			ActionsLeft.style.left = '2px';
			ActionsLeft.style.top = ActionsLeftTop+'px';
			//ActionsLeft.style.cssFloat = 'left';
			for(var i = 0; i < LeftActions.length; i++)
			{
				ActionsLeft.appendChild(LeftActions[i]);
			}
			MapOverlay.appendChild(ActionsLeft);
		}
		if(RightActions.length > 0)
		{
			var ActionsRight = document.createElement('div');
			ActionsRight.setAttribute('class', 'OBJECT_OVERLAY_ACTION');
			ActionsRight.style.position = 'absolute';
			ActionsRight.style.right = '2px';
			ActionsRight.style.top = ActionsRightTop+'px';
			ActionsRight.style.lineHeight = '0px';
			//ActionsRight.style.cssFloat = 'right';
			for(var i = 0; i < RightActions.length; i++)
			{
				ActionsRight.appendChild(RightActions[i]);
			}
			MapOverlay.appendChild(ActionsRight);
		}
		
		if(this.CloseButton == true && OverlayIsAlwayActive == false)
		{
			var ActionClose = document.createElement('img');
				ActionClose.setAttribute('src', '/gfx/gif/transparent.gif');
				ActionClose.setAttribute('id', 'OverlayCloseButton');
				ActionClose.className = 'close_button';
				ActionClose.style.position = 'absolute';
				ActionClose.style.top = '0px';
				ActionClose.style.right = '5px';
				ActionClose.style.cursor = 'pointer';
				ActionClose.onclick = function(e) {
					//document.getElementById('map').onmouseover = function() { };
					//document.getElementById(SlideInButtonId).style.display = 'block';
					SlideOutOverlay(MapOverlayType, MapOverlayId, 45, true);
					MapOverlayEnabled = false;
				};
			MapOverlay.appendChild(ActionClose);
		}
		
		MapOverlay.onmouseover = function() {  };
		MapOverlay.onmouseout = function() {  };
		MapContainer.appendChild(MapOverlay);
		
		if(document.getElementById('ChangePositionEnabled') && document.getElementById('ChangePositionEnabled').value == 'true')
		{
			AddCrossHair();
		}
	}
	
	this.AddAction = function(element, Position)
	{
		AddAction(element, Position);
	}
	
	function AddAction(element, Position)
	{
		switch(Position)
		{
			case 'left':
				LeftActions.push(element);
				break;
			case 'right':
				RightActions.push(element);
				break;
		}
	}
	
	function AddSlideInButton(MapOverlayType, MapOverlayId)
	{
		var MapDiv = document.getElementById('map');
		var MapWidth = MapDiv.offsetWidth;
		var SlideInButtonWidth = 45;
		var SlideInButtonLeft = parseInt((MapWidth / 2) - (SlideInButtonWidth / 2));
		var SlideInButton = document.createElement('div');
			SlideInButton.setAttribute('id', SlideInButtonId);
			SlideInButton.style.width = SlideInButtonWidth+'px';
			SlideInButton.style.height = '15px';
			SlideInButton.style.position = 'absolute';
			SlideInButton.style.cursor = 'pointer';
			SlideInButton.style.bottom = '0px';
			SlideInButton.style.left = SlideInButtonLeft+'px';
			SlideInButton.style.backgroundColor = '#000000';
			SlideInButton.style.MozOpacity = 0.7;
			SlideInButton.style.opacity = 0.7;
			SlideInButton.style.filter = 'alpha(opacity=70)';
			SlideInButton.style.zIndex = 50;
			SlideInButton.onmouseover = function() { SlideInButton.style.display = 'none'; SlideInOverlay(MapOverlayType, MapOverlayId); };
			SlideInButton.onmouseout = function() { };
	
		MapDiv.appendChild(SlideInButton);
	}
	
	function AddMapTypes()
	{
		var ActionGoogle = document.createElement('img');
			ActionGoogle.setAttribute('src', '/gfx/gif/transparent.gif');
			if(_MapSource == 'GOOGLE')
				ActionGoogle.className = 'images_28 map_symbol_google_selected';
			else
				ActionGoogle.className = 'images_28 map_symbol_google_not_selected';
			ActionGoogle.setAttribute('id', 'GOOGLE');
			ActionGoogle.id = 'GOOGLE';
			if(document.getElementById('L_MAP_GOOGLE'))
				ActionGoogle.setAttribute('title', document.getElementById('L_MAP_GOOGLE').value);
			ActionGoogle.style.marginRight = '5px';
			if(_MapSource != 'GOOGLE')
			{
				ActionGoogle.style.cursor = 'pointer';
				ActionGoogle.onclick = function() { MapChangeSource(this); };
			}
		var ActionYahoo = document.createElement('img');
			ActionYahoo.setAttribute('src', '/gfx/gif/transparent.gif');
			if(_MapSource == 'YAHOO')
				ActionYahoo.className = 'images_28 map_symbol_yahoo_selected';
			else
				ActionYahoo.className = 'images_28 map_symbol_yahoo_not_selected';
			ActionYahoo.setAttribute('id', 'YAHOO');
			ActionYahoo.id = 'YAHOO';
			if(document.getElementById('L_MAP_YAHOO'))
				ActionYahoo.setAttribute('title', document.getElementById('L_MAP_YAHOO').value);
			ActionYahoo.style.marginRight = '15px';
			if(_MapSource != 'YAHOO')
			{
				ActionYahoo.style.cursor = 'pointer';
				ActionYahoo.onclick = function() { MapChangeSource(this); };
			}
		var ActionMicrosoft = document.createElement('img');
			ActionMicrosoft.setAttribute('src', '/gfx/gif/transparent.gif');
			if(_MapSource == 'MICROSOFT')
				ActionMicrosoft.className = 'images_28 map_symbol_microsoft_selected';
			else
				ActionMicrosoft.className = 'images_28 map_symbol_microsoft_not_selected';
			ActionMicrosoft.setAttribute('id', 'MICROSOFT');
			ActionMicrosoft.id = 'MICROSOFT';
			if(document.getElementById('L_MAP_MICROSOFT'))
				ActionMicrosoft.setAttribute('title', document.getElementById('L_MAP_MICROSOFT').value);
			ActionMicrosoft.style.marginRight = '5px';
			if(_MapSource != 'MICROSOFT')
			{
				ActionMicrosoft.style.cursor = 'pointer';
				ActionMicrosoft.onclick = function() { MapChangeSource(this); };
			}
		
		AddAction(ActionMicrosoft, 'right');
		AddAction(ActionGoogle, 'right');
		AddAction(ActionYahoo, 'right');
	}
	
	function CreateOverlay(MapOverlayType, MapOverlayId)
	{
		var MapOverlay = document.createElement('div');
		MapOverlay.setAttribute('id', 'OBJECT_OVERLAY_'+MapOverlayType+'_'+MapOverlayId);
		MapOverlay.style.position = 'absolute';
		MapOverlay.style.width = '100%';
		MapOverlay.style.left = '0px';
		if(OverlayIsAlwayActive == false)
			MapOverlay.style.bottom = '-'+(OverlayBottom+10)+'px';
		else
			MapOverlay.style.bottom = '0px';
		MapOverlay.style.height = OverlayBottom+'px';
		MapOverlay.style.paddingTop = '10px';
		MapOverlay.style.paddingLeft = '5px';
		MapOverlay.style.overflow = 'hidden';
		MapOverlay.style.backgroundColor = '#000000';
		MapOverlay.style.zIndex = 50;
		MapOverlay.style.cursor = 'default';
		MapOverlay.style.MozOpacity = 0.7;
		MapOverlay.style.opacity = 0.7;
		MapOverlay.style.filter = 'alpha(opacity=70)';
		
		return MapOverlay;
	}
	
	function AddCrossHair()
	{
		if(document.getElementById('cross_hair_hline') && document.getElementById('cross_hair_vline'))
			return;

		// Fadenkreuz
		var LineLength = 50;
		var LineStroke = 2;
		var MapWidth = MapContainer.style.width.replace('px', '');
		var MapHeight = MapContainer.style.height.replace('px', '');
		var HLine = document.createElement('div');
			HLine.setAttribute('id', 'cross_hair_hline');
			HLine.style.width = LineLength+'px';
			HLine.style.height = LineStroke+'px';
			HLine.style.position = 'absolute';
			HLine.style.left = Math.floor((MapWidth / 2) - (LineLength / 2)) + 'px';
			HLine.style.top = Math.floor((MapHeight / 2) - (LineStroke / 2)) + 'px';
			HLine.style.backgroundColor = document.getElementById('CorporateDesignColor').value;
			HLine.style.zIndex = '50';
			HLine.style.fontSize = '0px';
			if(OverlayIsAlwayActive == true)
				HLine.style.display = 'block';
			else
				HLine.style.display = 'none';
		
		var VLine = document.createElement('div');
			VLine.setAttribute('id', 'cross_hair_vline');
			VLine.style.width = LineStroke+'px';
			VLine.style.height = LineLength+'px';
			VLine.style.position = 'absolute';
			VLine.style.top = Math.floor((MapHeight / 2) - (LineLength / 2)) + 'px';
			VLine.style.left = Math.floor((MapWidth / 2) - (LineStroke / 2)) + 'px';
			VLine.style.backgroundColor = document.getElementById('CorporateDesignColor').value;
			VLine.style.zIndex = '50';
			if(OverlayIsAlwayActive == true)
				VLine.style.display = 'block';
			else
				VLine.style.display = 'none';

		MapContainer.appendChild(HLine);
		MapContainer.appendChild(VLine);
	}
	
	this.GetCloseButton = function()
	{
		if(document.getElementById('OverlayCloseButton'))
			return document.getElementById('OverlayCloseButton');
		else
			return false;
	}
}

var aktiv = new Array();
var inaktiv = new Array();
var tMOtest = new Array();
var tMsec = new Array();
var tRun	= new Array();
var SlideInCounter = new Array();
var SlideOutCounter = new Array();
var SlideCounterLimit = 20;
var Limit;
var ForceCompleteSlideOut = false;
var InfoBoxHidden = true;
var ShowInfoBoxSlow = true;
var CheckCursorInFrameTimeoutActive = false;
var MapOverlayEnabled = true;

function SlideInOverlay(Type, Id)
{
	if(ForceCompleteSlideOut == false)
	{
		if(inaktiv[Id])
			window.clearInterval(inaktiv[Id]);
		SlideInCounter[Id] = 0;
		aktiv[Id] = window.setInterval("SlideIn("+Type+","+Id+")", 5);
	} else
	{
		//console.log('ForceCompleteSlideOut == true');
	}
}

function SlideOutOverlay(Type, Id, Height, Force)
{
	if(Force && Force == true)
	{
		ForceCompleteSlideOut = true;
	}
	window.clearInterval(aktiv[Id]);
	if(document.getElementById('OBJECT_OVERLAY_'+Type+'_'+Id))
	{
		inaktiv[Id] = window.setInterval("SlideOut("+Type+","+Id+","+Height+")", 5);
	}
}

function SlideIn(Type, Id)
{
	if(ForceCompleteSlideOut == true && inaktiv[Id])
	{
		window.clearInterval(aktiv[Id]);
		return;
	}
	
	SlideInCounter[Id]++;
	if(SlideInCounter[Id] > SlideCounterLimit)
	{
		window.clearInterval(aktiv[Id]);
		ForceCompleteSlideOut = false;
		return;
	}
	
	if(document.getElementById('OBJECT_OVERLAY_'+Type+'_'+Id))
	{
		if(parseInt(document.getElementById('OBJECT_OVERLAY_'+Type+'_'+Id).style.bottom.replace("px","")) < 0)
		{
			document.getElementById('OBJECT_OVERLAY_'+Type+'_'+Id).style.bottom = (parseInt(document.getElementById('OBJECT_OVERLAY_'+Type+'_'+Id).style.bottom.replace("px",""))+5)+'px';
		}
		else
		{
			window.clearInterval(aktiv[Id]);
			ForceCompleteSlideOut = false;
			
			//Untere Statische Overlays ausblenden
			if(document.getElementById('OBJECT_OVERLAY_BOTTOM_LEFT_'+Type+'_'+Id))
			{
				document.getElementById('OBJECT_OVERLAY_BOTTOM_LEFT_'+Type+'_'+Id).style.display = 'none';
			}
			
			if(document.getElementById('OBJECT_OVERLAY_BOTTOM_RIGHT_'+Type+'_'+Id))
			{
				document.getElementById('OBJECT_OVERLAY_BOTTOM_RIGHT_'+Type+'_'+Id).style.display = 'none';
			}
		}
	}
}

function SlideOut(Type,Id,Height)
{
	if(document.getElementById('OBJECT_OVERLAY_'+Type+'_'+Id))
	{
		if(parseInt(document.getElementById('OBJECT_OVERLAY_'+Type+'_'+Id).style.bottom.replace("px","")) > -Height)
		{
			document.getElementById('OBJECT_OVERLAY_'+Type+'_'+Id).style.bottom = (parseInt(document.getElementById('OBJECT_OVERLAY_'+Type+'_'+Id).style.bottom.replace("px",""))-5)+'px';
		}
		else
		{
			window.clearInterval(inaktiv[Id]);
			ForceCompleteSlideOut = false;
			//Untere Statische Overlays einblenden
			if(document.getElementById('OBJECT_OVERLAY_BOTTOM_LEFT_'+Type+'_'+Id))
			{
				document.getElementById('OBJECT_OVERLAY_BOTTOM_LEFT_'+Type+'_'+Id).style.display = 'inline';
			}
			
			if(document.getElementById('OBJECT_OVERLAY_BOTTOM_RIGHT_'+Type+'_'+Id))
			{
				document.getElementById('OBJECT_OVERLAY_BOTTOM_RIGHT_'+Type+'_'+Id).style.display = 'inline';
			}
		}
	}
}

function ShowActions(Type, Id)
{
	if(document.getElementById('TOP_LEFT_ACTIONS_'+Type+'_'+Id))
	{
		document.getElementById('TOP_LEFT_ACTIONS_'+Type+'_'+Id).style.display = 'block';
	}
}

function HideActions(Type, Id)
{
	if(document.getElementById('TOP_LEFT_ACTIONS_'+Type+'_'+Id))
	{
		document.getElementById('TOP_LEFT_ACTIONS_'+Type+'_'+Id).style.display = 'none';
	}
}

function InitMOCheck(oTarget,oTime,oAction)
{
	if(!tRun[oTarget])
	{
		if(ShowInfoBoxSlow == true)
			Limit = oTime * 500;
		else
			Limit = oTime * 10;
		tRun[oTarget] = 1;
		tMsec[oTarget] = 0;
		//console.log('Init('+oTarget+')');
		tMOtest[oTarget] = window.setInterval('RunMoCheck(\''+oTarget+'\',\''+oAction+'\')',10);
	}
	
	window.setInterval('CheckCursorInFrameForInfoBox()', 100);
}

function RunMoCheck(oTarget,oAction)
{
	var con		= document.getElementById(oTarget);

	var pLeft 	= absLeft(con);
	var pRight	= absLeft(con) + con.offsetWidth;
	var pTop	= absTop(con);
	var pBottom	= absTop(con) + con.offsetHeight;

	tMsec[oTarget]+=10;
	//console.log(tMsec[oTarget]+' / '+Limit+' -> '+oTarget);
	if(tMsec[oTarget] >= Limit)
	{
		//aufruf der  jeweiligen function
		window.setTimeout(oAction,0);
		
		window.clearInterval(tMOtest[oTarget]);
		tMsec[oTarget]=0;
		tRun[oTarget]=0;
		ShowInfoBoxSlow = false;
	}
	else
	{
		if(Mouse.X < pLeft || Mouse.X > pRight || Mouse.Y < pTop || Mouse.Y > pBottom)
		{
			//console.log('Reset: '+oTarget);
			window.clearInterval(tMOtest[oTarget]);
			tMsec[oTarget]=0;
			tRun[oTarget]=0;
			
			//console.log('Left: '+pLeft+ 'Mouse: '+MouseX+' Right: '+pRight);
			//console.log('Top: '+pTop+ 'Mouse: '+MouseY+' Bottom: '+pBottom);
		}
	}
}

function CheckCursorInFrameForInfoBox(Recursive)
{
	var InAnyCon = false;
	for(var i = 0; i < InfoBoxFrameNames.length; i++)
	{
		if(document.getElementById(InfoBoxFrameNames[i]))
		{
			var FrameCon = document.getElementById(document.getElementById(InfoBoxFrameNames[i]).value);
			var FrameLeft = absLeft(FrameCon);
			var FrameTop = absTop(FrameCon);
			var FrameRight = FrameLeft + FrameCon.offsetWidth;
			var FrameBottom = FrameTop + FrameCon.offsetHeight;
			
			if(Mouse.X > FrameLeft && Mouse.X < FrameRight && Mouse.Y > FrameTop && Mouse.Y < FrameBottom)
			{
				MouseInFrame[InfoBoxFrameNames[i]] = true;
				InAnyCon = true;
			} else
			{
				MouseInFrame[InfoBoxFrameNames[i]] = false;
			}
		}
	}
	
	if(InAnyCon == false && InfoBoxHidden == false)
	{
		if(CheckCursorInFrameTimeoutActive == true && Recursive && Recursive == 'true')
		{
			HideInfoBox();
			ShowInfoBoxSlow = true;
			InfoBoxHidden = true;
			CheckCursorInFrameTimeoutActive = false;
		} else
		if(CheckCursorInFrameTimeoutActive == false)
		{
			window.setTimeout('CheckCursorInFrameForInfoBox(\'true\')', 500);
			CheckCursorInFrameTimeoutActive = true;
		}
	}
}

function MapOverlayEventer(e)
{
	if(document.getElementById(MapId))
	{
		var MouseCheck = CheckIfMouseInFrame(e, MapId);
		if(MouseCheck.Changed == true)
		{
			if(MapOverlayEnabled == true && MouseCheck.InFrame == true)
			{
				if(document.getElementById('cross_hair_hline') && document.getElementById('cross_hair_vline'))
				{
					document.getElementById('cross_hair_hline').style.display = 'block';
					document.getElementById('cross_hair_vline').style.display = 'block';
				}
				SlideInOverlay(MapOverlayType, MapOverlayId);
			} else
			{
				if(document.getElementById('cross_hair_hline') && document.getElementById('cross_hair_vline'))
				{
					document.getElementById('cross_hair_hline').style.display = 'none';
					document.getElementById('cross_hair_vline').style.display = 'none';
				}
				SlideOutOverlay(MapOverlayType, MapOverlayId, 45);
			}
		}
	}
}

function CheckIfMouseInFrame(e, MapId)
{
	var MouseCheck = new Object();
	MouseCheck.Changed = false;
	MouseCheck.InFrame = false;
	if(document.getElementById(MapId))
	{
		var MapContainer = document.getElementById(MapId);
		var FrameLeft = absLeft(MapContainer);
		var FrameTop = absTop(MapContainer);
		var FrameRight = FrameLeft + MapContainer.offsetWidth;
		var FrameBottom = FrameTop + MapContainer.offsetHeight;
		if(!e) { // für den IE
			e = window.event;
		}
		var MouseX = e.clientX;
		var MouseY = e.clientY;
		var ScrollY = 0;

		if(document.all)
		{
			if (!document.documentElement.scrollTop)
				ScrollY = document.body.scrollTop;
			else
				ScrollY = document.documentElement.scrollTop;
		}
		else
		{
			ScrollY = window.pageYOffset;
		}

		if(MouseX > FrameLeft && MouseX < FrameRight && MouseY+ScrollY > FrameTop && MouseY+ScrollY < FrameBottom)
		{
			if(MouseInFrameLastStatus[MapId] == false)
			{
				MouseCheck.Changed = true;
				MouseInFrameLastStatus[MapId] = true;
			} else
			{
				MouseCheck.Changed = false;
			}
			MouseInFrame[MapId] = true;
			MouseCheck.InFrame = true;
		} else
		{
			if(MouseInFrameLastStatus[MapId] == true)
			{
				MouseCheck.Changed = true;
				MouseInFrameLastStatus[MapId] = false;
			} else
			{
				MouseCheck.Changed = false;
			}
			MouseInFrame[MapId] = false;
			MouseCheck.InFrame = false;
		}
	}
	return MouseCheck;
}

function ShowAddXInfoBoxPhoto(PhotoId,Id2) {
	var myImage = new Image();
		myImage.Id = PhotoId;
		myImage.Caption = document.getElementById('PhotoCaption_'+PhotoId).value;
		myImage.PhotoObject = document.getElementById('OVERLAY_FRAME_'+Id2+'_'+PhotoId);
		myImage.PhotoDetail = document.getElementById('PhotoDetailUrl_'+PhotoId).value;
		myImage.name = document.getElementById('PhotoCaption_'+PhotoId).value;
		myImage.onload = ShowAddXInfoBox;
		myImage.onerror = loadFailure;
		myImage.src = document.getElementById('PhotoMediumUrl_'+PhotoId).value;
}

function ShowAddXInfoBox() {
	var Content = '<a href="'+this.PhotoDetail+'"><img src="'+this.src+'" alt="" /></a><br />'+this.Caption;
	var InfoBox = new InfoBoxObject();
		InfoBox.SetWidth(250);
		InfoBox.Show(this.PhotoObject, Content);
	InfoBoxHidden = false;
	CheckCursorInFrameTimeoutActive = false;
	var CloseButton = InfoBox.GetCloseButton();
		CloseButton.onclick = function() { InfoBox.Self().style.visibility = "hidden"; ShowInfoBoxSlow = true; InfoBoxHidden = true; };

	return true;
}
function loadFailure() {
	var Content = '<img src="/gfx/png/no_photo_100.png" alt="" /><br />'+this.Caption;
	var InfoBox = new InfoBoxObject();
		InfoBox.SetWidth(250);
		InfoBox.Show(this.PhotoObject, Content);
	InfoBoxHidden = false;
	CheckCursorInFrameTimeoutActive = false;
	var CloseButton = InfoBox.GetCloseButton();
		CloseButton.onclick = function() { InfoBox.Self().style.visibility = "hidden"; ShowInfoBoxSlow = true; InfoBoxHidden = true; };
	
	return true;
}

