﻿var boolMode = false;
var divstate = 'visible';




function Startup() {
    
    if (document.getElementById('kweditor') != null) {
        document.getElementById('kweditor').contentWindow.document.designMode = "on";
        document.getElementById('kweditor').style.height = "100%";
    }
    
}

function LoadDiv(state) {
    var divupdate = document.getElementById('divsave');
    if (divupdate != null) {
        divupdate.style.zIndex = "1000";
        if (state == 0) {
            divupdate.style.visibility = "hidden";
        } else {
            divupdate.style.visibility = "visible";
        }
    }
}

window.onload = function() { Startup(); }

function checktoolbar() {
    //Kontrollera om toolbaren ska vara gömd eller synlig
    var beginindex, endindex, result
    start = document.cookie.indexOf("toolbarstate") + 13;
    end = start;
    while (document.cookie.charAt(endindex) != ";" && end <= document.cookie.length) { end++ }
    var cookie = document.cookie.substring(start, end);
    if (cookie == 'hidden') { showhidetoolbar(); }
}

function fiximages(state) {

    //Kör endast när designmode är på
    if (document.getElementById('kweditor').contentWindow.document.designMode != "On") {return; }
       
    //Korrigera bilderna som är positionerade absolute så de hamnar på rätt plats i iframen
    for (var i = 0; i < parent.kweditor.document.images.length; i++) {
        var img = parent.kweditor.document.images[i];
        //var iframe = document.getElementById('kweditor');
        
        var docwidth = (document.body.clientWidth / 2) - (document.getElementById('divcontainer').offsetLeft);


        if (state == 0) {
            //Bilderna laddas in från databasen och görs om för att visas i iframen
            img.style.position = 'absolute';
            var left = parseInt(img.style.left)-33;
            var top = parseInt(img.style.top)-35;

        } else {
            //Bildernas position ska sparas ner i databasen
            img.style.position = 'absolute';
            var left = parseInt(img.style.left)+33;
            var top = parseInt(img.style.top)+35;
        }

        
        if (!isNaN(left)) {
            img.style.left = left + 'px';
        }
        if (!isNaN(top)) {
            img.style.top = top + 'px';
        }
        
    }

    //Korrigera även ev flytande textrutor
    for (var i = 0; i < parent.kweditor.document.getElementsByTagName('div').length; i++) {
        var div = parent.kweditor.document.getElementsByTagName('div')[i];
        if (div.className == 'floatingtext') {
            if (state == 0) {
                var left = parseInt(div.style.left) - 36;
                var top = parseInt(div.style.top)-36;
            } else {
                var left = parseInt(div.style.left) + 36;
                var top = parseInt(div.style.top)+36;
            }
            if (!isNaN(left)) {
                div.style.left = left + 'px';
            }
            if (!isNaN(top)) {
                div.style.top = top + 'px';
            } 
        }
    }


    document.getElementById("kweditor").contentWindow.document.execCommand('2D-Position', true, null);
    if (state == 0) { LoadDiv(0); }
}

var BrowserVersion;
function isIE() {
    var BrowserName = navigator.appName;
    BrowserVersion = navigator.appVersion;
    var index = BrowserVersion.indexOf('MSIE');
    if (index != -1) {
        BrowserVersion = parseFloat(BrowserVersion.substring(index + 5));
    }
   
    if (BrowserName == "Microsoft Internet Explorer") {
        return true;
    } else {
        return false;
    }
}



function toggletoolbar() {
    if (divstate == 'visible') {
        divstate = 'hidden';
    } else {
        divstate = 'visible';
    }
    DIVid = 'kweditorToolbarButtons';
    if (document.layers) {
        document.layers[DIVid].visibility = divstate;
    }
    if (document.getElementById && !document.all) {
        DIVelement = document.getElementById(DIVid);
        DIVelement.style.visibility = divstate;
    }
    if (document.all) {
        eval("document.all." + DIVid + ".style.visibility = divstate");
    }
}

