// mootools 1.2 necessary for script to work

// open external links in new window
var externalLinks = {
	
	// CSS CLASSES
	externalLinkClass: 'external_link',
	externalLinkTitle: 'öffnet in neuem Fenster',
	
	init: function() {
		var links = $$('a.'+externalLinks.externalLinkClass);
		for(var i=0; i<links.length; i++) {
			links[i].target = "_blank";
			links[i].title += " ("+externalLinks.externalLinkTitle+")";
		};
	}
};

// add text-resize widget to page
var textResize = {
	
	// CSS CLASSES AND IDS
	insertAfterID: 'textResizeHeading', // ID of the element that the textResize Widget is inserted after
	textResizeID: 'textResize',
	textResizeIDSmaller: 'smaller',
	textResizeIDNormal: 'normal',
	textResizeIDBigger: 'bigger',
	currClass: 'current',
	
	// TEXTS
	textResizeTitleSmaller: 'Schrift verkleinern',
	textResizeTitleNormal: 'Normale Schriftgröße',
	textResizeTitleBigger: 'Schrift vergrößern',
	
	// STYLESHEET TITLE
	smallerTitle: 'Kleine Schrift',
	normalTitle: 'Standard',
	biggerTitle: 'Grosse Schrift',
	
	// GLOBAL VARIABLES
	activeStyleTitle: '',
	cookieName: 'styleTitle',
	
	init: function() {
		if (!document.getElementsByTagName) return;
		
		// set preferred Stylesheet saved in Cookie
		var cookie = textResize.readCookie(textResize.cookieName);
		textResize.activeStyleTitle = (cookie) ? cookie : textResize.normalTitle;
		textResize.setActiveStylesheet(textResize.activeStyleTitle);
		
		var insertInto = $(textResize.insertAfterID).getParent();
		
		// create list
		var textResizeList = new Element('ul', {id: textResize.textResizeID});
		
		// create List items
		var tmpLi1 = new Element('li');
		// create link
		var tmpA1 = new Element('a', {
			href: '#',
			title: textResize.textResizeTitleSmaller,
			id: textResize.textResizeIDSmaller
		});
		tmpA1.set('text', textResize.textResizeTitleSmaller);
		tmpA1.onclick = function() {
			textResize.setActiveStylesheet(textResize.smallerTitle, this);  
			return false;
		}
		
		// create List items
		var tmpLi2 = new Element('li');
		// create link
		var tmpA2 = new Element('a', {
			href: '#',
			title: textResize.textResizeTitleNormal,
			id: textResize.textResizeIDNormal
		});
		tmpA2.onclick = function() {
			textResize.setActiveStylesheet(textResize.normalTitle, this); 
			return false;
		}
		tmpA2.set('text', textResize.textResizeTitleNormal);
		
		// create List items
		var tmpLi3 = new Element('li');
		// create link
		var tmpA3 = new Element('a', {
			href: '#',
			title: textResize.textResizeTitleBigger,
			id: textResize.textResizeIDBigger
		});
		tmpA3.onclick = function() {
			textResize.setActiveStylesheet(textResize.biggerTitle, this); 
			return false;
		}
		tmpA3.set('text', textResize.textResizeTitleBigger);
		
		// set current link depending on preferred Stylesheet
		switch(textResize.activeStyleTitle) {
			
			case textResize.smallerTitle:
				tmpA1.addClass(textResize.currClass);
				break;
				
			case textResize.biggerTitle:
				tmpA3.addClass(textResize.currClass);
				break;
				
			default:
				tmpA2.addClass(textResize.currClass);
		}
		
		// append list items to list
		tmpLi1.appendChild(tmpA1);
		textResizeList.appendChild(tmpLi1);
		tmpLi2.appendChild(tmpA2);
		textResizeList.appendChild(tmpLi2);
		tmpLi3.appendChild(tmpA3);
		textResizeList.appendChild(tmpLi3);
		
		// append list to body
		insertInto.appendChild(textResizeList);		
	},
	
	setClass: function(elm) {
		var list = elm.getParent().getParent();
		listLinks = list.getElements('a');
		
		for(var i=0; i<3; i++) {
			if(listLinks[i].hasClass(textResize.currClass)) {
				listLinks[i].removeClass(textResize.currClass);
			}
		}
		elm.addClass(textResize.currClass);
	},
	
	setActiveStylesheet: function(styleTitle, link) {
		
		// set calss for active link
		if(link) {
			textResize.setClass(link);
		}
		
		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].getAttribute("media") != "print" ) {
				el[i].disabled = true;
				//el[i].rel = "alternate stylesheet";
				//enable selected stylesheet
				if(el[i].getAttribute("title") == styleTitle) {
					el[i].disabled = false;
					//el[i].rel = "stylesheet";
					textResize.activeStyleTitle = styleTitle;
					// set cookie with current stylesheet
					textResize.createCookie("styleTitle", styleTitle, 365);
				}
			}
		}
	},
	
	createCookie: function(name, value, days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			var expires = '; expires=' + date.toGMTString();
		} 
		else {
			expires = '';
		}
		document.cookie = name + '=' + value + expires + '; path=/';
	},

	readCookie: function(name) {
		var nameEQ = name + '=';
		var ca     = document.cookie.split(';');
	
		for (var i = 0; i < ca.length; i++) {
			var c = ca[i];
	
			while (c.charAt(0) == ' ') {
				c = c.substring(1, c.length);
			}
	
			if (c.indexOf(nameEQ) == 0) {
				return c.substring(nameEQ.length, c.length);
			}
		}
		return null;
	}
	
};

var equalHeights = {
	
	// CLASSES
	triggerClass: 'equal_height', // class for elements that should have equal height
	
	// VARIABLES
	boxPadding: 15, // padding of boxes
	maxh: 0,
	boxes: Array(),
	num: 0,
	op_test: false,
	
	init: function() {
		// get all direct children of trigger element
		this.boxes = $$('.'+this.triggerClass);
		this.num = this.boxes.length;
		for (var i=0;i<this.num;i++) if (!this.boxes[i]) return;
		this.maxheight();
		for (var i=0;i<this.num;i++) this.boxes[i].style.height = this.maxh+"px";
	},
	
	maxheight: function() {
		var heights = new Array();
		for (var i=0;i<this.num;i++) {
			if (navigator.userAgent.toLowerCase().indexOf('opera') == -1) {
				heights.push($(this.boxes[i]).scrollHeight);
			} else {
				heights.push($(this.boxes[i]).offsetHeight);
			}
		}
		heights.sort(this.sortNumeric);
		this.maxh = heights[this.num-1]-(2*this.boxPadding);
	},
	
	sortNumeric: function(f,s) {
		return f-s;
	}
}



// INITIALISIERUNG ALLER FUNKTIONEN NACH LADEN DER SEITE
window.addEvent('domready', function() {
	// add CSS class to body tag to indicate that JavaScript is available
	$$("body").addClass('has_js');
	
	externalLinks.init();
	textResize.init();
	equalHeights.init();
});