var cPath = (window.getRelativeWebRoot ? window.getRelativeWebRoot() : '') + 'js/';
document.write ('<SCR' + 'IPT LANGUAGE="JavaScript1.2" SRC="'+ cPath +'Lib/BPage.js" TYPE="text/javascript"><\/SCR' + 'IPT>');

var nPageID = 0;
var oPages = null;
var bPageLoaded = false;

function onPageLoad() {
	with (navigator) if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.pgW=innerWidth; document.pgH=innerHeight; onresize=onPageResize; 
	}
	initMenus();
	initPages();
	if (window.initFlashSound) initFlashSound();
	if (window.onLoad) window.onLoad();
	if (oMatrix) oMatrix.onPageLoad();
	if (oSound) oSound.load('page');
	bPageLoaded = true;
}

function onPageResize() {
	with (navigator) if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
		if (document.pgW && (innerWidth!=document.pgW || innerHeight!=document.pgH)) location.reload();
	}
	setSoundPosition();
}

function initFlashSound() {
	setSoundPosition();
	if (window.FlashSound) {
		oSound = new FlashSound();
		oSound.getFlash();
	}
}

function setSoundPosition() {
	var aSoundPos = new Array(10, -60);
	var oL = getLayer ('Sound');
	if (oL) {
		var nWindowHeight = (window.innerHeight ? window.innerHeight : document.body.clientHeight);
		if (aSoundPos[0] < 0) aSoundPos[0] = aSoundPos[0] + (window.innerWidth ? window.innerWidth : document.body.clientWidth);
		if (aSoundPos[1] < 0) aSoundPos[1] = aSoundPos[1] + (window.innerHeight ? window.innerHeight : document.body.clientHeight);
		oL.moveTo (aSoundPos[0], aSoundPos[1]);
		oL.set ('visibility', true);
	}
}

function Winamp() {
	oWin = window.open ('winamp.php', 'Winamp', 'status=no,toolbar=no,menubar=no,location=no,resizable=no,width=276,height=30');
	if (oWin && oWin.focus) oWin.focus();
}

function addSID (cURL) {
	if (SID > ' ') cURL += (cURL.indexOf('?') >= 0 ? '&' : '?') + 'SID=' + SID;
	return (cURL);
}

function QueryString (cParameterName, oDocument) {
	var aParams, i, nLength;
	
	cParameterName = cParameterName.toLowerCase();
	nLength = cParameterName.length;
	if (oDocument == null) oDocument = document;
	aParams = oDocument.location.search.substr(1).split("&");
	for (i=0; i<aParams.length; i++)
		if (aParams[i].toLowerCase().substr(0, nLength+1) == cParameterName + '=')
			return (aParams[i].substr(nLength+1));

	return ('');
}

function getDocumentImage (cImageName) {
			var oImg = document.images[cImageName];
			if ((oImg == null || oImg+''=='undefined') && document.layers) {
				for (var i=0; i<document.layers.length; i++) {
					oImg = document.layers[i].document.images[cImageName];
					if (oImg != null && oImg+''!='undefined') return (oImg);
				}
			}
			return (oImg);
		}

//**************************************************************************************
// Pages
//**************************************************************************************
function createPages (bInteractive) {
	oPages = new Pages(bInteractive);
}

function initPages() {
	if (oPages) oPages.onLoad();
}


function Pages (bInteractive) {
	this.aPages 			= new Array();
	this.bInteractive = bInteractive;
	this.bHasPageList = true;
	
	this.onLoad 								= Pages_onLoad;
	this.sendBackCollidingPages	= Pages_sendBackCollidingPages;
	this.select									= Pages_select;
	this.createList							= Pages_createList;
	
	this.onHashSection = function (nSectionID) {
		for (var i=0; i<aSectionIDs.length; i++) if (aSectionIDs[i] != nSectionID) this.select (i+1, 0);

		for (var i=0; i<aSectionIDs.length; i++) if (aSectionIDs[i] == nSectionID) this.select (i+1, 1);
	}
}


function Pages_onLoad () {
	this.bHasPageList = (getLayer('PageList') != null);

	var oLastPageLayer = this.aPages[this.aPages.length-1].oLayer;
	var nMaxPos = (window.innerWidth ? window.innerWidth : document.body.clientWidth) - 210;
	nMaxPos = Math.min (nMaxPos, 110 + 3*210);
	
	this.nMaxZIndex = this.aPages.length*2 + 2;
	
	var bCorrectPosition = (oLastPageLayer.get('left') > nMaxPos);
	for (var i=0; i<this.aPages.length; i++) this.aPages[i].onLoad(bCorrectPosition);
	this.createList();
	
	var cHash = window.location.hash;
	if (cHash.substr (0, 8) == '#Section') {
		var nSectionID = cHash.substr(8);
		if (nSectionID > 0) setTimeout ('oPages.onHashSection (' + nSectionID + ')', 100);
	}
}


