var menu = new function () {

	this.moveTime = 300;

	this.colors = {	'blue':		'#3f8cb3',
					'green':	'#6ea749',
					'orange':	'#b88339',
					'purple':	'#6249a7',
					'red':		'#a74a49',
					'aqua':		'#49a3a7',
					'grey':		'#737373',
					'white':	'#ffffff',
					'pink':		'#b61b87'	};

	this.findIndex = function (link) {
		for (var i = 0; i < this.links.length; i++)
			if (this.links[i] == link)
				return i;
	}

	this.init = function () {
	/*	var b = 13;
		var out = '';
		for (var i = 7; i >= 0; i--) {
			out += ((b >> i) & 1) ? '1' : '0';
		}
		alert(out); */
	
		var table = document.getElementById('menu');
		this.links = table.getElementsByTagName('a');
		
		var schema = document.body.className.substr(7);

		this.color = (typeof this.colors[schema] != 'undefined') ? this.colors[schema] : this.colors['blue'];		
		
		
		for (var i = 0; i < this.links.length; i++) {
			if (this.links[i].parentNode.className.match(/active/))
				continue;
			this.links[i].onmouseover = function (e) {
				var style = this.style;

				new Motion(this, 'backgroundColor', null, false, '#000000', menu.color, menu.moveTime - 100);
				new Motion(this, 'backgroundColor', null, false, menu.color, '#ffffff', 100, menu.moveTime - 100);
				new Motion(this, 'backgroundColor', null, false, '#ffffff', menu.color, 100, menu.moveTime);
	
				new Motion(this, 'opacity', null, false, 0, 1,  menu.moveTime);
				new Motion(this, 'height', 'px', true, 40, 46,  menu.moveTime);
				new Motion(this, 'borderTopWidth', 'px', true, 0, 8,  menu.moveTime);
			}
			this.links[i].onmouseout = function (e) {
				var style = this.style;
				
				new Motion(this, 'backgroundColor', null, false, menu.color, '#000000', menu.moveTime);
				
				new Motion(this, 'opacity', null, false, 1, 0,  menu.moveTime);
				new Motion(this, 'opacity', null, false, 0, 1, 100,  menu.moveTime);
				new Motion(this, 'height', 'px', true, 46, 40,  menu.moveTime);
				new Motion(this, 'borderTopWidth', 'px', true, 8, 0,  menu.moveTime);
				
				window.setTimeout(function () { style.backgroundColor = 'transparent'; },  menu.moveTime);
			}
		}
		
	}
}