function showhidetoolbar() {
    var toolbar = document.getElementById('kweditorToolbar');
    
    for (var i = 0; i < toolbar.getElementsByTagName('div').length; i++) {
        if (toolbar.getElementsByTagName('div')[i].id != 'colorpicker1' && toolbar.getElementsByTagName('div')[i].id != 'colorpicker1ok') {
            toolbar.getElementsByTagName('div')[i].style.visibility = (toolbar.getElementsByTagName('div')[i].style.visibility != 'visible' && toolbar.getElementsByTagName('div')[i].style.visibility != '' ? 'visible' : 'hidden');
        }
    }
    if (toolbar.style.width == '' || toolbar.style.width != '14px') {
        toolbar.style.width = '14px';
        document.cookie = "toolbarstate=hidden";
    } else {
        toolbar.style.width = '100%';
        document.cookie = "toolbarstate=show";
    }
}


function changeMode(_boolMode) {

    if (_boolMode) {
        if (isIE()) {
            kweditor.document.body.innerText = kweditor.document.body.innerHTML;
            kweditor.document.body.style.backgroundColor = '#fff';
        } else {
            var html = document.createTextNode(document.getElementById("kweditor").contentWindow.document.body.innerHTML);
            document.getElementById("kweditor").contentWindow.document.body.innerHTML = "";
            document.getElementById("kweditor").contentWindow.document.body.appendChild(html);
        }
        // toggletoolbar();
        boolMode = true;
    } else {
        if (isIE()) {
            kweditor.document.body.innerHTML = kweditor.document.body.innerText;
            kweditor.document.body.style.backgroundColor = 'transparent';
        } else {
            var html = document.getElementById("kweditor").contentWindow.document.body.ownerDocument.createRange();
            html.selectNodeContents(document.getElementById("kweditor").contentWindow.document.body);
            document.getElementById("kweditor").contentWindow.document.body.innerHTML = html.toString();
        }
        // toggletoolbar();
        boolMode = false;
    }
    document.getElementById("kweditor").contentWindow.focus()
}

function bold() {
    document.getElementById("kweditor").contentWindow.document.execCommand('bold', false, '');
    

}

function italic() {
    document.getElementById("kweditor").contentWindow.document.execCommand('italic', false, '');
}

function underline() {
    document.getElementById("kweditor").contentWindow.document.execCommand('underline', false, '');
}

function InsertUnorderedList() {
    document.getElementById("kweditor").contentWindow.document.execCommand('InsertUnorderedList', false, '');
}

function InsertLink() {

    var sel = document.getElementById("kweditor").contentWindow.document.selection.createRange();
    if (sel.text == '') {
        alert('Du måste markera den text där du vill infoga länken');
        return false;
    }
    var URL = prompt("Ange sidans adress", "http://");
    document.getElementById("kweditor").contentWindow.document.execCommand('CreateLink', false, URL);
}

function setformat(value) {
    switch (value) {
        case '0':
            InsertNormal();
            break;
        case '1':
            InsertHeader();
            break;
        case '2':
            SetFont('verdana');
            break;
        case '3':
            SetFont('Trebuchet MS');
            break; 
        case '4':
            SetFont('arial');
            break;     
        case '5':
            SetFont('tahoma');
            break;
        case '6':
            SetFont('times new roman');
            break;  
        case '7':
            SetFontSize(1);
            break;  
        case '8':
            SetFontSize(2);
            break;   
        case '9':
            SetFontSize(3);
            break;   
        case '10':
            SetFontSize(4);
            break;
        case '11':
            SetFontSize(5);
            break;
        case '12':
            SetFontSize(6);
            break;                                                                                                                         
        default:
    }

}


function SetFontSize(size) {
    document.getElementById("kweditor").contentWindow.document.execCommand('FontSize', false, size);
}

function SetFont(font) {
    document.getElementById("kweditor").contentWindow.document.execCommand('fontname', false, font);
}

