/* Javascript for LachKaarten V2010 */
/* JQuery v1.4.4 needed */

/* Hover for the cards */
/* Create the Hover DIV */
$(document).ready(
    function(){
        $('body').append('<div id="lkbigpreview"></div>');
    }
    );
/* Add the Hover function to the previews */
$(document).ready(
    function(){
        $("div.lkcardpreview").hover(
            function(event){
                $(this).css('border-color', '#43BFC7').css('background-color','#d1f4ff');
                var div = $("#lkbigpreview");
                if(div){
                    var html = $(".lkcardpreviewsub",$(this)).html();
                    div.html('<span style="font-size:8px;font-style:italic;">Voorbeeld van een bericht:</span><br /><br />'+html);
                    var top = ($(this).offset().top)+($(this).height());
                    var left = ($(this).offset().left);
                    var width = ($(this).width())-10;
                    div.css("top", top+'px').css("left", left+'px').css("width",width+'px');
                    div.slideDown("slow");
                }
            },
            function(event){
                $(this).css('border-color', '#eeeeee').css('background-color','');
                var div = $("#lkbigpreview");
                if(div){
                    div.hide();
                }
            })
    }
    );

/* Add the AddThis button on page load */
$(document).ready(
    function(){
        $('#placeholder_addthis').html('<a style="padding:5px;" class="addthis_button" href="http://www.addthis.com/bookmark.php?v=250&amp;username=lachjekrom"><img src="http://s7.addthis.com/static/btn/v2/lg-bookmark-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0" /></a><script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=lachjekrom"></script>');
    }
    );

/* LachKaarten Text Editor */
var isLoaded = false;
var isEditable= false;
var isIE;
var isGecko;
var isSafari;
var isKonqueror;
var edtr;

function initiateEditor() {
    var browser = navigator.userAgent.toLowerCase();
    isIE = ((browser .indexOf("msie") != -1) && (browser .indexOf("opera") == -1) && (browser .indexOf("webtv") == -1));
    isGecko = (browser .indexOf("gecko") != -1);
    isSafari = (browser .indexOf("safari") != -1);
    isKonqueror = (browser.indexOf("konqueror") != -1);

    if (document.getElementById && document.designMode && (isIE || isGecko || !isSafari || !isKonqueror)) {
        isEditable= true;
    }
    isLoaded = true;
}

function displayEditor(editor, html, width, height) {
    if(!isLoaded){
        initiateEditor();
    }
    edtr=editor;
    if(isEditable){
        document.writeln('<iframe id="' + editor + '" name="' + editor + '" width="' + width + 'px" height="' + height + 'px"></iframe>');
        document.writeln('<input type="hidden" id="hidden' + editor + '" name="hidden' + editor + '" value="">');
        document.getElementById('hidden' + editor).value = html;
        designer(editor, html);
    }else{
        document.writeln('<textarea name="' + editor + '" id="' + editor + '" style="width:395px; height:250px;">' + html + '</textarea>');
    }
}

function designer(editor, html) {
    edtr=editor;
    var mainContent= "<html id=" + editor + '><head><title>LachKaarten.com</title><link href="blank.css" rel="stylesheet" type="text/css" /></head><body>'+html+"</body></html>" ;
    var edit = document.getElementById(editor).contentWindow.document;
    edit.write(mainContent);
    edit.close();
    edit.designMode =  "On" ;
}

function execCmd(command, option) {
    var mainField = document.getElementById(edtr).contentWindow;
    try {
        mainField.focus();
        mainField.document.execCommand(command, false, option);
        mainField.focus();
    } catch (e) {
    //alert('deze functie werkt niet');
    }
}

function updateEditor(editor) {
    if (!isEditable) return;
    var hiddenField = document.getElementById('hidden' + editor);
    if (hiddenField.value == null) hiddenField.value = "";
    hiddenField.value = document.getElementById(editor).contentWindow.document.body.innerHTML;
}

function beforeSubmitEditor(field) {
    updateEditor(field);
    return true;
}
/* Buttons Bold - Italic - Underline */
$(document).ready(
    function(){
        $('img#imgbold').click(
            function(event){
                execCmd('bold',null);
            }
            );
        $('img#imgunderline').click(
            function(event){
                execCmd('underline',null);
            }
            );
        $('img#imgitalic').click(
            function(event){
                execCmd('italic',null);
            }
            );
        $('img.imgaction').hover(
            function(event){
                $(this).css('background-color','lime');
            },
            function(event){
                $(this).css('background-color','');
            }
            );
    }
    );
/* Add Smiley */
$(document).ready(
    function(){
        $('img.imgsmiley').click(
            function(event){
                execCmd('insertimage',$(this).attr('src'));
            }
            );
        $('img.imgsmiley').hover(
            function(event){
                $(this).css('background-color','lime');
            },
            function(event){
                $(this).css('background-color','');
            }

            );
    }
    );
