// kanjikoohiistorywithlinebreaks.user.js! user script for Firefox's GreaseMonkey extension
// version 0.7 BETA! Copyright (c) 2006, Ricardo Mendonga Ferreira
// Released under the GPL license: http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// 0.7 2007.07.08  Ricardo  Fix compatibility issue with "Copy Story"
// 0.6 2007.06.09  Ricardo  Fix after site changes by Fabrice
// 0.5 2007.05.28  Ricardo  Bugfix for the alert below
// 0.4 2007.05.01  Ricardo  Alert when story is > 512 bytes
//          (not sure if it will work on al platforms though...)
// 0.3 2007.01.10  woelpad  Recognize multiple keywords
// 0.2 2006.12.14  woelpad  Added keyword boldening and the like
// 0.1 2006.08.09  Ricardo  First release
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.  To install it, you need
// Greasemonkey 0.5 (?) or later: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "Kanji.Koohii: Story with Line Breaks", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          Kanji.Koohii: Story with Line Breaks
// @namespace     http://sites.mpc.com.br/ric/nihongo
// @description   Copies another user's story to your edit box with a click on a link.
// @include       http://kanji.koohii.com/study.php*
// @exclude       
// ==/UserScript==
//
// Used to be:
// @namespace     http://userscripts.org/scripts/show/6745

// References: http://developer.mozilla.org/en/docs/DOM:element
//             http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference
//             http://www.regular-expressions.info/javascript.html

String.prototype.addSlashes = function(){
	return this.replace(/(["\\\.\|\[\]\^\*\+\?\$\(\)])/g, '\\$1');
}
String.prototype.trim = function () {
    return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
};

var storyedit = document.getElementById('storyedit');
if (storyedit == null) return;

//-- Verify story size limit
var checksize = " onClick=\"v=document.frmFrame.txtStory.value; s=v.length;m1=v.match(/\\n/g);m2=escape(v).match(/%u\\d\\d\\d\\d/g); if(m1!=null) s+=m1.length; if(m2!=null) s+=2*m2.length; if (s>512) {alert('Story too long: '+s+' bytes (max. 512). Remove '+(s-512)+ ' bytes.');return false;} else {return true;};\"";
var btn = document.getElementById('btnUpdate').parentNode;
btn.innerHTML = btn.innerHTML.replace('title="Update story"', 'title="Update story"'+checksize);

//-- Line Breaks
var keyword = storyedit.parentNode.parentNode.getElementsByTagName('div')[0].firstChild.nodeValue.trim();
var textarea  = storyedit.getElementsByTagName('textarea')[0];
var story = textarea.innerHTML;
if (story == '') return;
var storyview = document.getElementById('sv-textarea');
var frames_array = storyview.innerHTML.match(
   /<i>[^<]+?<\/i>\s+\(<a\s+href=".framenum=\d+">FRAME\s+\d+<\/a>\)/ig);

var stories = story.split('\n');
for (var i = 0; i < stories.length; i++) {
	if (!stories[i]) continue;
	if (/^(\s*(\[\S+]\s*)?[#*]*\s*)([a-z])/.test(stories[i])) {
		stories[i] = RegExp["$1"] + RegExp["$3"].toUpperCase() + RegExp["$'"];
	}
	if (/(\w)(\s*[#*]*\s*)$/.test(stories[i])) {
		stories[i] = RegExp["$`"] + RegExp["$1"] + "." + RegExp["$2"];
	}
	stories[i] = stories[i].replace(/\*(.+?)\*/g, '<i>$1</i>')
		.replace(/\#(.+?)\#/g, '<b>$1</b>')
		.replace(new RegExp(keyword.addSlashes().replace(/\//g, '|'), 'gi'), '<b>$&</b>');
}
story = stories.join('<br>');

if (frames_array) {
	for (var i = 0; i < frames_array.length; i++) {
		story = story.replace(/\{\d+\}/, frames_array[i]);
	}
}

storyview.innerHTML = story;

