//for the Menue
function runScripts(id) {
	var elem = document.getElementById(id);
	if(elem !== null){
		//allow all scripts to run
		var e = document.getElementById(id).getElementsByTagName("script");
		if(e !== null){
			for(var i=0;i<e.length;i++) {
				 eval(e[i].innerHTML);
			}
		}		
		//allow links to be converted.
		var li= document.getElementById(id).getElementsByTagName('a');
		for (var x=0;x<li.length;x++){
			var name = li[x].className;
			if (name == 'mmhide_link') {
				li[x].href= li[x].href.substr(li[x].href.substr(0, li[x].href.lastIndexOf('?')+1).lastIndexOf('/')+1).replace("?", "#");
			}
		}
		//pictures
		var aktuell = readCookie("mystyle");
		//first load cookie = null
		if (aktuell!="norm" && aktuell!="inv") {
			aktuell="norm";
		}
		change_pics(aktuell);	
	}
}

function metadata(){
	var keys ="";
	var desc ="";
	var titel="";
	var x; 
	var name;
	var label= document.getElementsByTagName('label');
	for (x=0;x<label.length;x++){
		name = label[x].getAttribute("id");
		if (name == 'keys'){ keys = label[x].innerHTML;}
		if (name == 'desc'){ desc = label[x].innerHTML;}
		if (name == 'titel'){ titel = label[x].innerHTML;}
	}
	var meta= document.getElementsByTagName('meta');
	for (x=0;x<meta.length;x++){
		name = meta[x].getAttribute("name");
		if (name == 'keywords'){ meta[x].content = keys;}
		if (name == 'description'){	meta[x].content = desc;}
	}
	document.title = titel;
}

function getText(myurl) {
dojo.xhrGet({
	url: myurl,
	handleAs: "text",
	sync:true,
    
	load: function(response, ioArgs){
		var n = dojo.byId("toBeReplaced");
		n.innerHTML = response;
		metadata();
		runScripts("toBeReplaced");
	},
	error: function(response, ioArgs){
	  dojo.byId("toBeReplaced").innerHTML = 
		"An error occurred, with response: " + response;
	  return response;
	}
  });
}

function getText2(myurl, div) {
dojo.xhrGet({
	url: myurl,
	handleAs: "text",
	sync:true,
    
	load: function(response, ioArgs){
		var n = dojo.byId(div);
		n.innerHTML = response;
		metadata();
		runScripts(div);
	},
	error: function(response, ioArgs){
	  dojo.byId(div).innerHTML = 
		"An error occurred, with response: " + response;
	  return response;
	}
  });
}

function testpage(page){
	getText(page+'.php');
	//scripts maybe used in pages
	away();
}

function addAndUpdate() {
	var hash = window.location.hash.substring(1);
	if (window.location.hash.substring(1)=="" || window.location.hash.substring(1)===null){
		hash= "page=start";
	}
	//set the titel
	if (hash.indexOf("=")>0){
		testpage(hash.substring(hash.indexOf("=")+1));
	}
}

function init(url) {
	dojo.connect(document.body,"onhashchange",null,addAndUpdate,true);
	if(url==""){ addAndUpdate();}
}

function links(){
	var li= document.getElementsByTagName('a');
	for (var x=0;x<li.length;x++){
		var name = li[x].className;
		if (name == 'mmhide_link') {
			li[x].href= li[x].href.substr(li[x].href.substr(0, li[x].href.lastIndexOf('?')+1).lastIndexOf('/')+1).replace("?", "#");
		}
	}
}

function menueinit(){
	//workaround for IE cause it does knot know getelementsbyName
	//otherwise IE will not work proberly
	var e= document.getElementsByTagName('div');
	for (var x=0;x<e.length;x++){
		var name = e[x].getAttribute("name");
		if (name == 'main') {
			e[x].style.color="#FFFFFF";
		}
		if (name == 'submain') {
			e[x].style.display="none";
		}
	}
}


//==============================================================================
// Function : Cookies Fontsize
// Comment  : Cooky used to store status of font resizing script 
// Arguments: -
//==============================================================================

