classAttribute = (function(){
	var ie7 = /MSIE 7.0/g.test(navigator.userAgent),
	ie6 = /MSIE 6.0/g.test(navigator.userAgent),
	docMode = document.documentMode ? document.documentMode.toString() : "",
	classAttr = "class";
	if(docMode === "7" || docMode === "5") {
		classAttr = "className";
	} else if(docMode === "") {
		classAttr = (ie7 || ie6) ? "className" : "class";
	};
	return classAttr;
})(),
parseTweet = (function(){
	var rex = {
		shout: /@[\w-]*/,
		hash: /#[\w-]*/,
		link: /(http:\/\/).*/
	};
	return function(str){
		var strArr = str.split(" ");
		for(var x in strArr) {
			var word = strArr[x];
			if(rex.shout.test(word)) { // shoutouts
				var name = word.match(rex.shout)[0];
				name = "<a target='_blank' href='http://www.twitter.com/" + name.substring(1,name.length)+"'>" + name + "</a>"
				strArr[x] = word.replace(rex.shout,name);				
			} else if(rex.hash.test(word)) { // hashtags
				var hash = word.match(rex.hash)[0];
				hash = "<a target='_blank' href='http://search.twitter.com/search?q=" + encodeURIComponent(hash) + "'>" + hash + "</a>"
				strArr[x] = word.replace(rex.hash,hash);	
			} else if (rex.link.test(word)) { // links
				var link = word.match(rex.link)[0];
				link = "<a target='_blank' href='" + link +"'>" + link + "</a>"
				strArr[x] = word.replace(rex.link,link);		
			};
		};
		return strArr.join(" ");
	};
})();
/*

Fat Media Twitter feed script
© Fat Media

*/

