TWBL = window.TWBL || {};
(function() {
	if (TWBL && TWBL.Widget) {
		return;
	}

	TWBL.Widget = function(opts) {
		this.init(opts);
	};

	(function() {

		/**
		 * The Twitalinkahashifyer! http://www.dustindiaz.com/basement/ify.html
		 * Eg: ify.clean('your tweet text');
		 */

		var ify = {
			link : function(tweet) {
				return tweet
						.replace(
								/\b(((https*\:\/\/)|www\.).+?)(([!?,.\)]+)?(\s|$))/g,
								function(link, m1, m2, m3, m4) {
									var http = m2.match(/w/) ? 'http://' : '';
									return '<a class="twbl_link" style="color:'+TWBL.Widget.LINKCOLOR+'" target="_blank" rel="nofollow" href="'
											+ http
											+ m1
											+ '">'
											+ ((m1.length > 25) ? m1.substr(0,
													24) + '...' : m1) + '</a>'
											+ m4;
								});
			},

			at : function(tweet) {
				return tweet.replace(/\B\@([a-zA-Z0-9_]{1,20})/g, function(m,
						username) {
					return '@<a class="twbl_link" style="color:'+TWBL.Widget.LINKCOLOR+'" target="_blank" rel="nofollow" href="http://twitter.com/'
							+ username + '">' + username + '</a>';
				});
			},

			list : function(tweet) {
				return tweet
						.replace(
								/\B\@([a-zA-Z0-9_]{1,20}\/\w+)/g,
								function(m, userlist) {
									return '@<a class="twbl_link" style="color:'+TWBL.Widget.LINKCOLOR+'" target="_blank" rel="nofollow" href="http://twitter.com/'
											+ userlist
											+ '">'
											+ userlist
											+ '</a>';
								});
			},

			hash : function(tweet) {
				return tweet
						.replace(
								/\B\#(\w+)/gi,
								function(m, hash) {
									return '<a class="twbl_link" style="color:'+TWBL.Widget.LINKCOLOR+'" target="_blank" rel="nofollow" href="http://twitter.com/search?q=%23'
											+ hash + '">#' + hash + '</a>';
								});
			},

			clean : function(tweet) {
				return this.hash(this.at(this.list(this.link(tweet))));
			}
		};
		function Tweet(tweet) {
			var html = '<div style="padding: 6px 8px;overflow: hidden;"> \
        <div style="float: left;overflow: hidden;display: block;"> \
          <div><a target="_blank" rel="nofollow" href="http://twitter.com/'
					+ tweet.user
					+ '"><img style="width: 30px;height: 30px;border:0;" alt="'
					+ tweet.user
					+ ' profile" src="'
					+ tweet.avatar
					+ '"></a></div> \
        </div> \
        <div style="margin-left: 40px;"> \
          <p class="twbl_twtext" style="margin:0 !important;color:' + this.twbl_twtext + '"> \
            <a class="twbl_link" style="color:'+this.twbl_link+'" target="_blank" rel="nofollow" href="http://twitter.com/'
					+ tweet.user + '">' + tweet.user + '</a> ' + tweet.tweet;
			if (tweet.created_at != 0) {
				html += '<i style="font-size: 9px; font-style: normal;display: block;margin-top: 2px;">\
              <a class="twbl_link" style="color:'+this.twbl_link+'" target="_blank" rel="nofollow" href="http://twitter.com/'
						+ tweet.user
						+ '/status/'
						+ tweet.id
						+ '">'
						+ tweet.created_at + '</a> \
					</i>';
			}

			html += '</p></div></div>';
			var div = document.createElement('div');

			div.id = 'tweet-id-' + ++Tweet.counter;
			div.innerHTML = html;
			div.style.borderBottom = "1px dotted #ddd";
			div.style.overflow = "hidden";
			return div;
		};
		
		TWBL.Widget.COUNTER = 0;
		TWBL.Widget.HOST = 'http://www.twitblend.com/';
		TWBL.Widget.SHTEXT = '#cefffd';
		TWBL.Widget.LINKCOLOR = '#0099B9';
		Tweet.counter = 0;

		TWBL.Widget.jsonCB = function(url) {
			var script = document.createElement('script');
			script.type = 'text/javascript';
			script.src = url;
			document.getElementsByTagName('head')[0].appendChild(script);
		};

		TWBL.Widget.prototype = function() {
			return {
				init : function(opts) {
					this.id = opts.id;
					this.w = opts.width;
					this.h = opts.height;
					this.twbl_shbg = opts.shbg;
					TWBL.Widget.SHTEXT = opts.shtext;
					this.twbl_twbg = opts.twbg;
					this.twbl_twtext = opts.twtext;
					TWBL.Widget.LINKCOLOR = opts.link;
					this._widgetNumber = ++TWBL.Widget.COUNTER;
					this.wId = 'twbl-widget-' + this._widgetNumber;
					document.write('<div style="position: relative;	font-size: 12px !important;	font-family: \'lucida grande\', lucida, tahoma, helvetica, arial,	sans-serif !important;" id="' + this.wId + '"></div>');
					this.widgetEl = document.getElementById(this.wId);

					TWBL.Widget['cb_' + this._widgetNumber] = function(resp) {
						if (resp.er == 1) {
							document.getElementById("twbl-title-"
									+ TWBL.Widget.COUNTER).innerHTML = 'Can\'t find this blended';
							return;
						}
						document.getElementById("twbl-title-"
								+ TWBL.Widget.COUNTER).innerHTML = '<a class="twbl_shtext" style="text-decoration:none;color:'+TWBL.Widget.SHTEXT+'" target="_blank" href="'
								+ TWBL.Widget.HOST
								+ 'a/'
								+ resp.id
								+ '">'
								+ resp.title
								+ '</a>'
								+ '<h4 class="twbl_shtext" style="margin: 0 !important; padding: 0; line-height: 1.2; width: auto; font-size: 12px;	font-weight: normal;color:'+TWBL.Widget.SHTEXT+';">blended by <a class="twbl_shtext" style="color:'+TWBL.Widget.SHTEXT+'" target="_blank" href="http://twitter.com/'
								+ resp.author
								+ '">@'
								+ resp.author
								+ '</a></h4>';
						if (resp.spt != null && resp.spl != null) {
							document.getElementById("twbl-sp-"
									+ TWBL.Widget.COUNTER).innerHTML = '<a href="'
									+ resp.spl + '">' + resp.spt + "</a>";
						}
						var tweetsEle = document.getElementById("twbl-tweets-"
								+ TWBL.Widget.COUNTER);
						var len = resp.items.length;
						for (i = 0; i < len; i++) {
							var status = resp.items[i];
							if (status.er == 0) {
								var tw = Tweet( {
									id : status.id,
									user : status.name,
									tweet : ify.clean(status.status),
									avatar : status.avatar,
									created_at : status.date
								});
								tweetsEle.appendChild(tw);
							} else {
								var tw = Tweet( {
									id : 0,
									user : "Unknown",
									tweet : "This tweet is protected or deleted",
									avatar : TWBL.Widget.HOST
											+ "static/notfound.png",
									created_at : 0
								});
								tweetsEle.appendChild(tw);
							}
						}
					};
				},
				draw : function() {
					this.widgetEl.innerHTML = this.getMainHtml();
					TWBL.Widget.jsonCB(TWBL.Widget.HOST + "a/" + this.id
							+ "?cb=" + 'cb_' + this._widgetNumber);
				},
				getMainHtml : function() {
					var html = '<div class="twbl_shbg" style="background-color:' + this.twbl_shbg + ';overflow: hidden;text-align: left;font-weight: normal;-moz-border-radius: 5px;-webkit-border-radius: 5px;border-radius: 5px;width: '
							+ this.w
							+ 'px;">\
            <div style="padding: 10px;	position: relative;overflow: hidden;">'
							+ '<h3 style="margin: 0 !important; padding: 0; color:#FFF;line-height: 1.2; width: auto; font-size: 16px;	font-weight: bold;" id="twbl-title-'
							+ this._widgetNumber
							+ '">Loading...<img src="'
							+ TWBL.Widget.HOST
							+ 'static/spinner.gif"/></h3>'
							+ ' \
            </div>\
            <div style="padding: 0 4px;">\
              <div class="twbl_twbg" style=" background-color:' + this.twbl_twbg + ';overflow-x: hidden;overflow-y:auto; -moz-border-radius: 6px;-webkit-border-radius: 6px;border-radius: 6px;	position: relative;auto; height:'
							+ this.h
							+ 'px;">\
                <div id="twbl-tweets-'
							+ this._widgetNumber
							+ '">\
                </div>\
              </div>\
            </div>\
            <div style="overflow: hidden;padding: 7px;">\
              <div><a target="_blank" href="'
							+ TWBL.Widget.HOST
							+ '"><img height="15px" style="border:0;" alt="twitblend" src="'
							+ TWBL.Widget.HOST
							+ 'static/logo.png"></a>\
              <span id="twbl-sp-'
							+ this._widgetNumber
							+ '"></span><div style="float:right;margin-top:3px;"><a class="twbl_shtext" style="color:'+TWBL.Widget.SHTEXT+'" target="_blank" href="'
							+ TWBL.Widget.HOST
							+ '">Make your own</a></div>\
              </div>\
            </div>\
          </div>';
					return html;
				}
			}

		}();
	})();
})(); // #end TWBL
if(typeof(twbl_width) == "undefined")
	var twbl_width = 350;
if(typeof(twbl_height) == "undefined")
	var twbl_height = 300;
if(typeof(twbl_shbg) == "undefined")
	var twbl_shbg = '#a75917';
if(typeof(twbl_shtext) == "undefined")
	var twbl_shtext = '#cefffd';
if(typeof(twbl_twbg) == "undefined")
	var twbl_twbg = '#ffffff';
if(typeof(twbl_twtext) == "undefined")
	var twbl_twtext = '#000000';
if(typeof(twbl_link) == "undefined")
	var twbl_link = 'rgb(0,154,185)';
new TWBL.Widget({
	id: twbl_id,
	width: twbl_width,
	height: twbl_height,
	shbg: twbl_shbg,
	shtext: twbl_shtext,
	twbg: twbl_twbg,
	twtext: twbl_twtext,
	link: twbl_link
	}).draw();
