var g_bIE = (document.all && !window.opera)?true:false;
var g_bSafari = /Safari/.test(navigator.userAgent);
//Detect IE old versions
var g_bIE5 = false;
var g_bIE6 = false;
var version=0
if (navigator.appVersion.indexOf("MSIE")!=-1)
{
	temp=navigator.appVersion.split("MSIE")
	version=parseFloat(temp[1])
}
if (version>=5.0 && version<6.0)g_bIE5 = true;
if (version>=6.0 && version<7.0)g_bIE6 = true;
var g_bIElt7 = g_bIE5 || g_bIE6;


//gegen framesets
if( window.top != window ) window.top.location = window.location;


// information about the current viewport
var vport;
// current mouse position, always tracked
var currMousePos_x = 0;
var currMousePos_y = 0;
// the current footnote div
var currDivID = '';
var footnoteOpenedPermanently = false;
var footnoteIsActive = false;
var skipClickGlobally = false;
var bodyPadding = 50;
var oFN = 0x0;

function openFootnoteAtMouse(which)
{
	if(document.getElementById && !footnoteOpenedPermanently)
	{
		var scr = getScrollXY();
		width = getCSSProperty(which, 'width');
		width = parseInt(width.replace(/px/g, ""));
		newLeft = 10+currMousePos_x;
		if(newLeft+width>vport.width - bodyPadding)
		{
			newLeft = vport.width - width - bodyPadding;
		}
		document.getElementById(which).style.left = newLeft + 'px';

		height = getCSSProperty(which, 'height');
		height = parseInt(height.replace(/px/g, ""));
		newTop = 10 + currMousePos_y +(document.all?scr[1]:0);
//		if(newTop+height>vport.height)
//		{
//			newTop = vport.height - height - bodyPadding;
//		}
		document.getElementById(which).style.top = newTop + 'px';

		document.getElementById(which).style.visibility = 'visible';
		currDivID = which;
		footnoteIsActive = true;
	}
}

function openFootnotePermanently(which)
{
	if(document.getElementById && !footnoteOpenedPermanently)
	{
		try
		{
			oFN = document.getElementById(which);
			putCloseLinkToFNoteFooter('Schlie'+entity('&szlig;')+'en');
			// show it
			oFN.style.visibility = 'visible';
			currDivID = which;
			footnoteOpenedPermanently = true;
		}
		catch(e)
		{
			;// noop
		}
	}
}

function closeFootnote(which)
{
	if(document.getElementById && !footnoteOpenedPermanently)
	{
		if(typeof(which)== 'object')
			which.style.visibility = 'hidden';
		else
			document.getElementById(which).style.visibility = 'hidden';
		footnoteOpenedPermanently = false;
	}
	footnoteIsActive = false
}


document.onclick = function(e)
{
	if(skipClickGlobally)
	{
		skipClickGlobally = false;
		return;
	}
    if(document.getElementById && document.getElementById(currDivID) && !footnoteIsActive)
    {
		changeFNoteFooterText('Zum Fixieren klicken');
        document.getElementById(currDivID).style.visibility = 'hidden';
		footnoteOpenedPermanently = false;
    }
}



document.onmousemove = function(e){
	if(document.all)
	{
		currMousePos_x = window.event.clientX;
		currMousePos_y = window.event.clientY;
	}
	else
	{
		currMousePos_x = e.pageX;
		currMousePos_y = e.pageY;
	}
}
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}
function init()
{
	vport = new viewport();
}

window.onresize = init;
window.onload = init;


function lock()
{
	skipClickGlobally = true;
}

function changeFNoteFooterText(txt)
{
	if(!oFN)return;
	// change bottom text in footnote
	for(i=0; i<oFN.childNodes.length; i++)
	{
		if(oFN.childNodes[i].className=='footnoteFooter')
		{
			// remove all childs
			while(oFN.childNodes[i].childNodes.length > 0)
			{
				oFN.childNodes[i].removeChild(oFN.childNodes[i].firstChild);
			}
			t = document.createTextNode(txt);
			oFN.childNodes[i].appendChild(t);
		}
	}
}

