/* 
Author: de77.com
Licence: MIT
*/
function action(e,a,p)
{
	/*
	$(e).parents().each(function(){
		var obj = $(this);
		var classes = obj.attr('class').split(' ');
		if ($.inArray('de77_wysiwyg', classes) > -1)
		{
			obj.find('.de77_editor').focus();
		}		
	});
	 */
	if (p == null) p = false;
	document.execCommand(a,null,p);
}

function makeToolbar(el)
{
	toolbar = $.ajax({url:'/res/wysiwyg/jquery.wysiwyg.htm',async:false}).responseText;
	el.parent().prepend(toolbar);

}

(function($){
 $.fn.de77_wysiwyg = function(options) {
 	if (options == false)
 	{
		return this.each(function() {
			var obj = $(this);
			obj.parent().find('.de77_toolbar').detach();
			obj.unwrap();
			obj.removeClass('de77_editor');
			this.contentEditable="false";			
		});				
	}
	else
	{
		return this.each(function() {
			if(typeof(options) != 'undefined'){
				var obj = $("#edit_window_content");
				obj.html($(this).html());
				edit_window(options.type, options.id, options.field);
			}
			else
				var obj = $(this);
			
			var classes = obj.attr('class').split(' ');
			if ($.inArray('de77_editor', classes) > -1)
			{
				return false;
			}
			obj.addClass('de77_editor');
			obj.wrap('<div class="de77_wysiwyg"></div>');		
			if(typeof(options) != 'undefined')
				document.getElementById('edit_window_content').contentEditable="true";
			else
				this.contentEditable="true";		
			
			makeToolbar(obj);
		});	
	}
 };
})(jQuery);