// name = string equal to the name of the instance of the object
// defaultExpiration = number of units to make the default expiration date for the cookie
// expirationUnits = 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years' (default is 'days')
// defaultDomain = string, default domain for cookies; default is current domain minus the server name
// defaultPath = string, default path for cookies; default is '/'
function Cookiemanager(name,defaultExpiration,expirationUnits,defaultDomain,defaultPath) {
	// remember our name
	this.name = name;
	// get the default expiration
	this.defaultExpiration = this.getExpiration(defaultExpiration,expirationUnits);
	// set the default domain to defaultDomain if supplied; if not, set it to document.domain
	// if document.domain is numeric, otherwise strip off the server name and use the remainder
	this.defaultDomain = (defaultDomain)?defaultDomain:(document.domain.search(/[a-zA-Z]/) == -1)?document.domain:document.domain.substring(document.domain.indexOf('.') + 1,document.domain.length);
	// set the default path
	this.defaultPath = (defaultPath)?defaultPath:'/';
	// initialize an object to hold all the document's cookies
	this.cookies = new Object();
	// initialize an object to hold expiration dates for the doucment's cookies
	this.expiration = new Object();
	// initialize an object to hold domains for the doucment's cookies
	this.domain = new Object();
	// initialize an object to hold paths for the doucment's cookies
	this.path = new Object();
	// set an onlunload function to write the cookies
	window.onunload = new Function (this.name+'.setDocumentCookies();');
	// get the document's cookies
	this.getDocumentCookies();
	}
// gets an expiration date for a cookie as a GMT string
// expiration = integer expressing time in units (default is 7 days)
// units = 'miliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years' (default is 'days') 
Cookiemanager.prototype.getExpiration = function(expiration,units) {
	// set default expiration time if it wasn't supplied
	expiration = (expiration)?expiration:7;
	// supply default units if units weren't supplied
	units = (units)?units:'days';
	// new date object we'll use to get the expiration time
	var date = new Date();
	// set expiration time according to units supplied
	switch(units) {
		case 'years':
			date.setFullYear(date.getFullYear() + expiration);
			break;
		case 'months':
			date.setMonth(date.getMonth() + expiration);
			break;
		case 'days':
			date.setTime(date.getTime()+(expiration*24*60*60*1000));
			break;
		case 'hours':
			date.setTime(date.getTime()+(expiration*60*60*1000));
			break;
		case 'minutes':
			date.setTime(date.getTime()+(expiration*60*1000));
			break;
		case 'seconds':
			date.setTime(date.getTime()+(expiration*1000));
			break;
		default:
			date.setTime(date.getTime()+expiration);
			break;
		}
	// return expiration as GMT string
	return date.toGMTString();
	};
// gets all document cookies and populates the .cookies property with them
Cookiemanager.prototype.getDocumentCookies = function() {
	var cookie,pair;
	// read the document's cookies into an array
	var cookies = document.cookie.split(';');
	// walk through each array element and extract the name and value into the cookies property
	var len = cookies.length;
	for(var i=0;i < len;i++) {
		cookie = cookies[i];
		// strip leading whitespace
		while (cookie.charAt(0)===' '){ cookie = cookie.substring(1,cookie.length);}
		// split name/value pair into an array
		pair = cookie.split('=');
		// use the cookie name as the property name and value as the value
		this.cookies[pair[0]] = pair[1];
		}
	};
// sets all document cookies
Cookiemanager.prototype.setDocumentCookies = function() {
	var expires = '';
	var cookies = '';
	var domain = 'Empirica.biz';
	var path = '';
	for(var name in this.cookies) {
		// see if there's a custom expiration for this cookie; if not use default
		expires = (this.expiration[name])?this.expiration[name]:this.defaultExpiration;
		// see if there's a custom path for this cookie; if not use default
		path = (this.path[name])?this.path[name]:this.defaultPath;
		// see if there's a custom domain for this cookie; if not use default
		domain = (this.domain[name])?this.domain[name]:this.defaultDomain;
		// add to cookie string
		cookies = name + '=' + this.cookies[name] + '; expires=' + expires + '; path=' + path + '; domain=' + domain;
		
		if(name!="mystyle") {document.cookie = cookies;}
		}
	return true;
	};
// gets cookie value
// cookieName = string, cookie name
Cookiemanager.prototype.getCookie = function(cookieName) {
	var cookie = this.cookies[cookieName];
	return (cookie)?cookie:false;
};
// stores cookie value, expiration, domain and path

