// doc: 090809

/*
==================================================
Executing JavaScript on page load 060528 (Simon Willison)
==================================================
-> http://simonwillison.net/2004/May/26/addLoadEvent/

Hiermit können beliebig viele Funktionen beim "window.onload" aufgerufen werden
ohne dass ein vorher definiertes window.onload überschrieben wird.

- addLoadEvent einfach als erste Funktion einbinden
- Funktionen per addLoadEvent(Funktionsname) dem "window.onload" hinzufügen
  anstatt direkt per window.onload=Funktionsname;
*/

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}



// Namespacing
var gRel = {};

/*
==================================================
Helper-Funktionen 090808 (Wolfgang)
==================================================

ein paar kleine allgemeine Hilfsfunktionen
*/


// einfache Methode zum "Kopieren" von Objekten
gRel.clone = function (vorlage, argumente) {
	var F = function () {}, obj;
	F.prototype = vorlage;
	obj = new F();
	if (obj.configure && argumente) {
		obj.configure (argumente);
		}
	return obj;
	};

// Allgemeine Methode zum Erstellen von Events
gRel.addEvent = function (el, type, fn){
	if (window.addEventListener) {
		el.addEventListener(type, fn, false);
		}
	else if (window.attachEvent){
		el.attachEvent('on' + type, fn);
		}
	else {
		el['on' + type] = fn;
		}
	};

// Checkt, ob der übergebene Wert ein Array ist
gRel.isArray = function (val) {
	return val && typeof val === 'object' && val.constructor === Array;
	};

// Checkt, ob zwei Arrays die gleichen Werte enthalten
gRel.sameArrays = function (array1, array2) {
	if ( gRel.isArray(array1) && gRel.isArray(array2) && array1.length === array2.length ) {
		for (var i=0; i<array1.length; i++){
			if (array1[i] != array2[i]) {
				return false;
				}
			}
		return true;
		}
	return false;
	};

// Konvertiert einen Farbstring in einen Array mit drei Dezimalzahlen
gRel.colStringToColorArray = function (str) {
	var colorarray = [];
	if (str.substr(0,3)==="rgb"){
		//Rgb-String = rgb(xxx, xxx, xxx)
		colorarray = str.substring(4,str.length-1).split(",");
		for (var i=0;i<colorarray.length;i++){
			colorarray[i]=Number(colorarray[i]);
			}
		} else {
		//Hex-String = #xxxxxx
		colorarray [0] = parseInt(str.substr(1,2).toUpperCase(),16);
		colorarray [1] = parseInt(str.substr(3,2).toUpperCase(),16);
		colorarray [2] = parseInt(str.substr(5,2).toUpperCase(),16);
		}
	return colorarray;
	};
	
// Konvertiert einen Array mit Dezimalzahlen zurück in einen Rgb-String 
gRel.colorArrayToRgb = function (colorarray) {
	var rgb ="rgb("+colorarray[0]+","+colorarray[1]+","+colorarray[2]+")";
	return rgb;
	};



/*
==================================================
Animierte Hover-Objekte 090810 (Wolfgang)
==================================================

Html-Elemente (vor allem Links) erhalten hover-animationen
*/

// 
gRel.animator_obj = {
	styleproperty : "color",
	normalstate : "#969857",
	hoverstate : "#ffffff",
	speed : 10,
	decode : function(propstring) {
		gRel.colStringToColorArray(propstring);
		},
	encode : function(proparray) {
		gRel.colorArrayToRgb(proparray);
		},
	animate : function(obj, num) {			
		var start, ziel, schritt;
		start = gRel.colStringToColorArray(obj.html_elem.style[this.styleproperty]);	
		if (obj.hover) {
			ziel = gRel.colStringToColorArray(this.hoverstate);
			} 
		else {
			ziel = gRel.colStringToColorArray(this.normalstate);
			}
		schritt = gRel.arrayIncrement (start, ziel, this.speed);
		obj.html_elem.style[this.styleproperty] = gRel.colorArrayToRgb(schritt);			
		if (gRel.sameArrays(schritt, ziel)){
			obj.stop_anim(num);
			}
		} 
	};