function Pages_select (nID, bFront) {
	var oPage = this.aPages[nID-1];
	if (oPage) {
		if (Pages_select.arguments.length == 2) 
			oPage.setFront (bFront);
		else
			oPage.setFront (!oPage.bFront);
	}
	if (!bPageLoaded) onPageLoad();
}

function Pages_sendBackCollidingPages (oPage) {
	var oL = oPage.oLayer;
	var aRect = new Rect (oL.get('left'), oL.get('top'), 200, 500);
	
	for (var i=0; i<this.aPages.length; i++) 
		if (this.aPages[i].bFront && this.aPages[i] != oPage) {
			oL = this.aPages[i].oLayer;
			var aOtherRect = new Rect (oL.get('left'), oL.get('top'), 200, 500);
			if (aRect.intersects (aOtherRect)) this.aPages[i].setFront (false);
		}
}

function Pages_createList() {
	if (this.bHasPageList) {
		var oL = getLayer('PageList');
		var cHTML = '<nobr>';
		for (var i=0; i < this.aPages.length; i++) {
			cHTML = cHTML + '<a href="Javascript:oPages.select(' + (i+1) + ')" onmouseover="return moPg(' + i + ',1)" onmouseout="return moPg(' + i + ',0)"><img name="PG' + i + '" src="img/nav/pg' + (i+1) + '_' + (this.aPages[i].bFront ? '1' : '0') + '.gif" border=0></a>';
		}
		cHTML = cHTML + '<nobr>';
		oL.replaceContent (cHTML);
		
		for (var i=0, oLastMenu=_aMs[0]; i<2 && oLastMenu.oChild && oLastMenu.oChild.aElements; i++) oLastMenu = oLastMenu.oChild;

		if (oLastMenu) {
			oL.set('top', _aMs[0].aPos[1] + 28);	
			oL.set('left', oLastMenu.aLayers[0].get('left')+8);	
			oL.set('visibility', true);
		}
	}
}

function mo (cText) {
	window.status = cText;
	return (true);
}

function moPg (nPage, nOver) {
	if (!oPages.aPages[nPage].bFront) {
		var oImg = getDocumentImage ('PG' + nPage);
		if (oImg) oImg.src = 'img/nav/pg' + (nPage+1) + '_' + (nOver ? '1' : '0') + '.gif';
	}
	return false;
}


//**************************************************************************************
// Page
//**************************************************************************************

function Page (nID) {
	this.oPages			= oPages;
	this.nID				= nID;
	this.oLayer			= null;
	this.bFront			= (nID == 0);
	
	oL = getLayer ('Page' + this.nID);
	if (!oL) alert ('Page' + this.nID + ' nicht gefunden!');
	oL.oPage = this;
	
	oL2 = getLayer ('Page' + this.nID + 'inactive');
	if (!oL2) alert ('Page' + this.nID + 'inactive nicht gefunden!');
	oL2.oPage = this;
	
	if (oPages.bInteractive) {
		oL.dragable 	= true;
		oL.bringToFront	= PageLayer_bringToFront;
		oL.onStartDrag = 'Page__onStartDrag(this)';
		oL.onStopDrag = 'Page__onStopDrag(this)';
		oL.activateInteraction();

		oL2.clickable = true;
		oL2.bringToFront	= PageLayer2_bringToFront;
		oL2.onClicks = 'this.bringToFront()';
		oL2.activateInteraction();
	}

	this.oLayer = oL;
	this.oLayer2= oL2;
	
	this.onLoad 		= Page_onLoad;
	this.setFront		= Page_setFront;
}





function Page_onLoad (bCorrectPosition) {
	var nWindowWidth = (window.innerWidth ? window.innerWidth : (document.body && document.body.clientWidth ? document.body.clientWidth : 800));

	if (this.nID > 0 && bCorrectPosition) {
		var nWidth = Math.min (nWindowWidth - 110 - 210, 630 - 210);
		var nLeft = 110 + this.nID * (nWidth / (this.oPages.aPages.length-1));
		nLeft = Math.round(nLeft / 2) * 2;
		if (nLeft != this.oLayer.get('left')) {
			this.oLayer2.set ('left', nLeft);
			this.oLayer2.set ('top', Math.round(this.oLayer.get('top') / 2) * 2);
		}
	}
	if (this.nID > 0) this.oLayer2.set ('visibility', true);
	
	this.oLayer.set('width', 200);
//	this.oLayer.set('height', 500);
	this.oLayer2.set('width', 200);
//	this.oLayer2.set('height', 500);
	this.oLayer2.onclick = PageLayer__onclick;
	this.oLayer2.oPage = this;
}