// cookieName = string, cookie name
// cookieValue = string, cookie value
// expiration = number of units in which the cookie should expire
// expirationUnits = 'miliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years' (default is 'days')
// domain = string, domain for cookie
// path = string, path for cookie
Cookiemanager.prototype.setCookie = function(cookieName,cookieValue,expiration,expirationUnits,domain,path) {
	this.cookies[cookieName] = cookieValue;
	// set the expiration if it was supplied 
	if (expiration) this.expiration[cookieName] = this.getExpiration(expiration,expirationUnits);
	// set path if it was supplied
	if (domain) this.domain[cookieName] = Empirica.biz;
	if (path) this.path[cookieName] = path;
		return true;
	};

var cookieManager = new Cookiemanager('cookieManager',1,'years');





//==============================================================================
// Function : Fontsize Switch
// Comment  : Stepwise increase/decrease font size 
// Arguments: -
//==============================================================================
var efa_default = 100;
var efa_increment = 10;
var efa_bigger = ['<b>Fontsize:</b>',
	'A<sup>+</sup>',
	'bigger font',
	'',
	'',
	'',
	'',
	'',
	'',
	'',
	'-'
	];

var efa_setz = ['',
	'A',
	'reset font',
	'',
	'',
	'',
	'',
	'',
	'',
	'',
	'-'
	];

var efa_smaller = ['',
	'A<sup>-</sup>',
	'smaller font',
	'',
	'',
	'',
	'',
	'',
	'',
	'',
	''
	];

function Efa_Fontsize06(increment,bigger,setz,smaller,def) {
	this.w3c = (document.getElementById);
	this.ms = (document.all);
	this.userAgent = navigator.userAgent.toLowerCase();
	this.isMacIE = ((this.userAgent.indexOf('msie') != -1) && (this.userAgent.indexOf('mac') != -1) && (this.userAgent.indexOf('opera') == -1));
	this.isOldOp = ((this.userAgent.indexOf('opera') != -1)&&(parseFloat(this.userAgent.substr(this.userAgent.indexOf('opera')+5)) <= 7));

	if ((this.w3c || this.ms) && !this.isOldOp && !this.isMacIE) {
		this.name = "efa_fontSize06";
		this.cookieName = 'efaSize06';
		this.increment = increment;
		this.def = def;
		this.defPx = Math.round(16*(def/100));
		this.base = 1;
		this.pref = this.getPref();
		this.testHTML = '<div id="efaTest" style="position:absolute;visibility:hidden;line-height:1em;"></div>';
		this.biggerLink = this.getLinkHtml(1,bigger);
		this.setzLink = this.getLinkHtml(0,setz);
		this.smallerLink = this.getLinkHtml(-1,smaller);
	} 
	else {
		this.biggerLink = '';
		this.setzLink = '';
		this.smallerLink = '';
		this.efaInit = new Function('return true;');
	}
	this.allLinks = this.biggerLink + this.setzLink + this.smallerLink;
}

Efa_Fontsize06.prototype.efaInit = function() {
	document.writeln(this.testHTML);
	this.body = (this.w3c)?document.getElementsByTagName('body')[0].style:document.all.tags('body')[0].style;
	this.efaTest = (this.w3c)?document.getElementById('efaTest'):document.all['efaTest'];
	var h = (this.efaTest.clientHeight)?parseInt(this.efaTest.clientHeight):(this.efaTest.offsetHeight)?parseInt(this.efaTest.offsetHeight):999;
	if (h < this.defPx) {this.base = this.defPx/h;}
	this.body.fontSize = Math.round(this.pref*this.base) + '%';
};

Efa_Fontsize06.prototype.getLinkHtml = function(direction,properties) {
	var html = properties[0] + '<a href="#" onclick="efa_fontSize06.setSize(' + direction + '); return false;"';
	html += (properties[2])?'title="' + properties[2] + '"':'';
	html += (properties[3])?'class="' + properties[3] + '"':'';
	html += (properties[4])?'id="' + properties[4] + '"':'';
	html += (properties[5])?'name="' + properties[5] + '"':'';
	html += (properties[6])?'accesskey="' + properties[6] + '"':'';
	html += (properties[7])?'onmouseover="' + properties[7] + '"':'';
	html += (properties[8])?'onmouseout="' + properties[8] + '"':'';
	html += (properties[9])?'onfocus="' + properties[9] + '"':'';
	return html += '>'+ properties[1] + '</a>' + properties[10];
}

