$(document).ready(
function()
{
	$('#keywords').livesearch('/blog/livesearch/',{width: 200});
	$('#search form').attr("action", "/blog");
	resetImageMargins();
	setupMiniNav();
	setupComments();
	setupArchive();

	$('.dsq-sharing-options').attr("original-title", "");
});

function resetImageMargins()
{
//$('#posts img').parent('p').attr('style','margin: 0px;padding: 0px;');

}

function setupMiniNav()
{
// mini nav tabs
$("#tabs a").click(
	function()
	{	
		var currentBox = $(this).attr("href");
		openMiniNav(currentBox);
		return false;
	}
);
}

function openMiniNav(tabID)
{
// reset other tabs
$("#tabs li").attr("id", "");
$("#mini-nav .box>div").attr("style", "display: none;");

// open tab content & highlight tab
$(tabID).slideDown("fast");
$("a[href='" + tabID + "']").parent('li').attr("id", "current");
}

function ajaxComments()
{
//give comment form unique ID on multi-entry pages
var commentForms = $(".comment-box form");

 $.each(commentForms, function()
 {
 var parent_id = $(this).parents("div.comment-box").attr("id");
 var target_name = "#" + parent_id;
 var form_name = target_name + " #comment_form";

 $(this).ajaxify({
	 target: target_name,
	 forms: form_name,
	 link: 'index.php/blog/ajax_comments_template/', 
	 loading_img:'/images/uploads/site_images/loading_gray.gif',
	 event: 'submit',
	 method: 'POST',
	 onSuccess: function(options,data){ ajaxComments(); }
	 });
 });
}

function setupComments()
{
ajaxComments();


// show/hide comments

$(".post .comments .cta a").click(
	function()
	{
		var thisPost =  "div#" + this.rel;
		var commentOpen = false;
		var linkCopy = "";
		
		commentOpen = toggleCommentDisplay(thisPost);
		
		if(commentOpen)
		{
			linkCopy = "Hide Comments";
		}
		else
		{
			if( $(this).hasClass("comment-enabled") )
			{
			linkCopy = "Leave a Comment";
			}
			else
			{
			linkCopy = "View Comments";
			}
		}
		
		//$(this).html(linkCopy);
		//return false;
	}
);
}

function toggleCommentDisplay(thisPost)
{
	if ( $(thisPost).hasClass("open") )
	{
		$(thisPost).removeClass("open").addClass("closed").slideUp("fast");
		return false;
	}
	else
	{
		$(thisPost).removeClass("closed").addClass("open").slideDown("fast");
		return true;
	}
}

function setupArchive()
{

// archive page
$("#nav_cat_archive li ul").hide().append('<li class="close"><a href="#">^ close</a></li>');

$("#nav_cat_archive li ul li:last-child").click(function(){
	var itemTitle = $(this).parent('ul').parent('li').children('h3');
	$.scrollTo(itemTitle);
	$(this).parent('ul').slideUp();
	return false;
});

$("#nav_cat_archive li h3").click(
function()
{
$(this).parent('li').children('ul').toggle("fast");
if ($(this).hasClass("selected"))
{
$(this).removeClass("selected");
}
else
{
$(this).addClass("selected");
}
});

}
