$(document).ready(
	function(){
		// see if the scrolling tweets box is present, if so start scrolling
		if ($('#scrolling_tweets')) {
			if (0 == $('#scrolling_tweets_no_resize').length) { 
				// only resize if element #buzz_no_resize not found
				var new_height = 200;
				if ($('#scrolling_tweets_size').val()) {
					new_height = new Number($('#scrolling_tweets_size').val())
				}
				$('#scrolling_tweets').height(new_height);
			}
			setInterval(doScrollTweets, 8000);
			//doScrollTweets();
		}
	}
);

// Tweet class to hold info
function Tweet(quote, username, uri) {
	this.uri = uri;
	this.username = username;
	this.quote = quote;
}

// global var to hold tweets
var tweets = getTweets();
var tweet_index = 0;
var tweets_seen = Array();

function doScrollTweets() {
	//tweet_index++;
	
	// 10 tries to find an unseen tweet
	for (var i = 0; i < 10; i++) {
		tweet_index = Math.round(Math.random() * tweets.length);
		if (tweet_index >= tweets.length) {
			tweet_index = 0;
		}
		if ( ! hasSeen(tweet_index)) {
			break;
		}
	}

	// add to seen array
	tweets_seen.push(tweet_index);
	
	if (tweets_seen.length >= tweets.length) {
		// seen a lot of tweets.  clear out the tweets_seen array.
		// note: this doesn't guarantee all tweets have been seen,
		// since the above only has 10 tries to find a unique tweet.
		tweets_seen = Array();
		//alert("clearing seen " + tweets_seen.length);
	}
	
	if (0 == $('#scrolling_tweets_no_resize').length) { 
		// only resize if element #buzz_no_resize not found
		var new_height = 200;
		if ($('#scrolling_tweets_size').val()) {
			new_height = new Number($('#scrolling_tweets_size').val());
		}
		$('#scrolling_tweets').height(new_height);
	}
	
	$('#scrolling_tweets div.tweet_body').fadeOut(1500, doUpdateTweetText);
}

function hasSeen(tweet_index) {
	for (var i = 0; i < tweets_seen.length; i++) {
		if (tweet_index == tweets_seen[i]) {
			return true;
		}
	}
	return false;
}

function doUpdateTweetText() {
	var tweet = tweets[tweet_index];
	$('#scrolling_tweets').html('<div class="tweet_body">&quot;' + tweet.quote + '&quot;<div class="tweet_source"><a href="' + tweet.uri + '" target="_blank">@' + tweet.username + '</a></div></div>');
	$('#scrolling_tweets div.tweet_body').fadeIn(1000);
}