function putCloseLinkToFNoteFooter(txt)
{
	if(!oFN)return;
	// change bottom in footnote
	for(i=0; i<oFN.childNodes.length; i++)
	{
		if(oFN.childNodes[i].className=='footnoteFooter')
		{
			// remove all childs
			while(oFN.childNodes[i].childNodes.length > 0)
			{
				oFN.childNodes[i].removeChild(oFN.childNodes[i].firstChild);
			}
			if(document.createAttribute)// ie<6 doesn't understand
			{
				a = document.createElement('a');
				// no href, since ie won't support onclick then
				var of = document.createAttribute("onfocus");
				of.nodeValue = "this.blur();";
				a.setAttributeNode(of);
				a.onclick = function() {
					changeFNoteFooterText('Zum Fixieren klicken');
					document.getElementById(currDivID).style.visibility = 'hidden';
					footnoteOpenedPermanently = false;
					return false;
				};

				t = document.createTextNode(txt);
				a.appendChild(t);
				oFN.childNodes[i].appendChild(a);
			}
			else
			{
				t = document.createTextNode(txt + ' durch Klick au'+entity('&szlig;')+'erhalb');
				oFN.childNodes[i].appendChild(t);
			}
			// done it, quit the loop
			break;
		}
	}
}

// First paramter is a nodes id, second the CSS property you want
function getCSSProperty(sNodeId, sProperty)
{
	var oNode = document.getElementById(sNodeId);

	if(document.defaultView)
	{
		return document.defaultView.getComputedStyle(oNode, null).getPropertyValue(sProperty);
	}
	else if(oNode.currentStyle)
	{
		var sProperty = sProperty.replace(/-\D/gi, function(sMatch)
			{
				return sMatch.charAt(sMatch.length - 1).toUpperCase();
			}
		);

		var val = oNode.currentStyle[sProperty];
		if(val=="auto" || val=="" )
		{
			if(sProperty == 'height')
			{
				return oNode.offsetHeight+'px';
			}
			else if(sProperty == 'width')
			{
				return oNode.offsetWidth+'px';
			}
			else
			{
				return val;
			}

		}
		else
		{
			return val;
		}
	}
	else return null;
}

function getElementsByClass(searchClass,node,tag)
{
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (var i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function entity(str, mode)
{
	str = (str) ? str : "";
	mode = (mode) ? mode : "string";

	var e = document.createElement("div");
	e.innerHTML = str;

	if (mode == "numeric") {
		return "&#" + e.innerHTML.charCodeAt(0) + ";";
	}
	else if (mode == "utf16") {
		var un = e.innerHTML.charCodeAt(0).toString(16);
		while (un.length < 4) un = "0" + un;
		return "\\u" + un;
	}
	else return e.innerHTML;
}

function viewport()
{
	var width,height;
	if (self.innerHeight) // all except Explorer
	{
		this.width = self.innerWidth;
		this.height = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		this.width = document.documentElement.clientWidth;
		this.height = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		this.width = document.body.clientWidth;
		this.height = document.body.clientHeight;
	}
}


// mem leaks?
function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
	{
		obj.addEventListener( type, fn, false );
	}
	else if (obj.attachEvent)
	{
		// IE
		obj["e"+type+fn] = fn;
		//window.event.cancelBubble
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
	else
	{
		obj["on"+type] = obj["e"+type+fn];
	}
}

function unFocusLinks()
{
	var linkObjs = document.links;
	for(var i=0; i<linkObjs.length; i++)
	{
		var link = linkObjs[i];
		link.onfocus=function(){this.blur();};
	}
}
addEvent(window, 'load', unFocusLinks);

function addTextFaders()
{
	if(g_bIElt7) return;
	var topDiv = document.createElement('div');
	topDiv.className = 'textFaders';
	topDiv.id = 'topFader';
	document.getElementsByTagName('body')[0].appendChild(topDiv);
	var bottomDiv = document.createElement('div');
	bottomDiv.className = 'textFaders';
	bottomDiv.id = 'bottomFader';
	document.getElementsByTagName('body')[0].appendChild(bottomDiv);
}
addEvent(window, 'load', addTextFaders);