function InsertHeader() {

    document.getElementById("kweditor").contentWindow.document.execCommand('ForeColor', false, '#802910');
    document.getElementById("kweditor").contentWindow.document.execCommand('FontSize', false, '2');
    document.getElementById("kweditor").contentWindow.document.execCommand('bold', false, '');
}

function SetColor(color) {
    document.getElementById("kweditor").contentWindow.document.execCommand('ForeColor', false, color);
    document.getElementById('colorpicker1').style.visibility = 'hidden';
}

function InsertNormal() {
    document.getElementById("kweditor").contentWindow.document.execCommand('ForeColor', false, '#000000');
    document.getElementById("kweditor").contentWindow.document.execCommand('FontSize', false, '2');
}

function AlignRight() {

    document.getElementById("kweditor").contentWindow.document.execCommand('JustifyRight', false, null);

}

function AlignLeft() {

    document.getElementById("kweditor").contentWindow.document.execCommand('JustifyLeft', false, null);

}

function AlignCenter() {

    document.getElementById("kweditor").contentWindow.document.execCommand('JustifyCenter', false, null);

}

function Undo() {
    document.execCommand('undo', false, null);
}

function Redo() {
    document.execCommand('redo', false, null);
}

function InsertImage() {
                  
  


    document.getElementById("kweditor").contentWindow.focus()
    var ImgSrc = showModalDialog("imagegallery.aspx", "", "dialogHeight: 500px; dialogWidth: 920px; dialogTop: px; dialogLeft: px; edge: Raised; center: Yes; help: No; resizable: Yes; status: yes;");
    if (ImgSrc) {
        var sel = document.getElementById("kweditor").contentWindow.document.selection.createRange();
        var selIsImg = false;
        if (document.getElementById("kweditor").contentWindow.document.selection.type == 'Control') {
            if (sel.item(0).tagName) {
                if (sel.item(0).tagName == 'IMG') {
                    //En bild är markerad, byt ut bilden istället för att lägga upp en ny
                    var newimg = document.getElementById("kweditor").contentWindow.document.createElement("IMG");
                    newimg.src = ImgSrc;
                    newimg.width = sel.item(0).width;
                    newimg.height = sel.item(0).height;
                    newimg.style.position = "absolute";
                    newimg.style.top = sel.item(0).style.top;
                    newimg.style.left = sel.item(0).style.left;
                    newimg.style.cursor = "hand";
                    
                    if (isIE()) {
                        if (BrowserVersion == 9) {
                            newimg.setAttribute('onclick', 'LoadImgDiv("' + ImgSrc + '");'); // IE9
                        } else {
                            newimg.onClick = "LoadImgDiv('" + ImgSrc + "');"; // för tidigare versioner av IE
                        }
                    } else {
                        newimg.setAttribute('onclick', 'LoadImgDiv("' + ImgSrc + '");'); // övriga
                    }
                
                    
                    document.getElementById("kweditor").contentWindow.document.body.replaceChild(newimg, sel.item(0));

                                      
                    selIsImg = true;
                }
            }
        }

        if (!selIsImg) {
            var img = document.getElementById("kweditor").contentWindow.document.createElement("IMG");
            img.src = ImgSrc;
            img.style.cursor = "hand";
            
            if (isIE()) {
                if (BrowserVersion == 9) {
                    img.setAttribute('onclick', 'LoadImgDiv("' + ImgSrc + '");'); // IE9
                } else {
                    img.onClick = "LoadImgDiv('" + ImgSrc + "');"; // för tidigare versioner av IE
                }
            } else {
                img.setAttribute('onclick', 'LoadImgDiv("' + ImgSrc + '");'); // övriga
            }
            
            img.style.position = "absolute";
            document.getElementById("kweditor").contentWindow.document.body.appendChild(img);
        }          
    }   
    
    for (var i = 0; i < parent.kweditor.document.images.length; i++) {
        var img = parent.kweditor.document.images[i]
        tmp = new Date();
        tmp = img.src + "?" + tmp.getTime()
    }
}

