/******************************************************************************
 evelyne-wannack.ch Javascripts
 Author:   Robert Hilbe
 Version:  30 December 2005
******************************************************************************/

/******************************************************************************
 Suckerfish: Makes Use of :hover, :active, :focus und :target for IE possible,
 define .hover beside :hover in CSS
******************************************************************************/

function suckerfish(type, tag, parentId) {
	if (window.attachEvent) {
		window.attachEvent("onload", function() {
			var sfEls = (parentId == null) ? document.getElementsByTagName(tag) : document.getElementById(parentId).getElementsByTagName(tag);
			type(sfEls);
		});
	}
}

hover = function(sfEls) {
	for (i = 0; i < sfEls.length; i++) {
		sfEls[i].onmouseover = function() {
			this.className+= " hover";
		}
		sfEls[i].onmouseout = function() {
			this.className = this.className.replace(new RegExp(" hover\\b"), "");
		}
	}
}

suckerfish(hover, "tr");

/******************************************************************************
 expandTextarea: Shows a control to expand textarea #message on contact form,
 needs onload handler
******************************************************************************/

function expandTextarea(Id) {
	
	// Stop if you can't find textarea with id
	if (!document.getElementById(Id)) return;
	
	var target = document.getElementById(Id);
	
	// Create a div with controllink
	var controldiv = document.createElement('div');
	var controllink	= document.createElement('a');
	controllink.innerHTML = 'Textfeld vergrössern &#8595;';
	controllink.setAttribute('onclick', 'document.getElementById(\'' + Id + '\').rows += 10;');
	controllink.style.fontSize = '80%';
	controldiv.appendChild(controllink);
	
	// Append control to textarea
	target.parentNode.appendChild(controldiv);
}


/******************************************************************************
 backToTop: Smooth scrolling to top of page
******************************************************************************/

function backToTop() {
    var x1 = x2 = x3 = 0;
    var y1 = y2 = y3 = 0;

    if (document.documentElement) {
        x1 = document.documentElement.scrollLeft || 0;
        y1 = document.documentElement.scrollTop || 0;
    }

    if (document.body) {
        x2 = document.body.scrollLeft || 0;
        y2 = document.body.scrollTop || 0;
    }

    x3 = window.scrollX || 0;
    y3 = window.scrollY || 0;

    var x = Math.max(x1, Math.max(x2, x3));
    var y = Math.max(y1, Math.max(y2, y3));

    window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));

    if (x > 0 || y > 0) {
        window.setTimeout("backToTop()", 2);
    }
}

/******************************************************************************
 perpareTopLinks: Perpare Smooth scrolling to top of page
******************************************************************************/

function prepareTopLinks() {
	links = document.getElementsByTagName('a');
	for (i = 0; i < links.length; i++) {
		if(links[i].className == 'top') {
			links[i].onclick = function() {
				backToTop();
				return false;
			}
		}
	}
}

/******************************************************************************
 Fadomatic: Fads Elements in or out (siehe http://chimpen.com/fadomatic/)

 INTERVAL_MILLIS: Fade interval in milliseconds (Make this larger if you experience performance issues
 function Fadomatic creates a fader: element to falde, speed (0-100), minimum opacity (0-100), maximum opacity (0-100)
 function fadeIn: starts fading in 
 function fadeOut: starts fading out:
 function makeVisible: sets opacity to maximum
 function makeTransparent: sets opacity to minimum
******************************************************************************/

Fadomatic.INTERVAL_MILLIS = 50;

function Fadomatic (element, rate, initialOpacity, minOpacity, maxOpacity) {
	this._element = element;
	this._intervalId = null;
	this._rate = rate;
	this._isFadeOut = true;
	this._minOpacity = 0;
	this._maxOpacity = 99;
	this._opacity = 99;

	if (typeof minOpacity != 'undefined') {
		if (minOpacity < 0) {
			this._minOpacity = 0;
		} else if (minOpacity > 99) {
			this._minOpacity = 99;
		} else {
			this._minOpacity = minOpacity;
		}
	}

	if (typeof maxOpacity != 'undefined') {
		if (maxOpacity < 0) {
			this._maxOpacity = 0;
		} else if (maxOpacity > 99) {
			this._maxOpacity = 99;
		} else {
			this._maxOpacity = maxOpacity;
		}

		if (this._maxOpacity < this._minOpacity) {
			this._maxOpacity = this._minOpacity;
		}
	}

	if (typeof initialOpacity != 'undefined') {
		if (initialOpacity > this._maxOpacity) {
			this._opacity = this._maxOpacity;
		} else if (initialOpacity < this._minOpacity) {
			this._opacity = this._minOpacity;
		} else {
			this._opacity = initialOpacity;
		}
	}
	
	if(typeof element.style.opacity != 'undefined') {
		this._updateOpacity = this._updateOpacityW3c;
	} else if(typeof element.style.filter != 'undefined') {
		var existingFilters="";
		if (element.style.filter) {
			existingFilters = element.style.filter+" ";
		}
		element.style.filter = existingFilters+"alpha(opacity="+this._opacity+")";
		this._updateOpacity = this._updateOpacityMSIE;
	} else {
		this._updateOpacity = this._updateVisibility;
	}
	this._updateOpacity();
}