var submenu = new function () {

	this.rootlist;
	this.menus = new Object();
	this.moveTime = 300;
	this.height = new Object();
	this.opened = new Object();
	this.newsPerPage = 4;
	this.moveTime = 400;

	this.init = function (modus) {
		this.rootlist = document.getElementById('submenu').getElementsByTagName('ul')[0];
		if (modus == 'style')
			this.initNewsList(this.rootlist);
		else
			this.initList(this.rootlist);	
	}
	
	
	this.initNewsList = function (list) {
		var entries = list.getElementsByTagName('li');
		this.entries = entries;
		

		if (entries.length > this.newsPerPage) {
			var selector = document.createElement('div');
			selector.className = 'selector';
			list.parentNode.insertBefore(selector, list);
		}
		
		var page = 1;
		
		var act_loc = document.location.href.split('/');
		var same_max = 0;
		var open_page = 1;
		
		for (var i = 0; i < entries.length; i++) {
			entries[i].realHeight = entries[i].offsetHeight;
			entries[i].style.cursor = 'pointer';
			entries[i].style.overflow = 'hidden';
			entries[i].style.height = '58px';
			if (i >= this.newsPerPage) {
				entries[i].style.display = 'none';
			}
			
			entries[i].onmouseover = function (e) {
				var img = this.getElementsByTagName('img')[0];
				if (img) {
					new Motion(img, 'borderColor', null, false, menu.color, '#ffffff', submenu.moveTime);
					new Motion(img, 'opacity', null, false, 0.5, 1, submenu.moveTime);
				}
				if (this.realHeight > 58) {
					new Motion(this, 'height', 'px', 'true', 58, this.realHeight, submenu.moveTime);
				}
			}
			
			entries[i].onmouseout = function (e) {
				var img = this.getElementsByTagName('img')[0];
				if (img) {
					new Motion(img, 'borderColor', null, false, '#ffffff', menu.color, submenu.moveTime);
					new Motion(img, 'opacity', null, false, 1, 0.5, submenu.moveTime);
				}
				if (this.realHeight > 58) {
					new Motion(this, 'height', 'px', 'true', this.realHeight, 58, submenu.moveTime);
				}
			}
			
			entries[i].onclick = function (e) {
				var link = this.getElementsByTagName('a')[0];
				document.location.href = link.href;
			}




			
						
			
			if (selector && (i % this.newsPerPage == 0)) {
				var button = document.createElement('span');
				button.className = i == 0 ? 'active' : '';
				button.style.cursor = 'pointer';
				button.onclick = function (e) {
					var index;
					for (var j = 0; j < submenu.buttons.length; j++) {
						if (submenu.buttons[j] == this) {
							index = j;
						}
					}
					new Motion(submenu.rootlist, 'opacity', null, false, 1, 0, submenu.moveTime);
					window.setTimeout('submenu.setNewsDisplay('+index+')', submenu.moveTime);
					new Motion(submenu.rootlist, 'opacity', null, false, 0, 1, submenu.moveTime, submenu.moveTime);
				}
				
				button.appendChild(document.createTextNode(page));
				page++;
				
				selector.appendChild(button);
			}
			
			
			var link_loc = entries[i].getElementsByTagName('a')[0].href.split('/');
			var samecount = 0;
			for (var j = 0; j < act_loc.length; j++) {
				if (typeof link_loc[j] != 'undefined' && link_loc[j] == act_loc[j])
					samecount++;
				else
					break;
			}
			if (samecount > same_max) {
				same_max = samecount;
				open_page = page - 1;
			}
			 
			
			
		}
		
		
		
		
		
		if (selector) {
			this.buttons = selector.getElementsByTagName('span');
			
			selector.style.paddingLeft = Math.round((selector.offsetWidth - selector.childNodes.length*selector.firstChild.offsetWidth) / 2) + 'px';
			this.setNewsDisplay(open_page - 1);
		}
		
		var images = list.getElementsByTagName('img');
		for (var i = 0; i < images.length; i++)
			images[i].style.opacity = 0.5;
	}
	
	this.setNewsDisplay = function (index) {
		for (var j = 0; j < this.entries.length; j++) {
			this.entries[j].style.display = (j >= index*this.newsPerPage && j < (index+1)*this.newsPerPage) ? 'block' : 'none';
		}
		for (var i = 0; i < this.buttons.length; i++) {
			this.buttons[i].className = (i == index) ? 'active' : '';
		}
	}
	
	this.initList = function (list) {
		var link,parentList;
		var entries = list.getElementsByTagName('li');
		for (var i = 0; i < entries.length; i++) {
			parentList = entries[i].parentNode;
			if (parentList.className == 'level1') {
				parentList.id = "list"+i;
				this.height[parentList.id] = parentList.offsetHeight;
				this.opened[parentList.id] = true;
			}
		}
		
		for (var i = 0; i < entries.length; i++) {
			parentList = entries[i].parentNode;
			link = entries[i].getElementsByTagName('a')[0];
			
			if (parentList.className == 'level0') {
				link.onclick = function (e) {
					var childList = this.parentNode.getElementsByTagName('ul')[0];
					if (childList) {
						if (submenu.opened[childList.id])
							submenu.close(childList);
						else {
							var list;
							for (var j in submenu.opened) {
								if (j != childList.id && submenu.opened[j]) {
									if (list = document.getElementById(j)) {
										submenu.close(list);
									}
								}
							}
							submenu.open(childList);
						}
					} else return true;
					return false;
				}
				
				var childList = link.parentNode.getElementsByTagName('ul')[0];
				childList.style.height = '0px';
				this.opened[childList.id] = false;
			}
			
			
			if (link.className == 'opened') {
				var root = parentList;
				if (root.className != 'level0')
					while (root.className != 'level1')
						root = root.parentNode;
				for (var j in this.opened)
					if (j != root.id) {
						if (list = document.getElementById(j)) {
							list.style.height = '0px';
							this.opened[list.id] = false;
						}
					} else {
						if (list = document.getElementById(j)) {
							list.style.height = this.height[list.id]+'px';
							this.opened[list.id] = true;
						}
					}	
			}
			
		}

	}
	
	this.open = function (list) {
		new Motion (list, 'height', 'px', true, 0, this.height[list.id], this.moveTime);
		new Motion (list, 'opacity', null, false, 0, 1, this.moveTime);
		this.opened[list.id] = true;
	}
	
	this.close = function (list) {
		new Motion (list, 'height', 'px', true, this.height[list.id], 0, this.moveTime);
		new Motion (list, 'opacity', null, false, 1, 0, this.moveTime);
		this.opened[list.id] = false;
	}
}