// BackDrop(TM) WYSIWYG Control
// Copyright Fall Web Concepts 2008
	
	var Control_Editor_Array = [];
	
	function CtrlEditor() {
		this.config = {instancename: "", gallerypanel: "", id: 0};
		this.panels = {};
		this.galleryRef;
		this.currentSelection;
		this.doc = {};
		this.doc.head = "";
	}
	CtrlEditor_fixHTML = function(superclass){

	}
	
	CtrlEditor_loadGallery = function() {
		var superclass = this;
		this.panels.galleryPanel = new YAHOO.widget.Panel(
			this.config.gallerypanel, 
			{visible: false, draggable: true, constraintoviewport: true, zIndex: 0, fixedcenter: true}
			);
		this.panels.galleryPanel.render(document.body);
		
		var config = {instancename: "editorGallery1", parentref: this.panels.galleryPanel, skeletonId: "Application_Gallery_Skeleton"};
		this.galleryRef = BackDrop.dialog.request(config);
		this.galleryRef.subscribe("imageDblClick", function(imgRef) {
			superclass.Gallery_Insert_Image(imgRef.id);
		});
		this.galleryRef.init();
		this.panels.galleryPanel.setBody(this.galleryRef.node.innerHTML);
		
	}

	CtrlEditor.prototype.saveHTMLCallback = {
		success: function(o) {
			var good = "File Saved Successfully<br><br><img src='../Libraries/Assets/Check_64.png' border=0>";
			var bad = "There was a problem saving your file.<br><br><img src='../Libraries/Assets/fileclose-64.png' border=0>";
			var closeBtn = "<br><br><input type='button' value='Ok' onclick='JavaScript: sDialog.hide();'>";

			try {
				eval("var status = " + o.responseText + ";");
				if(status.error == false) {
					sDialog.setBody("<div style='text-align:center;'>" + good + closeBtn + "</div>");
				} else {
					sDialog.setBody("<div style='text-align:center;'>" + bad + "<br>" + status.msg + closeBtn + "</div>");
				}
			} catch(e) {
				sDialog.hide();
				var ePanel = new YAHOO.widget.Panel("ePanel", {fixedcenter:true, visible: true});
				ePanel.setBody("<div style='width: 400px; height:400px; overflow:auto; background-color: white;'>" + o.responseText + "</div>");
				ePanel.render(document.body);
			}
		},
		failure: function(o) {alert("Could not save HTML\r\n" + o.responseText);}
	};

	CtrlEditor.prototype.init = function(config) {
		Control_Editor_Array[Control_Editor_Array.length] = this;
		this.config.instancename = config.instancename;
		this.config.gallerypanel = config.gallerypanel;
		this.config.id = config.id;
		/*{
				group: 'fileOptions',
				label: 'Template',
				buttons: [ {
					type: 'push',
					label: 'Save Template',
					value: 'saveFile'
				}]
				},

				{
				type: 'separator'
			},
		*/
		this.config.toolbar = {
			collapse: true,

			titlebar: '',
			draggable: false,
			buttonType: 'advanced',
			buttons: [
			{
				group: 'fontstyle',
				label: 'Font Name and Size',
				buttons: [
				{
					type: 'select',
					label: 'Arial',
					value: 'fontname',
					disabled: true,
					menu: [
					{
						text: 'Arial',
						checked: true
					},

					{
						text: 'Arial Black'
					},

					{
						text: 'Comic Sans MS'
					},

					{
						text: 'Courier New'
					},

					{
						text: 'Lucida Console'
					},

					{
						text: 'Tahoma'
					},

					{
						text: 'Times New Roman'
					},

					{
						text: 'Trebuchet MS'
					},

					{
						text: 'Verdana'
					}
					]
				},
				{
					type: 'spin',
					label: '13',
					value: 'fontsize',
					range: [ 9, 75 ],
					disabled: true
				}
				]
			},
			{
				type: 'separator'
			},

			{
				group: 'textstyle',
				label: 'Font Style',
				buttons: [
				{
					type: 'push',
					label: 'Bold CTRL + SHIFT + B',
					value: 'bold'
				},

				{
					type: 'push',
					label: 'Italic CTRL + SHIFT + I',
					value: 'italic'
				},

				{
					type: 'push',
					label: 'Underline CTRL + SHIFT + U',
					value: 'underline'
				},

				{
					type: 'separator'
				},

				{
					type: 'push',
					label: 'Subscript',
					value: 'subscript',
					disabled: true
				},

				{
					type: 'push',
					label: 'Superscript',
					value: 'superscript',
					disabled: true
				},

				{
					type: 'separator'
				},

				{
					type: 'color',
					label: 'Font Color',
					value: 'forecolor',
					disabled: true
				},

				{
					type: 'color',
					label: 'Background Color',
					value: 'backcolor',
					disabled: true
				},

				{
					type: 'separator'
				},

				{
					type: 'push',
					label: 'Remove Formatting',
					value: 'removeformat',
					disabled: true
				},

				{
					type: 'push',
					label: 'Show/Hide Hidden Elements',
					value: 'hiddenelements'
				}
				]
			},
			{
				type: 'separator'
			},

			{
				group: 'alignment',
				label: 'Alignment',
				buttons: [
				{
					type: 'push',
					label: 'Align Left CTRL + SHIFT + [',
					value: 'justifyleft'
				},

				{
					type: 'push',
					label: 'Align Center CTRL + SHIFT + |',
					value: 'justifycenter'
				},

				{
					type: 'push',
					label: 'Align Right CTRL + SHIFT + ]',
					value: 'justifyright'
				},

				{
					type: 'push',
					label: 'Justify',
					value: 'justifyfull'
				}
				]
			},
			{
				type: 'separator'
			},

			{
				group: 'parastyle',
				label: 'Paragraph Style',
				buttons: [
				{
					type: 'select',
					label: 'Normal',
					value: 'heading',
					disabled: true,
					menu: [
					{
						text: 'Normal',
						value: 'none',
						checked: true
					},

					{
						text: 'Header 1',
						value: 'h1'
					},

					{
						text: 'Header 2',
						value: 'h2'
					},

					{
						text: 'Header 3',
						value: 'h3'
					},

					{
						text: 'Header 4',
						value: 'h4'
					},

					{
						text: 'Header 5',
						value: 'h5'
					},

					{
						text: 'Header 6',
						value: 'h6'
					}
					]
				}
				]
			},
			{
				type: 'separator'
			},

			{
				group: 'indentlist',
				label: 'List',
				buttons: [
				{
					type: 'push',
					label: 'Indent',
					value: 'indent',
					disabled: true
				},

				{
					type: 'push',
					label: 'Outdent',
					value: 'outdent',
					disabled: true
				},

				{
					type: 'push',
					label: 'Create an Unordered List',
					value: 'insertunorderedlist'
				},

				{
					type: 'push',
					label: 'Create an Ordered List',
					value: 'insertorderedlist'
				}
				]
			},
			{
				type: 'separator'
			},

			{
				group: 'insertitem',
				label: 'Insert Item',
				buttons: [
				{
					type: 'push',
					label: 'Auto Link',
					value: 'autolink',
					disabled: true
				},

				{
					type: 'push',
					label: 'HTML Link CTRL + SHIFT + L',
					value: 'createlink',
					disabled: true
				},

				{
					type: 'push',
					label: 'Insert Image',
					value: 'insertimage'
				},

				{
					type: 'push',
					lable: 'View Source',
					value: 'viewSource'
				}
				]
			}
			]
		};

		this.panels.autoLinkPanel = new YAHOO.widget.Panel('Control_Autolink_Editor', 
			{ 
				visible: false, 
				draggable: false,
				fixedcenter: true,
				constraintoviewport: true,
				iesync: true
			}
		);
		this.panels.autoLinkPanel.render(document.body);

		if(!config.fullscreen) {
			var e = document.getElementById(config.id);

			var _width = e.parentNode.style.width;
			var _height = e.parentNode.style.height;
			// Unset the width and height of the parent div
			e.parentNode.style.width = "";
			e.parentNode.style.height = "";
		} else {
			var cSize = BackDrop.util.getClientSize();
			_width = cSize[0] + "px";
			_height = (cSize[1] - 115)  + "px";
		}
		var node = document.getElementById(config.id);
		this.HTML = node.value;
		
		// We are now going to swipe the css thats in the incoming file so we can set it on the default css
		
		this.editor = new YAHOO.widget.Editor(config.id, {
			height: _height,
			width: _width,
			dompath: true, //Turns on the bar at the bottom
			animate: true, //Animates the opening, closing and moving of Editor windows
			toolbar: this.config.toolbar,
			css: ""
		});
		this.editor.render();
		
		var result = YAHOO.util.Event.addListener(window, "load", CtrlEditor_loadGallery, this, true);
		YAHOO.util.Event.onAvailable(this.config.id + "_editor", CtrlEditor_fixHTML, this, true);
		
		if(!result) alert("window.onload event: loadGallery failed");
		var superclass = this;
		this.editor.on('toolbarLoaded', function() {
			var ptObject = this;
			
			this.toolbar.on('autolinkClick', function() {
				var _sel = superclass.editor._getSelectedElement();
				var txtSel = superclass.editor._getSelection();
				txtSel = txtSel.toString();
				if(_sel && _sel.tagName) {
					superclass.currentSelection = _sel;
				} else if(txtSel.length > 0) {
					superclass.currentSelection = txtSel;
				}
				
				// Subscribe
				CtrlEditorEvents.subscribe('selectAutoLink', function(args) {
						
						var el = superclass.editor._getSelectedElement();
						var txtSel = superclass.editor._getSelection();
						
						if(txtSel.toString().length > 0) {
							superclass.editor.execCommand('createlink');
							el.setAttribute('href', args);
						} else if(el.tagName == "IMG"){
							
							superclass.editor.execCommand('createlink');
							
							var a = "<a href='" + args + "'>";
							a += "<img ";
							for(var s = 0; s < el.attributes.length ;s++) {
								a += el.attributes[s].name + "='" + el.attributes[s].value + "' ";
							}
							a += "></a>";
							superclass.editor.execCommand('inserthtml', a);
						}
						/*
						if (el.tagName == "a") {
							superclass.editor.execCommand('createlink');
							el.setAttribute('href', args);
						} else if(typeof el == "HTMLImageElement") {
							alert(el.attributes.length);
						}*/
						superclass.panels.autoLinkPanel.hide();
					}
				);
				
				superclass.panels.autoLinkPanel.show();
			});
			
			this.toolbar.on('insertimageClick', function() {
				var _sel = ptObject._getSelectedElement();
				
				if (_sel && _sel.tagName && ( _sel.tagName.toLowerCase() == 'img') ) {

				} else {
					superclass.panels.galleryPanel.show();
					//This is important.. Return false here to not fire the rest of the listeners
					return false;
				}
				
			});
			
			this.toolbar.on('saveFileClick', function() {
				superclass.editor.saveHTML();
				var loc = "index.php?app=ContentMgrWindow&action=saveHTML";
				var nHTML = superclass.editor.getEditorHTML();
				var trans = YAHOO.util.Connect.asyncRequest('POST', loc, superclass.saveHTMLCallback, nHTML);
				
			});
			
			this.toolbar.on('viewSourceClick', function() {
					var sPanel = new YAHOO.widget.Panel('sourcePanel', {fixedcenter: true, resizable:true});
					sPanel.setHeader("View Source");
					sPanel.setBody("<div style='overflow:auto; width: 700px; height: 400px; background-color: white;'><xmp>" + superclass.editor.getEditorHTML() + "</xmp></div>");
					sPanel.render(document.body);
					sPanel.show();
				}
			);
			
			
			
		});

		
		this.editor.on('afterNodeChange',
			function() {
				CtrlEditorEvents.fire('disable', superclass);
				var txtSel = this._getSelection();
				var _sel = this._getSelectedElement();
				txtSel = txtSel.toString();
				
				if(txtSel.length > 0 || _sel.tagName == "IMG") {
					superclass.editor.toolbar.enableButton('autolink');
				}
			}
		);
		
	};

	CtrlEditor.prototype.Gallery_Insert_Image = function(imgRefId) {
		var url = document.getElementById(imgRefId).src;
		url = url.replace("Thumbnails", "Uploaded");
		this.editor.execCommand('insertimage', url);
		this.panels.galleryPanel.hide();
	}
	
	var CtrlEditorEvents = {};
	CtrlEditorEvents.events = [];

	CtrlEditorEvents.subscribe = function(evt, action) {
		CtrlEditorEvents.events[evt] = action;
	}
	
	CtrlEditorEvents.fire = function(evt, args) {
		if(CtrlEditorEvents.events[evt]) {
			CtrlEditorEvents.events[evt](args);
		}
	}
