var base='http://www.tolkschumann.de/';
var chosenli;
var chiefDuration=500;
var clientDuration=9000;
var clientPeriodical;
var timers={};

function cineShowLi(li){
	li.morph({
		'opacity':1
	});

	li.div.morph({
		'background-color':'#054059',
		'padding':'2px',
		'margin':'2px'
	});

	var height=0;
	height+=li.getParent('ul').getElement('li#clientprogress').height;
	height+=parseInt(chosenli.getElement('blockquote').getStyle('height'))+parseInt(chosenli.getElement('blockquote').getStyle('padding-top'))+parseInt(chosenli.getElement('blockquote').getStyle('padding-bottom'));

	li.getParent('ul').morph({
		'height':height+'px'
	});
}

function cineUnshowLi(li){
	li.morph({
		'opacity':0
	});

	li.div.morph({
		'background-color':'#CCD9DE',
		'padding':'0',
		'margin':'4px'
	});
}

window.addEvent('load',function(){
	if(chosenli){
		cineShowLi(chosenli);
	}
});

window.addEvent('domready',function(){
	$('claim').setStyle('opacity',1);
	$('claim').set('morph',{duration:chiefDuration,transition:Fx.Transitions.Sine.easeOut});

	timers['claim']=(function(){$('claim').morph({
		'opacity':0
	})}).delay(chiefDuration);

	$('hassist').addEvents({
		'mouseenter':function(){
			timers['claim']=$clear(timers['claim']);

			$('claim').morph({
				'opacity':1
			});
		},
		'mouseleave':function(){
			timers['claim']=(function(){$('claim').morph({
				'opacity':0
			})}).delay(chiefDuration);
		}
	});

	$$('#content a.icon').each(function(a){
		var div=new Element('div',{
			'class':'iconbg'
		});

		div.set('morph',{duration:chiefDuration,transition:Fx.Transitions.Sine.easeOut});

		div.inject(a);

		a.addEvents({
			'mouseenter':function(){
				this.getElement('div.iconbg').morph({
					'background-color':'#054059'
				});
			},
			'mouseleave':function(){
				this.getElement('div.iconbg').morph({
					'background-color':'#CCD9DE'
				});
			}
		});
	});

	for(i=0;i<document.styleSheets.length;i++){
		var css=document.styleSheets[i];

		if(css.title=='nojs'){
			document.styleSheets[i].disabled=true;
		}
	};

	$('tools').setStyle('display','block');

	var forms=['search','login'];
	var chosenform;

	forms.each(function(form){
		$(form).setStyles({
			'opacity':0,
			'display':'block'
		});
		$(form).set('morph',{duration:chiefDuration,transition:Fx.Transitions.Sine.easeOut});

		$$('#tools .'+form).addEvents({
			'click':function(){
				Cookie.dispose('tools');

				forms.each(function(f){
					if(f==form&&f!=chosenform){
						$$('#tools .'+f).addClass('active');
						$(f).morph({'opacity':1});

						Cookie.write('tools',f);
					}else if(f==chosenform){
						var el=$$('#tools .'+f)[0];

						el.removeClass('active');
						$(f).morph({'opacity':0});

						el.getParent().getElement('div.bgassist').morph({
							'opacity':0
						});
					}
				});

				if(chosenform!=form){
					chosenform=form;
				}else{
					chosenform=false;
				}
			}
		});

		var cread=Cookie.read('tools');

		if(cread){
			$$('#tools .'+cread).addClass('active');
			$(cread).setStyles({'opacity':1});

			chosenform=cread;
		}
	});

	if(!(Browser.Engine.trident&&Browser.Engine.version<6)&&$('clients')){
		var lis=[];

		$('clients').set('morph',{duration:chiefDuration,transition:Fx.Transitions.Sine.easeOut});
		$$('#clients li').each(function(li){
			li.setStyles({
				'display':'block',
				'opacity':0
			});

			li.set('morph',{duration:chiefDuration,transition:Fx.Transitions.Sine.easeOut});
			li.addClass('client');

			lis.push(li);
		});

		if(lis.length){
			chosenli=lis[0];
			chosenli.setStyle('opacity',1);

			var n=new Element('li',{
				'id':'clientprogress'
			});

			var table=new Element('table');
			table.inject(n);

			var tr=new Element('tr');
			tr.inject(table);

			lis.each(function(li){
				var td=new Element('td',{
				});

				td.inject(tr);

				var div=new Element('div',{
					'class':'iconbg'
				});

				div.set('morph',{duration:chiefDuration,transition:Fx.Transitions.Sine.easeOut});

				div.inject(td);

				div.addEvents({
					'mouseenter':function(){
						this.morph({
							'background-color':'#054059'
						});
					},
					'mouseleave':function(){
						if(chosenli==div.li)
							return;

						this.morph({
							'background-color':'#CCD9DE'
						});
					},
					'click':function(){
						if(chosenli==div.li)
							return;

						clientPeriodical=$clear(clientPeriodical);

						cineUnshowLi(chosenli);
						chosenli=div.li;
						cineShowLi(chosenli);
					}
				});

				li.div=div;
				div.li=li;
			});

			n.inject($('clients'));
			n.height=parseInt(n.getStyle('height'));

			var switchLis=function(){
				for(i=0;i<lis.length;i++){
					if(chosenli==lis[i])
						break;
				}

				cineUnshowLi(chosenli);

				i++;
				if(i>=lis.length)
					i=0;

				chosenli=lis[i];

				cineShowLi(chosenli);
			}

			clientPeriodical=switchLis.periodical(clientDuration);
		}
	}

	var menus=['main'];

	menus.each(function(menu){
		$$('#'+menu+' a').each(function(a){
			if(!a.hasClass('active'))
				return;

			var img=a.getElement('img')

			if(!img)
				return;

			img.naming=img.src.match(/^(.+)\.(png)$/)

			img.active=img.naming[1]+'-active.'+img.naming[2];
			img.inactive=img.src;

			a.img=img;

			img.src=img.active;
		});
	});

	var menus=['holding'];

	menus.each(function(menu){
		$$('#'+menu+' a').each(function(a){
			var img=a.getElement('img')

			if(!img)
				return;

			img.naming=img.src.match(/^(.+)\.(png)$/)

			img.active=img.naming[1]+'-active.'+img.naming[2];
			img.inactive=img.src;

			a.img=img;

			if(!a.hasClass('active')){
				a.addEvents({
					'mouseenter':function(){
						this.img.src=this.img.active;
					},
					'mouseleave':function(){
						this.img.src=this.img.inactive;
					}
				});
			}else{
				img.src=img.active;
			}
		});
	});

	$$('#main a,#tools a').each(function(a){
		var el=new Element('div',{
			'class':'bgassist',
			'styles':{
				'opacity':a.hasClass('active')?1:0
			}
		});

		el.inject(a,'before');

		a.set('morph',{duration:chiefDuration,transition:Fx.Transitions.Sine.easeOut});
		el.set('morph',{duration:chiefDuration,transition:Fx.Transitions.Sine.easeOut});

		a.addEvents({
			'mouseenter':function(){
				this.getParent().getElement('div.bgassist').morph({
					'opacity':1
				});
			},
			'mouseleave':function(){
				if(this.hasClass('active'))
					return;

				this.getParent().getElement('div.bgassist').morph({
					'opacity':0
				});
			}
		});
	});

	$$('#sub a').each(function(a){
		if(a.hasClass('active'))
			return;

		var el=new Element('div',{
			'class':'bgassist',
			'styles':{
				'opacity':0
			}
		});

		el.inject(a,'before');

		a.set('morph',{duration:chiefDuration,transition:Fx.Transitions.Sine.easeOut});
		el.set('morph',{duration:chiefDuration,transition:Fx.Transitions.Sine.easeOut});

		a.addEvents({
			'mouseenter':function(){
				this.morph({
					'color':'#000'
				});

				this.getParent().getElement('div.bgassist').morph({
					'opacity':1
				});
			},
			'mouseleave':function(){
				this.morph({
					'color':'#fff'
				});

				this.getParent().getElement('div.bgassist').morph({
					'opacity':0
				});
			}
		});
	});

	$$('#holding a').each(function(a){
		if(a.hasClass('active'))
			return;

		var el=new Element('div',{
			'class':'bgassist',
			'styles':{
				'opacity':0
			}
		});

		el.inject(a,'before');

		el.set('morph',{duration:chiefDuration,transition:Fx.Transitions.Sine.easeOut});

		a.addEvents({
			'mouseenter':function(){
				this.getParent().getElement('div.bgassist').morph({
					'opacity':1
				});
			},
			'mouseleave':function(){
				this.getParent().getElement('div.bgassist').morph({
					'opacity':0
				});
			}
		});
	});
});