(function ($) {
	$.fn.tweets = function (options) {
		var $control = $(this);

		var socketurl = options.socketurl;
		var socketdata = options.socketdata;
		var maxresults = options.maxresults ? options.maxresults : 3;
		//overridable events
		var onsuccess = options.onsuccess;
		var onaftersuccess = options.onaftersuccess;
		var onfailed = options.onfailed;
		var onerror = options.onerror;
		var onnoresult = options.onnoresult;
		var ondatabind = options.ondatabind;
		var onbeforesend = options.onbeforesend;
		var onbeforedatabind = options.onbeforedatabind;

		if ('undefined' == typeof socketurl) { alert('Twitter: socketurl is undefined. Please enter the url of the corresponding socket.') }

		$.ajax({
			url: socketurl,
			data: socketdata,
			dataType: 'json',
			beforeSend: function (jqXHR, settings) {
				if ("undefined" != typeof onbeforesend) {
					onbeforesend(jqXHR, settings);
				}
			},
			success: function (data, textStatus, XMLHttpRequest) {
				if ("undefined" != typeof onsuccess) {
					// Custom implementation
					onsuccess(data, textStatus, XMLHttpRequest);
				}
				else {
					// Default implementation
					switch (data.status) {
						case '1': //Success
							if (0 == data.twitterresult.results.length) {
								//noresult!!
								if ("undefined" != typeof onnoresult) {
									onnoresult(data);
								}
								else {
									//Default no result logics
								}
							}
							else {
								if ("undefined" != typeof onbeforedatabind) {
									onbeforedatabind();
								}

								if ("undefined" != typeof ondatabind) {
									ondatabind(data);
								}
								else {
//									var $title = $control.find('h3');
									$control.children('.tweets').append('<ul></ul>');
									//seperate selection because .append() does not give back the ul properly
									var $ul = $control.find('.tweets > ul');

//									$(data.friendlysearchstrings).each(function () {
//										$title.text($title.text() + this + ' ');
//									});
									$(data.twitterresult.results).each(function (index) {
										var $result = this
										if (index >= maxresults) { return false; }
										var date = new Date($result.created_at.replace(/^\w+ (\w+) (\d+) ([\d:]+) \+0000 (\d+)$/, "$1 $2 $4 $3 UTC"));
										var strDate = date.getHours() + ":" + date.getMinutes() + ' ' + date.getDate() + '-' + (date.getMonth() + 1) + '-' + date.getFullYear();
										$ul.append('<li class="tweet' + index + '"><p>' + $.convertAllToLink($result.text, $result.from_user) + '</p><span class="date">' + strDate + '</span></li>');
									});

								}
								if ("undefined" != typeof onaftersuccess) {
									onaftersuccess(data, textStatus, XMLHttpRequest);
								}
							}
							break;
						default:
							if ("undefined" != typeof onfailed) {
								onfailed(data, textStatus, XMLHttpRequest);
							}
							break;
					}
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert('error: ' + textStatus);
				if ("undefined" != typeof onerror) {
					onerror(XMLHttpRequest, textStatus, errorThrown);
				}
			}
		});
	};
})(jQuery);