Fadomatic.prototype.fadeOut = function () {
	this._isFadeOut = true;
	this._beginFade();
}

Fadomatic.prototype.fadeIn = function () {
	this._isFadeOut = false;
	this._beginFade();
}

Fadomatic.prototype.makeVisible = function () {
	this.haltFade();
	this._opacity = this._maxOpacity;
	this._updateOpacity();
}

Fadomatic.prototype.makeTransparent = function () {
	this.haltFade();
	this._opacity = 0;
	this._updateOpacity();
}

Fadomatic.prototype.haltFade = function () {
	clearInterval(this._intervalId);
}

Fadomatic.prototype.resumeFade = function () {
	this._beginFade();
}

Fadomatic.prototype._beginFade = function () {
	this.haltFade();
	var objref = this;
	this._intervalId = setInterval(function() { objref._tickFade(); },Fadomatic.INTERVAL_MILLIS);
}

Fadomatic.prototype._tickFade = function () {
	if (this._isFadeOut) {
		this._opacity -= this._rate;
		if (this._opacity < this._minOpacity) {
			this._opacity = this._minOpacity;
			this.haltFade();
		}
	} else {
		this._opacity += this._rate;
		if (this._opacity > this._maxOpacity ) {
			this._opacity = this._maxOpacity;
			this.haltFade();
		}
	}
	this._updateOpacity();
}

Fadomatic.prototype._updateVisibility = function () {
	if (this._opacity > 0) {
		this._element.style.visibility = 'visible';
	} else {
		this._element.style.visibility = 'hidden';
	}
}

Fadomatic.prototype._updateOpacityW3c = function () {
	this._element.style.opacity = this._opacity/100;
	this._updateVisibility();
}

Fadomatic.prototype._updateOpacityMSIE = function () {
	this._element.filters.alpha.opacity = this._opacity;
	this._updateVisibility();
}

Fadomatic.prototype._updateOpacity = null;

/******************************************************************************
 Keyboard Shortcuts: Use the same shortcuts as accesskeys
******************************************************************************/

if(document.captureEvents) {
    
    // non IE (NS 4, NS 6+, Mozilla 0.9+)
    if(Event.KEYUP) {
    	document.captureEvents(Event.KEYUP);
    }
}

// when a key is pressed, run the alertkey function
document.onkeyup = alertkey;

// process the event

function alertkey(e) {
    
	if(!e) {
		if(window.event) e = window.event;
		else return;
	}
    
    // check, if focus is on input element and stop if so
    
	if(document.all) target = e.srcElement;
	else target = e.target;
	if(target.tagName == "TEXTAREA" || target.tagName == "INPUT" || e.ctrlKey || e.altKey || e.shiftKey) return;
    
    if(typeof(e.which) == 'number') {
        
        //NS 4, NS 6+, Mozilla 0.9+, Opera
        e = e.which;
        
    } else if(typeof(e.keyCode) == 'number') {
        
        //IE, NS 6+, Mozilla 0.9+
        e = e.keyCode;
        
    } else if(typeof(e.charCode) == 'number') {
        
        //also NS 6+, Mozilla 0.9+
        e = e.charCode;
        
    } else return;
 		
	// h: Homepage
	if (e == 72) location.href = "/";

	// b: Biographie
	else if (e == 66) location.href = "/biographie/";

	// f: Forschung
	else if (e == 70) location.href = "/forschung/";

	// p: Publikationen
	else if (e == 80) location.href = "/publikationen/";

	// a: Austausch
	else if (e == 65) location.href = "/austausch/";

	// l: Links
	else if (e == 76) location.href = "/links/";
	
	// k: Kontakt
	else if (e == 75) location.href = "/kontakt/";
}

/******************************************************************************
 Onload Behaviour
******************************************************************************/

window.onload = function() {

	// Prepare Links to Top
	prepareTopLinks();
	
	// Fade in Content
	if (document.getElementById('content')) {
		var content = document.getElementById('content');
		fadecontent = new Fadomatic(content, 23, 0);
		fadecontent.fadeIn();
	}
	
	// Add control to textarea #message on contact form
	expandTextarea('message');
}
