var editing = null,
Dom = YAHOO.util.Dom,
myConfig = {
    height: '300px',
    width: '600px',
    animate: true,
    dompath: true
},
state = 'off';
myEditor = null;

function init(){
	//Now let's load the Editor.
	myEditor = new YAHOO.widget.Editor('editor', myConfig);
	
	myEditor.on('toolbarLoaded', function() {
	    this.toolbar.on('toolbarCollapsed', function() {
	        Dom.setXY(this.get('element_cont').get('element'), [-99999, -99999]);
	        //Dom.removeClass(this.toolbar.get('cont').parentNode, 'yui-toolbar-container-collapsed');
	        myEditor.saveHTML();
	    	var editorId = "editable-" + editing.id.substring(0,editing.id.indexOf('-'));
	    	var editorElement = document.getElementById(editorId);
	    	var editorHTML = myEditor.cleanHTML(myEditor.get('element').value);
	    	
	    	editing.innerHTML = editorHTML;
	    	
	    	// Hack for IE
	    	try{
	           editorElement.innerText = editorHTML;
	        }catch(err){}
	        
	        try{
	           editorElement.innerHTML = editorHTML;
	        }catch(err){}
	        
	        editing = null;
	    }, myEditor, true);
	    
	  //When the toolbar is loaded, add a listener to the insertimage button
	    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 {
	            //They don't have a selected image, open the image browser window
	            win = window.open('control/imagebrowser.php', 'IMAGE_BROWSER',
	                'left=20,top=20,width=500,height=500,toolbar=0,resizable=0,status=0');
	            if (!win) {
	                //Catch the popup blocker
	                alert('Please disable your popup blocker!!');
	            }
	            //This is important.. Return false here to not fire the rest of the listeners
	            return false;
	        }
	    }, this, true);
	    
	    // Code for editing HTML
        var codeConfig = {
                type: 'push', label: 'Edit HTML Code', value: 'editcode'
            };
            this.toolbar.addButtonToGroup(codeConfig, 'insertitem');
           
            this.toolbar.on('editcodeClick', function() {
                var ta = this.get('element'),
                    iframe = this.get('iframe').get('element');

                if (state == 'on') {
                    state = 'off';
                    //this.toolbar.set('disabled', false);
                    this.setEditorHTML(ta.value);
                    if (!this.browser.ie) {
                        this._setDesignMode('on');
                    }

                    Dom.removeClass(iframe, 'editor-hidden');
                    Dom.addClass(ta, 'editor-hidden');
                    this.show();
                    this._focusWindow();
                } else {
                    state = 'on';
                    this.cleanHTML();
                    Dom.addClass(iframe, 'editor-hidden');
                    Dom.removeClass(ta, 'editor-hidden');
                    //this.toolbar.set('disabled', true);
                    this.toolbar.getButtonByValue('editcode').set('disabled', false);
                    this.toolbar.selectButton('editcode');
                    this.dompath.innerHTML = 'Editing HTML Code';
                    this.hide();
                }
                return false;
            }, this, true);

            this.on('cleanHTML', function(ev) {
                this.get('element').value = ev.html;
            }, this, true);
            
            this.on('afterRender', function() {
                var wrapper = this.get('editor_wrapper');
                wrapper.appendChild(this.get('element'));
                this.setStyle('width', '100%');
                this.setStyle('height', '100%');
                this.setStyle('visibility', '');
                this.setStyle('top', '');
                this.setStyle('left', '');
                this.setStyle('position', '');

                this.addClass('editor-hidden');
            }, this, true);	    
	}, myEditor, true);
	myEditor.on('editorWindowBlur',function(){
		//this.toolbar.collapse();
		editing.innerHTML = myEditor.getEditorHTML();
	},myEditor,true);

	myEditor.render();
};

YAHOO.util.Event.onDOMReady(init); 

function showEditor(id){
    var tar = document.getElementById(id);
    
    if (editing !== null) {
    	myEditor.saveHTML();
    	editing.innerHTML = myEditor.get('element').value;
    }
    var xy = Dom.getXY(tar);
	myEditor.setEditorHTML(tar.innerHTML);
	Dom.setXY(myEditor.get('element_cont').get('element'), xy);
	editing = tar;
	myEditor.focus();
}

function showImageSelector(id){
    //They don't have a selected image, open the image browser window
    win = window.open('control/imageeditor.php?id='+id+'&context='+context, 'IMAGE_BROWSER',
        'left=20,top=20,width=500,height=500,toolbar=0,resizable=0,status=0');
    if (!win) {
        //Catch the popup blocker
        alert('Please disable your popup blocker!!');
    }
}

function deletePage(){
	document.getElementById("pageAction").value="delete";
}