/*
 	Title: javascript.js

	About:
		Created - 2008-09-19
		Author - Susan Kelly

	Description: 
		This file contains the javascript functions to support the asynchronus update of blog categories

	Methods:
	
		deleteRecord(catID)
			Purpose: Removes a category
			Inputs: catID - BlogCategoryID of category being edited
		
 */
 
var cfcPath = 'cf_blog/include/forum.cfc';

function okToDelete(pageId,entryId,commentId,view) {
	
	var params = 'method=getReplyThread&edittext=1&currentCommentID=' + commentId + '&entryID=' + entryId;
	
	var AjaxObject = {
		
		handleSuccess:function(o){
	
			var xmlString = o.responseText;
			
			startPos = xmlString.indexOf('<string>',0);
			endPos = xmlString.indexOf('</string>',0);
			
			stringPart = xmlString.slice(startPos,endPos).replace('<string>','');
	
			var idCount = 0;
			// If the a string was returned, split the string at the commas and get the length of the array
			if (stringPart.length > 0) {
				stringPart += ',';
				var idArray = stringPart.split(',');
				idCount = idArray.length;
			}
		
			var confirmText = '';
			
			var okToDelete = false;
			
			var numBranches = idCount - 2; // remove 2 as the first item is the comment and the last item is "blank" from array split
			
			if (numBranches == 0) {
				confirmText = 'Selecting OK will permanently delete this comment.  Are you sure you want to delete this comment.  '; 
			} else if (numBranches == 1) {
				confirmText = 'Selecting OK will permanently delete this comment and the one reply to this comment. '; 
			} else {
				confirmText = 'Selecting OK will permanently delete this comment and the ' + numBranches + ' replies to this comment. '; 
			}
			
			if (idCount > 0) { okToDelete = confirm(confirmText); }
			
			if (okToDelete) { document.location.href = 'page.cfm?p=' + pageId + '&eid=' + entryId + '&cid=' + commentId + '&do=deletecomment&view=' + view }
			
			return okToDelete;
		},
	
		handleFailure:function(o){
			// Failure handler
			alert('There was an error deleting the category.');
			return false;
		},
	
		startRequest:function() {
		   YAHOO.util.Connect.asyncRequest('POST', cfcPath, callback, params);
		}
	
	};
	
	/*
	 * Define the callback object for success and failure
	 * handlers as well as object scope.
	 */
	var callback =
	{
		success:AjaxObject.handleSuccess,
		failure:AjaxObject.handleFailure,
		scope: AjaxObject
	};
	
	// Start the transaction.
	AjaxObject.startRequest();	

	
}

function toggleCommentView(which) {
	commentBlock = document.getElementById('show_'+which);
	if(commentBlock.style.display == 'none') {
		commentBlock.style.display = 'block';
		document.getElementById('viewlink_'+which).innerHTML = 'hide';
	}
	else {
		commentBlock.style.display = 'none';
		document.getElementById('viewlink_'+which).innerHTML = 'expand';
	}
}

function hideAll() {
	for(var i=0; i<commentArray.length; i++){
		document.getElementById('show_'+commentArray[i]).style.display = 'none';
		document.getElementById('viewlink_'+commentArray[i]).innerHTML = 'expand';
	}
}

function expandAll() {
	for(var i=0; i<commentArray.length; i++){
		document.getElementById('show_'+commentArray[i]).style.display = 'block';
		document.getElementById('viewlink_'+commentArray[i]).innerHTML = 'hide';
	}
}

function quoteThis(cid,originatorName) {
	var quotedText = document.getElementById('show_'+cid).innerHTML;
	var inst = tinyMCE.activeEditor;
	
	var nameText = 'Originally posted by <strong>' + originatorName + '</strong><br>';
	
	var currentText = inst.getContent();
	inst.setContent(currentText + '<blockquote class="blockquote">' + nameText + quotedText + '</blockquote><br>');
}



function updatereply(isParentPublished) {
	
	var okToPublish = true;
	
	// if the previous comment is published, the reply can be posted.  Otherwise, prompt the user (admin user) letting them know
	// by responding they will be publishing the parent post.  If the current post is marked as private, publishing the post will
	// mark the parent comment as private
	if (isParentPublished == 0) {

		// firrst check to see if markPrivate is a checkbox or a hidden value.
		var elems = document.getElementsByName('markPrivate');
		var makePrivate = 0;
		
		// You are replying to an unpublished comment.  Publishing your reply will privately publish both your reply and the associated comment.  Do you wish to continue with publishing your reply?
		
		if ( elems.length == 1 ) {
			var elemType = elems[0].type;
			if (elemType == 'hidden' || (elemType == 'checkbox' && elems[0].checked)) {
				makePrivate = elems[0].value * 1;
			}
			
			var msg = 'You are replying to an unpublished comment.  Publishing your reply will publish both your reply and the associated comment.  Do you wish to continue with publishing your reply?';
			
			if (makePrivate) {
				msg = 'You are replying to an unpublished comment.  Publishing your reply will privately publish both your reply and the associated comment.  Do you wish to continue with publishing your reply?';
			}
			
			okToPublish = confirm(msg);
		}
	}
	
	
	if ( okToPublish ) {
		document.update.publish.value  = 1;
		document.update.submit();
	} 
}

function updateForumPost(currentStatus)
{
	document.update.publish.value = currentStatus;
	document.update.submit();
}
function unpublish(currentStatus)
{
	var okToUnpublish = true;
	if (currentStatus == 1) {
		okToUnpublish = confirm('Are you sure you want to un-publish this comment.  Un-publishing this comment will remove it from the front end until it is published.');
	}
	
	if (okToUnpublish) {
		document.update.publish.value  = 0;
	}
	document.update.submit();
}

function saveForumDraft()
{
	document.update.publish.value = 0;
	document.update.submit();
}

// ========================================================================== 
// ! jQuery plugin gently breaks long, unwrappable strings to assist layout   
// ========================================================================== 
$j(document).ready(function(){
	// every 38th character we'll add a zero-length space character
	$j("div.blogtopic div, div.blogpost div, div.commentContent").fsBreakly(38);
});


