jQuery.iXmlFeed = {
	build : function(options)
	{
		var defaults = {  
			itemWidth: 200,	//width of the items (not your div)
			itemHeight: 70,	//height ""
			navHeight: 20,	//height of the prev/next images/text
			maxItems: 3, 	//maximum items to read from feed
			stripTitle: 0,	//strip leading character from title
			titleLen: 40,	//length of title max
			stripDesc:0,	//strip leading characters from description
			descLen: 350,	//lenght of description
			linkLen: 30,	//length of link to display
			displayTitle: true, 
			displayDescription: true,
			displayPubDate: true,
			displayLink: true,
			titleAsLink: true,	//use the title for a link
			literalDate: false,	//true = Sat, Mar 1st 2002 | false= about 1 hour ago
			prevImg: "",	//image for previous/next buttons if left blank then the
			nextImg: "",	//alt text of prev/next will show respectivly
			clickOnUl: false,//should clicking on body of text scroll it (can't select text with this on)
			linkImage: "",
			phpFile: "../xml.php" //location of the php file from the package
		};  
		var options = $.extend(defaults, options); 
		return $(this).each(
		function()
		{
			var $el = $(this);
			$(this).append("<span class='prev'></span><span class='next'></span><ul></ul>");
			var $ul = $(this).children("ul");
			var $spanl = $(this).children("span:first");
			var $spanr = $(this).children("span:last"); 
			//setup the div
			var elHieght = options.itemHeight+options.navHeight;
			var elCss = {
				width: options.itemWidth+"px",
				height: elHieght+"px",
				overflow:"hidden"}	
			//setup the ul
			var ulWidth = options.itemWidth*options.maxItems;
			var ulCss = {
				position:"relative",
				width: ulWidth+"px",
				height: options.itemHeight+"px",
				clear:"both",
				'list-style':"none",
				padding:0}
			//setup the lis
			var liCss = {
				width: options.itemWidth+"px",
				height: options.itemHeight+"px",
				float:"left"}	
			//and the spans
			var spanlCss ={
				height:options.navHeight,
				float:"left"
				}
			var spanrCss ={
				height:options.navHeight,
				float:"right"
				}
			$el.css(elCss);
			$spanl.css(spanlCss);
			$spanr.css(spanrCss);
			$ul.css(ulCss);	
			$spanr.bind("click", function(){$ul.trigger("click",['next'])});
			$spanl.bind("click", function(){$ul.trigger("click",['prev'])});
			$ul.bind("click",function(event,whichway)
			{	
				if((options.clickOnUl==false)&&(whichway==undefined)){return false;}
				var left = $(this).css("left").slice(0,-2);
				left = left*-1; //sliding to left so we need to think positve to make this work right
				if(whichway=="prev"){
					if(left-options.itemWidth<0){
						var end = (options.itemWidth*(options.maxItems-1))*-1;
						$(this).animate({left:end+"px"},500);
					}else{
						var newLeft = (left-options.itemWidth)*-1;
						newLeft = newLeft+"px";
						$(this).animate({left:newLeft},500);
					}				
				}else{
					if(left+options.itemWidth>ulWidth-options.itemWidth){
						$(this).animate({left:"0px"},500);
					}else{
						var newLeft = (left+options.itemWidth)*-1;
						newLeft = newLeft+"px";
						$(this).animate({left:newLeft},500);
					}
				}				
			});
			var i = 0;
			$.ajax({
				type: "GET",
				url: options.phpFile,
				data: "uri="+options.feedURI,
				dataType: "xml", 
				success: function(xml){
					$(xml).find("item").each(function()
					{
						if(i>=options.maxItems){return false};
						var input = "";
						var link =  $(this).find('link').text();
						if(options.displayTitle){
							var title = $(this).find('title').text().slice(options.stripTitle);
							if(title.length>options.titleLen){
								title = title.substr(0,options.titleLen)+"...";
							}
							if(options.titleAsLink){
								input = input+"<span class='title'><a target='_blank' href='"+link+"'>"+title+"</a></span><br />";
							}else{
								input = input+"<span class='title'>"+title+"</span>";
							}
						}
						if(options.displayDescription){
							var description = $(this).find('description').text().slice(options.stripDesc);
							description = description.replace(/(<([^>]+)>)/ig,""); 
							if(description.length>options.descLen){
								description = description.substr(0,options.descLen)+"...";
							}
							input = input+"<span class='description'>"+description+"</span>";
						}
						if((options.titleAsLink==false) && (options.displayLink==true)){
							var linkRef = link;
							if(link.length>options.linkLen){
								link = link.substr(0,options.linkLen)+"...";
							}
							var linkImg ="";
							if(options.linkImage!=""){linkImg = "<img src='"+options.linkImage+"'/>"}
							input = input+"<span class='link'>"+linkImg+"<a href='"+linkRef+"'>"+link+"</a></span>";
						}
						if(options.displayPubDate){
							var pubDate = $(this).find('pubDate').text();
							if(!options.literalDate){
								pubDate = new Date(pubDate).getTime();
								var nowTime = new Date().getTime();
								var diff = (nowTime-pubDate)/1000;
								//alert(diff);
								if(diff<300){
									pubDate = "less than 5 minutes ago"
								}else if(diff<3600){
									diff = Math.round(diff/60);
									pubDate = "about "+diff+" minutes ago";
								}else if(diff<6000){
									diff = Math.round((diff-3600)/60);
									pubDate = "1 hour "+diff+" minutes ago";
								}else if(diff<86400){
									diff = Math.round(diff/3600);
									pubDate = "about "+diff+" hours ago";
								}else if(diff<172800){
									diff = Math.round((diff-86400)/3600);
									pubDate = "1 day "+diff+" hours ago";
								}else{
									diff = Math.round(diff/86400);
									pubDate = "about "+diff+" days ago";
								}
							}
							input = input+"<span class='pubdate'>"+pubDate+"</span>";
						}
						$ul.append("<li>"+input+"</li>");
						i++;
					});
					$ul.children("li").each(function(){
						$(this).css(liCss);
					});
			   }
			});
		});
	}
}
jQuery.fn.XmlFeed = jQuery.iXmlFeed.build;