

jQuery('#nav-comments a').click(function() {
    var tab = jQuery(this);
    var tabType = tab.attr('id');

    if (tab.hasClass('current')) {
    } else {

        var nav = jQuery('#nav-comments');
        var tabTarget = '#comments-' + tabType.replace(/tab-/, "");

        nav.find('a.current').removeClass('current');
        tab.addClass('current');

        jQuery('.module-comments.show').css('display', 'none').removeClass('show');
        jQuery(tabTarget).css('display', 'block').addClass('show');
    }
    return false;
});



// clear filter box
jQuery('.clear-table-filter').click(function(){
	var clearLink = jQuery(this);
	clearLink.prev('input.table-filter').val('').trigger('keyup');
	return false;
});



/** functions to handle reply to comment click **/
jQuery('.comment-reply').click(function(){

	var comment = jQuery(this).parents('li');
	var commentId = comment.attr('id').replace(/comment-/, "");
	var author = comment.find('.author').attr('title');
	var form = jQuery('#comment_form');

	// jump to form
	location.href = '#comment_form';

	// update form fields
	form.find('h3').html("REPLY TO "+author+"'s COMMENT");
	form.find('#comment_reply_to').val(commentId);
	form.find('.comment-reply-cancel').show();

	return false;
});



jQuery('.comment-reply-cancel').click(function(){

	var form = jQuery('#comment_form');

	form.find('h3').html("ADD A COMMENT");
	form.find('#comment_reply_to').val('');

	jQuery(this).hide();

	// jump to comments
	location.href = '#comments-posted';

	return false;
});


function decrementCommentCount() {

	var countElement = jQuery('#comment_count');
	var countValue = countElement.html();

	if (countValue != 0) {
		countValue = countValue - 1;
  		countElement.html(countValue);
	}
}


function pushToPublished(id){

	var row = jQuery('#unpublished-comments-table tbody').find('#'+id);
	var html = row.html();
	var tbody = jQuery('#published-comments-table tbody');

	row.remove();
	html = jQuery(document.createElement('tr')).append(html);
	html.attr('id',id);
	html.find('a.approve').remove();

	tbody.prepend(html);

	jQuery("table.sorted").tablesorter({
	  widgets: ["zebra"],
	  widgetZebra: {css: ["odd","even"]}
	});
}


// sort tables that have a .sorted class
jQuery("table.sorted").tablesorter({
  widgets: ["zebra"],
  widgetZebra: {css: ["odd","even"]}
});



// set filtering on various tables
jQuery('#tags-filter-box').keyup(function(){
	var theTable = jQuery('#tags-table');
	jQuery.uiTableFilter(theTable,this.value);
});

jQuery('#published-filter-box').keyup(function(){
	var theTable = jQuery('#published-comments-table');
	jQuery.uiTableFilter(theTable,this.value);
});

jQuery('#unpublished-filter-box').keyup(function(){
	var theTable = jQuery('#unpublished-comments-table');
	jQuery.uiTableFilter(theTable,this.value);
});

jQuery('#locations-filter-box').keyup(function(){
	var theTable = jQuery('table#locations');
	jQuery.uiTableFilter(theTable,this.value);
});


jQuery('#comment_user_id').change(function(){

	var optionValue = jQuery(this).val();
	var fieldsVisitor = jQuery('#fields-visitor');

	if (optionValue != '') {
		fieldsVisitor.hide();
	}

	else {
		fieldsVisitor.show();
	}
});


jQuery('#locations-sortable').sortable({
    update: function(){jQuery('#reset-locations').show();}
});


function buildInputs(){

	var locations = jQuery('#locations').find('li.location');
    var editForm = jQuery('#article_edit form.edit_article');
 	var orders = jQuery('#locations-sortable').sortable('toArray');


	locations.each(function(i){
        var locationId = jQuery(this).attr('id');

        editForm.append('<input class="reorder" type="hidden" value="' + i + '" name="' + locationId + '">');

	});

 	 editForm('submit');
}


jQuery('form.edit_article').submit(function(){

	buildInputs();

	return false;
});





/** functions to handle mouse-over and mouse-off for comments **/



function mouseOnComment(){

	var tools = jQuery(this).find('.comment_tools');

	tools.show();
}

function mouseOffComment(){

	var tools = jQuery(this).find('.comment_tools');

	tools.hide();
}

jQuery('#admin_comments li.comment').hover(mouseOnComment, mouseOffComment);

 