//MooTools More, <http://mootools.net/more>. Copyright (c) 2006-2009 Aaron Newton <http://clientcide.com/>, Valerio Proietti <http://mad4milk.net> & the MooTools team <http://mootools.net/developers>, MIT Style License.

MooTools.More={version:"1.2.4.4",build:"6f6057dc645fdb7547689183b2311063bd653ddf"};var Asset={javascript:function(f,d){d=$extend({onload:$empty,document:document,check:$lambda(true)},d);
if(d.onLoad){d.onload=d.onLoad;}var b=new Element("script",{src:f,type:"text/javascript"});var e=d.onload.bind(b),a=d.check,g=d.document;delete d.onload;
delete d.check;delete d.document;b.addEvents({load:e,readystatechange:function(){if(["loaded","complete"].contains(this.readyState)){e();}}}).set(d);if(Browser.Engine.webkit419){var c=(function(){if(!$try(a)){return;
}$clear(c);e();}).periodical(50);}return b.inject(g.head);},css:function(b,a){return new Element("link",$merge({rel:"stylesheet",media:"screen",type:"text/css",href:b},a)).inject(document.head);
},image:function(c,b){b=$merge({onload:$empty,onabort:$empty,onerror:$empty},b);var d=new Image();var a=document.id(d)||new Element("img");["load","abort","error"].each(function(e){var g="on"+e;
var f=e.capitalize();if(b["on"+f]){b[g]=b["on"+f];}var h=b[g];delete b[g];d[g]=function(){if(!d){return;}if(!a.parentNode){a.width=d.width;a.height=d.height;
}d=d.onload=d.onabort=d.onerror=null;h.delay(1,a,a);a.fireEvent(e,a,1);};});d.src=a.src=c;if(d&&d.complete){d.onload.delay(1);}return a.set(b);},images:function(d,c){c=$merge({onComplete:$empty,onProgress:$empty,onError:$empty,properties:{}},c);
d=$splat(d);var a=[];var b=0;return new Elements(d.map(function(e){return Asset.image(e,$extend(c.properties,{onload:function(){c.onProgress.call(this,b,d.indexOf(e));
b++;if(b==d.length){c.onComplete();}},onerror:function(){c.onError.call(this,b,d.indexOf(e));b++;if(b==d.length){c.onComplete();}}}));}));}};



