﻿/* START GOOGLE ANALYTICS */
if (window.urchinTracker)
{
	_uacct = "UA-2482611-2";
	_udn="romanfrontiers.org";
	Event.observe(document,'dom:loaded',function(){urchinTracker();});
}
/* END GOOGLE ANALYTICS */
Event.observe(document,'dom:loaded', function(){
	$('page_wrapper').insert(
		'<div id="map_control"><a href="javascript:toggleMap();">'
		+ ( mapVisibility ? "Skizze ausblenden" : "Skizze einblenden" )
		+ '</a></div>'
		);
	
	mapElement=$('map');
	if (!mapElement) return;
	$$('#topnavimagemap area').invoke('observe', 'mousemove', function( e ) {
		var rel = $(this).readAttribute('rel');
		if (window.tooltipElem)
		{
			if (tooltipElem.readAttribute('rel')==rel)
			{
				if (e) cursor=e.pointer();
				tooltipElem.style.left=(cursor.x<600?cursor.x+10:cursor.x-220)+"px";
				tooltipElem.style.top =cursor.y+3+"px";
				return;
			}
			else
			{
				tooltipElem.remove();
				tooltipElem=false;
			}
		}
		
		cursor=e.pointer();
		var text;
		if (!siteInformations[rel] || !siteInformations[rel][0]) return;
		text  = "<b>"+siteInformations[rel][0].replace(/\+/g," ")+"</b>";
		if (siteInformations[rel][1].length)
			text += "<br>\n" + siteInformations[rel][1].replace(/\+/g," ");
		if (siteInformations[rel][2].length)
			text += "<br>\n<br>\n" + siteInformations[rel][2].replace(/\+/g," ");

		text = "<div id='tooltip' rel='"+rel+"' style='position:absolute;left:"+(cursor.x<600?cursor.x+10:cursor.x-220)+"px;top:"+(cursor.y+3)+"px'>"+text+"</div>";
		var bodyElem = $$('body')[0].insert(text);
		tooltipElem = $('tooltip');
	}).invoke('observe', 'mouseout', function(){
		if (window.tooltipElem)
		{
			tooltipElem.remove();
			tooltipElem=false;
		}
	}).invoke('observe', 'click', function(e){
		var rel = parseInt($(this).readAttribute('rel'));
		if (rel) window.location.href="/php/site.php?ID="+rel;
		else alert('Diese Fundstelle ist noch nicht in der Datenbank!');
		e.stop();
	}).each(function(a){a.href='#';});
	if (window.currPosTopNav)
	{
		miniMapMarkerPos = [currPosTopNav[0]+165-15,currPosTopNav[1]+100-28];
		mapElement.insert({
			after:
				new Element('img', { id: 'minimapMarker', src: '/pix/marker.'+(Prototype.Browser.IE?'gif':'png') }).setStyle({
					position:'absolute',
					width: 71,
					height: 24,
					left: miniMapMarkerPos[0]+"px",
					top:  miniMapMarkerPos[1]+"px"
					})
			});
		minimapMarkerElem = $('minimapMarker');
		if (window.mapVisibility) Event.observe(document,'mousemove', checkVisibility );
		else minimapMarkerElem.hide();
	}
});

function slowlyHide()
{
	var time = ((new Date).getTime()-startHidingAt);
	if (time>300)
	{
		minimapMarkerElem.setStyle({opacity: 0});
		minimapMarkerElem.hide();
		startHidingAt=false;
	}
	else
	{
		minimapMarkerElem.setStyle({opacity: 1-time/300});
		showHideTimeOut = setTimeout(slowlyHide,10);
	}
}

function slowlyShow()
{
	var time = ((new Date).getTime()-startShowingAt);
	if (time>300)
	{
		minimapMarkerElem.setStyle({opacity: 1});
		startShowingAt=false;
	}
	else
	{
		minimapMarkerElem.setStyle({opacity: time/300});
		showHideTimeOut = setTimeout(slowlyShow,10);
	}
}

