// Business Catalyst Blog Dates customizer
// Based on JQuery 1.4.2
// By 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);

 

j$.fn.bcBlogDates = function(noStrings,insertBefore,usFormat) {
	j$(this).each(function() {
	
		datePreText = jQuery(this).text().trim();

		DayA = datePreText.charAt(0);
		DayB = datePreText.charAt(1);
		Day = DayA+DayB;

		MonthA = datePreText.charAt(3);
		MonthB = datePreText.charAt(4);
		MonthC = datePreText.charAt(5);
		Month = MonthA+MonthB+MonthC;
		
		YearA = datePreText.charAt(7);
		YearB = datePreText.charAt(8);
		YearC = datePreText.charAt(9);
		YearD = datePreText.charAt(10);
		Year = YearA+YearB+YearC+YearD;
		
		// Set the names of the months.  You might update
		if (Month == "Jan") {
			monthNum = "01";
			monthName = "January";
		}
		else if (Month == "Feb") {
			monthNum = "02";
			monthName = "February";
		}
		else if (Month == "Mar") {
			monthNum = "03";
			monthName = "March";
		}
		else if (Month == "Apr") {
			monthNum = "04";
			monthName = "April";
		}
		else if (Month == "May") {
			monthNum = "05";
			monthName = "May";
		}
		else if (Month == "Jun") {
			monthNum = "06";
			monthName = "June";
		}
		else if (Month == "Jul") {
			monthNum = "07";
			monthName = "July";
		}
		else if (Month == "Aug") {
			monthNum = "08";
			monthName = "August";
		}
		else if (Month == "Sep") {
			monthNum = "09";
			monthName = "September";
		}
		else if (Month == "Oct") {
			monthNum = "10";
			monthName = "October";
		}
		else if (Month == "Nov") {
			monthNum = "11";
			monthName = "November";
		}
		else if (Month == "Dec") {
			monthNum = "12";
			monthName = "December";
		}
		else {
			monthNum = null;
			monthName = null
		}
	
		if (usFormat && noStrings) {
			Day = DayA+DayB;
			Month = monthNum;
		}
		else if (usFormat) {
			Month = monthName;
			
			if (Day.charAt(0) == "0") {
				Day = DayB;
			}
		}

		if (noStrings) {
			Month = monthNum;
		}

		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();
	
	});
}
