//////////////////////////////////////////////////////////////// 
// Toolbar Funktionen
//////////////////////////////////////////////////////////////// 

var p_oEditor = null;
var p_oCurrentBar = null;
var p_sCurrentBar = null;

editor_checkRange = function(){
	if (!p_oEditor) return;
	p_oEditor.focus();
	if (document.selection.type == "None") {
		document.selection.createRange();
	}
}

editor_onSelectionChange = function(i_oEditor, i_sCurrentBar){
	
	if (typeof i_oEditor != 'undefined') {
		if (i_oEditor != p_oEditor) {;
		    editor_resetMode();
    		p_oEditor = i_oEditor;
		}
		if (typeof p_oEditor.m_sMode == 'undefined') {
			p_oEditor.m_sMode = 'html';
		}
		// *******************************************************************
		// ** Nils Schumacher ************************************************
		// *******************************************************************
		
		if (event.ctrlKey && event.keyCode == 86) {
			fChangeModus = (p_oEditor.m_sMode != 'text');
        	if (fChangeModus) {
		    	p_oEditor.m_sMode = 'text';
		    	// ** AntjeM 
		    	p_oEditor.innerText = '<P>' + p_oEditor.innerText + '</P>';
            }
            if (fChangeModus) {
		      editor_doMode();
            }
		}
					
		// *******************************************************************
		p_sCurrentBar = i_sCurrentBar;
	    var oBar = document.getElementById('bar_' + p_sCurrentBar);
	    if (oBar != p_oCurrentBar) {
	    	if (p_oCurrentBar) p_oCurrentBar.style.visibility = 'hidden';
	    	p_oCurrentBar = oBar;
	    	p_oCurrentBar.style.visibility = 'visible';
	    }
	}
	editor_updateToolbar();
}

editor_doCommand = function (i_sCommand) {
    if (!editor_isCommandEnabled(i_sCommand)) return;
    
	editor_checkRange();

	switch (i_sCommand) {
        case 'Cut':
        case 'Copy':
        case 'Paste':
        	// *******************************************************************
			// ** Nils Schumacher ************************************************
			// *******************************************************************
        	fChangeModus = (p_oEditor.m_sMode != 'text');
        	if (fChangeModus) {
		    	p_oEditor.m_sMode = 'text';
		    	// ** AntjeM 
		    	p_oEditor.innerText = '<P>' + p_oEditor.innerText + '</P>';
            }
            document.execCommand(i_sCommand);
            if (fChangeModus) {
		      editor_doMode();
            }
            // *******************************************************************
            break; 

        case 'Undo':
            document.editUndo(); break;
        case 'Redo':
            document.editRedo(); break;
        
        case 'Find':
            document.find(); break;
        case 'Findnext':
            document.findNext(); break;
        case 'Replace':
            document.replace(); break;
            
        case 'Bold':
        case 'Italic':
        case 'Underline':
            if (editor_resetMode()) break;
            document.execCommand(i_sCommand); break;
            
        case 'JustifyLeft':
        case 'JustifyCenter':
        case 'JustifyRight':
            if (editor_resetMode()) break;
            document.execCommand(i_sCommand); break;

        case 'Image':
            if (editor_resetMode()) break;
            editor_chooseImage(); break;
        case 'Link':
            if (editor_resetMode()) break;
            editor_chooseLink(); break;
        case 'Removelink':
            if (editor_resetMode()) break;
            document.execCommand("Unlink", false);
            break;

        case 'InsertOrderedList':
        case 'InsertUnorderedList':
            if (editor_resetMode()) break;
            document.execCommand(i_sCommand); break;

        case 'Indent':
        case 'Outdent':
            if (editor_resetMode()) break;
            document.execCommand(i_sCommand); break;

        case 'InsertBR':
            if (editor_resetMode()) return;
            editor_insert('BR'); break;

        case 'Mode':
            editor_doMode(); break;
    }
    editor_updateToolbar();
}

