var site_path = '/';

function ajax(){
    var xmlHttp;
    try{
        xmlHttp=new XMLHttpRequest();
    }catch(e){
        try{
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }catch(e){
            try{
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }catch(e){
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    return xmlHttp;
}

function scaleByContent(obj){
    document.getElementById(obj).style.height = document.getElementById(obj).scrollHeight + "px";
}

function checkModified(editor_name){
    var modified = false; 
    if(nicEditors.findEditor(editor_name).getContent() != document.getElementById(editor_name + '_saved').value){
        modified = true;    
    }
    
    if(modified){
        document.getElementById(editor_name + '_save_button').disabled = false;
    }
    else {
        document.getElementById(editor_name + '_save_button').disabled = true;
    }
}

var editors = new Object();
var save_elements = new Object();
function toggleEditContent(editor_name){

    if(!editors[editor_name]) {
        editors[editor_name] = new nicEditor({iconsPath : site_path + 'images/nicEditorIcons.gif', buttonList : ['bold','italic','underline','left','center','right','justify','fontFormat','link']}).panelInstance(editor_name,{hasPanel : true});
        createSavedContentTextarea(editor_name);
        createSaveButton(editor_name);
        createResetButton(editor_name);
    } else {
        editors[editor_name].removeInstance(editor_name);
        editors[editor_name] = null;
    }
}

function createSavedContentTextarea(editor_name){
    if( !document.getElementById(editor_name + '_saved') ){
        var textArea = document.createElement('textarea');
        textArea.id = editor_name + '_saved';
        textArea.className = 'hidden';
        textArea.innerHTML = encodeURIComponent(nicEditors.findEditor(editor_name).getContent());
        document.getElementById('page_form').appendChild(textArea);
    }
}

function saveTitle(title_text){
    var req = ajax();
   	req.open("POST",site_path + "ajax/save_content.ajax.php",false);
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    var parameters = "content_id=title&content=" + escape(addslashes(title_text));
   	req.send(parameters);
    
    if(req.responseText == 200){
        window.document.title = title_text;
        displayMessage('Sidans titel sparades');
    }
    else {
        displayMessage('Fel uppstod! Sidans titel kunde inte sparas', 'error');
    }
}

function displayMessage(message, type){
    var notification = document.getElementById('action_message');
    if(typeof type == 'undefined'){
        type = 'normal';    
    }
    notification.className = type;
    notification.innerHTML = "<blink>" + message + "</blink>";
    if(message!=''){
        var timer = setTimeout("displayMessage('')",3000);
    }
}

function addslashes(str) {
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\0/g,'\\0');
return str;
}


function createSaveButton(editor_name){
    if( !document.getElementById(editor_name + '_save_button') ){
        var button = document.createElement('input');
        button.id = editor_name + '_save_button';
        button.value = 'Spara';
        button.type = 'button';
        button.onclick = function(){    
            var content = nicEditors.findEditor(editor_name).getContent();
            var req = ajax();
        	req.open("POST",site_path + "ajax/save_content.ajax.php",false);
            req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            var parameters = "content_id=" + editor_name + "&content=" + escape(addslashes(content));
        	req.send(parameters);
            if(req.responseText == 200){
                clearEditor(editor_name);  
                document.getElementById(editor_name + '_saved').innerHTML = encodeURIComponent(content);
                displayMessage('Texten sparades');
            }
            else {
                displayMessage('Fel uppstod! Texten kunde inte sparas', 'error');   
            }
            
            return false;
        }
        document.getElementById('buttons_' + editor_name).appendChild(button);
    }
}

function createResetButton(editor_name){
    if( !document.getElementById(editor_name + '_reset_button') ){
        var button = document.createElement('input');
        button.id = editor_name + '_reset_button';
        button.value = 'Avbryt';
        button.type = 'button';
        button.onclick = function(){
            nicEditors.findEditor(editor_name).setContent(decodeURIComponent(document.getElementById(editor_name + '_saved').innerHTML)); 
            clearEditor(editor_name);
        }
        document.getElementById('buttons_' + editor_name).appendChild(button);
    }
}

function clearEditor(editor_name){
    editors[editor_name].removeInstance(editor_name);
    editors[editor_name] = null;  
    document.getElementById('buttons_' + editor_name).innerHTML = '';    
}

function insertAfter(newElement, referenceElement) { 
    referenceElement.parentNode.insertBefore(newElement,referenceElement.nextSibling); 
}

function createButtonDiv(editor_name){
    var buttonDiv = document.createElement('div');
    buttonDiv.id = 'buttons_' + editor_name;
    return buttonDiv;
}

function drawEditElements(){
    var modifiable_nic_area = document.getElementById('modifiable_content');    
    var divElements = modifiable_nic_area.getElementsByTagName('div');
    for(var i = 0; i < divElements.length; i++){
        if(divElements[i].getAttribute('name') == 'editable'){                        
            divElements[i].onmouseover = function(){
                this.style.borderColor = '#888';
            }
            divElements[i].onmouseout = function(){
                this.style.borderColor = '#fff';
            }
            divElements[i].onclick = function(){
                toggleEditContent(this.id);
            }
            divElements[i].className = 'modify';   
            insertAfter(createButtonDiv(divElements[i].id), document.getElementById(divElements[i].id));
        }        
    }
}