// tweets to display
function getTweets() {
	var tweets = Array();


	// keep this as the first tweet, since it is hard coded on the site.
	tweets.push(
			new Tweet("American Library Association Goes Mobile with Mosio for Annual Conference: Mosio's Text a Librarian is partnering...",
								'stksthailand',
								'http://tinyurl.com/mxyfrj')
						);

	tweets.push(
			new Tweet("Mosio does it again. Text-a-librarian hits the ALA conference in the form of Text-an-ambassador http://tiny.cc/8CQQL <a href='http://search.twitter.com/search?q=%23ala2009' target='_blank'>#ala2009</a>",
								'animated_me',
								'http://twitter.com/animated_me/status/2427695886')
						);

	tweets.push(
			new Tweet("I think the text-a-librarian feature for <a href='http://search.twitter.com/search?q=%23ala2009' target='_blank'>#ala2009</a> is totally rad. Thank you thank you thank you!!",
								'leahlibrarian',
								'http://twitter.com/leahlibrarian/status/2426397887')
						);

	tweets.push(
			new Tweet('Texted a librarian and renewed my book online. Yay!',
								'thekeyofv',
								'http://twitter.com/thekeyofv/status/2389238073')
						);


	tweets.push(
			new Tweet('I just Text-A-Librarian (@FreeLibrary) and got my answer in 1 minute! Very cool. Send text to 66746 with the keyword askfree and ur question',
								'camillereads',
								'http://twitter.com/camillereads/statuses/1991411971')
						);




	tweets.push(
			new Tweet('@stanford37b: <a href="http://search.twitter.com/search?q=%23sla2009" target="_blank">#sla2009</a> Interesting. ALA convention will have a "Text a Librarian" service. Per Resourceshelf and http://tinyurl.com/nh3w45',
								'sla2009',
								'http://twitter.com/sla2009/statuses/2018457465')
						);

	tweets.push(
			new Tweet('@hugeinjapan, do you want to write a jingle about how great it is to text your questions to a librarian? ;)',
								'rockityroll',
								'http://twitter.com/rockityroll/status/2182208670')
						);

	tweets.push(
			new Tweet("will be taking a webinar on how to use Text a Librarian tomorrow. I'm looking forward to it. Never hurts to learn new library tools",
								'loofrin',
								'http://twitter.com/loofrin/status/2179779740')
						);






	tweets.push(
			new Tweet('just back from exhibits. textalibrarian looks very easy to implement and use. <a href="http://search.twitter.com/search?q=%23alamw09" target="_blank">#alamw09</a>',
								'kongtemplation',
								'http://friendfeed.com/kongtemplation/7b98cd30/just-back-from-exhibits-textalibrarian-looks')
						);

	tweets.push(
			new Tweet('Mosio text a librarian service - VERY impressive! <a href="http://search.twitter.com/search?q=%23librarians" target="_blank">#librarians</a>',
								'millerlibrarian',
								'http://explore.twitter.com/millerlibrarian/status/1485970267')
						);
		
	tweets.push(
			new Tweet('RT @Carleen: In a Text a Librarian Webinar http://www.textalibrarian.com/ - SMS integration for libraries. Awesome!',
								'JSW_EdTech',
								'http://twitter.com/JSW_EdTech/status/1284091764')
						);
		




	tweets.push(
			new Tweet('Text a Librarian at ALA Annual in Chicago.',
								'sljournal',
								'http://bit.ly/fxFSC')
						);

	tweets.push(
			new Tweet('We just launched our new service Text-a-Librarian. Get answers on your cell.',
								'FreeLibrary',
								'http://bit.ly/1abtxR')
						);






	tweets.push(
			new Tweet('Text a librarian just got best question yet - about tv show featuring cop sent back from hell. & staff answered in 5 minutes. So proud.',
								'rockityroll',
								'https://twitter.com/rockityroll/status/1672346873')
						);
		
	tweets.push(
			new Tweet('checked out the "text a librarian" service yesterday via Gabe\'s webinar. Nice job - hoping to participate if others agree.',
								'gailgolderman',
								'http://twitter.com/gailgolderman/statuses/1649110853')
						);
	tweets.push(
			new Tweet('LOVE the marketing materials -- makes life so much easier. Thanks!',
								'Text a Librarian Facebook Comment',
								'http://tinyurl.com/d2vwpz')
						);







	tweets.push(
			new Tweet('Impressed with the Text a Librarian interface #hhlib',
								'librarybazaar',
								'http://twitter.com/librarybazaar/statuses/2934683461')
						);

	tweets.push(
			new Tweet('Seeing the text-a-librarian interface. Nice. They also use encryption and handle the cell carrier stuff. #hhlib',
								'scottchilders',
								'http://twitter.com/scottchilders/statuses/2934641638')
						);

	tweets.push(
			new Tweet('Switching to the Launching a Text a Librarian Service. #hhlib',
								'greaterumbrage',
								'http://twitter.com/greaterumbrage/statuses/2934312595')
						);

	tweets.push(
			new Tweet('I <3 Text a librarian. Its so cool! #Ala2009',
								'chelliemo',
								'http://twitter.com/chelliemo/statuses/2584220937')
						);

	tweets.push(
			new Tweet('In an excellent webinar with 30 students and Jamieson from Text a Librarian',
								'mstephens7',
								'http://twitter.com/mstephens7/statuses/4059073712')
						);

	tweets.push(
			new Tweet('hee, so many ppl using our @textalibrarian svce sign off with <3 They <3 the library!',
								'Rockityroll',
								'http://twitter.com/rockityroll/status/4151353754')
						);

	tweets.push(
			new Tweet('Attending a webinar on Text a Librarian texting service. Really cool features.',
								'Korenic',
								'http://twitter.com/korenic/statuses/4059378088')
						);

	tweets.push(
			new Tweet("@textalibrarian we're up and runnin with ya at IUPUI had some pos. feedback already for users.",
								'loofrin',
								'http://twitter.com/loofrin/status/3559541978')
						);

	tweets.push(
			new Tweet('Over 370 students used our new Text a Librarian service during New Student Orientation.',
								'DavidsonCollLib',
								'http://twitter.com/DavidsonCollLib/status/3492554489')
						);

	tweets.push(
			new Tweet('Text a Librarian stats were good in August. I think the teens have finally discovered it.',
								'refchef',
								'http://twitter.com/refchef/status/3700423000')
						);



	return tweets;
}