function resizeimage() {
    var sel = document.getElementById("kweditor").contentWindow.document.selection.createRange();
    if (document.getElementById("kweditor").contentWindow.document.selection.type == 'Control') {
        if (sel.item(0).tagName) {
            if (sel.item(0).tagName == 'IMG') {
                var oImg = sel.item(0);
                oImg.style.width = '95px';
                oImg.style.height = '135px';
            }
        }
    }
}

function sortimages() {
    //Korrigera bildernas plats automatiskt
    var startleft = 369;
    var starttop = 23;
    var rowbreak = 0;
    for (var i = 0; i < parent.kweditor.document.images.length; i++) {
        var img = parent.kweditor.document.images[i];

        //img.style.visibillity = 'hidden';

        img.style.position = 'absolute';

        if (i == 0) {
            var left = parseInt(startleft);
            var top = parseInt(starttop);
            rowbreak++;
        } else {
            var left = parseInt(startleft + (rowbreak * 100));
            var top = parseInt(starttop);
            if (rowbreak != 2) {
                rowbreak++;
            } else {
                starttop = starttop + 150;
                rowbreak = 0;
            }
        }


        if (!isNaN(left)) {
            img.style.left = left + 'px';
        }
        if (!isNaN(top)) {
            img.style.top = top + 'px';
        }

    }
}

function ToggleAbsolute() {
    
    var sel = document.getElementById("kweditor").contentWindow.document.selection.createRange();
    
    if (document.getElementById("kweditor").contentWindow.document.selection.type == 'Text') {
        sel.pasteHTML('<div class="floatingtext" style="position:absolute">' + sel.text + '</div>');
    }
    else if (document.getElementById("kweditor").contentWindow.document.selection.type == 'Control') {
        if (sel.item(0).tagName) {
            if (sel.item(0).tagName == 'DIV') {
                var oDiv = sel.item(0);
                var oOrgText = oDiv.innerHTML;
                oDiv.outerHTML = '<p>' + oOrgText + '</p>';
            }
        }
    }
}

function InsertFile() {

    var sel = document.getElementById("kweditor").contentWindow.document.selection.createRange();
    if (sel.text == '') {
        alert('Du måste markera den text där du vill infoga länken');
        return false;
    }
    document.getElementById("kweditor").contentWindow.focus()
    var FileSrc = showModalDialog("filegallery.aspx", "", "dialogHeight: 500px; dialogWidth: 898px; dialogTop: px; dialogLeft: px; edge: Raised; center: Yes; help: No; resizable: Yes; status: No;");

    if (FileSrc!=undefined) {
        document.getElementById("kweditor").contentWindow.document.execCommand('CreateLink', false, FileSrc);
    }
}

function updatetitle(newvalue) {
    kweditorTMPtitle = document.getElementById('kweditorTMPtitle');
    kweditorTMPtitle.value = newvalue;
}

function save() {
    kweditorTMPdata = document.getElementById('kweditorTMPdata');

    title = document.getElementById('ctl00_ContentPlaceHolderCenter_Editor1_title');



    if (boolMode) {
        //Kontrollera så att vi inte är i HTML läget
        alert('Stäng av HTML-kod för innan du sparar');
    } else {
        if (title) { //Om title inte finns så är det en kategori och då behöver vi inte spara sidnamn
            if (title.value == '') {
                alert('Du måste ge sidan ett namn innan du kan spara den');
            } else {
                //Gömmer iframen så uppdateringen inte syns för användaren
                LoadDiv(1);
                fiximages(1);
                kweditorTMPdata.value = kweditor.document.body.innerHTML;
                document.forms[0].submit();
            }
        } else {
            //Gömmer iframen så uppdateringen inte syns för användaren
            LoadDiv(1);
            fiximages(1);
            kweditorTMPdata.value = kweditor.document.body.innerHTML;
            document.forms[0].submit();
        }
    }
}
