var browser = navigator.userAgent.toLowerCase();
var isEditable = document.getElementById && document.designMode && !isSafari && !isKonqueror;
var isIE = ((browser.indexOf( "msie" ) != -1) && (browser.indexOf( "opera" ) == -1) && (browser.indexOf( "webtv" ) == -1));
var isGecko = (browser.indexOf( "gecko" ) != -1);
var isSafari = (browser.indexOf( "safari" ) != -1);
var isKonqueror = (browser.indexOf( "konqueror" ) != -1);

var Preceptor = {};

Preceptor.widget = {};

/**
 * Classe principal do editor
 * 
 * @param {String} id 			Id do textarea que ira virar o editor
 * @param {Object} attributes	parametros de configuracao
 */
Preceptor.widget.Editor = function( id , attributes )
{
	this.width    = attributes.width       || "550";
	this.height   = attributes.height      || "350";
	var className = attributes.className   || "pre_editor";
	
	this.toolbar   = attributes.toolbar    || main;
	this.id        = id;
	
	var div = document.createElement( "DIV" );
	div.id = "pre_editor" + this.id;
	div.style.width 	= this.width + "px";
	div.style.height 	= this.height + "px";
	div.className = className;

	$( id ).parentNode.insertBefore( div , $( id ) );
	Toggle.display( id );
	
	this.init();
}

Preceptor.widget.Editor.prototype = {
	
	init: function(){
		this._createIframe();
		this._createToolbar();
	},
	
	_initIframe: function(){
		var css = document.getElementsByTagName( "LINK" );
		var mainContent = '<html><head>';
		var html = $( this.id ).value;
			    
	    for( var i = 0; i < css.length; i++ )
	    {
	    	if( css[i].rel == "stylesheet" && css[i].type == "text/css" )
	    		mainContent += '<link rel="stylesheet" type="text/css" href="' + css[i].href + '">';
	    }
	    
	    mainContent += '</head><body style="background: none; color: white;">' + html + '<br /></body></html>' ;
		
	    this._getDoc().designMode = "On";
	    this._getDoc().open();
	    this._getDoc().write( mainContent );
	    this._getDoc().close();
	    this._getWindow().focus();
	},
	
	_createIframe: function(){
		var iframe = document.createElement("IFRAME");
		iframe.id = "pre_editor_iframe" + this.id;
		iframe.border = '0';
		iframe.frameBorder = '0';
		iframe.style.padding = '5px';
		iframe.allowTransparency = 'true';
		iframe.width = this.width - 10 + "px";
		iframe.height = this.height - 10 + "px";
	//      iframe.src = 'javascript:false';
        
        $( "pre_editor" + this.id ).appendChild( iframe );
		this._initIframe();
	},
	
	_createToolbar: function(){
		new Preceptor.widget.Editor.Toolbar( this , { toolbar: this.toolbar } );
	},
	
	_getDoc: function(){
		return $( "pre_editor_iframe" + this.id ).contentWindow.document;
	},
	
	_getWindow: function(){
		return $( "pre_editor_iframe" + this.id ).contentWindow;
	},

	update: function() 
	{
	  	$( this.id ).value = this._getDoc().body.innerHTML;
	}
}

/**
 * Classe de criacao da toolbar do editor
 * @param {Object} objeto Preceptor.widget.Editor que o toolbar vai usar
 */
Preceptor.widget.Editor.Toolbar = function( editor , attributes )
{
	/**
	 * Preceptor.widget.Editor 
	 */
	var editor = editor;
	
	/* Tipos de configuracao de barra disponiveis */
	var simple 	 = [ "bold" , "italic" , "underline" , "createlink" ];
	var main 	 = [ "bold" , "italic" ,  "underline" , "createlink" , "unorderedlist" , "orderedlist" , "horizontalrule" ];
	var advanced = [ "bold" , "italic" , "underline" , "createlink" , "color" , "bgcolor" , "justifyleft" , "justifycenter" , "justifyright" , "justifyfull" , "unorderedlist" , "orderedlist" , "alignobject" ];
	
	var bold 			= function(){ _execCommand( "bold" ) }
	var italic 			= function(){ _execCommand( "italic" ) }
	var underline 		= function(){ _execCommand( "underline" ) }
	var justifyleft 	= function(){ _execCommand( "justifyleft" ) }
	var justifycenter 	= function(){ _execCommand( "justifycenter" ) }
	var justifyright 	= function(){ _execCommand( "justifyright" ) }
	var justifyfull 	= function(){ _execCommand( "justifyfull" ) }
	var unorderedlist 	= function(){ _execCommand( "InsertUnorderedList" ) }
	var orderedlist 	= function(){ _execCommand( "InsertOrderedList" ) }
	var horizontalrule  = function(){ _execCommand( "InsertHorizontalRule" )}
	
	var createlink = function( ev )
	{
		var url = prompt("Enter a URL:", "http://");
   		if ((url != null) && (url != "")) {
      		_execCommand.call( this , "createlink" , false , url );
   		}
	}
	
	this._createButton = function( command , className )
	{
		var button = document.createElement("A");
		var toolbar = $( 'pre_editor_toolbar' + editor.id );
		
		button.className = "pre_editor_button " + className;
		button.href      = "#" + command;
			
		Event.observe( button , 'click' , eval( command ) );
		toolbar.appendChild( button );
	}
	
	
	var _execCommand = function( command , flag , option )
	{
		var flag = flag || false;
		var option = option || "";

		try 
		{
			editor._getWindow().focus();
			editor._getDoc().execCommand( command , flag , option );
			editor._getWindow().focus();
			
		}catch(e){ alert( e.message ) }
	}
	
	var div = document.createElement( "DIV" );
	div.id = "pre_editor_toolbar" + editor.id;
	div.className = "pre_editor_toolbar";

	$( "pre_editor" + editor.id ).parentNode.insertBefore( div , $( "pre_editor" + editor.id ) );
	
	this.init( eval( attributes.toolbar ) , editor );
}

Preceptor.widget.Editor.Toolbar.prototype = {
	
	init: function( toolbar , editor ){
		for( var i = 0; i < toolbar.length; i++ )
			this._createButton( toolbar[i] , "pre_editor_" + toolbar[i] );

		var clear = document.createElement( "DIV" );
		clear.style.clear = 'both';
		$( 'pre_editor_toolbar' + editor.id ).appendChild( clear );
	}
}

