var popUpWindows = new Array();		// array, das alle durch die Seite gešffneten Window-Elemente enthŠlt (auch nachdem das jeweilige Fenster geschlossen wurde)


function sortNumber(a,b)
{	return a-b;
}

function h2d(h)
{	return parseInt(h,16);
}

function scrollWindow()
{	if (location.hash != "")
	{	window.scrollBy(0,-100);
	}
}

function highlightWord(node,word,color,exact)
{	if (node.hasChildNodes)
	{	var hi_cn;
		
		for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++)
		{	highlightWord(node.childNodes[hi_cn],word,color,exact);
		}
	}

	if (node.nodeType == 3)
	{	tempNodeVal = node.nodeValue.toLowerCase();
		tempWordVal = word.toLowerCase();
		
		var isexact		= new RegExp("\\b" + word + "\\b", "i");
				
		if ((exact == true && isexact.test(node.nodeValue)) || (exact == false && tempNodeVal.indexOf(tempWordVal) != -1))
		{	pn = node.parentNode;
			clN = "highlight_" + color;
		
			if (pn.className != clN && pn.nodeName != 'TEXTAREA')
			{	nv = node.nodeValue;
				ni = tempNodeVal.indexOf(tempWordVal);
			
				before = document.createTextNode(nv.substr(0,ni));
				docWordVal = nv.substr(ni,word.length);
				after = document.createTextNode(nv.substr(ni+word.length));
				
				hiwordtext = document.createTextNode(docWordVal);
				hiword = document.createElement("span");
				hiword.className = clN;
				hiword.appendChild(hiwordtext);
			
				pn.insertBefore(before,node);
				pn.insertBefore(hiword,node);
				pn.insertBefore(after,node);
				pn.removeChild(node);
			}
		}
	}
}

function unhighlightWord(node,word,color)		// verbessern !!
{	if (node.hasChildNodes)
	{	var hi_cn;
		
		for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++)
		{	unhighlightWord(node.childNodes[hi_cn],word,color);
		}
	}

	if (node.nodeType == 3)
	{	tempNodeVal = node.nodeValue.toLowerCase();
		tempWordVal = word.toLowerCase();
		
		if (tempNodeVal.indexOf(tempWordVal) != -1)
		{	pn = node.parentNode;
			clN = "highlight_" + color;
			
			if (pn.className == clN)
			{	prevSib = pn.previousSibling;
				nextSib = pn.nextSibling;
				nextSib.nodeValue = prevSib.nodeValue + node.nodeValue + nextSib.nodeValue;
				prevSib.nodeValue = '';
				node.nodeValue = '';
			}
		}
	}
}

