window.jmlWYSIWYGdata = {
	jq      : ( typeof jQuery != "undefined" ),
	path    : "http://www.underabridge.com/components/com_jmylife/libraries/wysiwyg/",
	links   : 1,
	images  : 1,
	list    : 1,
	color   : 1,
	strings : {
		enterimageurl : "Enter Image URL:",
		enterlinkurl  : "Enter a URL:",
		enterlinktext : "Link Text:",
		noimage       : "No image added!",
		nourl         : "No URL added!",
		noselect      : "Please select some text first!",
		modalok       : "OK"
	}
};

/*
WYSIWYG-BBCODE v1.3
Copyright (c) 2009, Jitbit Sotware, http://blog.jitbit.com/2009/08/wysiwyg-bbcode-editor.html
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY Jitbit Software ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Jitbit Software BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

This release has been unpacked/modified by Jeff Channell for release with JMyLife

modalPrompt "prompt()" replacement written by Jeff Channell,
 and relies on Joomla! MooTools and modal behaviors
*/
var myeditor, ifm;
var body_id, textboxelement;
var content;
var isIE = /msie|MSIE/.test(navigator.userAgent);
var isChrome = /Chrome/.test(navigator.userAgent);
var isSafari = /Safari/.test(navigator.userAgent) && !isChrome;
var browser = isIE || window.opera;
var textRange;
var enter = 0;
var editorVisible = true;

