/**
 * Technosophos RSS Widget
 * Copyright (c) 2007 Technosophos/Aleph-Null, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.  
 *
 * ============
 * Typical Use:
 * Technosophos.rssWidget("http://myfeed/rss.xml", "#my-target-div");
 *
 * NOTE: Due to the browser security model, the RSS URL must reside on the
 * same server as HTML page into which this widget is placed.
 *
 * This script requires jQuery 1.2 or greater. http://jquery.com
 *
 * @author Matt Butcher <mbutcher@aleph-null.tv>
 */
 
 
var Technosophos = Technosophos || {widgets:{},settings:{}};

jQuery.extend( Technosophos, {
	shorten: function(myString, maxlen, sep) {
		var s = sep || '...';
		var m = maxlen || 256;
		if ( myString.length <= m ) return myString
		 
		myString = myString.substring(0, maxlen - s.length);
		var l = myString.lastIndexOf(' ');
		return (l < 0 ? myString : myString.substring(0, l)) + s;
	},
	rssWidget: function(myFeed, myTarget) {
		jQuery().ready(function() {
			Technosophos.widgets.rss.create(myFeed, myTarget);
		});
	}
});

jQuery.extend(Technosophos.widgets, {
	rss: {
		settings: {
			baseURI: "", // RELATIVE PATH FOR LOADING IMAGES
			backgroundImage: "", // BG IMAGE
			refreshInterval: 5000, // HOW LONG TEXT IS DISPLAYED
			refreshDuration: "fast", // HOW LONG A FADE EFFECT SHOULD TAKE (default = "slow")
			maxTextLength: 115 // MAX NUM OF CHARS TO DISPLAY IN DESCRIPTION
		},
		create: function(myfeed, mytarget) {
			var _rss = this;	
			if(myfeed) _rss.settings.feed = myfeed;
			if(mytarget) _rss.settings.target = mytarget;
			
			_rss.insertWidget();
			var res = jQuery.get(this.settings.feed, function(data) {
				_rss.feedContents = data;
				_rss.title = jQuery(Technosophos.widgets.rss.feedContents).find("channel > title").text();
				_rss.items = jQuery.makeArray(jQuery(Technosophos.widgets.rss.feedContents).find("item"));
				jQuery(_rss.settings.target)
					.find(".rss-widget-inner-div")
					.html("<span class='rss-widget-title'>"
						+ _rss.title
						+"</span>"
						+"<span class='rss-widget-item'></span>"
					);
				_rss.periodicRefreshWidget();
			});
		},
		insertWidget: function() {
			jQuery("head").append(

			);
			jQuery(Technosophos.widgets.rss.settings.target).html(
				"<div class='rss-widget-outer-div'>"
				+"<div class='rss-widget-inner-div'>"
				+ "Loading..."
				+"</div></div>"
			);
			//this.refreshWidget();
		},
		pauseRefresh: function() {
			clearInterval(Technosophos.widgets.rss.intID);
		},
		unpauseRefresh: function() {this.periodicRefreshWidget()},
		periodicRefreshWidget: function() {
			Technosophos.widgets.rss.refreshWidget(); // do one right now
			Technosophos.widgets.rss.intID = setInterval(
				"Technosophos.widgets.rss.refreshWidget()", 
				Technosophos.widgets.rss.settings.refreshInterval
			); 
		},
		refreshWidget: function() {
			var _rss = this;
			jQuery(Technosophos.widgets.rss.settings.target)
				.find(".rss-widget-item")
				.fadeOut(Technosophos.widgets.rss.settings.refreshDuration, _rss.changeItem);
		},
		changeItem: function() {
			var _rss = Technosophos.widgets.rss;
			if(_rss.items.length == 0) return; // Do nothing if no content.
			_rss.c < _rss.items.length -1 ? _rss.c++: _rss.c = 0; 
			var item = _rss.items[_rss.c] || {title:"No Content"};
			var j = jQuery(item);
			var l = j.find('link').text();
			if( l.indexOf('javascript') == 0 ) l = "#";
			var t = j.find('title').text();
			var d = Technosophos.shorten(j.find('description').text(), _rss.settings.maxTextLength - t.length)
			jQuery(this).html(
				"<span class=\"rss-widget-item\"><a href=\""
				+ l
				+"\">"
				+ t
				+"<span class=\"rss-widget-item-desc\">"
				+ d
				+"</a></span>"
			).fadeIn(_rss.settings.refreshDuration);
		},
		// More or less private vars:
		title: "No Feed Found",
		items: [],//{title:"Get Some Content",description:""}],
		c: 0,
		feedContents: null
	}
}); 

 