function checkVisibility(e)
{
	if (!e) return;
	var pointer = e.pointer();
	var x = pointer.x-(miniMapMarkerPos[0]+35);
	var y = pointer.y-(miniMapMarkerPos[1]+12);
	if (x*x+4*y*y>3000)
	{
		if (window.markerVisibility=='hidden')
		{
			markerVisibility='shown';
			if (window.showHideTimeOut) clearTimeout(showHideTimeOut);
			startShowingAt = window.startHidingAt ? (new Date).getTime()*2-300-window.startHidingAt : (new Date).getTime();
			showHideTimeOut = setTimeout(slowlyShow,10);
			minimapMarkerElem.show();
		}
	}
	else
	{
		if (window.markerVisibility!='hidden')
		{
			markerVisibility='hidden';
			if (window.showHideTimeOut) clearTimeout(showHideTimeOut);
			startHidingAt = window.startShowingAt ? (new Date).getTime()*2-300-window.startShowingAt : (new Date).getTime();
			showHideTimeOut = setTimeout(slowlyHide,10);
		}
	}
}

function toggleMap()
{
	// toggle the visibility of the map
	mapVisibility = !mapVisibility;
	
	//change the view accordingly
	document.getElementById('page_wrapper').style.backgroundImage = "url(" + (mapVisibility ? "/pix/header.gif" : "/pix/header_nomap.gif") + ")";
	document.getElementById('page_wrapper').style.paddingTop = (mapVisibility ? "241px":"98px" );
	if (document.getElementById("side_menu")) document.getElementById("side_menu").style.top = (mapVisibility ? "270px" : "118px" );
	document.getElementById("map").style.display = (mapVisibility ? "block" : "none" );
	document.getElementById("map_control").innerHTML = (mapVisibility ? "<a href='javascript:toggleMap();'>Skizze ausblenden</a>" : "<a href='javascript:toggleMap();'>Skizze einblenden</a>" );
	if (window.minimapMarkerElem)
	{
		if (window.markerVisibility!='hidden') (mapVisibility ? minimapMarkerElem.show() : minimapMarkerElem.hide());
		(mapVisibility ? Event.observe : Event.stopObserving)(document,'mousemove', checkVisibility);
	}
	setCookie("mapVisibility", mapVisibility,0,"/");
}

function enlarge(image)
{
	window.open(image,"imageDisplayWindow","width=400,height=500,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,toolbar=no");
}

function enlargeme(nr, nW, nH, nw, nh, signature, copyright )
{
	var sl, st;
	myPic = document.getElementById("picdiv"+nr);
	W=nW;
	H=nH;
	w=nw;
	h=nh;
	l=getX(myPic);
	t=getY(myPic);
	ratio = W/H;
	window_end = getViewDimensions();
	W=Math.min(W,window_end[0]-12);
	H=Math.round(H*W/nW);
	if (H>window_end[1]-52)
	{
		H=Math.min(H,window_end[1]-52);
		W=Math.round(H/nH*nW);
	}
	if (document.documentElement && document.documentElement.scrollTop)
	{
		st = document.documentElement.scrollTop;
		sl = document.documentElement.scrollLeft;
	}
	else if (document.body)
	{
		st = document.body.scrollTop;
		sl = document.body.scrollLeft;
	}
	window_end[0] += sl-12;
	window_end[1] += st-47;
	L=Math.round(Math.max(sl+5,Math.min(window_end[0]-W,l-(W-w)/2)));
	T=Math.round(Math.max(st+5,Math.min(window_end[1]-H,t-(H-h)/3)));
	if (typeof largePictureDiv != "object")
	{
		largePictureDiv = document.createElement("div");
		largePictureDiv.setAttribute("id","largePicDiv")
		document.body.appendChild(largePictureDiv);
	}
	else
	{
		largePictureDiv.style.display="none";
	}
	largePictureDiv.style.position = "absolute";
	largePictureDiv.style.width = w+"px";
	largePictureDiv.style.height = h+"px";
	largePictureDiv.style.left = l+"px";
	largePictureDiv.style.top = t+"px";
	largePictureDiv.innerHTML = "<img style='position:relative;' onclick='unenlargeme();' src='"+myPic.firstChild.src+"'><p>"+signature+(copyright?"<br>&copy; "+copyright:"")+"</p>";
	PictureStatus = "growing";
	to = setTimeout("enlargemeabit(0);",20);
	
}

