/*
    Datei: fixlayer.js
    Datum: 20.11.07
*/

////////////////////////////////////////////////////////////
// body onload and config
var objID = 'glideLogo';
var IE_HeightDiverenzFromBottom = 20; // IE rechnet padding zu gesamthoehe
function fixlayeronload() {
	fixObjPos(objID);
}

////////////////////////////////////////////////////////////
// Ein Layer fixieren
var canFix = null;

////////////////////////////////////////////////////////////

var origTop = null;
function glidern() {
	var win = window;
	var id = objID;
	var obj = document.getElementById ? win.document.getElementById(id) : document.all ? win.document.all[id] : document.layers ? win.document.layers[id] : null;
	var sticky = new Sticky(obj, win);
	if(!sticky) return alert('Es trat ein Fehler auf!');
	if (!origTop) {origTop = sticky.top;}
	var newTop = ( origTop + getScrollY() ) - IE_HeightDiverenzFromBottom;
	var px = window.opera? 0 : "px";
	obj.style.top = newTop+'px';
	//window.status = 'Y:' + getScrollY() + ' / ' +obj.top+ ' / ' +obj.offsetTop + ' / ' +obj.style.top + ' / ' +newTop;
}

////////////////////////////////////////////////////////////
// Ein fliegendes Layer
var hasGlider = null;

// returns height of window
function getWinHeight() {
	var winHt = 0;
	if (window.innerHeight) winHt = window.innerHeight-18;
	else if (document.documentElement && document.documentElement.clientHeight)
		winHt = document.documentElement.clientHeight;
	else if (document.body && document.body.clientHeight)
		winHt = document.body.clientHeight;
	return winHt;
}

// returns width of window
function getWinWidth() {
	var winWt = 0;
	if (window.innerWidth) winWt = window.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth)
		winWt = document.documentElement.clientWidth;
	else if (document.body && document.body.clientWidth)
		winWt = document.body.clientWidth;
	return winWt;
}

function fixObjPos(id,win) {
	var canFix = null;

	if(!win) win = window;
	var obj = document.getElementById ? win.document.getElementById(id) : document.all ? win.document.all[id] : document.layers ? win.document.layers[id] : null;

	var px = window.opera? 0 : "px";

	// als erstes die Fensterbreite
	var winWt = getWinWidth();

	// wurde das Glider Object gefunden?
	if(!obj) return alert('Fehler\n\nID:' + id + ' existiert nicht.');

	// das 'page' div
	var objPage = document.getElementById ? win.document.getElementById('page') : document.all ? win.document.all['page'] : document.layers ? win.document.layers['page'] : null;
	var page = new Sticky(objPage, win);
	if(!page) return alert('Es trat ein Fehler auf!');

	// jetzt das flying div
	var sticky = new Sticky(obj, win);
	if(!sticky) return alert('Es trat ein Fehler auf!');
	
	// ausrechnen wo das div hin muss
	// Fensterbreite - DIV#page Breite durch 2, ergibt die Breite der
	// Linken und Rechten Spalte. Diesen Wert + DIV#page Breite - die 
	// Breite des Flying Div ergibt die linke position
	
	var posLeft = (Math.round((winWt - page.width) / 2) - sticky.width) + page.width;
	
	// jetzt die linke position speichern
	obj.style.left = posLeft + px;

	// ausrechnen der unteren position, das ist einfacher
	var winHt = getWinHeight();
	var posTop = winHt - sticky.height;
	
	if(document.all) {
	  posTop = posTop -IE_HeightDiverenzFromBottom;
	}

	// testen ob das div gefixed werden kann
		// IE6 könnte es... aber es geht nicht!
		if(document.all) {
			obj.style.position = 'absolute';
			obj.style.top = posTop;
			window.setInterval("glidern()",10);
		} else {
			obj.style.position = 'fixed';
			obj.style.top = posTop +px;
			return;
		}

	return obj;
}

function setObjAddTopPos(id,top) {
	if(!win) win = window;
	var obj = document.getElementById ? win.document.getElementById(id) : document.all ? win.document.all[id] : document.layers ? win.document.layers[id] : null;
	var px = window.opera? 0: "px";
	obj.style.top = obj.style.top + top;
}

////////////////////////////////////////////////////////////
// Sticky -> Hilfsstruktur um die Startwerte zu sichern
function Sticky(obj, win)
{
	var px = window.opera? 0: "px";
	this.top  = obj.top ? obj.top : obj.offsetTop;
	this.left = obj.left ? obj.left : obj.offsetLeft;
	this.width = obj.width ? obj.width : obj.offsetWidth;
	this.height = obj.height ? obj.height : obj.offsetHeight;
	this.obj = obj;
	this.win = win;
}

////////////////////////////////////////////////////////////
// returns amount of vertical scroll
function getScrollY() {
	var sy = 0;
	if (document.documentElement && document.documentElement.scrollTop)
		sy = document.documentElement.scrollTop;
	else if (document.body && document.body.scrollTop)
		sy = document.body.scrollTop;
	else if (window.pageYOffset)
		sy = window.pageYOffset;
	else if (window.scrollY)
		sy = window.scrollY;
	return sy;
}