/*
// Register event handlers
myEditor.on('toolbarLoaded', function() {
	
	this.toolbar.on('insertimageClick', function() {
        //Get the selected element
        var _sel = this._getSelectedElement();
        //If the selected element is an image, do the normal thing so they can manipulate the image
        
		if (_sel && _sel.tagName && ( _sel.tagName.toLowerCase() == 'img') ) {
            //Do the normal thing here..
		} else {
			 panel1.show(); 
            //This is important.. Return false here to not fire the rest of the listeners
			return false;
        }
    }, this, true);
	
});

myEditor.render();

cNode = null;
Control_Unique_ID = null;
EditorGalleryIndex = null;

</script>
*/
/*


<script language="javascript" type="text/javascript">
var dlgRef, panel1, sUrl, transaction;

YAHOO.util.Event.addListener(window, "load", loadGallery );

function loadGallery() {
	panel1 = new YAHOO.widget.Panel(Dialog_Gallery_Panel, {visible: false, draggable: true, constraintoviewport: true, zIndex: 0});
	var config = {instancename: "editorGallery1", parentref: panel1, skeletonId: "Application_Gallery_Skeleton"};
	dlgRef = BackDrop.dialog.request(config);
	//dlgRef.fObj.init(dlgRef.node);
	dlgRef.subscribe("imageDblClick", function(imgRef) {
		Gallery_Insert_Image(imgRef.id);
	});
	dlgRef.init();
	init();
}
// Setup Unique ID for control
var cNode = document.getElementById("Control_TemporaryID");
cNode.id = "";
var Control_Unique_ID = YAHOO.util.Dom.generateId(null, "Control-Editor");
cNode.name = Control_Unique_ID;
cNode.id = Control_Unique_ID;

var resetting = false;

// Gallery Transaction
/*var callback = 
{
	success: function(o) {
		alert("callback still going");
		panel1.setBody(o.responseText);
		if(resetting) {
			panel1.show();
			resetting = false;
		}
		YAHOO.util.Event.onAvailable('Application_Gallery_Upload_Dialog',  Gallery.init, null, true, true);
	},
	failure: function(o) {alert('failure');},
	cache:false
};

function init() {
	panel1.setBody(dlgRef.node.innerHTML);
	panel1.render();
	
	var oPushButton1 = new YAHOO.widget.Button("panel1CloseBtn");
	oPushButton1.on("click", panelClick);
}

function panelClick() {
	panel1.hide();
}
// Insert Methods
function Gallery_Insert_Image(id) {
	var url = document.getElementById(id).src;
	url = url.replace("Thumbnails", "Uploaded");
	myEditor.execCommand('insertimage', url);
	panel1.hide();
}


*/