editor_updateToolbar = function () {
	editor_checkRange();
	
//    editor_setCommandEnabled('save', p_oEditor.isChanged());

    if (p_oEditor.m_sMode == 'text') {
        editor_setCommandEnabled('Link' , false);
        editor_setCommandEnabled('Removelink' , false);

        editor_setCommandEnabled('Bold' , false);
        editor_setCommandEnabled('Italic' , false);
//        editor_setCommandEnabled('Underline' , false);

        editor_setCommandEnabled('JustifyLeft' , false);
        editor_setCommandEnabled('JustifyCenter' , false);
        editor_setCommandEnabled('JustifyRight' , false);

        editor_setCommandEnabled('InsertOrderedList' , false);
        editor_setCommandEnabled('InsertUnorderedList' , false);
        editor_setCommandEnabled('Indent' , false);
        editor_setCommandEnabled('Outdent' , false);
    }
    else {
        editor_setCommandEnabled('Link' , true);
        editor_setCommandEnabled('Removelink' , true);

        editor_setTextStateCommandState('Bold');
        editor_setTextStateCommandState('Italic');
//        editor_setTextStateCommandState('Underline');
    
        editor_setTextStateCommandState('JustifyLeft');
        editor_setTextStateCommandState('JustifyCenter');
        editor_setTextStateCommandState('JustifyRight');
    
        editor_setTextStateCommandState('InsertOrderedList');
        editor_setTextStateCommandState('InsertUnorderedList');
        editor_setTextStateCommandState('Indent');
        editor_setTextStateCommandState('Outdent');
    }
    editor_setCommandSelected('Mode' , p_oEditor.m_sMode == 'text');
    
/*    
    if (p_oEditorElement && p_oEditorElement.Name == "image"){
        editor_setCommandSelected("image", true);
    }
    else {
        editor_setCommandEnabled("image", editor_canCreate("image"));
    }

    if (p_oEditorElement && (p_oEditorElement.Name == "link" || (p_oEditorElement.Parent && p_oEditorElement.Parent.Name == "link"))){
        editor_setCommandSelected("link", true);
    }
    else {
        editor_setCommandEnabled("link", (p_oEditorElement && p_oEditorElement.Name == "image") || editor_canCreate("link"));
    }
*/
}

editor_setTextStateCommandState = function (i_sCommand) {
    editor_setCommandSelected(i_sCommand, document.queryCommandValue(i_sCommand));
}

editor_setCommandSelected = function (i_sCommand, i_fSelected) {
    var oButton = document.getElementById('but_' + p_sCurrentBar + '_' + i_sCommand);
    if (oButton) {
        if (i_fSelected) {
            oButton.className = 'rebar_button_selected';
        }
        else {
            oButton.className = 'rebar_button';
        }
    }
}

editor_isCommandEnabled = function (i_sCommand) {
    var oButton = document.getElementById('but_' + p_sCurrentBar + '_' + i_sCommand);
    if (oButton) {
        return oButton.className != 'rebar_button_disabled';
    }
    return false;
}

editor_setCommandEnabled = function (i_sCommand, i_fEnabled) {
    var oButton = document.getElementById('but_' + p_sCurrentBar + '_' + i_sCommand);
    if (oButton) {
        if (i_fEnabled) {
            oButton.className = 'rebar_button';
        }
        else {
            oButton.className = 'rebar_button_disabled';
        }
    }
}


