/*jshint asi: false, bitwise: true, boss: false, curly: true, debug: false, devel: false, eqeqeq: true, evil: false, forin: true, immed: true, laxbreak: false, newcap: true, noarg: true, noempty: true, nonew: true, nomen: true, onevar: true, plusplus: true, regexp: false, undef: true, sub: false, strict: true, white: false*/
/*global jQuery, console, baseUrl, window */
(function ($) {	
	"use strict";
	
	if ($("#nieuws")[0]) {
		var twitter	=	function (options) {
			var tweets,
			// Template and escapeRegExp function, stolen from underscore.js
			_escapeRegExp	=	function (s) {
				return s.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1');
			},
			_template	=	function (str, data) {
				var c		=	{
					start:		'<%',
					end:		'%>',
					interpolate:	/<%=(.+?)%>/g
				},
				endMatch	=	new RegExp("'(?=[^"+c.end.substr(0, 1)+"]*"+_escapeRegExp(c.end)+")","g"),
				fn		=	new Function('obj',
					'var p=[],print=function(){p.push.apply(p,arguments);};' +
					'with(obj){p.push(\'' +
					str.replace(/[\r\t\n]/g, " ")
					.replace(endMatch,"\t")
					.split("'").join("\\'")
					.split("\t").join("'")
					.replace(c.interpolate, "',$1,'")
					.split(c.start).join("');")
					.split(c.end).join("p.push('")
					+ "');}return p.join('');"
				);
				
				return data ? fn(data) : fn;
			},
			
			// Output twitter feed
			output		=	function (target, template) {
				var output	=	"",
				template	=	_template(template);
				
				for (var i = 0; i < tweets.length; i += 1) {
					output	+=	template(tweets[i]);
				}
				
				output	=	output.replace(/(http:\/\/[\w-.\/]+)/g, "<a href='\$1' target='_blank'>\$1</a>");
				
				$(target).html(output);
				
				if (options.load) {
					options.load();
				}
			},
			
			// Load twitter feed
			load		=	function (username, amount) {
				$.getJSON(
					"http://twitter.com/status/user_timeline/" + username + ".json?callback=?&count=" + amount,
					function (data) {
						tweets	=	data;
						output(options.target, options.template);
					}
				);
			};
			
			load(options.username, options.amount);
		};
		var username	=	"makelaardongen";
		twitter({
			username:	username,
			amount:		3,
			target:		".twitter .tweets",
			template:	'<div class="tweet"><div class="text"><p><%=text%></p></div><a href="#" class="bottom">' + username + '</a></div>',
			load:		function ( ) {
				$(".twitter .tweets").removeClass("loading");
				$(".twitter .tweets .tweet:last").addClass("last");
			}
		});
	}
}(jQuery));


