// Business Catalyst Blog Dates customizer
// Based on JQuery 1.4.2
// By Nelson Ho based on the work of Chris Matthias (chris@boldcode.com)


// This plugin takes one arguments;  true or false for "noStrings".  If you'd like to replace the month strings like "Feb" with "02" just pass in true. See usage below
// Most Business Catalyst installs will use the following selector: ".blogsitesummary span.date" but this should work for any parent element that contains a date with the format dd-mm-yyy.

// Usage Example:  $(".blogsitesummary span.date").bcBlogDates();
// Usage Example with no strings: $(".blogsitesummary span.date").bcBlogDates(true);

//indexOf on a Javascript array does not work in IE
//snippet source:http://www.pearweb.com/javascript/array-index-of.html
if(!Array.indexOf){
  Array.prototype.indexOf = function(obj){
   for(var i=0; i<this.length; i++){
    if(this[i]==obj){
     return i;
    }
   }
   return -1;
  }
}

jQuery.fn.bcBlogDates = function(noStrings,insertBefore,usFormat) {
	$(this).each(function() {
	
		/*datePreText = $(this).text().trim();*/
		datePreText = $.trim( $(this).text() );/*had to write this way for IE*/


      var Day   = 0;
      var Month = 0;
      var Year  = 0;

      // Regular expression to match the expected date format:
      var pattern = /(\d\d)-(\w\w\w)-(\d\d\d\d)/;
      match = pattern.exec(datePreText);

      if (match) {
         // The match was successful.  This is a valid date format.
         Day   = match[1];
         Year  = match[3];
         var monthCompact = match[2];
         var monthMap = new Array ( 
            "Jan", "Feb", "Mar", "Apr", "May", "Jun",
            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
         );
         Month = monthMap.indexOf(monthCompact) + 1;
      }

      if (Day <= 0 || Month <= 0 || Year <= 0) {
         // Unable to parse the date string, use today's date.
         var today = new Date();
         Month = today.getMonth() + 1;
         Day   = today.getDate();
         Year  = today.getFullYear();
      }

      // format the Day and Month
      if (usFormat && noStrings) {
         if (parseInt(Day) < 10) {
            Day = Day[1];
         }
      }
      else if (usFormat) {
         var monthStrings = new Array (
            "January",
            "February",
            "March",
            "April",
            "May",
            "June",
            "July",
            "August",
            "September",
            "October",
            "November",
            "December"
         );

         Month = monthStrings[Month-1];
      }

		var c = "<div class='bcBlogDate'><span class='bcDay'>"+Day+"</span> <span class='bcMonth'>"+Month+"</span> <span class='bcYear'>"+Year+"</span></div>"
		var e = "<div class='bcBlogDate'><span class='bcMonth'>"+Month+"</span> <span class='bcDay'>"+Day+"</span>, <span class='bcYear'>"+Year+"</span></div>"
		
		// Update the date's to a div and add inner spans with classes for easier CSS
		jQuery(this).parent().find("div.bcBlogDate").remove();
		
		if ((noStrings == false && insertBefore && usFormat == null) || (noStrings == false && insertBefore && usFormat == false)) {
			jQuery(this).parent().prepend(c);
		}
		else if (noStrings && usFormat && insertBefore) {
			jQuery(this).parent().prepend(c);
		}
		else if (noStrings && usFormat && insertBefore == false) {
			jQuery(this).after(c);
		}
		else if (insertBefore && usFormat) {
			jQuery(this).parent().prepend(e);
		}
		else if (insertBefore == false && usFormat) {
			jQuery(this).after(e);
		}
		else if (noStrings == null && insertBefore == null &&  usFormat == null ) {
			jQuery(this).after(c);
		}
		else {
			jQuery(this).after(c);
		}
		jQuery(this).hide();
	
	});
}


