/*
Author: Dean Stringer (deeknow @ pobox . com)
Original Release Date: 17/Apr/2006
This Release Date: 27/Apr/2006
Version: 0.7
Description/Purpose:

	The following are custom functions to be built as optional handlers for wrapping
	content in the body of items, are only called if tree.customBodyHandlers=true

LICENSE

treeCustom.js: custom text handler utils for treeEdit based Jiki
Copyright (C) 2006  Dean Stringer (deeknow @ pobox . com)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/



// custom vars - used by/in customHandler() sub
var navHistory = new Array();	// history of words recently clicked
var navLastWord = '';		// the word last clicked in our custom handler area
var navLoadCount=0;
var navTagHistory = new Object();
var customDoFlickrLookups = false;
var customKeepTagHistory = true;
var customShowRecent = false;
var customFlickrProxy = '';		// needs to point to local flickr agent CGI, e.g. flickr-search.cgi

/**
 * Transforms HTML body for a note by wrapping custom javascript handler calls around
 * each wiki-word of the body. 
 * @param (String) thisBody - the HTML to custom format
 */
function customFormat(thisBody) {
	if (thisBody =='') { return "<em>-- blank, double click to edit --</em>"; }
	thisBody = thisBody.replace(/\n/g, '<br>\n'); 
	var temp = new Array();
	temp = thisBody.split(' ');
	var returnBody = '';
	for (var i=0;i<temp.length;i++) {
		var thisString = temp[i];
		//if (! thisString.match(/\'|,|\.|\?/g)) {
		// format this word as a link, unless its the last word, or contains non-word chars or isnt longer than 2 chars
		if ((thisString != navLastWord) &&
			((thisString.match(/^([A-Z]{1}[a-z]*){2,}$/g)) || (thisString.match(/^%\w+%/g))) &&
			(thisString.length > 3)) {

			thisString = thisString.replace(/^%(.+)%$/g, '$1'); 
			thisLabelString = thisString.replace(/([a-z]+)([A-Z]{1})/g, '$1 $2'); 

				thisString = "<a class='treeWord' onclick='javascript:handleClick(event.type,\"" + thisString + "\");'" +
				" ondblclick='javascript:handleClick(event.type,\"" + thisString + "\");'" + 
				">" + thisLabelString + "</a>";
		}
		returnBody = returnBody + thisString + ' ';
	}
	return returnBody;
}


/**
 * Transforms HTML body for a note by wrapping custom javascript handler calls around
 * each word of the body in a Wiki like fashion. Stop-words are skipped as are words
 * shorter than 3chars.
 * @param (String) thisBody - the HTML to custom format
 */
function customFormatSingleWords(thisBody) {
	if (thisBody =='') { return "<em>-- blank, double click to edit --</em>"; }
	thisBody = thisBody.replace(/\n/g, '<br>\n'); 
	var temp = new Array();
	temp = thisBody.split(' ');
	var returnBody = '';
	for (var i=0;i<temp.length;i++) {
		var thisString = temp[i];
		//if (! thisString.match(/\'|,|\.|\?/g)) {
		// format this word as a link, unless its the last word, or contains non-word chars or isnt longer than 2 chars
		if ((thisString != navLastWord) &&
			(! thisString.match(/[^\w|\-]/g)) &&
			(thisString.length > 2)) {

			var stopWords = new Array('all','and','are','but','can','for','from','had','have','her','his','its','may','not','that','the','this','was','which','will','with','you','your');

			var isStop = 0;
			for (var j=0;j<stopWords.length;j++) {
				var thisStringLower = thisString.toLowerCase();
				if (thisStringLower == stopWords[j]) {
					isStop = 1;
					j = stopWords.length;
				}
			}
			if (! isStop) {
				//thisString = "<a class='treeWord' onclick='javascript:customHandler(\"" +
				//	thisString + "\");'>" + thisString + "</a>";
				thisString = "<a class='treeWord' onclick='javascript:handleClick(event.type,\"" + thisString + "\");'" +
				" ondblclick='javascript:handleClick(event.type,\"" + thisString + "\");'" + 
				">" + thisString + "</a>";
			}
		}
		returnBody = returnBody + thisString + ' ';
	}
	return returnBody;
}




function customHandler(thisWord) {
	tree.editing = false;	// in case we click a nav link w/out closing the edit box
	tree.sourceFile = thisWord + ".xml";
	asynchMsgStatusUpdate('Loading...');
	tree.init(thisWord + ".xml","url");
	if (thisWord != 'help' && thisWord != 'index' && customDoFlickrLookups) {
		asynchFlickrGet(thisWord);	// skip if index or help
	}
	var maxItems = 5; var isInList = 0;
	for (i=1;i<maxItems;i++ ) {
		if (thisWord == navHistory[i-1]) { isInList = 1; }
	}

	if (customKeepTagHistory)	{
		var histDivider = 2;	var histMax = 8;
		var tagList = utils.getByID('tagHistory');
		if (navTagHistory[thisWord]) {
			navTagHistory[thisWord]++;
		} else {
			navTagHistory[thisWord] = 1;
		}
		//alert(navTagHistory[thisWord]);
		var newTagHistoryVal = '';
		for (i in navTagHistory) {
			var thisCount = Math.round((navTagHistory[i]/histDivider)+1);
			if (thisCount > histMax) { thisCount = histMax;	}
			newTagHistoryVal += "<a class='t" + thisCount + "' onclick='javascript:handleClick(event.type,\"" + i + "\");'>" + i + '</a> ';
		}
		tagList.innerHTML = newTagHistoryVal;
	}
	
	if (customShowRecent)	{
	if ((navLastWord != thisWord) && (thisWord != 'help') && (thisWord != 'index') && ! isInList) {
		if (navLoadCount == maxItems) {
			for (i=1;i<maxItems;i++ ) {
				navHistory[i-1] = navHistory[i];
			}
			navHistory[maxItems-1] = thisWord;
		} else {
			navHistory[navLoadCount] = thisWord;
			navLoadCount++;
		}
		var navMenu = utils.getByID('myNavHistory');
		var	newNavMenu = 'Recent: ';
		for (i=0;i<navLoadCount;i++ ) {
			newNavMenu = newNavMenu + " > <a class='treeWord' onclick='javascript:customHandler(\"" +
			navHistory[i] + "\");'>" + navHistory[i] + "</a>"; 
		}
		navMenu.innerHTML = newNavMenu;
	}

	}
	navLastWord = thisWord;
}
