
/*
FILE CONCAT ADD FILE
PATH: /ft/resources/client/modules/glossary/TextEditor.js
*/
/* Requires Element */

function TextEditor_Class(oArgs) {

	this._sourceFrame = oArgs.sourceFrame;
	this._controlPanel = oArgs.controlPanel;
	
	if(!this._sourceFrame) {
		throw "No Iframe found.";
	}
	
	this._init();
}

TextEditor_Class.prototype._init = function() {
		
	this._editPane = this._getEditableDocument(this._sourceFrame);
	
	this._attachControlEvents();

}

TextEditor_Class.prototype._getEditableDocument = function(oIframe) {
	
	var oDocument, oFrame;
	
	oFrame = WSDOM.Element.get(oIframe);

	if(!oFrame) { throw "No Iframe found."; }
	
	oDocument = this._setEditableDocument(oFrame);
	
	if(!oDocument) {throw "The text editor does not appear to work with this browser."; }

	oDocument.designMode = "On";
	
	// IE needs to reassign the document element after designMode is set
	if(oFrame.contentWindow.document) {
		oDocument = this._setEditableDocument(oFrame);
	}
	
	return oDocument;

}

TextEditor_Class.prototype._setEditableDocument = function(oFrame) {
	
	return oFrame.contentDocument || oFrame.contentWindow.document || null;
	
}

TextEditor_Class.prototype._attachControlEvents = function() {

	var rControls = WSDOM.Element.parseSelector(".tecontrol", this._controlPanel);
	
	WSDOM.Events.add({
		element:rControls
		,type:"click"
		,handler:this._handleControlAction
		,context:this
	});

}

TextEditor_Class.prototype._handleControlAction = function(oEvent, oEl, oData) {

	WSDOM.Events.cancel(oEvent);

	var sCommandType = oEl.getAttribute("tecommand");
	var sCustomCommand = oEl.getAttribute("customCommand")
	var sClassLink = WSDOM.Element.hasClass(oEl, "link")
		
	if(sClassLink){
		this._showLinkEditor();
	}else if(sCommandType) {
		this._invokeCommand(sCommandType);
	}else if(sCustomCommand){
		this._customCommand(sCustomCommand);
	}

	
}

TextEditor_Class.prototype._customCommand = function(sCustomCommand){
	var txt = '';
	var defFrame = Element.get("editor");
	//firefox    
	if (window.getSelection){
        txt = window.frames[0].getSelection();
     }	
	else if (window.frames[0].document.getSelection){
       txt = window.frames[0].document.getSelection();
     }
	//IE 6, IE 7, IE 8
    else if (window.frames[0].document.selection){
		txtObj = window.frames[0].document.selection.createRange();
		txt = window.frames[0].document.selection.createRange().text;
		
     }
	 
    else return;
	
	if(txt != ""){
		this.txt = txt;
		switch(sCustomCommand)
		{
			case "ref":
				this._addRefURL()
				break;
			case "flash":
				this._addFlashTag()
				break;
            case "movie":
                this._addMovieTag()
                break;
			default:
				break;
		}
	}
		
}

TextEditor_Class.prototype._showRefEditor = function(){
	var value = prompt("Please enter a URL to Reference", "")
	this._addRefURL(value);
}

TextEditor_Class.prototype._addRefURL = function(value) {
    //Remove Link Attribute (For IE)
    this._editPane.execCommand("Unlink", false, null);
    
	var defFrame = Element.get("editor");
	var url = value || "";
	var contentFrame = defFrame.contentWindow.document.body
	var fullCopy = contentFrame.innerHTML

	var text = this.txt+'';
		 text = text.replace(/&/gi, '&amp;').replace(/\</gi, '&lt;').replace(/\>/gi, '&gt;');

	var splitText = fullCopy.split(text),
		newText = [];
	
	if (splitText.length > 1) {
		for (var i = 0; i < splitText.length; i++) {
			if (i > 0) {
				newText.push('[ref url="', url, '"]', text, '[/ref]', splitText[i]);
			} else {
				newText.push(splitText[i]);
			}
		}
	}
	
	contentFrame.innerHTML = newText.join('');	

}

