<!--

	/**
	 * CopyRight 2004-2010 Scott MacNeill. All rights reserved
	 *
	 * Terms:
	 * You may use this library for no charge. Though you may NOT distribute it.
	 * You may modify this library for use in your application only.
	 * All information above this statement including this statement must remain un-altered.
	 * Any question or modification request can be sent to macneill.scott@gmail.com
	 * http://www.legionware.com/
	 */


	/*
	 * ==> Parse twitter feed for specified user and populate recent posts to screen
	 */


	/** Global variables */

	/** Screen name to parse */
	var feed_screen_name = '';

	/** number of entries to return */
	var entries = 1;

	/** Resulting object from webservice call */
	var response_object = new Array();

	/** Show only entries posted by screen name */
	var show_only_screen_name = false;

	/** Interval object that will run periodic polls for updated entries */
	var check_updates = '';

	/** Script tag element that we are using as JSON callback utility */
	var json_element = '';


	/**
	 * Poll twitter webservices and populate response_object array with entries
	 * Returns (void) nothing
	 */
	function poll_twitter () {

		/** Create a dynamic <script> tag with the src attribute set to json request */
		json_element = document.createElement('script');
		json_element.setAttribute('type', 'text/javascript');
		json_element.setAttribute('src', 'http://twitter.com/statuses/user_timeline.json?screen_name=' + feed_screen_name + '&callback=receive_json&count=10&clientsource=FrostyDrewOBSY');

		/** Attach new script tag to document head */
		document.getElementsByTagName('head').item(0).appendChild(json_element);

	}


	/**
	 * Reset global variables and remove JSON <script> tag
	 * Returns (void) Nothing
	 */
	function clean_up () {

		/** Reset response_object global */
		response_object = new Array();

		/** Remove JSON element from document head */
		document.getElementsByTagName('head').item(0).removeChild(json_element);

	}


	/**
	 * Parse the JSON response. And add applicable entries to local variable list
	 * This is the JSON request callback function
	 * json_response (object): A JSON object sent back in the request
	 */
	function receive_json (json_response) {

		/** Make sure a JSON response is received */
		if ( ! json_response) return false;

		/** Get the number of entries in response */
		entries_list = json_response.length;

		/** Iterate through returned entries */
		for (i = 0; i < entries_list; i++) {

			/** Only display tweets from screen_name if set */
			if (show_only_screen_name && json_response[i].user.screen_name != 'FrostyDrewOBSY') continue;

			/** Add entry object to local variable */
			response_object.push(json_response[i]);

			/** If we have the amount of requested entries, stop */
			if (response_object.length == entries) break;

		}

		/** Populate page with entries to show */
		show_entries ()

		/** Clean up */
		clean_up();

	}


	/**
	 * Parse and format feed data for display on our page
	 * Returns (boolean) True on success. False on error
	 */
	function show_entries () {

		/** Output */
		html_to_show = '';

		/** Iterate through entries */
		for (i in response_object) {

			/** Get latest tweet text */
			tweet_content = response_object[i].text;

			/** Get latest tweet date */
			tweet_date = response_object[i].created_at.split((' '));

			/** Format tweet to show */
			html_to_show += '<div class="tweets">' + tweet_date[0] + ' ' + tweet_date[1] + ' ' +  tweet_date[2] + ', ' + tweet_date[5] + ': ' + tweet_content + '</div>';

		}

		/** Write resulting HTML to screen */
		document.getElementById('tweet').innerHTML = html_to_show;

		return true;

	}


	/**
	 * Update current post on page
	 * screen_name (string): The screen_name of feed to update
	 * entries_count (int): The number of entries to return to screen. Defaults to 1
	 * only_from_screen_name (boolean): True: Only return entries posted by screen name user. False: Post all entries. Defaults to true
	 * Returns (boolean) True on success. False on error
	 */
	function update (screen_name, entries_count, only_from_screen_name) {

		/** Make sure a screen name is specified */
		if (screen_name == '') return false;

		/** Set screen name */
		feed_screen_name = screen_name;

		/** Set entries limit if specified */
		if (entries_count) entries = entries_count;

		/** Set only show entries from screen name if specified */
		if (only_from_screen_name) show_only_screen_name = true;

		/** Poll feed and setup into array of objects */
		poll_twitter()

		/** Setup polling interval to run every minute */
		check_updates = setInterval('poll_twitter()', 60000);

		return true;

	}

//-->
