// JavaScript Document

var Twitter = new Class({
	Implements: [Events],
	initialize: function() {
		this.active = 0;
		this.height = 16;
		this.firstRun = true;
		this.container = $("twitterContainer");
		this.container.set('morph');
		this.today = new Date();
		this.loader();
	},
	autolink: function(text){
		return text.replace(/(https?:\/\/[\w\-:;?&=+.%#\/]+[\w\-:;?&=+%#\/])/gi, '<a href="$1">$1</a>')
		   .replace(/(^|\W)@(\w+)/g, '$1<a href="http://twitter.com/$2" target="_blank">@$2</a>')
		   .replace(/(^|\W)#(\w+)/g, '$1<a href="http://search.twitter.com/search?q=%23$2" target="_blank">#$2</a>');
	},
	loader: function() {
		new Request.HTML({
			url:"http://"+document.location.href.toURI().get("host")+"/twitter.php",
			method:"get",
			onComplete:function(a,b,response){
				this.response = response;
				this.container.empty().set("html",this.response);				
				$$("#twitter .tweetText, .tweet2-text").each(function(tweet){
					tweet.set("html",this.autolink(tweet.get("html")));
				}.bind(this));
				this.allTweets = $$(".tweet");
				this.allTweets.setStyle("display","none");
				this.recentTweets = this.allTweets.filter(function(tweet){
					return new Date().parse(tweet.getElement("span.date").get("html")).diff(this.today) < 8;
				}.bind(this));	
				$$("#twitter .date, .date2").each(function(date){								
					date.set({"html":this.prettyDate(date.get("html"))});
				}.bind(this));		
				this.tweets = this.recentTweets;
				this.twitter();					
			}.bind(this)
		}).get();
	},
	twitter: function(){				
		if(this.tweets.length>0) {						
			this.height = 82;	
			this.container.morph({'height': this.height});		
			this.container.addEvents({
				'mouseenter': this.pause.bind(this),
				'mouseleave': this.resume.bind(this)
			});	
			this.showFirst();
		} else {
			//this.container.morph({'height': 32});
			var s1 = new Element("span").setStyle("color","grey").set("html","Er zijn momenteel geen actuele tweets.").inject(this.container);	
			//var s2 = new Element("br").inject(this.container);
			//var s3 = new Element("a",{href:"#"}).set("html","Toon oudere tweets").addEvent("click",function(){
			//	s1.destroy();
			//	s2.destroy();
			//	s3.destroy();
			//	this.tweets = this.allTweets;				
			//	this.twitter();
			//}.bind(this)).inject(this.container);
		}
		if(this.tweets.length>1){
			this.showFirst();
			this.run();
		}
	},
	run: function(){
		this.elSched = this.showTweet.periodical(5000,this);
	},
	pause: function(){
		this.elSched = clearInterval(this.elSched);
		var size = this.container.getScrollSize().y;
		if(size > this.height){			
			this.container.morph({'height': size});
		}
	},	
	resume: function(){		
		if(this.tweets.length>1) this.run();
		this.container.morph({'height': this.height});
	},
	prettyDate:function(time){
		var date = new Date().parse(time);
		return date.timeDiffInWords();
	},	
	showFirst: function(){
		var el = this.tweets[this.active];
		el.setStyle("display","block");
		new Fx.Tween(el,{transition:Fx.Transitions.easeIn}).start("opacity",1);
	},
	showTweet: function(){
		var old = this.tweets[this.active];
		if(old){
			new Fx.Tween(old,{
				transition:Fx.Transitions.easeOut,
				onComplete:function(){
					old.setStyle("display","none");	
				}
			}).start("opacity",0);
		}				
		this.active = this.active + 1;
		if(this.active == this.tweets.length){
			this.active = 0;				
		}
		var el = this.tweets[this.active];
		el.setStyle("display","block");
		new Fx.Tween(el,{transition:Fx.Transitions.easeIn}).start("opacity",1);
	}
});

window.addEvent("domready",function(){
	Locale.use('nl-NL');
	if(Browser.Platform.ios||Browser.Platform.webos||Browser.Platform.android||Browser.Platform.other){
		$("emma").getElement("a").removeClass("site").set("target","_blank");			
		var isiPad = navigator.userAgent.match(/iPad/i) != null;

		$$("#logo, #top").setStyle("position","relative");
$("logo").setStyle("top",16);
$("page-outer").setStyle("padding-top",30);
	} else {
		document.body.setStyle("background-image","url(http://graspiepers.nl/image/background.jpg)");
	}
	$$('a.site').cerabox({
		group: false,
		fixedPosition:true,
		width:"95%",
		height:"95%"
	});
	$$('.fotoalbum a').cerabox({
		animation: 'ease',
		fixedPosition:true,
		loaderAtItem: true,
		displayTitle: false
	});
	checkStandenTabel();
	stripeRows();
	var tips = new Tips('.tooltip');
	
	$$(".component ul li a").each(function(el){
		var height = el.getCoordinates().height;
		el.getParent("li").setStyle("height",height);
		el.setStyle("height",height-4);
	});
	
	
});

function checkStandenTabel(){
	var rows = $$("#standentabel tr");
	rows.each(function(row, index){	
		if(index==0){
			row.className="noStripe";
		}
		var tdCollection = row.getElements("td");
		if (tdCollection.length != 0){									
			var value = tdCollection[1].innerHTML;
			if (value.indexOf('Graspiepers',0)!= -1){
				row.style.fontWeight = "bold";
			}
		}
	});
}
function stripeRows(){
	var tableBodies = $$('tbody');
	tableBodies.each(function(bod){
		if(bod.className!="noStripe"){
			var tableRows = bod.getElements("tr");		
			var odd = false;
			tableRows.each(function(row){
				if(row.className!="noStripe"){
					if(row.className=="copyStripe"){
						if(odd==true){
							odd=false;
						} else {
							odd=true;
						}
					}
					if(odd == true) {
						row.style.backgroundColor = "#fff";
						odd = false;
					} else {				
						row.style.backgroundColor = "#eee";
						odd = true;
					}
				}			
			});	
		} else {
			
		}
	});
}
