	// EDITOR

tinyMCE.init({
	mode: 'exact',
	elements: 'yui-editor',
	theme : "advanced",
	height: "400",
	plugins : "safari,spellchecker,pagebreak,style,layer,table,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imgmap",

	relative_urls : false,
	dialog_type : "nomodal",
	inlinepopups_zindex : 500000,
	element_format : "xhtml",
	cleanup:true,
	
	// Theme options
	theme_advanced_buttons1 : "pdsave,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
	theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,media,cleanup,help,code,imgmap,|,insertdate,inserttime,preview,|,forecolor,backcolor",
	theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,advhr,|,print,|,ltr,rtl,|,fullscreen",
	theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	theme_advanced_resizing : true,
	
	theme_advanced_buttons3_add : "tablecontrols",
	table_styles : "Header 1=header1;Header 2=header2;Header 3=header3",
	table_cell_styles : "Header 1=header1;Header 2=header2;Header 3=header3;Table Cell=tableCel1",
	table_row_styles : "Header 1=header1;Header 2=header2;Header 3=header3;Table Row=tableRow1",
	extended_valid_elements: "img[usemap|class|src|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name|style],map[id|name],area[shape|alt|coords|href|target],script[language|type|src],iframe[width|height|name|align|frameborder|scrolling|src|marginheight|marginwidth|style]",

	// Replace values for the template plugin
	template_replace_values : {	username:"username"},
	
	setup : function(ed) {
      
	  ed.addButton('pdsave', {
            title : 'Save',
            image : '/media/images/save.png',
            onclick : function() {

				var html = ed.getContent();
				ed.ajaxSubmit(html);
            }
        });
		
	  },
	file_browser_callback : 'fileBrowser'

});

var fbmember = null;
function fileBrowser(field_name, url, type, win) 
{
	var mpath = "";
	if(type == 'file')
	{
		if(fbmember != null)
		{
			mpath = 'members/' + fbmember
		}
		
		doImageDialog({type:'file',choices: mpath}, 
			function(p){
			f = win.document.getElementById(field_name);
			f.value = p;
		});
		adjustLayers();
	}
	
	if (type == 'image') {
		
		if(fbmember != null)
		{
			mpath = '/images/members/' + fbmember
		}
		
		doImageDialog({choices: 'image:' + mpath}, 
			function(p){
			f = win.document.getElementById(field_name);
			f.value = p;
		});
		adjustLayers();
	}
	
	if (type == 'media') {
		
		if(fbmember != null)
		{
			mpath = 'members/' + fbmember
		}
		
		doImageDialog({choices: 'media:',choices: mpath}, 
			function(p){
			f = win.document.getElementById(field_name);
			f.value = p;
		});
		adjustLayers();
	}
}

// DIALOG 

//create a namespace 
YAHOO.namespace("qw.editor")

//create a new object for this module:
YAHOO.qw.editor.inDialog = function(){

	//Some shortcuts to use in our example:
	var Event = YAHOO.util.Event, Dom = YAHOO.util.Dom, lang = YAHOO.lang;
	
	return {
		
		init: function()
		{
			
            // GET VIEW PORT 
            
            vp = {
                width: screen.availWidth,
                height: screen.availHeight
            };
            vpw = vp.width * 0.8;
            vph = vp.height * 0.6;
            
            // Instantiate the Dialog
            this.dialog = new YAHOO.widget.Dialog("yui-dialog-texteditor", {
                fixedcenter: true,
                visible: false,
                close: false,
                modal: true,
                traintoviewport: false,
                evalScripts: true,
                underlay: 'none',
                width: "750px"
            
            });
            
            this.show = function(op){
				
                showPleaseWait();
                this.dialog.op = op;
                this.op = op
                caption = $('qwtexthd');
                caption.update(op.title);
                
                showOnComplete = function(transport){
					$('yui-imagepicker-panel_c').style.zIndex = '20000';
					$('yui-dialog-texteditor_c').style.zIndex = '300000';
					
					tinyMCE.get('yui-editor').show();
					tinyMCE.get('yui-editor').setContent( transport.responseText);					
					
					this.dialog.show();
                    ft = $('qwtedlgft');
                    ft.insert({
                        after: "<a class='container-close' href='#' onclick='closeEditor();return false;'>Close</a>"
                    });
                    hidePleaseWait();
					
					adjustLayers();
                    
                }.bind(this);
                
                this.dialogApplet = op.applet;
                var dlgform = $('yui-dialog-teform');
                dlgform.action = '/ajax/' + op.applet + '/'
                
				tinyMCE.get('yui-editor').ajaxSubmit = function(html){
					
					var dlgform = $('yui-dialog-teform');
					
					var oncomplete = function(transport){
						ta = $(this.op.scope);
						if (ta) {
							ta.update(html);
						}
						closeEditor();
						updateControlPanels();
					}.bind(this);
				
					if(this.op.cb != null)
					{
						ta = $(this.op.scope);
						if (ta) {
							ta.update(html);
						}
						this.dialog.hide();
						op.cb(html);
						return;	
					}
					
					if(this.op.save == false)
					{
						ta = $(this.op.scope);
						if (ta) {
							ta.update(html);
						}
						this.dialog.hide();
						return;
					}
					
					new Ajax.Request('/ajax/' + this.op.applet + '/', {
						parameters: {
							_textview: 'save',
							html: html
						},
						onComplete: oncomplete
					});
				}.bind(this);
				
				if(op.load == false)
				{
					var src = $(op.scope);
					if(src)
					{
						tinyMCE.get('yui-editor').show();
						tinyMCE.get('yui-editor').setContent(src.innerHTML);
					}
					this.dialog.show();
                    ft = $('qwtedlgft');
                    ft.insert({
                        after: "<a class='container-close' href='#' onclick='closeEditor();'>Close</a>"
                    });
                    hidePleaseWait();
					return;
				}
				
				
				
                new Ajax.Request('/ajax/' + op.applet + '/', {
                    parameters: op.choices.parameters,
                    onComplete: showOnComplete
                });
                
            }
            
            this.hide = function(op){
				$('yui-imagepicker-panel_c').style.zIndex = '200000';
				tinyMCE.get('yui-editor').setContent('');
				tinyMCE.get('yui-editor').hide();
				$('yui-editor').update('');
                this.dialog.cancel();
            }
            
            // We're all set up with our Dialog's configurations. Now, render the Dialog 
            
            this.dialog.render();
			
			}
		
	}

}()

YAHOO.util.Event.onDOMReady(YAHOO.qw.editor.inDialog.init, YAHOO.qw.editor.inDialog, true); 


function doEditor(op)
{
	fbmember = null;
	if (op != null) {
		if (op.member != null) {
			fbmember = op.member;
		}
	}
	YAHOO.qw.editor.inDialog.show(op);
}

function closeEditor(op)
{
	YAHOO.qw.editor.inDialog.hide();
}

