



    function  start_transform_from_html() {
         start_full_loader(true);
         save_backup(nodeToString($('pageparts div')[0]).replace(/\r?\n|\r/g,''),start_transform_from_html_callback);

    }

    function start_transform_from_html_callback(acc){
      if(acc){
         var o = $('pageparts div')[0];
          $.post(xml_xsl, {
              req:"html",
              file1:html_xsl1,
              file2:html_xsl2,
              file3:html_xsl3,
              html:nodeToString(o).replace(/\r?\n|\r/g,'').replace(/&nbsp;/g,'&amp;nbsp;'),
          }).done(function( data ) {
              var data_ = $(data).clone();
              var error = $(data_[0]);
              if(error.is('error')){
                 switch(error.attr('type')) {
                    case "warning":
                       set_confirm_dialog(error.clone(),reload_record,500,600,'Zavřít');
                       setTimeout(function(){
                          set_backup_status();
                          submit_xml(nodeToString(data_[1]));
                       },700);
                       start_full_loader(false);
                       break;
                    case "fatal":
                       set_confirm_dialog(error.clone(),undefined,500,600,'Zavřít');
                       start_full_loader(false);
                       break;
                 }
              }else{
                setTimeout(function(){
                    submit_xml(data);
                    set_backup_status();
                    cache.reaload_page_submit = true;
                },500);
              }
          }).fail(function(data){
              set_error_dialog(error_transformation,500,150,true);
          });
      }else{
          start_full_loader(false);
      }
   }

    function save_txt(xml){
      $.post("/js/gorazd_editor/save_txt_file.php", {
         "requestType":"submit_gorazdXML",recID:cache.load_record_id,"gorazdXML":xml
      }).done(function( data ) {

      });
    }


   function  start_transform_from_html_clone() {
       var o = $('pageparts div')[0];
        $.post(xml_xsl, {
            req:"html",
            file1:html_xsl1,
            file2:html_xsl2,
            file3:html_xsl3,
            html:nodeToString(o).replace(/\r?\n|\r/g,'').replace(/&nbsp;/g,'&amp;nbsp;'),
        }).done(function( data ) {
            var data_ = $(data).clone();
            var error = $(data_[0]);
            if(error.is('error')){
               switch(error.attr('type')) {
                  case "warning":
                     set_confirm_dialog(error.clone(),clone_record_warn_accept,500,600,'Zavřít');
                     cache.clone_data_send = nodeToString(data_[1]);
                     break;
                  case "fatal":
                     set_confirm_dialog(error.clone(),undefined,500,600,'Zavřít');
                     break;
               }
            }else{
               clone(data);
            }
        }).fail(function(data){
            set_error_dialog(error_transformation,500,150,true);
        });
    }
    function clone_record_warn_accept() {
        clone(cache.clone_data_send);
    }

    function reload_record() {
         go_to_record(cache.load_record_id);
    }

   function html_part(part) {
      var o = $('pageparts div')[0];
      switch (part) {
         case 1:
         case 2:
         case 3:
             $.post(xml_xsl, {
               req:"part"+part,
               file1:html_xsl1,
               file2:html_xsl2,
               file3:html_xsl3,
               html:nodeToString(o).replace(/\r?\n|\r/g,'').replace(/&nbsp;/g,'&amp;nbsp;'),
            }).done(function( data ) {
               var blob = new Blob([data], {type: "text/plain;charset=utf-8"});
               saveAs(blob,"xsl_part_"+part+".xml");
            }).fail(function(data){
               set_error_dialog(error_transformation,500,150,true);
            });
            break;
         case "html":
            show_hint_msg("Stahuji HTML",false);
            var blob = new Blob([nodeToString($(pageparts_div)[0])], {type: "text/plain;charset=utf-8"});
            saveAs(blob,"editor_html.txt");
            break;
      }


   }

   function  show_source() {
      var o = $('pageparts div')[0];
      $.post(xml_xsl, {
            req:"html",
            file1:html_xsl1,
            file2:html_xsl2,
            file3:html_xsl3,
            html:nodeToString(o).replace(/&nbsp;/g,'&amp;nbsp;').replace(/\r?\n|\r/g,''),
        }).done(function( data ) {
            var data_ = $(data);
            $.post(xml_xsl, {
                req:"show_source",
                file1:prev_xsl,

            }).done(function(data2) {
                switch(get_url_params().dict){
                  case "SJS":
                      break;
                  case "MS":
                  case "RSI":
                     $('pagepreview').html(data2);
                      break;
                  default:
                     $('sourcepage').html(data2);
                     break;
               }
            });
        }).fail(function(data){
            set_error_dialog(error_transformation,500,150, true);
        });
    }

     function  start_transform_for_rep(html,all = false) {
        if (all) {
         alert();
           $.post(xml_xsl, {
                req:"html",
                file1:html_xsl1,
                file2:html_xsl2,
                file3:html_xsl3,
                html:nodeToString(html).replace(/&nbsp;/g,'&amp;nbsp;'),
            }).done(function( data ) {
               alert(data)
            });
        }else{
            $.post(xml_xsl, {
                req:"part_html",
                file1:html_xsl1,
                file2:html_xsl2,
                file3:html_xsl3,
                html:html,
            }).done(function( data ) {
                alert(data)
            });
        }

     }

    function nodeToString ( node ) {
        var tmpNode = document.createElement( "div" );
        tmpNode.appendChild( node.cloneNode( true ) );
        var str = tmpNode.innerHTML;
        tmpNode = node = null;
        return str;
    }

    function strip(html){
        var tmp = document.createElement("DIV");
        tmp.innerHTML = html;
        return tmp.textContent || tmp.innerText || "";
    }

    function displayResult(in_xml,in_xsl){
        var params = get_url_params();
        if(params.xmlVers!=undefined){
           in_xml = cache.full_record_json.gorazd_paths[params.xmlVers-1];
           cache.loaded_version = params.xmlVers;
        }else{
          cache.loaded_version = cache.full_record_json.gorazd_paths.length;
        }
        $.post(xml_xsl, {
            req:"files",
            file1:in_xml,
            file2:in_xsl
        }).done(function( data ) {
            if (data == "null" || data == null) {

               setTimeout(function(){  start_full_loader(false);  },500);
               set_confirm_dialog(error_when_loading_record,reaload_page,500,200,"Zkusit znovu",undefined,undefined,true);
               return false;
            }
            insert_html_start(data);

        }).fail(function(data){
            set_error_dialog(error_transformation,500,150,true);
        });
    }

    function insert_html_start(data) {
        if (cache.merge_load_temp_record != undefined) {
            if(cache.full_record_json.recID < cache.merge_load_temp_record.recID){
               show_hint_msg("Vkládám za heslo   id: "+cache.merge_load_temp_record.recID,true);
               $(pageparts).append(data);
            }else{
               show_hint_msg("Vkládám před heslo   id: "+cache.merge_load_temp_record.recID,true);
               $(pageparts).prepend(data);
            }
        }else{
            $(pageparts).append(data);
        }
        $('#arround_page').width(cache.img_prev_max_width);
        var imgs = cache.imgs;
        for(var i in imgs){
            if (cache.imgs_offset[imgs[i].source_id] == undefined) {
                cache.img_ratio[imgs[i].source_id] = $('#'+imgs[i].source_id+"[width]").attr('width') / imgs[i].width ;
                if (cache.first_loaded_xml == undefined) {
                    cache.imgs_offset[imgs[i].source_id] = $('canvas.'+imgs[i].id).offset().top-$(top_toolbar).height();
                }else{
                    cache.imgs_offset[imgs[i].source_id] = (imgs[i-1] == undefined)? 0 : imgs[i-1].height;
                }
            }
        }
        setTimeout(function(){  start_full_loader(false);  },1000);
        if (cache.first_loaded_xml) {
            meta_reorder();
        }
        _init_start();
    }
