(function($) {

//page functions
$(".BloggerBrowseByTags a.read-more").live("click", function() {
	$(".BloggerBrowseByTags ul li:gt(4)").show();
	$(this).hide();
	$(".BloggerBrowseByTags a.read-less").show();
});

$(".BloggerBrowseByTags a.read-less").live("click", function() {
	$(".BloggerBrowseByTags ul li:gt(4)").hide();
	$(this).hide();
	$(".BloggerBrowseByTags a.read-more").show();
});

function SplitVidID(stringToSplit, charToSplit) {
	var myArray = stringToSplit.split(charToSplit);
	return myArray;
}

function SplitPostID(stringToSplit, charToSplit) {
	var myArray = stringToSplit.split(charToSplit);
	return myArray;
}

function SplitPostURL(stringToSplit, charToSplit) {
	var myArray = stringToSplit.split(charToSplit);
	return myArray;
}

$(document).ready(function() {

if (!$("#hsnav").length) {
	$("p.Normal:has(a.MenuTopSelected)").css("display", "none");
}

//Put inner contents into .postcontent div on blog's homepage
if( $("#bb-container div.listing").length) {
	$(".post").each(function() {

		var title = $(this).find(".title");
		var byline = $(this).find(".byline");
		var postheader = $(this).find(".post-header");
		var submissions = $(this).find(".submissions");
		var comments = $(this).find(".comments");
		var tags = $(this).find(".tags");
		var commentslink = $(this).find(".commentlink");

		$(this).find(".title,.byline,.post-header,.submissions,.commentlink,.comments,.tags").remove();

		var postContent = $(this).html();
		$(this).empty();
		$(title).appendTo($($(this)));
		$(byline).appendTo($($(this)));

		//inner-content is used to hide undesirable content in post preview
		$('<div class="postcontent">' + postContent + '</div>').appendTo(this);
		$(tags).appendTo($($(this)));
		$(commentslink).appendTo($($(this)));
		$(submissions).appendTo($($(this)));

	});

	//Post Truncating
	$('div.postcontent').each(function(){

		$(this).find('object:first').each(function(){
			var vidUrl = $(this).find("param[name*='src']").attr("value");
			if(vidUrl != null&&vidUrl.match('http://www.youtube.com/v/')){
				var ytID = vidUrl.replace("http://www.youtube.com/v/","");
				var vidID = SplitVidID(ytID, "fs")[0];
				var ytVidID = vidID.replace("?","");

				var imgUrl = "http://img.youtube.com/vi/" + ytVidID + "/0.jpg";
	
				$(this).hide();
				$(this).after('<img class="thumbnail" alt="View This Video" src="' + imgUrl + '" />');
			}
		});

		$(this).find("img:nth-child(1)").not(".socialicons img,.metadata img,.cta-post img,td img").removeClass().addClass("thumbnail");
		$(this).find(".thumbnail").thumbs();
		$(this).children().not('.thumbnailLink:first,p:first,object:first,.thumb-img:first').hide();

		$(this).append($(this).find("p:first"));
		$(this).append('<p class="readmore"><a href="' + $(this).closest('.post').find('.title a:last').attr("href") + '"> Read More</a></p>');

		var image = $(this).find(".thumbnail:first");
		$(image).wrap('<a class="thumbnailLink" href="' + $(image).closest('.post').find('.title a:last').attr("href") + '" />');			
		$(image).each(function() {
			$(image).attr("align","");
			$(image).closest(".thumb-img").prependTo($(this).closest(".post").find(".postcontent"));
		});

		$(this).find(".thumb-img img").each(function(){
			var imgWidth = parseInt($(this).css("width").replace("px",""));
			if (imgWidth < 200) {
				$(this).closest(".thumb-inner").css('width', imgWidth + 'px');
			}
		});
	});


}


//Put inner contents into .postcontent div on individual blog posts page
if( ! $("#bb-container div.listing").length) {
	$(".post").each(function() {

		var title = $(this).find(".title");
		var byline = $(this).find(".byline");
		var postheader = $(this).find(".post-header");
		var submissions = $(this).find(".submissions");
		var buttons = $(this).find(".buttons");
		var tags = $(this).find(".tags");
		var commentslink = $(this).find(".commentlink");

		$(this).find(".title,.byline,.post-header,.submissions,.commentlink,.buttons,.tags").remove();

		var postContent = $(this).html();
		$(this).empty();
		$(title).appendTo($($(this)));
		$(byline).appendTo($($(this)));
		$(postheader).appendTo($($(this)));
		$(submissions).appendTo($(postheader));
		$(commentslink).appendTo($(postheader));

		//inner-content is used to hide undesirable content in post preview
		$('<div class="postcontent">' + postContent + '</div>').appendTo(this);

	});
}


//Set the link for .commentlink
$(".commentlink a").each(function() {
	var postLink = $(this).closest(".post").children(".title").find("a:last").attr("href");
	var postPageLink = window.location;

	if( $("#bb-container div.listing").length) {
		$(this).attr("href", postLink + "#Comments");
	}
	if( ! $("#bb-container div.listing").length) {
		$(this).attr("href", postPageLink + "#Comments");
	}
});

//social icon links
if( $("#bb-container div.listing").length) {
	$("div.submissions").each(function() {

		//grabbing post ID for email share
		var postURL = $(this).closest(".post").children(".title").find("a:last").attr("href");
		var postUrlEncode = encodeURIComponent(postURL);
		var postTitle = $(this).closest(".post").children(".title").find("a:last").text()
		var postID = SplitPostID(postURL, "/")[5];

		//these are blog specific and will need to be changed for each blog
		var blogPID = 41683; /* 41683; */
		var blogTID = 225544; /* 104045; */
		var blogMID = 444602; /* 126244; */

		$(this).empty();

		$(this).append(' <span class="text">Share:</span> <a class="social-link email-link" onclick="BBEmailArticle.EmailArticleClick(event, '+postID+',&quot;'+postTitle+'&quot;);" href="#emart-form-anchor" title="Email Article">Email Article</a> ');
		$(this).append(' <a class="social-link twitter-link" title="Tweet this" onmouseover="window.status=&quot;&quot;;return true;" rel="nofollow" href="http://twitter.com/share?url='+postUrlEncode+'&amp;text='+postTitle+'" target="_blank">Twitter</a> ');
		$(this).append(' <a class="social-link facebook-link" title="Share on Facebook" onmouseover="window.status=&quot;&quot;;return true;" rel="nofollow" href="/CMS/UI/Modules/BizBlogger/LinkSubmission.aspx?bid='+postID+'&amp;pid='+blogPID+'&amp;tid='+blogTID+'&amp;mid='+blogMID+'&amp;srv=facebook&amp;trk=0" target="_blank">Facebook</a> ');
		$(this).append(' <a class="social-link delicious-link" title="Add to delicious" onmouseover="window.status=&quot;&quot;;return true;" rel="nofollow" href="/CMS/UI/Modules/BizBlogger/LinkSubmission.aspx?bid='+postID+'&amp;pid='+blogPID+'&amp;tid='+blogTID+'&amp;mid='+blogMID+'&amp;srv=delicious&amp;trk=0" target="_blank">delicious</a> ');
		$(this).append(' <a class="social-link stumbleupon-link" title="Submit to StumbleUpon" onmouseover="window.status=&quot;&quot;;return true;" rel="nofollow" href="/CMS/UI/Modules/BizBlogger/LinkSubmission.aspx?bid='+postID+'&amp;pid='+blogPID+'&amp;tid='+blogTID+'&amp;mid='+blogMID+'&amp;srv=stumbleupon&amp;trk=0" target="_blank">StumbleUpon</a> ');
		$(this).append(' <a class="social-link linkedin-link" title="Share on LinkedIn" onmouseover="window.status=&quot;&quot;;return true;" rel="nofollow" href="/CMS/UI/Modules/BizBlogger/LinkSubmission.aspx?bid='+postID+'&amp;pid='+blogPID+'&amp;tid='+blogTID+'&amp;mid='+blogMID+'&amp;srv=linkedin&amp;trk=0" target="_blank">LinkedIn</a> ');

	});
}

if( ! $("#bb-container div.listing").length) {
	$("div.submissions").each(function() {

		var postURL = window.location;
		var postUrlEncode = encodeURIComponent(postURL);
		var postTitle = $(".title").text();

		//grabbing post ID for email share
		var postIDEmail = $("a.email-art").attr("id");
		var postID = postIDEmail.replace("emart-","");

		//these are blog specific and will need to be changed for each blog
		var blogPID = 41683;
		var blogTID = 104045;
		var blogMID = 126244;

		$(this).empty();

		$(this).append(' <span class="text">Share:</span> <a class="social-link email-link" onclick="BBEmailArticle.EmailArticleClick(event, '+postID+',&quot;'+postTitle+'&quot;);" href="#emart-form-anchor" title="Email Article">Email Article</a> ');
		$(this).append(' <a class="social-link twitter-link" title="Tweet this" onmouseover="window.status=&quot;&quot;;return true;" rel="nofollow" href="http://twitter.com/share?url='+postUrlEncode+'&amp;text='+postTitle+'" target="_blank">Twitter</a> ');
		$(this).append(' <a class="social-link facebook-link" title="Share on Facebook" onmouseover="window.status=&quot;&quot;;return true;" rel="nofollow" href="/CMS/UI/Modules/BizBlogger/LinkSubmission.aspx?bid='+postID+'&amp;pid='+blogPID+'&amp;tid='+blogTID+'&amp;mid='+blogMID+'&amp;srv=facebook&amp;trk=0" target="_blank">Facebook</a> ');
		$(this).append(' <a class="social-link delicious-link" title="Add to delicious" onmouseover="window.status=&quot;&quot;;return true;" rel="nofollow" href="/CMS/UI/Modules/BizBlogger/LinkSubmission.aspx?bid='+postID+'&amp;pid='+blogPID+'&amp;tid='+blogTID+'&amp;mid='+blogMID+'&amp;srv=delicious&amp;trk=0" target="_blank">delicious</a> ');
		$(this).append(' <a class="social-link stumbleupon-link" title="Submit to StumbleUpon" onmouseover="window.status=&quot;&quot;;return true;" rel="nofollow" href="/CMS/UI/Modules/BizBlogger/LinkSubmission.aspx?bid='+postID+'&amp;pid='+blogPID+'&amp;tid='+blogTID+'&amp;mid='+blogMID+'&amp;srv=stumbleupon&amp;trk=0" target="_blank">StumbleUpon</a> ');
		$(this).append(' <a class="social-link linkedin-link" title="Share on LinkedIn" onmouseover="window.status=&quot;&quot;;return true;" rel="nofollow" href="/CMS/UI/Modules/BizBlogger/LinkSubmission.aspx?bid='+postID+'&amp;pid='+blogPID+'&amp;tid='+blogTID+'&amp;mid='+blogMID+'&amp;srv=linkedin&amp;trk=0" target="_blank">LinkedIn</a> ');

	});
}






if( $("#bb-container div.listing").length) {    
	//This code has to be after the above fadeIn in order to get the Height
	$(".thumb-img img").each(function(){

		var imgHeight = parseInt($(this).css("height").replace("px",""));
		if(imgHeight < 200 && imgHeight > 100) {
			$(this).closest(".thumb-inner").css('height', imgHeight.toString() + 'px');
		}
	});
}


//email subscribe
$("div.block:has(.IngeniMailSubscribeContainer)").addClass("subscribe");
$("div.subscribe .IngeniMailSubscribeEmailInput").val("Enter e-mail address");
$("div.subscribe .IngeniMailSubscribeEmailInput").click(function() {
	if($(this).val() == "Enter e-mail address") $(this).val("");
});
$("div.subscribe .IngeniMailSubscribeEmailInput").blur(function() {
	if($(this).val() == "") $(this).val("Enter e-mail address");
});

$("div.subscribe .IngeniMailSubscribeButton").val("");


//read more tags in taglist
$(".BloggerBrowseByTags ul li:gt(4)").css("display","none");
$(".BloggerBrowseByTags").append(' <a class="read-more tagList">More</a> ');
$(".BloggerBrowseByTags").append(' <a class="read-less tagList">Less</a> ');
$(".BloggerBrowseByTags a.read-less").css("display","none");

});

})(jQuery);