// verändert einen Start-Array um einen Schritt zum Ziel-Array
gRel.arrayIncrement = function(start, ziel, speed) {
	var schritt=[];
	for (var i=0; i<start.length; i++){
		if (start[i] === ziel[i]) {
			schritt[i] = start[i];
		} else if (start[i] > ziel[i]) {
			schritt[i] = Math.max (start[i] - speed, ziel[i]);
		} else {
			schritt[i] = Math.min (start[i] + speed, ziel[i]);
			}
		}
	return schritt;
	};

// Einfaches Objekt zum Animieren von Menü-Einträgen
gRel.hover_obj = {
	html_elem : document.getElementById("M1_Startseite"),
	hover_elem : "html",
	interval : 0,
	hover : false,
	animators : [],
	animators_running : [false],
	ensure_property : function(obj) {
		if (!this.html_elem.style[obj.styleproperty] || this.html_elem.style[obj.styleproperty]==="") {
			this.html_elem.style[obj.styleproperty] = obj.normalstate;
			}		
		},
	init_anim : function() {
		var that = this;
		for (var i=0; i < this.animators.length; i++){
			this.animators_running[i] = true;
			this.ensure_property(this.animators[i]);
			}
		window.clearInterval(this.interval);
		this.interval = window.setInterval(function(){that.run_anim();}, 40);
		},
	run_anim : function() {
		for (var i=0; i < this.animators.length; i++){
			if (this.animators_running[i] || true) {
				this.animators[i].animate(this, i);
				}
			}
		},
	stop_anim : function(num) {
		this.animators_running[num] = false;
		for (var i=0; i < this.animators.length; i++){		
			if (this.animators_running[i]) {
				return false;
				}
			}
		window.clearInterval(this.interval);
		},
	mouseover_anim : function(over) {
		this.hover = over;
		this.init_anim();
		},
	attachEvents: function() {
		var that = this;
		gRel.addEvent(this.html_elem, 'mouseover', function() { that.mouseover_anim(true); });
		gRel.addEvent(this.html_elem, 'mouseout', function() { that.mouseover_anim(false); });
		},
	configure : function(thearray) {
		this.html_elem = thearray[0];
		this.attachEvents();
		}
	};


gRel.cloneHoverObjects = function(original,containerid,tag){
	var i, htmlelements;
	if (document.getElementById(containerid)) {
		htmlelements = document.getElementById(containerid).getElementsByTagName("a");
		for (i=0; i < htmlelements.length; i++){
			gRel[containerid+"_hoverobj_"+i] = gRel.clone(original,[htmlelements[i]]);
			} 
		}
	};

// Vorbereitung der Objekte
gRel.prepareHovers = function() {
	gRel.animator_obj_2 = gRel.clone(gRel.animator_obj,false);
	gRel.animator_obj_2.hoverstate = "#c2c200";
	gRel.animator_obj_2.speed = 5;
	
	gRel.animator_obj_3 = gRel.clone(gRel.animator_obj,false);
	gRel.animator_obj_3.hoverstate = "#ffffff";
	gRel.animator_obj_3.normalstate = "#F5F6F9";
	gRel.animator_obj_3.styleproperty = "backgroundColor";
	gRel.animator_obj_3.speed = 1;
	
	gRel.cloneHoverObjects(gRel.hover_obj,"MainMenue","a");
	gRel.hover_obj.animators = [gRel.animator_obj];
	
	gRel.hover_obj_2 = gRel.clone(gRel.hover_obj,false);
	gRel.cloneHoverObjects(gRel.hover_obj_2,"SideMenue","a");
	gRel.hover_obj_2.animators = [gRel.animator_obj_2,gRel.animator_obj_3];
	
	gRel.hover_obj_3 = gRel.clone(gRel.hover_obj,false);
	gRel.cloneHoverObjects(gRel.hover_obj_3,"Inhalt","a");
	gRel.hover_obj_3.animators = [gRel.animator_obj_3];
	};

//Initialisierung
addLoadEvent(gRel.prepareHovers);