function enlargemeabit(nr)
{
	var new_w, new_h, new_l, new_t;
	nr++;
	//get the size of the frame
	new_w = Math.round(w+(W-w)/10*nr);
	new_h = Math.round(h+(H-h)/10*nr);
	new_l = Math.round(l+(L-l)/10*nr);
	new_t = Math.round(t+(T-t)/10*nr);
	//get the size of the image
	img_w=new_w;
	img_h=Math.round(img_w/ratio);
	if (img_h<new_h)
	{
		img_h=new_h;
		img_w=Math.round(new_h*ratio);
	}
	largePictureDiv.style.width  = new_w+"px";
	if (PictureStatus=="growing")
		largePictureDiv.style.height = new_h+Math.round(3.5*nr)+"px";
	else
		largePictureDiv.style.height = new_h+Math.round(3.5*(10-nr))+"px";
	largePictureDiv.firstChild.style.width  = img_w+"px";
	largePictureDiv.firstChild.style.height = img_h+"px";
	largePictureDiv.firstChild.style.left   = Math.round((new_w-img_w)/2)+"px";
	largePictureDiv.firstChild.style.top    = Math.round((new_h-img_h)/2)+"px";
	largePictureDiv.style.left   = new_l+"px";
	largePictureDiv.style.top    = new_t+"px";
	if (nr==1)
	{
		largePictureDiv.style.display = "block";
	}
	if (nr<10)
	{
		to = window.setTimeout("enlargemeabit("+nr+");",20);
	}
	else
	{
		if (PictureStatus=="shrinking")
		{
			largePictureDiv.style.display="none";
			PictureStatus="small";
		}
		else
		{
			PictureStatus="large";
			document.onclick=unenlargeme;
		}
	}
}

function unenlargeme()
{
	var x;
	if (PictureStatus!="large")
	{
		return;
	}
	Large = false;
	x=W; W=w; w=x;
	x=H; H=h; h=x;
	x=T; T=t; t=x;
	x=L; L=l; l=x;
	PictureStatus="shrinking";
	enlargemeabit(0);
	document.onclick=null;
	return true;
}

function getX( obj )
{ 
	var left = 0;
	if (obj.x) left += obj.x;
	else do left += obj.offsetLeft;
	while (obj = obj.offsetParent);
	return left;
} 

function getY( obj )
{ 
	var top = 0;
	if (obj.y) top += obj.y;
	else do top += obj.offsetTop;
	while (obj = obj.offsetParent);
	return top;
}


function getViewDimensions ()
{
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' )
	{
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
	{
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
	{
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return Array(myWidth,myHeight);
}


function arrangeElements()
{
	var divs = $$("#datenblatt>div");
	var width = 0, height = 0;
	var dims = new Array();
	
	for(var i = 0; i<divs.length; i++)
	{
		if (divs[i].down() && divs[i].down().readAttribute('rel')=='remove') divs[i].down().remove();
		divs[i].style.position = "absolute";
		dims[i] = divs[i].getDimensions();
		width = Math.max(width, dims[i].width);
		height += dims[i].height;
	}
	
	if (window.freNumColumns) {
		var cols = freNumColumns;
	}
	else
	{
		var cols = Math.max(Math.floor((getViewDimensions()[0]-80)/width),1);
	}
	var avgHeight = Math.max(150, Math.round(height/cols+10) );
	
	var col = 0;
	var currHeight = 0;
	var lastHeading = "";
	
	var maxColHeight = 0;
	for(var i = 0; i<divs.length; i++)
	{
		/*
		if (currHeight>0 && currHeight+dims[i].height > 1.3*avgHeight)
		{
			currHeight = 0;
			col++;
		}
		*/
		if (divs[i].down() && divs[i].down().tagName=="H1")
		{
			lastHeading = divs[i].down().innerHTML;
		}
		else if (currHeight==0 && lastHeading)
		{
			new Insertion.Top(divs[i], "<h1 rel='remove'>"+lastHeading+" <small style='font-size:80%'>(Fortsetzung)</small></h1>");
		}
		divs[i].style.left     = col*width + "px";
		divs[i].style.top      = currHeight + "px";
		
		currHeight+=divs[i].getHeight();
		maxColHeight = Math.max(currHeight, maxColHeight);
		if (currHeight>avgHeight && col<cols)
		{
			currHeight = 0;
			col++;
		}
	}
	$("datenblatt").style.height=maxColHeight+"px";
	//$("datenblatt").style.width=(col+1)*width+"px";
	//$("datenblatt").style.margin="0 auto";
}
