$(document).ready(function(){

    //Bubbles-------------------------------------------------------------------
    //Loadings scripts via AJAX
    //http://brightbyte.de/page/Loading_script_tags_via_AJAX
    runScripts = function (e) {
        if (e.nodeType != 1) return; //if it's not an element node, return

        if (e.tagName.toLowerCase() == 'script') {
            alert('Spouštím skript');
            eval(e.text); //run the script
        } else {
            var n = e.firstChild;
            while ( n ) {
                if ( n.nodeType == 1 ) runScripts( n ); //if it's an element node, recurse
                n = n.nextSibling;
            }
        }
    }

    //After loading bubbles
    callbackAjax = function()
    {
        //runScripts($('#accelerator_bubbles'));
        reinitialiseScrollPane();
    }

    //Chrome is not compatabile with jScrollPane
    if(!chrome){
        //Initialize scrollpanes
        $('#pane1').jScrollPane({showArrows:true, scrollbarWidth: 11, arrowSize: 8});
        $('#pane2').jScrollPane({showArrows:true, scrollbarWidth: 11, arrowSize: 8});

        //For ajax calling
        reinitialiseScrollPane = function()
        {
            $('#pane1').jScrollPane({showArrows:true, scrollbarWidth: 11, arrowSize: 8});
            $('#pane2').jScrollPane({showArrows:true, scrollbarWidth: 11, arrowSize: 8});
        }
    }else{
        //For chrome only empty function
        reinitialiseScrollPane = function()
        {
        }
    }

    //Changing bubbles - small/big
    changeBubbles = function(){
        var b = $('#bubbles-container-type');
        //display small bubbles
        if(b.getValue() == 'big'){
            if(http_host=="blog.babyonline.cz"){
                file = path_web_blogy;
            }else{
                file = path_web;
            }
            file += 'application/bubbles-small.php';
            b.setValue('small');
        //display big bubbles
        }else{
            if(http_host=="blog.babyonline.cz"){
                file = path_web_blogy;
            }else{
                file = path_web;
            }
            file += 'application/bubbles-big.php';
            b.setValue('big');
        }
        $('#bubbles-container').load(file, '', callbackAjax);
    }

    //discussion textarea ------------------------------------------------------
    $('#text').keydown(function(){text_preview();})
              .keyup(function(){text_preview();})
              .change(function(){text_preview();});
    text_preview= function(){
        var text = $('#text').val();
        text = text.replace(/&s([0-9]+);/gi, '<img src="http://www.babyonline.cz/application/libraries/ForumBaby/smiles/'+"$1"+'.gif" />');
        $('#text_preview').html(text);
        return true;
    }

    //Lightbox -----------------------------------------------------------------
    //$("a[href^='http://data'][href$='jpg']").lightbox({
    $("a[href$='jpg']").lightbox({
        loopImages: true,
        fileLoadingImage : 'media/images/loading-transparent.gif',
        fileBottomNavCloseImage : 'plugins/jquery-lightbox/images/closelabel.gif',
        imageClickClose: false,
        strings : {
                    //help: ' \u2190 / P - previous image\u00a0\u00a0\u00a0\u00a0\u2192 / N - next image\u00a0\u00a0\u00a0\u00a0ESC / X - close image gallery',
                    prevLinkTitle: 'předchozí',
                    nextLinkTitle: 'další',
                    prevLinkText:  '&laquo; Přechozí',
                    nextLinkText:  'Další &raquo;',
                    closeTitle: 'Zavřít',
                    image: 'Obrázek ',
                    of: ' / '
                }
    });
    $("a[href$='png']").lightbox({
        loopImages: true,
        fileLoadingImage : 'media/images/loading-transparent.gif',
        fileBottomNavCloseImage : 'plugins/jquery-lightbox/images/closelabel.gif',
        imageClickClose: false,
        strings : {
                    //help: ' \u2190 / P - previous image\u00a0\u00a0\u00a0\u00a0\u2192 / N - next image\u00a0\u00a0\u00a0\u00a0ESC / X - close image gallery',
                    prevLinkTitle: 'předchozí',
                    nextLinkTitle: 'další',
                    prevLinkText:  '&laquo; Přechozí',
                    nextLinkText:  'Další &raquo;',
                    closeTitle: 'Zavřít',
                    image: 'Obrázek ',
                    of: ' / '
                }
    });
    //$("a[rel]").lightBox();

    //animated top box ---------------------------------------------------------
    $('#animation').cycle({
        fx:     'fade',
        timeout: 10000,
        delay:  -2000,
        cleartype:  0
    });

    //TinyMCE ------------------------------------------------------------------
   $('.wysiwyg').tinymce({
    theme:'advanced',
    language : "cs",
    elements : "text",
    extended_valid_elements : "a[href|target|name]",
    plugins:'table',//,smiles',//,contextmenu,save,print,advimage,advlink",
    theme_advanced_buttons2 : "undo,redo,separator,removeformat,separator,bullist,numlist,separator,link,unlink,separator,image,separator,sub,sup,separator,charmap,separator,hr",
    theme_advanced_buttons3 : "tablecontrols",
    //theme_advanced_buttons4 : "s01,s02,s03,s15,s05,s04,s17,s07,s08,s06,s11,s16",
    //theme_advanced_buttons5 : "s12,s19,s13,s14,s09,s10,s18",
    theme_advanced_styles : "Růžová=color_pink;Modrá=color_blue", // Theme specific setting CSS classes
    theme_advanced_blockformats : "p,h1,h2,h3",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    onchange_callback : "setChanged",
    content_css : path_web_blogy+'media/css/style.css',
    external_image_list_url : path_web+'application/libraries/Blogy/image_list.php',
    relative_urls : false,
    debug : false
   });
   $('.wysiwyg2').tinymce({
    theme:'advanced',
    language : "cs",
    elements : "text",
    extended_valid_elements : "a[href|target|name]",
    plugins:'table',//,contextmenu,save,print,advimage,advlink",
    theme_advanced_buttons2 : "undo,redo,separator,removeformat,separator,bullist,numlist,separator,image,separator,sub,sup,separator,charmap,separator,hr",
    theme_advanced_buttons3 : "tablecontrols",
    theme_advanced_styles : "Růžová=color_pink;Modrá=color_blue", // Theme specific setting CSS classes
    theme_advanced_blockformats : "p,h1,h2,h3",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    onchange_callback : "setChanged",
    content_css : path_web_blogy+'media/css/style.css',
    external_image_list_url : path_web+'application/libraries/Blogy/image_list.php',
    relative_urls : false,
    debug : false
   });
   $('.smiles').tinymce({
    theme:'advanced',
    language : "cs",
    elements : "text",
    extended_valid_elements : "img[src|alt]",
    plugins:'smiles',
    theme_advanced_buttons1 : "s01,s02,s03,s15,s05,s04,s17,s07,s08,s06,s11,s16",
    theme_advanced_buttons2 : "s12,s19,s13,s14,s09,s10,s18",
	theme_advanced_buttons3 : "",
    theme_advanced_toolbar_location : "bottom",
    theme_advanced_toolbar_align : "center",
    onchange_callback : "setChanged",
    debug : false
   });

   /*({
        script_url : 'plugins/tinymce/jscripts/tiny_mce/tiny_mce.js',
        theme : "advanced",
        mode : "textareas",
        save_callback : "customSave",
        //theme_advanced_buttons2 : "undo,redo,separator,bullist,numlist,separator,link,unlink,anchor,separator,image,separator,sub,sup,separator,charmap",
        //theme_advanced_buttons3 : "outdent,indent,separator,hr,removeformat,visualaid,separator,cleanup,newdocument",
        //theme_advanced_buttons3 : "",//,separator,cleanup",
        //invalid_elements : "a",
        //execcommand_callback : "myCustomExecCommandHandler",
    });*/

});