var gadget, Gadget = new Class({
	
	info : [],
	maxNewsItems : 5,
	timer : null,
	current : 0,
	doNotAnimate : false,
	backToItem : null,
	
	fixNews : function()
	{
		if($$('#jsfeeddump table').length > 0)
		{
			$$('#jsfeeddump .entry_rss').each(function(item,i){
				if(i >= this.maxNewsItems)return;
				
				var link = item.getElements('a')[0].get('href');
				var hl = item.getElements('a')[0].get('text');
				var teaser = item.get('text').replace(hl,'').trim(); 
				
				var hasDate = teaser.split('INSTAFAIL');//quick fix
				
				var date = hasDate.length > 1?hasDate[0]:null;
	
				teaser = hasDate.getLast();
				
				var h3 = new Element('h3').inject('rechts');
				
				var a = new Element('a',{ href : link, target : '_blank' }).inject(h3);
				
				if(date)new Element('span',{ 'class' : 'date', text :(date)}).inject(a);
				new Element('span',{ 'class' : 'title', text : hl }).inject(a);
				new Element('span',{ 'class' : 'teaser', text :(teaser)}).inject(a);
				
			},this);
			
			$$('#rechts h3').addEvents({
				mouseenter : function(){
					this.tween('backgroundColor','#eee');	
				},
				mouseleave : function(){
					this.tween('backgroundColor','#fff');
				}
				
			});
		}
	},
	
	initialize : function(){
		
		
		$$('#links h1').each(function(item,i){
			if(i != 0)item.setStyle('paddingTop',10);
		});
		
		if(!(window.location + '').match(/^http/))
		{
		
			new Element('div',{
				id : 'gadgeterror',
				text : 'Das AJAX funktioniert nur wenn die Seite über HTTP aufgerufen wird',
				styles : {
					opacity : 0
				}
			}).inject('bg').tween('opacity',0.7);
		
			return;
		}
		
		
		
		new Request({
			url : 'teaser.xml',
			onSuccess : function($xmlx, $xml){
				gadget.go($xml.getElementsByTagName('item'));
			}
		}).get();
		
	},

	processNode : function($pos,$node)
	{
		var obj = {};
		obj.active =  false; //$pos == 0;
		
		for(var i = 0, item; item = $node.childNodes[i]; i++)
		{
			if(item.tagName){
				obj[item.tagName] = item.textContent || item.text;
			}
			
		}
		
		this.info[$pos] = obj;
		
		
	},
	
	go : function($items)
	{
		new Element('div',{
					styles : {
						width : 990,
						height : 318,
						position : 'absolute',
						opacity : 0
					}
					}).inject('bg');
		
		var gul = new Element('ul',{
				id : 'gadgetcontent'
			}).inject('gadget');
		
		new Element('div',{
			'id' : 'gactive',
			styles : {
				opacity : 0, 
				display : 'block'
			}
		}).inject('gadget');
		
		$('gadget').addEvents({
			mouseenter : function(){
				if(!gadget.doNotAnimate)gadget.stopTimer();
			},
			mouseleave : function(){
				if(!gadget.doNotAnimate && !gadget.backToItem)gadget.startTimer();
				if(gadget.backToItem)$$('#gadget li a')[gadget.backToItem].fireEvent('mouseenter');
			}
		});
		
		var active = false;
		
		for(var i = 0, item; item = $items[i]; i++)
		{
			this.processNode(i,item);
			if(window.location.pathname.match(new RegExp(this.info[i].href+"$")) && window.location.pathname.indexOf('.html') > -1)
			{
				active = i;
				this.doNotAnimate = true;
				this.backToItem = i;
			}
			
		}
		
		if(active){
			this.info[active].active = true;	
		}
		else
		{
			this.info[0].active = true;	
		}

		
		for(i = 0, item; item = this.info[i]; i++)
		{



			
			if(this.info[i].active)
			{
				$('bg').setStyle('backgroundImage','url(content/'+this.info[i].image+')');
			}
			
			var li = new Element('li').inject(gul);
			var a = new Element('a',{
				href : this.info[i].href,
				target : this.info[i].openblank ? '_blank':'',
				'class' : this.info[i].active?'active':'',
				events : {
					mouseenter : function(){
						$('gactive').tween('top',this.getPosition('gadget').y + 23);
						$$('#gadget .active')[0].removeClass('active').tween('opacity',0.6);
						this.addClass('active').tween('opacity',1);
						
						
						$$('#bg div')[0].set({
											 
											 tween : {
												duration : 666,
												target : this,
												onComplete : function(){
													$('bg').setStyle('backgroundImage', this.subject.getStyle('backgroundImage'));
													this.options.target.addClass('active').setStyle('opacity',1);
													this.subject.setStyle('opacity',0);
												}
											 },
											 styles : {
												backgroundImage: 'url(content/'+
																	gadget.info[
																		this.retrieve('num')
																	].image
																	+')'												 
											 }
											 
											 }).tween('opacity',1);
					},
					mouseleave : function(){
						this.tween('opacity',0.6);
					}					
				},
				tween : {
					duration : 333
				},
				styles : {
					opacity : this.info[i].active?1:0.6
				}				
			}).store('num',i).inject(li);
			
			a.appendText(this.info[i].navi0);
			new Element('br').inject(a);
			new Element('strong',{ text : this.info[i].navi1 }).inject(a);
			
		}
		
		
		
		
		$('gactive').setStyle('top',
			$$('#gadget a.active')[0].getPosition('gadget').y + 23
		).tween('opacity',1);
		
		if(!this.doNotAnimate)this.startTimer();
		
	},
	
	startTimer : function(){
		this.timer = this.autoPlay.periodical(9000,this);
	},
	
	stopTimer : function(){
		$clear(this.timer);
	},
	
	autoPlay : function(){

		$$('#gadget li a')[this.current].fireEvent('mouseleave');
		this.current = (this.current >= this.info.length - 1)?0:this.current + 1;
		$$('#gadget li a')[this.current].fireEvent('mouseenter');
	}
	
});

$(window).addEvent('load',function(){
	gadget = new Gadget();
	gadget.fixNews();//erster versuch
	//gadget.timer = gadget.fixNews.periodical(300);
});

