// JavaScript Document

(function($){
	//code here
	$.fn.bnews = function(news_url,options) {
	 	
		var $opts = {};
		var $stories = [];
		$.extend($opts, $.fn.bnews._defaults, options);
		
		
		//make me a this i'll remember
		var $this = $(this);
		
		//when all files are done loading do this event
		$(this).ajaxStop(function(){
			//sort the stories
			$stories.sort(bnews_sort);
			if($opts.max == -1) $opts.max = $stories.length;
			$(this).html(make_html($opts,$stories));
		});
		
		//for each element do it:
		return $(this).each(function(){
			//code
			if(typeof news_url == "string") {
				news_url = [news_url];
			};
			for( var q=0; q<news_url.length; q++) {
			$.ajax({
				url:news_url[q],
				cache:false,
				context:$this,
				success:function(data){
					var t = $.trim(data).replace(/\n\s{1,}\n/g,"\n\n").split(/\n{2,}/g);
					
					for( var i=0; i<t.length; i++) {
						var splits = t[i].split('\n');
						var o = {
							url:false,
							date:false,
							title:false
						};//set up the 'o' so that it won't get added
						
						for( var j=0; j<splits.length; j++) {
							switch( bnewstype(splits[j]) ) {
								case "url":
								o.url = splits[j];
								break;
								
								case "date":
								var arr_date = splits[j].split(/\D/g);
								if(arr_date.length==3) {
									o.date = splits[j];
									o.date_month = arr_date[0];
									o.date_day = arr_date[1];
									o.date_year = arr_date[2];
								}
								break;
								
								default:
								o.title=splits[j];
							}
						}// end j loop
						if(o.url && o.date && o.title) {
							$stories.push(o);
						}
					}
					
					//write html here
				}//end return
			});//end ajax
			}//end for loop
		});
   };
   
   $.fn.bnews._defaults = {
	  max:		-1,
	  jrh:		false,
	  display_date:	false
   };
   
   $.fn.bnews.stories = new Array();
   
   var da_months = ["","January","February","March","April","May","June","July","August","September","October","November","December"];
   
   function make_html($opts,$stories) {
	   var html = "";
		var sep = ".";
		
	   html += "<ul>";
	   if($opts.display_date) {
		   if($opts.jrh) html += "<li style='font-weight:bold;list-style-type:none; background-image:none; padding-left:0px; padding-top:10px;'>"+da_months[$stories[0].date_month]+" "+$stories[0].date_day+", "+$stories[0].date_year+"</li>";
		   else html += "<li style='font-weight:bold;list-style-type:none; background-image:none'>"+da_months[$stories[0].date_month]+" "+$stories[0].date_day+", "+$stories[0].date_year+"</li>";
	   }
	   for( var i=0; i<$stories.length && i<$opts.max; i++) {
		   if(i>0 && (i<$stories.length) && $opts.display_date) {
			   if($stories[i-1].date_day != $stories[i].date_day) {
				   html+= "</ul><ul>";
				   if($opts.jrh) html += "<li style='font-weight:bold;list-style-type:none; background-image:none; padding-left:0px; padding-top:10px;'>"+da_months[$stories[i].date_month]+" "+$stories[i].date_day+", "+$stories[i].date_year+"</li>";
				   else html += "<li style='font-weight:bold;list-style-type:none; background-image:none'>"+da_months[$stories[i].date_month]+" "+$stories[i].date_day+", "+$stories[i].date_year+"</li>";
			   }//end if today is different then yesterday
		   }//end display_date if
		  
			html += "<li><a href='"+$stories[i].url+"' target='_blank'>"+$stories[i].title + "</a> <em style='font-size:75%'>" +$stories[i].date_month+sep+$stories[i].date_day+sep+$stories[i].date_year+ "</em></li>";
	   }
	   html += "</ul>";
	   
	   //if there's more stories to be told
	   if($opts.max < $stories.length ) html += "<p><a href='/about/news/daily.aspx'>More news...</a></p>";
	   
	   return html;
   }
   
   function bnews_sort(a,b) {
	   var asc = -1;
	   
	   var one = a.date.split(/\D/g);
	   var two = b.date.split(/\D/g);
	   
	   if(one.length==3 && two.length==3) {
		   //test year, month, day
		   if( parseInt(one[2]) > parseInt(two[2]))  return 1*asc;
		   else if(parseInt(one[2]) < parseInt(two[2])) return -1*asc;
		   else {
			   //test month
			   if(parseInt(one[0]) > parseInt(two[0])) return 1*asc;
			   else if(parseInt(one[0]) < parseInt(two[0])) return -1*asc;
			   else {
				   if(parseInt(one[1]) > parseInt(two [1])) return 1*asc;
				   else if(parseInt(one[1]) < parseInt(two [1])) return -1*asc;
				   else return 0;
			   }
		   }
	   }
   }
   
   function isnum(n){
	   if( n>47 && n<58){
		   return true;
		}
		return false;
   }
   
   function bnewstype(what) {
	   if(what.indexOf("http") == 0){
		  return "url";
	   } else if( isnum(what.charCodeAt(0)) ) {
		   return "date";
	   }
	   else {
		   return "title";
	   }
   }
})(jQuery);

