/* aRSSReader : jQuery RSS 2.0 parser plugin
 * Copyright (C) 2009 Access Programming Group, Inc - http://www.apginc.net/
 * Dual licensed under the MIT (MIT-license.txt)
 * and GPL (GPL-license.txt) licenses.
 *
 * Version 0.5 Beta
 *
 */
 
(function($) {
    
    $.fn.aRSSReader = function( options ) {
        
        // Merge the specified options with the default ones
        var options = $.fn.extend( {}, $.fn.aRSSReader.defaults, options );
		
		// Assign global reference of element
		//$.fn.aRSSReader.container[$.fn.aRSSReaderIndex] = $(this);
		options.container = $(this);

		// Set container requirements
		$(options.container).css({position:"relative"});
		
		if(options.url==null){
			$.fn.aRSSReader.error("Please provide a valid file");
			return;
		}
		
		// Set global vars
		options.feedTitle = "";
		options.feedURL = "";
		options.pubDate = "";
		options.feedLegal = "";
		options.elements = [];
		options.elementsCount = 0;

		// Get Data
		$.ajax({
			url : 'aRSSReader.fn.php',
			type : 'GET',
			data : "feed="+options.url+"&"+options.params,
			dataType : 'xml',
			beforeSend : function(){
				$.fn.aRSSReader.showLoading(options);
			},
			error : function(a,b,c){
				alert(a+"|"+b+"|"+c);
			},
			success : function(data){
				$.fn.aRSSReader.hideLoading(options);
				$.fn.aRSSReader.parseXML(data,options);
			}
		});
	}
	
	$.fn.aRSSReader.showLoading = function(options){
		if(options.loadingImage != ""){
			$(options.container).append("<img src=\""+options.loadingImage+"\" border=\"0\" alt=\"aRSSReader Loading\" title=\"aRSSReader Loading\" align=\"center\" style=\"position:absolute;\" />");
		}
	};
	
	$.fn.aRSSReader.hideLoading = function(options){
		if(options.loadingImage != ""){
			$(options.container).children("img").fadeOut("fast",function(){
				$(this).remove();
			});
		}
	};
	
	$.fn.aRSSReader.parseXML = function(){
		var options = arguments[1];
		var xml = arguments[0];
		if($(xml).find('item').length <= 0){
			$.fn.aRSSReader.error(options,"File is empty or not formatted correctly.");
			return;
		}
		
		var channel = $(xml).find("channel");
		options.feedTitle = $(channel).children("title").text();
		options.feedURL = $(channel).children("link").text();
		options.pubDate = $(channel).children("pubDate").text();
		options.feedLegal = $(channel).children("copyright").text();
		
		$(channel).children('item').each(function(i){
			if(options.elementCount == options.displayCount){
				$.fn.aRSSReader.placeItems(options);
				return;
			}
			var html = "<div class=\""+options.elementClass+"\">";
			
			html += "<a href=\""+$(this).children("link").text()+"\" class=\""+options.elementClass+" aRSSReaderElement_"+i+"\" rel=\""+i+"\" target=\"_blank\">";

			html += $(this).children("title").text();

			html += "</a><div class=\""+options.elementClass+"_description\">";
			
			if($(this).children("description").text().length > options.descriptionLength){
				html += $(this).children("description").text().substr(0,options.descriptionLength)+"...";
			}else{
				html += $(this).children("description").text();
			}
			
			html += "</div></div>";
			
			options.elements.push(html);
			options.elementsCount++;
		});
		
		$.fn.aRSSReader.placeItems(options);
	};
	
	$.fn.aRSSReader.placeItems = function(options){
		var t = "<div class=\""+options.titleClass+"\"><a href=\""+options.feedURL+"\" alt=\""+options.feedLegal+"\" title=\""+options.feedLegal+"\">"+options.feedTitle+"</a></div>";
		t += "<div class=\"publishDate\">Published: "+options.pubDate+"</div>";
		$(options.container).append(t);
		for(var i = 0; i < options.displayCount; i++){
			$(options.container).append(options.elements[i]);
		}
	}
	
	$.fn.aRSSReader.error = function(){
		var options = arguments[0];
		$(options.container).append("<span style=\"color:red\"><b>[aRSSReader Error]</b> "+arguments[1]+"</span>");
	};
	
	$.fn.aRSSReader.defaults = {
		"url"                : "http://images.apple.com/main/rss/hotnews/hotnews.rss",
		"elementClass"       : "aRSSReader",
		"titleClass"         : "aRSSReaderTitle",
		"displayCount"       : 5,
		"descriptionLength"  : 100,
		"params"             : "",
		"loadingImage"       : "/images/aRSSReader_loading.gif"
	};
})(jQuery);