var logo_count = 1;

$(function() {   
	$("#homeblok1 #header a").hover(function() {
		selectTab(this);
	}).click(function() {
		selectTab(this);
	});
	$("#homeblok4 #prev").click(function() {
		logo_count--;
		reloadLogos();
		return false;
	});	
	$("#homeblok4 #next").click(function() {
		logo_count++;
		reloadLogos();
		return false;
	});
	
	var settings = { verticalDragMaxHeight: 40 };
	
	$('#nieuws .content').jScrollPane(settings);
	$('#blog .content').jScrollPane(settings);
	$('#twitter .content').jScrollPane(settings);

	load_tweets();
	load_blog();	
	
});

function selectTab(e) {
	$("#homeblok1 #content > div").hide();
	$("#homeblok1 #header a.active").removeClass("active");
	var current = $(e).addClass("active").attr("tab");
	$('#homeblok1 #content div[id="' + current + '"]').show();
	return false;
}

function reloadLogos() {
	if (logo_count < 1) {
		logo_count = 3;
	} else if (logo_count > 3) {
		logo_count = 1;
	}
	$("#homeblok4 .content").hide();
	$("#homeblok4 #logo" + logo_count).show();
}

function load_blog() {
	if ($('#blog .content').length > 0) {
		var api = $('#blog .content').data('jsp');
		var url = "/proxy.php";
		jQuery.getFeed({
			url: url,
			success: function(feed) {			
				for(var i = 0; i < feed.items.length && i < 5; i++) {            
					var item = feed.items[i];
					if (i == 0) {
						html = '<h2>' + item.title + '</h2>';				               
						html += '<p class="feature">' + item.description.substr(0, 100);				
						html += '... ';
						html += '<a target="_blank" class="feature" href="' + item.link + '">';
						html += '&gt; lees meer</a></p><div class="breaker"></div>';
					} else {
						html = '<a target="_blank" class="meer" href="' + item.link + '">' + item.title + '</a>';
					}
					//$("#blog .content").append(html);				
					api.getContentPane().append(html);
					api.reinitialise();
				}
			}
		});
	}
}

function load_tweets() {
	 if($('#twitter .content').length > 0) {
		 var api = $('#twitter .content').data('jsp');
		 var last_ID = $(".tweet:first").attr("ID");     
		 var url = "http://search.twitter.com/search.json?q=blinkerbv&rpp=4&";
		 if (last_ID) {
			  last_ID = last_ID.replace("tweet", "");
			  url += "since_id=" + last_ID + "&callback=?";
		 } else {
			  url += "callback=?"; 
		 }
		 
		 $.getJSON(url, function(json) { 
			  $(json.results).each(function() {
				   if (this.id == undefined) return;
				   var div = "<div class='tweet' id='tweet" + this.id + "'><a href='http://twitter.com/" + this.from_user + "' target='_blank'><img width='24' height='24' alt='" + this.from_user + "' src='" + this.profile_image_url + "' /></a><div>" + flufUrls(this.text) + "</div></div>";
				   if (last_ID) {
						//$("#twitter .content").prepend(div);
						api.getContentPane().prepend(div);					
				   } else {
						//$("#twitter .content").append(div);
						api.getContentPane().append(div);
				   }
				   api.reinitialise();				   
			  });
		 });

		 $(".tweet:gt(4)").remove();
	 }
}

function flufUrls(t) {
	t += " ";
	t = t.replace(/([htps]+:\/\/[\w\d-_]+\.[\w\d-_:%&amp;\?\/.=]+)/gim, '<a href="$1" target="_blank">$1</a> ');
	t = t.replace(/[@]+([\w\d-_]+)/gim, '<a href="http://twitter.com/$1" target="_blank">@$1</a>' );	
	t = t.replace(/[#]+[\w\d-_]+/gim, function(t) {
		var tag = t.replace("#","%23")
		return '<a href="http://search.twitter.com/search?q=' + tag + '" target="_blank">' + t + '</a>';
	});
	return t;
}