// called to insert a Web-hyperlink.  It will use the selected text if
// there is some, or the URL entered if not.  If clicked when over a link,
// that linke is allowed to be edited.
editor_chooseLink = function () {
	var sURL = "http://";
	var sText;

	// First, pick up the current selection.
	p_oEditor.focus();
	var oRange = document.selection.createRange();
	var oParentElement = oRange.parentElement();

	// Is this aready a link?
	if (oParentElement && oParentElement.nodeName == "A") {
		oRange.moveToElementText(oParentElement);
		sURL = oParentElement.href;
	}


	// Get the text associated with this link
	sText = oRange.text;

    document.execCommand("CreateLink", true, sURL);
    
	var oRange = document.selection.createRange();
	var oParentElement = oRange.parentElement();

	if (oParentElement && oParentElement.nodeName == "A") {
        sURL = oParentElement.href;

        //Links auf die gleiche Domain in absolute Pfade umwandeln
        var sBaseURL = document.location.href;
    	var iProtocol = sBaseURL.indexOf(':');
    	if (iProtocol != -1) {
    	    sProtocol = sBaseURL.substring(0, iProtocol+3);
    	    sDomain = sBaseURL.substring(iProtocol+3);
        	var iDomain = sDomain.indexOf('/');
    	    sDomain = sDomain.substring(0, iDomain);
    
        	//Ein Root mit und eins ohne www
        	var iWWW = sDomain.indexOf('www.');
    	    var sRootURL1;
            var sRootURL2;
    	    if (iWWW == 0) {
        	    sRootURL1 = sProtocol + sDomain.substring (4);
        	    sRootURL2 = sProtocol + sDomain;
        	}
        	else {
        	    sRootURL1 = sProtocol + sDomain;
     	        sRootURL2 = sProtocol + 'www.' + sDomain;
        	}
    
    	    if (sURL.indexOf(sRootURL1) == 0) {
        	    sURL = sURL.substring(sRootURL1.length);
    	    }
    	    else if (sURL.indexOf(sRootURL2) == 0) {
        	    sURL = sURL.substring(sRootURL2.length);
    	    }
    	}
        oParentElement.href = sURL;

        if (sURL.indexOf(':') != -1) {
            //absoluter Link
            oParentElement.target = "_blank";
        }
        else {
            //relativer Link
            oParentElement.target = "_top";
        }
	}
	
	editor_onSelectionChange();
}

editor_insert = function (i_sTagname) {
	p_oEditor.focus();
	var oRange = document.selection.createRange();
	var oParentElement = oRange.parentElement();
	var sHTML = '<' + i_sTagname + ' />' + oRange.text;
	oRange.pasteHTML(sHTML);
	oRange.select();
}

function hover(on) {
	var el = window.event.srcElement;
	if (el && (el.nodeName == "IMG")) {
	    if (el.className.indexOf('disabled') != -1) return;
	    if (el.className.indexOf('rebar') == -1) return;
	    var sState = (el.className.indexOf('selected') != -1) ? '_selected' : '';
	
		if (on) {
			el.className = "rebar_button" + sState + "_hover";
		} else {
			el.className = "rebar_button" + sState;
		}
	}
}
function press(on) {
	var el = window.event.srcElement;
	if (el && (el.nodeName == "IMG")) {
	    if (el.className.indexOf('disabled') != -1) return;
	    if (el.className.indexOf('rebar') == -1) return;
	    var sState = (el.className.indexOf('selected') != -1) ? '_selected' : '';

	    if (on) {
			el.className = "rebar_button" + sState + "_down";
		} else {
			el.className = (el.className == "rebar_button" + sState + "_down") ?
                            "rebar_button" + sState + "_hover" :
							"rebar_button" + sState;
		}
	}
}


editor_fillForm = function (i_sForm) {		
    editor_resetMode();
    
	var a_oEditors = document.getElementsByTagName("DIV");
	for (var sID in a_oEditors) {
		var oEditor = a_oEditors[sID];
		if (oEditor.className && oEditor.className == 'editor') {
			var iPos = sID.indexOf("_");
			if (iPos) {
				var sInputID = 'V_' + sID.substr(iPos + 1);
				var oInput = document.forms[i_sForm].elements[sInputID];
				oInput.value = oEditor.innerHTML;
			}
		}
	}
}

editor_canClose = function () { 
/*
    if (p_oEditor.isChanged()) {
        if (confirm('Möchten Sie das Dokument speichern?')) {
            return p_oEditor.save();
        }
        return false;
    }
    */
    return true;
}

onbeforeunload = function () { 
    if (!editor_canClose()) {
        event.returnValue = "Sie haben das Dokument nicht gespeichert.\n\nAlle Änderungen gehen verloren.";
    }
}
        
editor_resetMode = function () {
    if (!p_oEditor) return;
    
    if (p_oEditor.m_sMode == 'text') {
        p_oEditor.m_sMode = 'html';
        p_oEditor.innerHTML = p_oEditor.innerText;
        return true;
    }
    return false;
}

editor_doMode = function () {
    if (p_oEditor.m_sMode == 'html') {
        p_oEditor.m_sMode = 'text';
        p_oEditor.innerText = p_oEditor.innerHTML;
    }
    else {
        p_oEditor.m_sMode = 'html';
        p_oEditor.innerHTML = p_oEditor.innerText;
    }
}