(function($){
	
	var config = {
		date: 'date', //  date or difference
		display: "list", // box or list
		errorHtml: "Twitter feed currently unavailable",
		hashTags: "",
		loadingHtml: "Loading twitter feed...",
		nextLinkText: "&raquo;",
		prevLinkText: "&laquo;",
		screenNames: "",
		shoutOuts: "",
		tweetCount: 10
	},
	
	singleFeed = false, // used for the different date formats twitter gives you
	
	TweetSet = function(tweets,elm){
		var $this = this;
		this.curTweet = 0;
		this.tweetCount = tweets.length;
		this.nextBtn = document.createElement("a");
		this.prevBtn = document.createElement("a");
		this.dateSpan = document.createElement("span");
		this.authorSpan = document.createElement("span");
		this.tweet = document.createElement("span");
		
		this.nextBtn.innerHTML = config.nextLinkText;
		this.prevBtn.innerHTML = config.prevLinkText;
		
		this.nextBtn.setAttribute(classAttribute,"twitterNext");
		this.prevBtn.setAttribute(classAttribute,"twitterPrev");
		this.dateSpan.setAttribute(classAttribute,"twitterDate");
		this.authorSpan.setAttribute(classAttribute,"twitterAuth");
		this.tweet.setAttribute(classAttribute,"twitterText");
		
		this.tweets = tweets;
		
		this.showTweet = function(i) {
			if(i < 0 || i > $this.tweetCount-1) { return false };
			var username = $this.tweets[i].from_user || $this.tweets[i].user.screen_name;
			$this.tweet.innerHTML = parseTweet($this.tweets[i].text);
			$this.dateSpan.innerHTML = doDate($this.tweets[i].created_at) + " ";
			$this.authorSpan.innerHTML = parseTweet("by @" + username);
			this.curTweet = i;
		};
		
		this.nextBtn.onclick = function(){
			$this.showTweet($this.curTweet+1);
		};
		
		this.prevBtn.onclick = function(){
			$this.showTweet($this.curTweet-1);
		};
		
		this.showTweet(0);
		
		$(elm).append(this.nextBtn, this.prevBtn, this.tweet, this.dateSpan, this.authorSpan);
	},
	
	TweetList = function(tweets,elm){
		var alt = false;
		this.tweets = tweets;
		this.Tweet = function(tweet){
			var username = tweet.from_user || tweet.user.screen_name;
			
			this.wrap = document.createElement("p");
			this.date = document.createElement("span");
			this.author = document.createElement("span");
			this.text = document.createElement("span");
			
			this.date.setAttribute(classAttribute,"twitterDate");
			this.author.setAttribute(classAttribute,"twitterAuth");
			this.text.setAttribute(classAttribute,"twitterText");
			
			this.text.innerHTML = parseTweet(tweet.text);
			this.date.innerHTML = doDate(tweet.created_at) + " ";
			this.author.innerHTML = parseTweet("by @" + username);
			
			$(this.wrap).append(this.text, this.date, this.author);
		};
		for(var x in this.tweets) {
			try {
				var tmpTwt = new this.Tweet(this.tweets[x]);
				tmpTwt.wrap.setAttribute(classAttribute, alt ? "even" : "odd");
				elm.appendChild(tmpTwt.wrap);
				alt = !alt;
			} catch(e) {
				window.twitterError = e;
			};
		};
	},		
	doDate = function(rawDate) {
		var $return = false;
		if(config.date == 'date') { return rawDate.split(" +")[0] };
		if(config.date == 'difference') {
			if(singleFeed) {
				theArray = rawDate.split(" ")
				theArray[0] += ",";				
				theArray.splice(3,0,theArray.pop());				
				theArray.splice(2,0, theArray.splice(1,1));				
				rawDate = theArray.join(" ");
			};
			var today = new Date(),
			tDate = new Date(rawDate),
			oneSec = 1000,
			oneMin = oneSec * 60,
			oneHour = oneMin * 60,
			oneDay = oneHour * 24,
			oneWeek = oneDay * 7,
			oneMonth = oneWeek * 4,
			msDiff = today.getTime()-tDate.getTime();
			if(msDiff >= oneMonth) $return = Math.round(msDiff/oneMonth) + " month";
			else if(msDiff >= oneWeek) $return = Math.round(msDiff/oneWeek) + " week";
			else if(msDiff >= oneDay) $return = Math.round(msDiff/oneDay) + " day";
			else if(msDiff >= oneHour) $return = Math.round(msDiff/oneHour) + " hour";
			else if(msDiff >= oneMin) $return = Math.round(msDiff/oneMin) + " minute";
			else if(msDiff >= oneSec) $return = Math.round(msDiff/oneSec) + " second";
			if(parseInt($return) > 1) $return = $return + "s";
			if($return) return ($return + " ago");
			return "less than 1 second ago";
		};
	},
	
	ajaxURI = "",
	
	setAjaxUri = function(){
		var rex = /^(@|#)/i,
		scArr = config.screenNames.split(","),
		hashArr = config.hashTags.split(","),
		shoutArr = config.shoutOuts.split(","),
		searchTerms = scArr.concat(hashArr).concat(shoutArr);
		
		if(config.hashTags == "" && config.shoutOuts == "" && config.screenNames.split(",").length == 1) {
			singleFeed = true;
			ajaxURI = "http://api.twitter.com/1/statuses/user_timeline.json?count="+config.tweetCount+"&include_rts=true&screen_name="+config.screenNames+"&callback=?"
		} else {
			for(var x in searchTerms) {
				var word = searchTerms[x];
				if(word != "") {
					searchTerms[x] = encodeURIComponent(rex.test(word) ? word : "from:" + word);
					ajaxURI += searchTerms[x] + "+OR+";
				};
			};
			ajaxURI = "http://search.twitter.com/search.json?q=" + ajaxURI.substring(0,ajaxURI.length-4) + "&rpp="+config.tweetCount+"&callback=?";
		};
	};
	
	$.fn.twitter = function(o) {
		
		var $this = this, x;
		
		$this.addClass("ajaxLoading");
		
		if(typeof o == 'object') { config = $.extend(config, o); }
		else {config.screenNames = o};
		setAjaxUri();
		
		x = $.ajax({
			type: 'GET',
			url: ajaxURI,
			dataType: 'json',
			success: function(o) {
				
				var data = o;
				$this.removeClass("ajaxLoading");
				return $this.each(function(){
					var feedData = data.results || data;
					tweets = config.display == "list" ? new TweetList(feedData,this) : new TweetSet(feedData,this);
					$(this).addClass("twitter" + config.display);
				});
			},
			error: function(e){
				$this.removeClass("ajaxLoading").html(config.errorHtml);
				window.ajaxError = e;
			}
		});
	};
	
})(window.jQuery);