function rep(a, b) {
	content = content.replace(a, b)
}
function initEditor(a) {
	body_id = a;
	textboxelement = document.getElementById(body_id);
	ifm = document.createElement("iframe");
	ifm.setAttribute("id", "rte");
	ifm.setAttribute("border", "0");
	ifm.setAttribute("frameborder", "0");
	ifm.style.width = textboxelement.style.width;
	ifm.style.height = textboxelement.style.height;
	textboxelement.parentNode.appendChild(ifm);
	textboxelement.style.display = 'none';
	textboxelement.className = "editorBBCODE";
	textboxelement.setAttribute('class', 'editorBBCODE');
	if (ifm) {
		ShowEditor();
	} else setTimeout('ShowEditor()', 100);
}
function getStyle(a, b) {
	var x = document.getElementById(a);
	if (x.currentStyle) var y = x.currentStyle[b];
	else if (window.getComputedStyle) var y = document.defaultView.getComputedStyle(x, null).getPropertyValue(b);
	return y;
}
function ShowEditor() {
	content = document.getElementById(body_id).value;
	myeditor = ifm.contentWindow.document;
	bbcode2html();
	myeditor.designMode = "on";
	myeditor.open();
	myeditor.write('<html><head><link href="editor.css" rel="Stylesheet" type="text/css" /></head>');
	myeditor.write('<body style="margin:0px 0px 0px 0px" class="editorWYSIWYG">');
	myeditor.write(content);
	myeditor.write('</body></html>');
	myeditor.close();
	if( window.jmlWYSIWYGdata.jq )
	{
		jQuery('button.htmlswitch').css({'background-image': 'url('+window.jmlWYSIWYGdata.path+'images/icon_html.gif)'});
	}
	else
	{
		$$('button.htmlswitch').each(function(el)
		{
			el.setStyles({'background-image': 'url('+window.jmlWYSIWYGdata.path+'images/icon_html.gif)'});
		});
	}
}
function SwitchEditor() {
	if (editorVisible) {
		doCheck();
		ifm.style.display = 'none';
		textboxelement.style.display = '';
		editorVisible = false;
		if( window.jmlWYSIWYGdata.jq )
		{
			jQuery('button.htmlswitch').css({'background-image': 'url('+window.jmlWYSIWYGdata.path+'images/icon_nohtml.gif)'});
		}
		else
		{
			$$('button.htmlswitch').each(function(el)
			{
				el.setStyles({'background-image': 'url('+window.jmlWYSIWYGdata.path+'images/icon_nohtml.gif)'});
			});
		}
	} else {
		ifm.style.display = '';
		textboxelement.style.display = 'none';
		ShowEditor();
		editorVisible = true;
	}
}
function html2bbcode() {
	rep(/<img\s[^<>]*?src=\"?([^<>]*?)\"?(\s[^<>]*)?\/?>/gi, "[img]$1[/img]");
	rep(/<\/(strong|b)>/gi, "[/b]");
	rep(/<(strong|b)(\s[^<>]*)?>/gi, "[b]");
	rep(/<\/(em|i)>/gi, "[/i]");
	rep(/<(em|i)(\s[^<>]*)?>/gi, "[i]");
	rep(/<\/u>/gi, "[/u]");
	rep(/\n/gi, "");
	rep(/\r/gi, "");
	rep(/<u(\s[^<>]*)?>/gi, "[u]");
	rep(/<br(\s[^<>]*)?>/gi, "\n");
	rep(/<p(\s[^<>]*)?>/gi, "");
	rep(/<\/p>/gi, "\n");
	rep(/<ul>/gi, "[ul]");
	rep(/<\/ul>/gi, "[/ul]");
	rep(/<li>/gi, "[li]");
	rep(/<\/li>/gi, "[/li]");
	rep(/<div([^<>]*)>/gi, "\n<span$1>");
	rep(/<\/div>/gi, "</span>\n");
	rep(/&nbsp;/gi, " ");
	rep(/&quot;/gi, "\"");
	rep(/&amp;/gi, "&");
	var a, sc2;
	do {
		a = content;
		rep(/<font\s[^<>]*?color=\"?([^<>]*?)\"?(\s[^<>]*)?>([^<>]*?)<\/font>/gi, "[color=$1]$3[/color]");
		if (a == content) rep(/<font[^<>]*>([^<>]*?)<\/font>/gi, "$1");
		rep(/<a\s[^<>]*?href=\"?([^<>]*?)\"?(\s[^<>]*)?>([^<>]*?)<\/a>/gi, "[url=$1]$3[/url]");
		sc2 = content;
		rep(/<(span|blockquote|pre)\s[^<>]*?style=\"?font-weight: ?bold;?\"?\s*([^<]*?)<\/\1>/gi, "[b]<$1 style=$2</$1>[/b]");
		rep(/<(span|blockquote|pre)\s[^<>]*?style=\"?font-weight: ?normal;?\"?\s*([^<]*?)<\/\1>/gi, "<$1 style=$2</$1>");
		rep(/<(span|blockquote|pre)\s[^<>]*?style=\"?font-style: ?italic;?\"?\s*([^<]*?)<\/\1>/gi, "[i]<$1 style=$2</$1>[/i]");
		rep(/<(span|blockquote|pre)\s[^<>]*?style=\"?font-style: ?normal;?\"?\s*([^<]*?)<\/\1>/gi, "<$1 style=$2</$1>");
		rep(/<(span|blockquote|pre)\s[^<>]*?style=\"?text-decoration: ?underline;?\"?\s*([^<]*?)<\/\1>/gi, "[u]<$1 style=$2</$1>[/u]");
		rep(/<(span|blockquote|pre)\s[^<>]*?style=\"?text-decoration: ?none;?\"?\s*([^<]*?)<\/\1>/gi, "<$1 style=$2</$1>");
		rep(/<(span|blockquote|pre)\s[^<>]*?style=\"?color: ?([^<>]*?);\"?\s*([^<]*?)<\/\1>/gi, "[color=$2]<$1 style=$3</$1>[/color]");
		rep(/<(blockquote|pre)\s[^<>]*?style=\"?\"? (class=|id=)([^<>]*)>([^<>]*?)<\/\1>/gi, "<$1 $2$3>$4</$1>");
		rep(/<span\s[^<>]*?style=\"?\"?>([^<>]*?)<\/span>/gi, "$1");
		if (sc2 == content) {
			rep(/<span[^<>]*>([^<>]*?)<\/span>/gi, "$1");
			sc2 = content;
			rep(/<pre\s[^<>]*?class=\"?code\"?[^<>]*?>([^<>]*?)<\/pre>/gi, "[code]$1[/code]");
			if (sc2 == content) {
				rep(/<blockquote\s[^<>]*?class=\"?memberquote\"?[^<>]*?id=\"?([^<>\"]*)\"?>([^<>]*?)<\/blockquote>/gi, "[quote$1]$2[/quote]");
				if (sc2 == content) {
					rep(/<blockquote\s[^<>]*?id=\"?([^<>\"]*?)\"? class=\"?memberquote\"?[^<>]*?>([^<>]*?)<\/blockquote>/gi, "[quote$1]$2[/quote]");
					if (sc2 == content) rep(/<blockquote\s[^<>]*?class=\"?memberquote\"?[^<>]*?>([^<>]*?)<\/blockquote>/gi, "[quote]$1[/quote]");
				}
			}
		}
	} while (a != content) rep(/<[^<>]*>/gi, "");
	rep(/&lt;/gi, "<");
	rep(/&gt;/gi, ">");
	do {
		a = content;
		rep(/\[(b|i|u)\]\[quote([^\]]*)\]([\s\S]*?)\[\/quote\]\[\/\1\]/gi, "[quote$2][$1]$3[/$1][/quote]");
		rep(/\[color=([^\]]*)\]\[quote([^\]]*)\]([\s\S]*?)\[\/quote\]\[\/color\]/gi, "[quote$2][color=$1]$3[/color][/quote]");
		rep(/\[(b|i|u)\]\[code\]([\s\S]*?)\[\/code\]\[\/\1\]/gi, "[code][$1]$2[/$1][/code]");
		rep(/\[color=([^\]]*)\]\[code\]([\s\S]*?)\[\/code\]\[\/color\]/gi, "[code][color=$1]$2[/color][/code]");
	} while (a != content) do {
		a = content;
		rep(/\[b\]\[\/b\]/gi, "");
		rep(/\[i\]\[\/i\]/gi, "");
		rep(/\[u\]\[\/u\]/gi, "");
		rep(/\[quote[^\]]*\]\[\/quote\]/gi, "");
		rep(/\[code\]\[\/code\]/gi, "");
		rep(/\[url=([^\]]+)\]\[\/url\]/gi, "");
		rep(/\[img\]\[\/img\]/gi, "");
		rep(/\[color=([^\]]*)\]\[\/color\]/gi, "");
	} while (a != content)
}
function bbcode2html() {
	rep(/--- Last Edited by [\s\S]*? ---/gi, "");
	rep(/\n/gi, "<br />");
	rep(/\[ul\]/gi, window.jmlWYSIWYGdata.list==1?"<ul>":"");
	rep(/\[\/ul\]/gi, window.jmlWYSIWYGdata.list==1?"</ul>":"");
	rep(/\[li\]/gi, window.jmlWYSIWYGdata.list==1?"<li>":"");
	rep(/\[\/li\]/gi, window.jmlWYSIWYGdata.list==1?"</li>":"");
	if (browser) {
		rep(/\[b\]/gi, "<strong>");
		rep(/\[\/b\]/gi, "</strong>");
		rep(/\[i\]/gi, "<em>");
		rep(/\[\/i\]/gi, "</em>");
		rep(/\[u\]/gi, "<u>");
		rep(/\[\/u\]/gi, "</u>");
	} else {
		rep(/\[b\]/gi, "<span style=\"font-weight: bold;\">");
		rep(/\[i\]/gi, "<span style=\"font-style: italic;\">");
		rep(/\[u\]/gi, "<span style=\"text-decoration: underline;\">");
		rep(/\[\/(b|i|u)\]/gi, "</span>");
	}
	rep(/\[img\]([^\"]*?)\[\/img\]/gi, window.jmlWYSIWYGdata.images==1?"<img src=\"$1\" />":"$1");
	var a;
	do {
		a = content;
		rep(/\[url=([^\]]+)\]([\s\S]*?)\[\/url\]/gi, window.jmlWYSIWYGdata.links==1?"<a href=\"$1\">$2</a>":"$2");
		rep(/\[url\]([\s\S]*?)\[\/url\]/gi, window.jmlWYSIWYGdata.links==1?"<a href=\"$1\">$1</a>":"$1");
		if (browser) {
			rep(/\[color=([^\]]*?)\]([\s\S]*?)\[\/color\]/gi, window.jmlWYSIWYGdata.color==1?"<font color=\"$1\">$2</font>":"$2");
		} else {
			rep(/\[color=([^\]]*?)\]([\s\S]*?)\[\/color\]/gi, window.jmlWYSIWYGdata.color==1?"<span style=\"color: $1;\">$2</span>":"$2");
		}
		rep(/\[code\]([\s\S]*?)\[\/code\]/gi, "<pre>$1</pre>&nbsp;");
	} while (a != content)
}
function doCheck() {
	if (!editorVisible) {
		ShowEditor();
	}
	content = myeditor.body.innerHTML;
	html2bbcode();
	document.getElementById(body_id).value = content;
}
function doQuote() {
	if (editorVisible) {
		ifm.contentWindow.focus();
		if (isIE) {
			textRange = ifm.contentWindow.document.selection.createRange();
			var a = "[quote=]" + textRange.text + "[/quote]";
			textRange.text = a;
		} else {
			var b = ifm.contentWindow.getSelection().getRangeAt(0);
			var c = b.toString();
			b.deleteContents();
			b.insertNode(document.createTextNode("[quote=]" + c + "[/quote]"));
		}
	} else {
		AddTag('[quote=]', '[/quote]');
	}
}
function InsertSmile(a) {
	InsertText(a);
	document.getElementById('divSmilies').style.display = 'none';
}
function InsertYoutube() {
	InsertText("http://www.youtube.com/watch?v=XXXXXXXXXXX");
}
function InsertText(a) {
	if (editorVisible) insertHtml(a);
	else textboxelement.value += a;
}
function doClick(a) {
	if (editorVisible) {
		ifm.contentWindow.focus();
		myeditor.execCommand(a, false, null);
	} else {
		switch (a) {
		case 'bold':
			AddTag('[b]', '[/b]');
			break;
		case 'italic':
			AddTag('[i]', '[/i]');
			break;
		case 'underline':
			AddTag('[u]', '[/u]');
			break;
		case 'InsertUnorderedList':
			if( window.jmlWYSIWYGdata.list==1 )
			{
				AddTag('[ul][li]', '[/li][/ul]');
			}
			break
		}
	}
}
function doColor(a) {
	ifm.contentWindow.focus();
	if( window.jmlWYSIWYGdata.color==0 ){ return; }
	if( editorVisible )
	{
		if (isIE) {
			textRange = ifm.contentWindow.document.selection.createRange();
			textRange.select();
		}
		myeditor.execCommand('forecolor', false, a);
	}
	else
	{
		AddTag('[color=' + a + ']', '[/color]');
	}
}
function doLink() {
	var callback, pArray, sText = '';
	if (editorVisible) {
		ifm.contentWindow.focus();
		if( isIE ) {
			var e = ifm.contentWindow.document.selection.createRange();
			sText = e.text;
		} else {
			sText = ifm.contentWindow.document.getSelection();
		}
		if( sText.toString() == "" )
		{
			alert( window.jmlWYSIWYGdata.strings.noselect );
			return;
		}
		callback = function( data )
		{
			if( data.url != null && data.url != "" && data.url != "http://" ) {
				if( isIE ){ e.select(); }
				myeditor.execCommand("CreateLink", false, data.url);
				return true;
			} else {
				alert( window.jmlWYSIWYGdata.strings.nourl );
				return false;
			}
		};
		pArray = [{
			label : window.jmlWYSIWYGdata.strings.enterlinkurl,
			name  : "url",
			txt   : "http://"
		}];
	} else {
		if (isIE) {
			if (document.selection) {
				textboxelement.focus();
				var e = document.selection.createRange();
				sText = e.text;
			}
		} else {
			var f = textboxelement.selectionStart;
			var g = textboxelement.selectionEnd;
			sText = textboxelement.value.slice( f, g );
		}
		callback = function( data )
		{
			if( data.url != null && data.url != "" && data.url != "http://" ) {
				if( isIE ){ e.select(); }
				AddTag('[url='+data.url+']'+data.txt, '[/url]', true);
				return true;
			} else {
				alert( window.jmlWYSIWYGdata.strings.nourl );
				return false;
			}
		};
		pArray = [{
			label : window.jmlWYSIWYGdata.strings.enterlinkurl,
			name  : "url",
			txt   : "http://"
		},{
			label : window.jmlWYSIWYGdata.strings.enterlinktext,
			name  : "txt",
			txt   : sText
		}];
	}
	modalPrompt(pArray, callback );
}
function doImage() {
	if( window.jmlWYSIWYGdata.images==0 ){ return; }
	var callback, sText = "";
	if (editorVisible) {
		ifm.contentWindow.focus();
		if( isIE ) {
			var e = ifm.contentWindow.document.selection.createRange();
			sText = e.text;
		} else {
			sText = ifm.contentWindow.document.getSelection();
		}
	} else {
		if (isIE) {
			if (document.selection) {
				textboxelement.focus();
				var e = document.selection.createRange();
				sText = e.text;
			}
		} else {
			var f = textboxelement.selectionStart;
			var g = textboxelement.selectionEnd;
			sText = textboxelement.value.slice( f, g );
		}
	}
	if( sText.toString() == "" ) {
		sText = "http://";
	}
	callback = function( data )
	{
		if( data.url != null && data.url != "" && data.url != "http://" ) {
			if( editorVisible ) {
				if( isIE ){ e.select(); }
				myeditor.execCommand("InsertImage", false, data.url);
			} else {
				if( isIE ){ e.select(); }
				AddTag('[img]'+data.url, '[/img]', true);
			}
			return true;
		} else {
			alert( window.jmlWYSIWYGdata.strings.noimage );
			return false;
		}
	};
	modalPrompt([
		{
			label : window.jmlWYSIWYGdata.strings.enterimageurl,
			name  : "url",
			txt   : sText
		}
	], callback );
}
function insertHtml(a) {
	ifm.contentWindow.focus();
	if (isIE) ifm.contentWindow.document.selection.createRange().pasteHTML(a);
	else myeditor.execCommand('inserthtml', false, a);
}
function MozillaInsertText(a, b, c) {
	a.value = a.value.slice(0, c) + b + a.value.slice(c);
}
function AddTag(a, b, replace) {
	var c = textboxelement;
	if (isIE) {
		if (document.selection) {
			c.focus();
			var d = c.value;
			var e = document.selection.createRange();
			if (e.text == "" || replace == true) {
				e.text = a + b;
			} else if (d.indexOf(e.text) >= 0) {
				e.text = a + e.text + b;
			} else {
				c.value = d + a + b;
			}
			e.select();
		}
	} else {
		var f = c.selectionStart;
		var g = c.selectionEnd;
		if( replace == true )
		{
			c.value = c.value.slice(0, f) + c.value.slice(g);
			MozillaInsertText(c, a + b, f);
		}
		else
		{
			MozillaInsertText(c, a, f);
			MozillaInsertText(c, b, g + a.length);
		}
		c.selectionStart = f;
		c.selectionEnd = ( replace == true ? f : g ) + a.length + b.length;
		c.focus();
	}
}

/* added specifically for JMyLife */
function modalPrompt( fields, callback )
{
	if( window.jmlWYSIWYGdata.jq )
	{
		modalPromptJQuery( fields, callback )
	}
	else
	{
		modalPromptMootools( fields, callback )
	}
}
function modalPromptJQuery( fields, callback )
{
	var promptBox = jQuery('<div id="modalPromptBox"></div>').css({ 'padding': '8px' });
	var promptField, promptLabel, promptInput, i = 0;
	for( ; i < fields.length; i++ )
	{
		promptField = jQuery('<div></div>').appendTo( promptBox );
		promptLabel = jQuery('<div></div>').css({
			'font-weight' : 'bold',
			'font-size'   : '14px',
			'line-height' : '18px'
		}).text( fields[i].label.toString() ).appendTo( promptField );
		promptInput = jQuery('<input type="text" name="field['+fields[i].name+']" />').val( fields[i].txt ).appendTo( promptField );
	}
	var promptSubmit = jQuery('<input type="button" value="'+window.jmlWYSIWYGdata.strings.modalok+'" />').addClass( 'modalPromptOk' ).appendTo( promptBox );
	var promptSubmitCallback = function( e )
	{
		jQuery('.modalPromptOk').unbind('click').click(function()
		{
			var $return = {};
			jQuery(this).parent().find('input[name^=field]').each( function()
			{
				$return[jQuery(this).attr('name').replace( /field\[(.*)\]/, '$1' )] = jQuery(this).val();
			});
			if( callback( $return ) )
			{
				jQuery.nyroModalRemove();
			}
		});
	};
	jQuery(function()
	{
		jQuery.fn.nyroModal.settings.processHandler = function(settings)
		{
			var h = parseInt( ( fields.length + 1 ) * 45 );
			jQuery.nyroModalSettings({
				minHeight  : h,
				minWidth   : 250,
				height     : h,
				width      : 250,
				resizable  : false,
				autoSizable: false,
				showBackground: function( elts, settings, callback )
				{
					elts.bg.css({opacity:0}).fadeTo(1, 0.01, callback);
				},
				endShowContent: function(elts) {
					promptSubmitCallback();
				}
			});
		};
	});
	jQuery.fn.nyroModalManual({
		content    : promptBox
	});
}
function modalPromptMootools( fields, callback )
{
	var promptBox = new Element( 'div', { 'id': 'modalPromptBox' } ).setStyles({ 'padding': '8px' });
	var promptField, promptLabel, promptInput, i = 0;
	for( ; i < fields.length; i++ )
	{
		promptField = new Element( 'div' ).injectInside( promptBox );
		promptLabel = new Element( 'div' ).setStyles({
			'font-weight' : 'bold',
			'font-size'   : '14px',
			'line-height' : '18px'
		}).setText( fields[i].label.toString() ).injectInside( promptField );
		promptInput = new Element( 'input', {
			type: 'text',
			name: 'field['+fields[i].name+']',
			value: fields[i].txt
		}).injectInside( promptField );
	}
	var promptSubmit = new Element( 'input', {
		type: 'button',
		value: window.jmlWYSIWYGdata.strings.modalok
	}).addClass( 'modalPromptOk' ).injectInside( promptBox );
	var promptSubmitCallback = function( e )
	{
		$$('.modalPromptOk').removeEvents( 'click' ).addEvent( 'click', function()
		{
			var $return = {};
			var $name = '';
			var elems = this.getParent().getElements('input[name^=field]');
			if( elems )
			{
				elems.each( function( el )
				{
					$name = el.getAttribute( 'name' ).replace( /field\[(.*)\]/, '$1' );
					$return[$name] = el.getValue();
				});
			}
			if( callback( $return ) )
			{
				SqueezeBox.close();
			}
		});
	};
	var oldOpts = SqueezeBox.options;
	SqueezeBox.initialize();
	SqueezeBox.options = null;
	SqueezeBox.initialize({
		closeWithOverlay: false,
		overlayOpacity: 0.01,
		onOpen: promptSubmitCallback
	});
	SqueezeBox.presets.size.x = 250;
	SqueezeBox.presets.size.y = parseInt( ( fields.length + 1 ) * 45 );
	SqueezeBox.presets.closeWithOverlay = false;
	SqueezeBox.presets.onOpen = promptSubmitCallback;
	SqueezeBox.setContent( 'adopt', promptBox );
	SqueezeBox.options = oldOpts;
}