function Page_setFront (bFront) {
	if (this.bFront != bFront) {
//		oDebug.write ('Page[' + this.nID + '].setFront(' + bFront + ')');
		if (bFront) {
			if (oSound) oSound.playKlick();
			this.oLayer.set ('left', this.oLayer2.get('left'));
			this.oLayer.set ('top', this.oLayer2.get('top'));
			this.oLayer.set ('zIndex', this.oPages.nMaxZIndex);
		}
		else {
			this.oLayer2.set ('left', this.oLayer.get('left'));
			this.oLayer2.set ('top', this.oLayer.get('top'));
		}
		this.oLayer.set ('visibility', bFront);
		this.oLayer2.set ('visibility', !bFront);
		if (bFront) oPages.sendBackCollidingPages (this);
		this.bFront = bFront;
		
		if (oPages.bHasPageList) {
			var oImg = getDocumentImage ('PG' + this.nID);
			if (oImg) oImg.src = 'img/nav/pg' + (this.nID+1) + '_' + (this.bFront ? '1' : '0') + '.gif';
		}
	}
}


function PageLayer2_bringToFront (bFront) {
	var oPage = this.oPage;
	oPage.setFront (true);
}

function PageLayer_bringToFront() {
	this.set ('zIndex', this.oPage.oPages.nMaxZIndex);
}

function PageLayer__onclick () {
	if (this.oPage) 
		oPages.select (this.oPage.nID+1);
	else
		alert ('PageLayer__onclick()');
}


function showLayers() {
	var aLayers = getAllLayers();
	for (var i=0; i<aLayers.length; i++) if (aLayers[i].get('ID') > '') {
		oDebug.write ('Layer(' + aLayers[i].get('ID') + '): zIndex=' + aLayers[i].get('zIndex'));
	}
}


var oDragSoundTimeout

function Page__onStartDrag (oLayer) {
	if (oSound) {
		nChannel = Math.ceil(Math.random() * 5);
		oSound.play('page', nChannel, 'play');
		oDragSoundTimeout = setTimeout('dragSound()', 500);
	}
}

function Page__onStopDrag (oLayer) {
	if (oSound) {
		clearTimeout(oDragSoundTimeout);
		oSound.unload('page');
		oSound.load('page');
	}
	var oThis = oLayer.oPage;
	oLayer.set ('left', Math.round(oLayer.get('left') / 2) * 2);
	oLayer.set ('top', Math.round(oLayer.get('top') / 2) * 2);
	oThis.oPages.sendBackCollidingPages (oThis);
	return (true);
}

function dragSound() {
	nChannel = Math.ceil(Math.random() * 5);
	oSound.play('page', nChannel, 'play');
	oDragSoundTimeout = setTimeout('dragSound()', 500);
}

//**************************************************************************************
// Page
//**************************************************************************************

function Rect(left, top, width, height) {
	this.left = left;
	this.top  = top;
	this.width = width;
	this.height = height;

	this.intersects = Rect_intersects;
}

function Rect_intersects (aRect) {
	return (this.left+this.width >= aRect.left && this.left <= aRect.left+aRect.width &&
	    		this.top+this.height >= aRect.top  && this.top  <= aRect.top+aRect.height)
}

function UEvent (e) {
	this.oTime = new Date();
	if (e.clientX || e.clientY) {
		this.nLeft 			= e.clientX + (document.body.scrollTop ? document.body.scrollLeft : 0);
		this.nTop  			= e.clientY + (document.body.scrollTop ? document.body.scrollTop : 0);
		this.nKeyCode 	= e.keyCode;
		this.bShiftKey	= e.shiftKey;
		this.bAltKey		= e.altKey;
		this.bCtrlKey		= e.ctrlKey;
	}
	else {
		this.nLeft 			= e.pageX + 2;
		this.nTop  			= e.pageY + 2;
		this.nKeyCode 	= e.which;
		this.bShiftKey	= (e.modifiers & e.SHIFT_MASK);
		this.bAltKey		= (e.modifiers & e.ALT_MASK);
		this.bCtrlKey		= (e.modifiers & e.CTRL_MASK);
	}
}


