/*global jQuery:false, document:false, window:false */
/*
 * jQuery ctRotator Plugin briget
 * Convert an ul or ol list to a useable data source for ctRotator
 * 
 * Under MIT license http://www.opensource.org/licenses/mit-license.php
 *
 * @author: Cuong Tham
 * @version: 1.0
 * @requires jQuery v1.2.6 or later
 * @requires ctRotator
 *
 * @headsTip: Examples and documentation at: http://thecodecentral.com/2008/11/12/ctrotator-a-flexible-itemimage-rotator-script-for-jquery
 */

window.itt = new Array();

function ctRotatorBridgeRss20(url, readyCallback, maxinlist){
  this.title = "";
  this.url = url;
  this.readyCallback = readyCallback;
  this.maxinlist = maxinlist || 25;
}

ctRotatorBridgeRss20.prototype = {
  getDataSource:function(){
   var readyCallback = this.readyCallback;
   var dataSource = [];
   var obj = this;
   
   jQuery.get(this.url, {}, function(data){
	 obj.title = jQuery(data).find('channel title').eq(0).text();
	 
     jQuery(data).find('channel item').each(function(){
       window.itt.push(this);
	   var e = jQuery(this);
	   dataSource.push({
	     title:e.find('title').text(),
		 url: e.find('link').text(),
		 tip: e.find('description').text(),
		 date: new Date(e.find('pubDate').text().substr(e.find('pubDate').text().indexOf(',')+1))
	   });
	 });
	 
	 dataSource.sort(function(a, b) {
		return b.date - a.date;
	 });
	 
	window.dsource = dataSource;
	 
	 readyCallback(dataSource.slice(0,obj.maxinlist));
   }, 'xml');
   
   return dataSource;
  }
  
};