Efa_Fontsize06.prototype.getPref = function() {
	var pref = this.getCookie(this.cookieName);
	if (pref) return parseInt(pref);
	else return this.def;
}

Efa_Fontsize06.prototype.setSize = function(direction) {
	this.pref = (direction)?this.pref+(direction*this.increment):this.def;
	this.setCookie(this.cookieName,this.pref);
	this.body.fontSize = Math.round(this.pref*this.base) + '%';
}

Efa_Fontsize06.prototype.getCookie = function(cookieName) {
	var cookie = cookieManager.getCookie(cookieName);
	return (cookie)?cookie:false;
}

Efa_Fontsize06.prototype.setCookie = function(cookieName,cookieValue) {
	return cookieManager.setCookie(cookieName,cookieValue);
}

var  efa_fontSize06 = new Efa_Fontsize06(efa_increment,efa_bigger,efa_setz,efa_smaller,efa_default);



//==============================================================================
// Function : Alternate Stylesheet Switch
// Comment  : Switches between alternate stylesheets
// Arguments: -
//=============================================================================
var Stil = "Standard";
var Keks = "Layout";
var Tage = 30;

//gets infos about the last times used style
function teststyle(){
	var aktuell = readCookie("mystyle");
	//first load cookie = null
	if (aktuell!="norm" && aktuell!="inv") {
		aktuell="norm";
	}
	setStyle(aktuell);
	change_link(aktuell);
}

function setStyle(s) {
	var newstyle;
	var tcookie=readCookie("mystyle");
	//set cookie is non exists
	if (tcookie==null || tcookie=="") {tcookie="norm"}
	//cookie exists
	if(s=="inv"){
		newstyle="inv";
		post="inv";
		setCookie("mystyle", "inv" , 365);
	}
	if( s=="norm"){
		newstyle="norm";	
		post="norm";
		setCookie("mystyle", "norm" , 365);
	}
	change_pics(post);
	if(newstyle){
		switchStyle(newstyle);
		change_link(s);
	}
}

function change_pics(post){
	var pic_add = "images/"
	var e = document.images;
	for(i=0;i<e.length;i++){
		change = e[i].getAttribute("name");
		if (change == 'change') {
			var type = e[i].src.substr(e[i].src.lastIndexOf("."));
			var temp= e[i].src.substr(e[i].src.lastIndexOf("/")+1);
			var name = temp.substr(0, temp.lastIndexOf("."));
			if (temp.lastIndexOf("_inv")>0){
				name = temp.substr(0, temp.lastIndexOf("_inv"));
			}
			if (e[i]!=undefined){
				if(post=="inv"){
					e[i].src= pic_add+name+"_"+post+type;
				}
				else{
					e[i].src= pic_add+name+type;
				}
			}
		}
	}
}

function change_link(post){
	if (post=="inv") {
		document.getElementById("high").style.display = 'none';
		document.getElementById("normal").style.display = 'inline';
	}
	if (post=="norm") {
		document.getElementById("normal").style.display = 'none';
		document.getElementById("high").style.display = 'inline';
	}
}

function switchStyle(s) {
	if (!document.getElementsByTagName) return;
	var el = document.getElementsByTagName("link");
	for (var i = 0; i < el.length; i++ ) {
		if (el[i].getAttribute("rel").indexOf("style") != -1 && el[i].getAttribute("title")) {
			el[i].disabled = true;
			if (el[i].getAttribute("title") == s) el[i].disabled = false;
		}
	}
}

// Set the cookie
function setCookie(name,value,days) {
	var domain = 'Empirica.biz';
	var path = '/';

	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

// Read the cookie
function readCookie(name) {
	var needle = name + "=";
	var cookieArray = document.cookie.split(';');
	for(var i=0;i < cookieArray.length;i++) {
		var pair = cookieArray[i];
		while (pair.charAt(0)==' ') {
			pair = pair.substring(1, pair.length);
		}
		if (pair.indexOf(needle) == 0) {
			return pair.substring(needle.length, pair.length);
		}
	}
	return null;
}

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;
		return elm['on' + evType];
	}
}