function addEvent(elm, evType, fn, useCapture)
{	if (elm.addEventListener)
	{	elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent)
	{	var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else
	{	elm['on' + evType] = fn;
	}
}


function addStyleSheet()
{	var userAgent = window.navigator.userAgent.toLowerCase();
	var platform = window.navigator.platform.toLowerCase();
    var href = "";
    
    if (platform.indexOf('win') != -1)
    {	href = "/aeruginis/system/themes/win.css";
    }
    else if (platform.indexOf('mac') != -1)
    {	href = "/aeruginis/system/themes/mac.css";
    }
    else if (platform.indexOf('unix') != -1 || platform.indexOf('linux') != -1 || platform.indexOf('sun') != -1)
    {	href = "";
    }
    
    if (userAgent.indexOf('firefox') != -1)
    {	href = "/aeruginis/system/themes/win.css";
    }
    
	document.getElementById("platform_specific_css").href = href;
}


function closeWin(i)
{	if (popUpWindows[i] != null)
	{	if (!popUpWindows[i].closed)
		{	popUpWindows[i].close();
		}
	}
}

// HOWTO add closebutton
//	<script language="JavaScript">
//	<!--
//	if (window.opener) document.write('<strong><a href="#" onclick="self.close();">Close this window</a></strong>');
//	//-->
//	</script>

// HOWTO use popuplinks
//	A normal link									- <a href="xyz">
//	A popup link									- <a href="xyz" rel="popup">
//	A popup in standard mode (toolbars included) 	- <a href="xyz" rel="popup standard 800 600">
//	A popup in console mode (no toolbars etc) 		- <a href="xyz" rel="popup console 800 600">
//	A popup link for fullscreen 					- <a href="xyz" rel="popup fullScreen">
//	A popup in standard mode (icon suppressed) 		- <a href="xyz" rel="popup standard 800 600 noicon">

function popUpWin(url, type, strWidth, strHeight, strLeft, strTop)
{	var i = 0;
	var j = 0;
	var tools ="";
	
	if (strLeft == "-1")
	{	strLeft = popUpWindows.length * 50;
	}
	
	if (strTop == "-1")
	{	strTop = popUpWindows.length * 50;
	}
	
	type = type.toLowerCase();
	
	if (type == "fullscreen")
	{	strWidth	= screen.availWidth;
		strHeight	= screen.availHeight;
		strLeft		= 0;
		strTop		= 0;
	}
	
	if (type == "standard")
	{	tools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width="+strWidth+",height="+strHeight+",left="+strLeft+",top="+strTop+"";
	}
	
	if (type == "console" || type == "fullscreen")
	{	tools = "resizable,toolbar=no,location=no,scrollbars=no,width="+strWidth+",height="+strHeight+",left="+strLeft+",top="+strTop+"";
	}
		
	popUpWindows.push(window.open(url, url, tools));	
	
	for (j in popUpWindows)
	{	if (popUpWindows[i] != null)
		{	if (!popUpWindows[i].closed)
			{	popUpWindows[j].focus();
			}
		}
	}
}

function doPopUp(e)
{	//set defaults - if nothing in rel attrib, these will be used
	var type	= "standard";
	var width	= "780";
	var height	= "580";
	var left	= "0";
	var top		= "0";
	
	//look for parameters
	attribs = this.rel.split(" ");
	if (attribs[1]!=null) {type		= attribs[1];}
	if (attribs[2]!=null) {width	= attribs[2];}
	if (attribs[3]!=null) {height	= attribs[3];}
	if (attribs[4]!=null) {left		= attribs[4];}
	if (attribs[5]!=null) {top		= attribs[5];}
	
	//call the popup script
	popUpWin(this.href,type,width,height,left,top);

	//cancel the default link action if pop-up activated
	if (window.event) 
	{	window.event.returnValue = false;
		window.event.cancelBubble = true;
	} 
	else if (e) 
	{	e.stopPropagation();
		e.preventDefault();
	}
}

function findPopUps()
{	var popups = document.getElementsByTagName("a");

	for (i=0;i<popups.length;i++)
	{	if (popups[i].rel.indexOf("popup")!=-1)
		{	// attach popup behaviour
			popups[i].onclick = popups[i].onkeypress = doPopUp;
					
			// add popup indicator
			if (popups[i].rel.indexOf("noicon")==-1)
			{	//popups[i].style.backgroundImage = "url(pop-up.gif)";
				//popups[i].style.backgroundPosition = "0 center";
				//popups[i].style.backgroundRepeat = "no-repeat";
				//popups[i].style.paddingLeft = "15px";
			}
		
			// add info to title attribute to alert fact that it's a pop-up window
			popups[i].title = popups[i].title + " [Opens in pop-up window]";
		}
	}
}


/*************************
	virtualKeyboard
*************************/
function virtualKeyboard_focus(event)
{	var virtualKeyboards = document.getElementsByName("virtualKeyboard");
	var foc = event.target.parentNode;

	for (var i = 0; i < virtualKeyboards.length; i++)
	{	for (var j = 0; j < virtualKeyboards[i].childNodes.length; j++)
		{	if (virtualKeyboards[i].childNodes[j].nodeName == "DIV")
			{	if (virtualKeyboards[i] == event.target.parentNode)
				{	virtualKeyboards[i].childNodes[j].style.visibility="visible";	// show focused virtualKeyboard
				}
				else
				{	virtualKeyboards[i].childNodes[j].style.visibility="hidden";	// hide all others
				}
			}
		}
	}
}

function virtualKeyboard_close(event)
{	var virtualKeyboard = event.target.parentNode.parentNode;						// close focused virtualKeyboard

	for (var i = 0; i < virtualKeyboard.childNodes.length; i++)
	{	if (virtualKeyboard.childNodes[i].nodeName == "DIV")
		{	virtualKeyboard.childNodes[i].style.visibility="hidden";
		}
	}
}

function virtualKeyboard_blur()
{	var virtualKeyboards = document.getElementsByName("virtualKeyboard");			// hide all virtualKeyboards
	
	for (var i = 0; i < virtualKeyboards.length; i++)
	{	for (var j = 0; j < virtualKeyboards[i].childNodes.length; j++)
		{	if (virtualKeyboards[i].childNodes[j].nodeName == "DIV")
			{	virtualKeyboards[i].childNodes[j].style.visibility="hidden";
			}
		}
	}
}

function virtualKeyboard_click(event)
{	var elm			= document.getElementById(event.target.parentNode.name);
	var startTag	= event.target.value;
	var endTag		= '';
	var length 		= 0;
	
	elm.focus();
  
  	if (typeof document.selection != 'undefined')		/* für Internet Explorer */
  	{	var range = document.selection.createRange();
    	var insText = range.text;
    					
    	if (endTag == '')
    	{	range.text = startTag;		/* insText wird überschrieben! */
    		length = startTag.length;
    	}
    	else
    	{	range.text = startTag + insText + endTag;
    		length = startTag.length + insText.length + endTag.length;
    	}
    					
    	if (insText.length == 0)
    	{	range.move('character', -endTag.length);
    	}
    	else
    	{	range.moveStart('character', length);      
    	}
    	range.select();
  	}
 	else if(typeof elm.selectionStart != 'undefined')	/* für neuere auf Gecko basierende Browser */
 	{	var start	= elm.selectionStart;
		var end		= elm.selectionEnd;
    	var insText = elm.value.substring(start, end);
    					
    	if (endTag == '')
    	{	elm.value	= elm.value.substr(0, start) + startTag + elm.value.substr(end);
    		length = startTag.length;
    	}
    	else
    	{	elm.value	= elm.value.substr(0, start) + startTag + insText + endTag + elm.value.substr(end);
       		length = startTag.length + insText.length + endTag.length;
       	}
    					
    	var pos;
    	if (insText.length == 0)
    	{	pos = start + startTag.length;
    	}
    	else
    	{	pos = start + length;
    	}
    	
    	elm.selectionStart = pos;
    	elm.selectionEnd = pos;
  	}
  	else	/* für die übrigen Browser */
  	{	var pos = elm.value.length;
    }
}

function virtualKeyboard_load()
{	var keyboards = document.getElementsByName("virtualKeyboard");
	
	var charCodes = new Array();
	charCodes[1] = '0391'; // value='Α' 
	charCodes[2] = '0392'; // value='Β' 
	charCodes[3] = '0393'; // value='Γ'
	charCodes[4] = '0394'; // value='Δ'
	charCodes[5] = '0395'; // value='Ε'
	charCodes[6] = '0396'; // value='Ζ'
	charCodes[7] = '0397'; // value='Η'
	charCodes[8] = '0398'; // value='Θ'
	charCodes[9] = '0399'; // value='Ι'
	charCodes[10] = '039A'; // value='Κ'
	charCodes[11] = '039B'; // value='Λ'
	charCodes[12] = '039C'; // value='Μ'
	charCodes[13] = '039D'; // value='Ν'
	charCodes[14] = '039E'; // value='Ξ'
	charCodes[15] = '039F'; // value='Ο'
	charCodes[16] = '03A0'; // value='Π'
	charCodes[17] = '03A1'; // value='Ρ'
	charCodes[18] = '03A3'; // value='Σ'
	charCodes[19] = '03F9'; // value='Ϲ'
	charCodes[20] = '2D4E'; // value='ⵎ'	
	charCodes[21] = '03A4'; // value='Τ'
	charCodes[22] = '03A5'; // value='Υ'
	charCodes[23] = '0474'; // value='Ѵ'
	charCodes[24] = '03A6'; // value='Φ'
	charCodes[25] = '03A7'; // value='Χ'
	charCodes[26] = '03A8'; // value='Ψ'
	charCodes[27] = '03A9'; // value='Ω'
	charCodes[28] = '0460'; // value='Ѡ'
	charCodes[29] = '21BA'; // value='↺'
	charCodes[30] = '21BB'; // value='↻'
	charCodes[31] = '2016'; // value='‖'
	charCodes[32] = '2022'; // value='•'
	charCodes[33] = '25CB'; // value='○'
	charCodes[34] = '2040'; // value='⁀'
	charCodes[35] = '00C6'; // value='Æ'
	charCodes[36] = '0404'; // value='Є'
	charCodes[37] = '0387'; // value='·'
	charCodes[38] = '00AB'; // value='«'
	charCodes[39] = '00BB'; // value='»'
	
	
	for (var i = 0; i < keyboards.length; i++)
	{	var input = null;
		var keyContainer = document.createElement("div");
		keyContainer.className = "keyContainer";
		keyContainer.tabIndex = -1;
		
		for (c = 1; c < charCodes.length; c++)
		{	var key = document.createElement("input");
			key.type = "button";
			key.className = "virtualKey";
			key.value = String.fromCharCode(h2d(charCodes[c]));
			key.tabIndex = -1;
			key.onclick = virtualKeyboard_click;
			keyContainer.appendChild(key);
		}
		
		var close = document.createElement("input");
		close.type = "button";
		close.name = "virtualKey";
		close.value = "Close";
		close.tabIndex = -1;
		close.onclick = virtualKeyboard_close;
		keyContainer.appendChild(close);
		
		keyboards[i].appendChild(keyContainer);
					
		for (var j = 0; j < keyboards[i].childNodes.length; j++)
		{	var elm = keyboards[i].childNodes[j]; 
			
			if (elm.nodeName == "INPUT" || elm.nodeName == "TEXTAREA")
			{	input = elm;
				elm.alt = "keyboardref";
				keyContainer.name = elm.getAttribute("name");
				
				addEvent(elm, 'focus', virtualKeyboard_focus, false);
				//addEvent(elm, 'blur', virtualKeyboard_blur, false);
			}
		}
			
		if (keyboards[i].getAttribute("id") == "focus")
		{	input.focus();
		}
	}
	
	for (var f = 0; f < document.forms.length; f++)
	{	for (var e = 0; e < document.forms[f].elements.length; e++)
		{	if (document.forms[f].elements[e].className != "virtualKey" && document.forms[f].elements[e].alt != "keyboardref")  // schließt die virtualKeys und die Eingabefelder (die mit virtualKeyboards ausgestattet sind) aus
			{	addEvent(document.forms[f].elements[e], 'focus', virtualKeyboard_blur, false);
			}
		}
	}
}


/*************************
		iPageBar
*************************/
function pb_jump_onclick(pb_count)
{	var pbj		 = "pb_jump_" + pb_count;
	var pbi	 	 = "pb_jump_input_" + pb_count;
	var pb_jump	 = document.getElementById(pbj);
	var pb_input = document.getElementById(pbi);
	
	if (pb_jump.style.visibility != "visible")
	{	pb_jump.style.visibility = "visible";
		pb_input.focus();
		pb_input.select();
	}
	else
	{	pb_jump.submit();
	}
}



/*************************
		iCoinImage
*************************/
function iCoinImage_load()
{	var iCoinImages = document.getElementsByName("coinimage");

	for (var i = 0; i < iCoinImages.length; i++)
	{	addEvent(iCoinImages[i], 'mousemove', iCoinImage_onmousemove, false);
		//addEvent(iCoinImages[i], 'click', iCoinImage_onclick, false);			// wird wegen Parameterübergabe inline aufgerufen!
		iCoinImages[i].style.visibility = "visible";
	}
}

function iCoinImage_onmousemove(event)
{	if (!event || event == "undefined")
	{	event = window.event;
	}
	
	if (event.srcElement != null)
	{	var elm = event.srcElement;
	}
	else if (event.target != null)
	{	var elm = event.target;
	}
	
	if (elm != null)
	{	var posX;
		
		if (event.offsetX)
		{	posX = event.offsetX;
		}
		else
		{	posX = event.layerX;			// img ist relativ positioniert, layerX bezieht sich ab Netscape 6 etc auf die ober linke Ecke des Bildes... ältere Browser unterstützen das nicht !!
		}

		if (posX < (elm.width/2))
		{	elm.title = "[Opens in pop-up window]";
		}
		else
		{	elm.title = "[Opens DetailView]";
		}
	}
}

function iCoinImage_onclick(event, width, height)
{	if (!event || event == "undefined")
	{	event = window.event;
	}

	if (event.srcElement != null)
	{	var elm = event.srcElement;
	}
	else if (event.target != null)
	{	var elm = event.target;
	}
	
	if (elm != null)
	{	if (event.shiftKey == 1)				// shift und linker Mausklick fŸgt MŸnze zu Auswahl hinzu ! 
		{	selection_update(elm.id);
		}
		else									// ohne shift wird je nach Cursor-Position entweder das Bild in Originalgrš§e in einem neuen Fenster gešffnet oder die Detailansicht der einzelnen MŸnze angezeigt.
		{	var posX;
		
			if (event.offsetX)
			{	posX = event.offsetX;
			}
			else
			{	posX = event.layerX;			// img ist relativ positioniert, layerX bezieht sich ab Netscape 6 etc auf die ober linke Ecke des Bildes... ältere Browser unterstützen das nicht !!
			}
			
			if (posX < (elm.width/2))
			{	popUpWin("/aeruginis/images/" + elm.id,"console",width,height,"-1","-1");
			}	
			else	
			{	window.location.assign("/aeruginis/Sammlung/Detail.php?ae_id=" + elm.id);
			}
		}
	}
}
