// Capitalises first letter of each word in string
function toTitleCase(str) {
    return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}

$(document).ready(function() {
	$('.roundabout ul').roundabout({
		'duration': 500,
		'btnNext': '#next',
		'btnPrev': '#previous',
		minScale: 0.5,
		maxScale: 1.0,
		minOpacity: 1.0
	});



	var $blogLink ='';
	$('#blog_preview .blog-post .post-body ').each( function(index) {
		// set limit for the length of description text under the title for the blog
		var $limit = 100;
		//set a limit for the title length
		var $titleLimit = 18;
		//define the description text
		var $str = $(this).html();

		// Get and format date, remove "details" div
		var $date = $(this).parent().find(".blog_post_date");

		$(this).parent().find(".post-details").remove();

		var dateInfo = $date.html().split(",");

		var month = $.trim(dateInfo[1]).substr(0, 3);
		var day = $.trim(dateInfo[1]).substr(-2, 3);
		
		// Create wrappers and add dates
		var prefix = '<div class="date_box"><span class="date">' + day + '</span><span class="month">' + month + '</span></div>';
		var suffix = '</div>';

		//if description is too long, make it smaller and append
		if ($str.length > $limit) {
			// Strip html tags
			$str = $str.replace(/(<([^>]+)>)/ig, "");
			$str = $.trim($str);

			var $smallArticle = $str.substr(0,$limit);
			$(this).html($smallArticle);

			//give the ..read more the same url as the title
			$blogLink = $(this).parent().find('.post-title').children('a').attr('href');

			//wrap the description in paragraph tags then append the ...read more
			$(this).wrapInner("<p>");
			$(this).find("p").last().append('... <a href="'+$blogLink+'" class="read_more">Read More</a>');
		}

		//Copy the title 
		$('.blog-special').html($(this).parent().find('.post-title').html());

		// Add dotted-bottom class to all posts except last
		var extraClass = " dotted-bottom";

		if (index == 1) {
			extraClass = "";
		}

		// Add wrappers
		$(this).parent().wrap('<div class="box-news' + extraClass + '"></div>')
					.parent().prepend(prefix).append(suffix);

		index++;
	});

	// Add blog tags to drop down menu
	var options = '<option value=" ">Select a category:</option>';
	$(".article_form .BlogTagList ul li").each(function() {
		$("a span", $(this)).remove();
		var tag = $("a", $(this)).html();

		options += '<option value="' + tag + '">' + toTitleCase(tag) + '</option>';
	});

	$("select.category_dropdown").html(options);

	// Remove tags
	$(".article_form .BlogTagList").remove();

	// Makes placeholder cross browser
	$.Placeholder.init();

	// Adds 'active' class to current page link in nav
	var page = window.location.pathname;
	//page = page.substring(0, page.lastIndexOf('/'));
	if (page == "") {
		page = window.location.pathname;
	}

	if ($('nav a[href="' + page + '"]') != null) {
		$('nav a[href="' + page + '"]').parents("nav ul li").last().addClass("active");
	}
});