TextEditor_Class.prototype._addFlashTag = function() {
    //Remove Link Attribute (For IE)
    this._editPane.execCommand("Unlink", false, null);
    
	var defFrame = Element.get("editor");
	var contentFrame = defFrame.contentWindow.document.body
	var fullCopy = contentFrame.innerHTML
	var text = this.txt+'';
		text = text.replace(/&/gi, '&amp;').replace(/\</gi, '&lt;').replace(/\>/gi, '&gt;');

	var splitText = fullCopy.split(text),
		newText = [];
	
	if (splitText.length > 1) {
		for (var i = 0; i < splitText.length; i++) {
			if (i > 0) {
				newText.push('[flash data="',text,'" height="500px" width="500px"][/flash]', splitText[i]);
			} else {
				newText.push(splitText[i]);
			}
		}
	}
	
	contentFrame.innerHTML = newText.join('');

}

TextEditor_Class.prototype._addMovieTag = function() {
    this._editPane.execCommand("Unlink", false, null);

    var defFrame = Element.get("editor");
    var contentFrame = defFrame.contentWindow.document.body
    var fullCopy = contentFrame.innerHTML
    var text = this.txt + '';
        text = text.replace(/&/gi, '&amp;').replace(/\</gi, '&lt;').replace(/\>/gi, '&gt;');

    var splitText = fullCopy.split(text),
    newText = [];

    if (splitText.length > 1) {
        for (var i = 0; i < splitText.length; i++) {
            if (i > 0) {
                newText.push('[movie data="', text, '" height="412px" width="486px"][/movie]', splitText[i]);
            } else {
                newText.push(splitText[i]);
            }
        }
    }

    contentFrame.innerHTML = newText.join('');
}

// TextEditor_Class.prototype._hideRefEditor = function(){

	// WSDOM.Element.addClass(this.oRefEditor, "displayNone")
	// this.oRefURL.value = "";
	
// }

TextEditor_Class.prototype._showLinkEditor = function(){
	var value = prompt("Please enter a URL", "")

	if(value){
		this._invokeCommand("CreateLink", value)
	}
}

TextEditor_Class.prototype._invokeCommand = function(sCommandName, sParam) {

		this._editPane.execCommand(sCommandName, false, sParam);

}

/* Public Methods */

TextEditor_Class.prototype.setCSSMode = function(bUseCss) {

	var bUseCss = bUseCss || false;
	this._editPane.execCommand("useCSS", false, bUseCss);
	
}





/*
FILE CONCAT ADD FILE
PATH: /ft/resources/client/events.draggable.js
*/
EventManager.prototype.draggable = function(oArgs) {
            
	var ret = {};
	var self = this;
	
	var newUp = function() {
		self.remove(self.dragEvent);
		self.remove(self.upEvent);

		self.dragEvent = null;
		self.upEvent = null;
		oArgs.up.apply(this, arguments);
	}
	
	var newMove = function () {
		
		oArgs.move.apply(oArgs.context, arguments);

		if(!self.upEvent) {
			self.upEvent = self.add({
				element:arguments[1]
				,type:"mouseup"
				,handler:newUp
				,context:oArgs.context
			});
		}
	}

	var move = function() {
		if(!self.dragEvent){
			self.dragEvent = self.add({
				element:document
				,type:"mousemove"
				,handler:newMove
				,context:oArgs.context
			});
		}
	}

	var newDown = function() {
		oArgs.down.apply(this,  arguments);
		move();
	}
	
	var down = this.delegate(oArgs.parent, "mousedown", {

		selector: oArgs.selector
		,handler : newDown
		,context : oArgs.context
	});
	
	ret.down = down;

	if(oArgs.over) {
				
		var over = this.delegate(oArgs.parent, "mouseover", {
		
			 selector: oArgs.selector
			,handler : oArgs.over
			,context : oArgs.context
			,dynamicEvent : oArgs.dynamicEvent
		});
		
		ret.over = over;
	}

	return  ret;
}
