var dr = '/home/www/www.handmade.net/www/www';
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function InputActualDir(dir) 
{
    $('#UploaderDirMdl').val(dir);
    $('#nameDirUpl').html(dir);
    $('#destination_folder').val(dir.replace("Photos/", ""));
    $('#upload_url').val(dir.replace("Photos/", ""));

    $('#async2dir').val(dir);
    $('#hDirMulti').val(dir);

    $('#nameDirUpl_multi').html(dir);

    var d = dir.replace('Photos/', '');
    $('#upload_form').attr('action', '/_1javascript/Upload/UploadProcess.php?a=1&destination_folder=' + d + '&upload_url=' + d);

}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function SetHiddenValues(dir, trgElmVal, trgTypeVal) {
    $('#trgElm').val(trgElmVal); //-- napr. sLIstsImg
    $('#trgType').val(trgTypeVal);//-- napr. toInput

    $('#typefile').val(trgElmVal); //-- napr. sLIstsImg

    InputActualDir(dir);
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function checkNextMulti() {
    $.ajax({
        url: '/_1ajx/_ajxCheckNextMulti.aspx',
        type: 'post', async: false, dataType: 'html',
        data : {'dir': $('#async2dir').val()},
        success: function (data) { BckMulti(data); }
    });
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function BckMulti(val) {
    $('#hTypeMulti').val(val);
    $('#nextmulti').val(val);
    $('#frow').val(val);
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function BckLImg(val)
{
    $('#limg').val(val);
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function toSeoUrl(url) {
    if (url === undefined) return url;
    return url.toString()               // Convert to string
        .normalize('NFD')               // Change diacritics
        .replace(/[\u0300-\u036f]/g, '') // Remove illegal characters
        .replace(/\s+/g, '-')            // Change whitespace to dashes
        .toLowerCase()                  // Change to lowercase
        .replace(/&/g, '-and-')          // Replace ampersand
        .replace(/[^a-z0-9\-]/g, '')     // Remove anything that is not a letter, number or dash
        .replace(/-+/g, '-')             // Remove duplicate dashes
        .replace(/^-*/, '')              // Remove starting dashes
        .replace(/-*$/, '');             // Remove trailing dashes
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function TestTitleSlimUploader(action)
{
    var ret = true;
    var seoname = $('#seoname').val();
    if (action != 'load') {
        if (seoname == '') {
            seoname = $('#inputTitle').val();
            if (seoname == '') {               
                ModalWindow('danger', '', 'Chybí titulek k článku!', 'Zavřít', '', '', '', '', 'true');
                ret = false;
            }
            else {
                seoname = toSeoUrl(seoname);
                $('#seoname').val(seoname);
            }
        }
    }

    return ret;
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function LoadSlimUploaderMulti(action) {
    if (1 == 2 && gA)
        alert('LoadSlimUploader - action: ' + action);


    $('#upl_body_multi').html('');
    $.ajax({
        url: '/_ajx/_ajxModalMultiUploader.php',
        type: 'post', async: false, dataType: 'html',
        data: { 'id': '0' },
        success: function (data) {
            $('#upl_body_multi').html(data);
            if (action == 'open')
                MultiSlimInit('load');
        }
    });
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function LoadSlimUploader(action)
{
    if (1 == 2 && gA)
        alert('LoadSlimUploader - action: ' + action);

    if (!TestTitleSlimUploader(action))
        return;

    $('#upl_body').html('');
    $.ajax({
        url: '/_ajx/_ajxModalUploader.php',
        type: 'post', async: false, dataType: 'html',
        data: { 'id': '0' },
        success: function (data) {
            $('#upl_body').html(data);
            if(action == 'open')
                SlimInit('load');
        }
    });
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function Image2SQL(dir) {
    $.ajax({
        url: '/_ajx/_ajxImage2SQL.php',
        type: 'post', async: true, dataType: 'html',
        data: { 'dir': dir },
        success: function (data) {
        }
    });
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function MultiSlimInit(action) {

    $(document).ready(function () {

        var element = document.querySelector('#my-multicropper');
        var path = document.querySelector('#async2dir');
        var seoname = document.querySelector('#seoname');
        var type = document.querySelector('#typefile');
        var multi = document.querySelector('#nextmulti');
        var image = "";

        var w = 1050;
        var h = 950;
        var cw = 1050;
        var ch = 950;
        var fw = 1050;//-- forced size - vynucena
        var fh = 950;
        var ratio = '16:9';
        var typefile = '1';

        if (multi.value != '')
        {
            typefile = multi.value;
        }

        var sUrlService = '/_UPLOAD/server/sync.php?act=toTmp&dir=' + path.value + '&seo=' + seoname.value + '&multi=' + typefile;

        try {
            objSlim.destroy(element);
        } catch (e) { }



        if (1 == 2 && gA)
            alert('MultiSlimInit - action: ' + action);

        if (typeof (element) == 'undefined' || element == null)
            return;

        objSlim = new Slim(element, {
            load: image,
            ratio: ratio,
            maxFileSize: 10,
            minSize: {
                width: w,
                height: h,
            },
            forceSize: {
                width: fw,
                height: fh,
            },
            crop: {
                x: 0,
                y: 0,
                width: cw,
                height: ch
            },
            service: sUrlService,
            download: false,
            didUpload: function (data, ready, response) {
                console.log(response);
                dirPhotos = 'C:\\inetpub\\www.techarena.cz\\websolution\\TechArena\\';
                var fn = response.repath.replace(dirPhotos, '');
                $('#' + type.value).val('/' + fn);
                valueTestField('#' + type.value);
                $('#btnModalUplClose_multi').trigger('click');

                this.destroy(element);
                //Image2SQL('/' + path.value);

                showToast('success', 'Uložení dat', '', 'Fotografie byla uploadnuta.'); 

            },
            label: 'Vlož zde svůj soubor.',

            buttonConfirmLabel: 'Ok',
            meta: {
                pathUpload: '',//path.value,
                seoName: '',//seoname.value,
                type: typefile
            }
        });

    });
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function SlimInit(action) {

    $(document).ready(function () {

    $('#div_limg').css('display', 'none');
    document.querySelector('#uplact').value = action;
    var element = document.querySelector('#my-cropper');
    var path = document.querySelector('#async2dir');
    
    if (1==2 && gA)
        debugger;
        
    var type = document.querySelector('#typefile');
    var limg = document.querySelector('#limg');
    var image = '';
    var sSeo = '';
    if($('#seoname').length > 0)
      sSeo = $('#seoname').val();

    if(sSeo == '')
    {
        ModalWindow('danger', '', 'Před výběrem - uploadem fotografie vyplňte titulek článku!', 'Zavřít', '', '', '', 'false');
        return false;
    }
    else
    {
        $('#btn-modal-uploader').trigger('click');
    }

    
    if (1 == 2 && gA)
        alert('SlimInit - action: ' + action);

    try {
        objSlim.destroy(element);
    } catch (e) { }

    if (1==2 && gA)
        debugger;

    if (!TestTitleSlimUploader(action))
        return;

    var w = 480;
    var h = 480;
    var cw = 100;
    var ch = 100;
    var fw = 480;//-- forced size - vynucena
    var fh = 480;
    var ratio = '1:1';
    var typefile = '-undefined';
    
    if(type.value.indexOf('sImage') == 0)
    {
            typefile = '-prd' + type.value.replace('sImage', '');
            ratio = '1:1';
            w = 500;
            h = 500;
            cw = 500;
            ch = 500;
            fw = 500;
            fh = 500;
    }
    else
    {  
      switch (type.value) {
          case 'sListsImg':
              typefile = '-list';
              ratio = '1:1';
              w = 370;
              h = 370;
              cw = 370;
              ch = 370;
              fw = 370;
              fh = 370;
              break;
  
          case 'sTitleImg':
              typefile = '-title';
              ratio = 'free';
              w = 800;
              h = 450;
              cw = 800;
              ch = 450;
              fw = 800;
              fh = 450;
              break;
  
          case 'sBGImg':
              typefile = '-blur';
              ratio = '16:9';
              w = 1920;
              h = 1080;
              cw = 1920;
              ch = 1080;
              fw = 1920;
              fh = 1080;
              break;
  
          case 'sFMImage':
              if (limg.value == '')
                  typefile = '-limg_01';
              else {
                  typefile = limg.value;
              }
              ratio = '';
              w = 150;
              h = 150;
              cw = 1050;
              ch = 950;
              fw = 1050;
              fh = 950;
              //$('#div_limg').css('display', 'block');
              break;
      }
    }
    if (1 == 2 && gA){
        alert('type.value: ' + type.value);
        alert('typefile: ' + typefile);
    }

    if (type.value.indexOf("sImg_") == 0) {

        typefile = '-slide-' + type.value.replace('sImg_', '');
        ratio = '16:9';
        w = 1920;
        h = 1080;
        cw = 1920;
        ch = 1080;
        fw = 1920;
        fh = 1080;

        var s = ($('#sNameSlider').length > 0 ? $('#sNameSlider').val() : '');

        //-- toSeoUrl
        if (s != '')
        {
            sSeo = toSeoUrl(s);
        }
        else {

            if (sSeo == '') {

                s = ($('#sNameItem_' + type.value.replace('sImg_', '')).length > 0 ? $('#sNameItem_' + type.value.replace('sImg_', '')).val() : '');
                if (s != '')
                {

                    sSeo = toSeoUrl(s);
                    
                    showToast('danger', 'POZOR', '', 'Chybí název slideru (carouselu)!<br/> Název souboru jednotlivých sliderů bude vytvořen z názvu slideru.'); 
                }
            }
            else {

                showToast('danger', 'POZOR', '', 'Chybí název slideru (carouselu)!<br/> Název souboru jednotlivých sliderů bude vytvořen z názvu článku.');
            }
        }
    }


    if (typefile != '-undefined') {
        
        image = document.querySelector('#' + type.value).value;
        image = image.replace('/Photos', 'Photos');

        var sUrlService = '/_UPLOAD/server/async.php?dir=' + path.value + '&seo=' + sSeo + '&type=' + typefile;

        //--  || typefile.indexOf("-blur") > -1 || typefile.indexOf("-slide") > -1

        if (1 == 2 && gA) {
            alert(
                    'type.value: ' + type.value +
                    '\ntypefile: ' + typefile +
                    '\nsUrlService: ' + sUrlService +
                    '\nSEO: ' + sSeo +
                    '\nimage: ' + image +
                    '\nwidth: ' + w +
                    '\nheight: ' + h

                );

        }


        if (typefile.indexOf("-limg_") > -1)
        {

            objSlim = new Slim(element, {
                load: image,
                ratio: 'free',
                maxFileSize: 2,
                minSize: {
                    width: w,
                    height: h,
                },

                service: sUrlService,
                download: false,
                didUpload: function (data, ready, response) {
                    console.log(response);
                    console.log(response.status);
                    dirPhotos = '/home/www/www.handmade.net/www/www';
                    var fn = '';
                    
                    if(response.repath != null)
                      fn = response.repath.replace(dirPhotos, '');
                    else
                      fn = response.path.replace(dirPhotos, '');
                      
                    $('#' + type.value).val('/' + fn);
                    valueTestField('#' + type.value);
                    $('#btnModalUplClose').trigger('click');

                    
                    if (response.status == 'success'){
                        Image2SQL('/' + path.value);
                        if ($('#selectIP').length > 0) {
                            var prd = $('#selectIP').val();
                            if (prd != '') {
                                saveImageToProduct('/' + fn, prd);
                            }
                        }
                        //$('#upl_body_new').html('');
                    }
                    this.destroy(element);
                    
                    showToast('success', 'Uložení dat', '', 'Fotografie byla uploadnuta.');  
                    
                },
                didSave: function (data, ready, response) {
                    if (1 == 1 && gA)
                        alert('didSave');
                },
                didCancel: function (data, ready, response) {
                    if (1 == 1 && gA)
                        alert('didCancel');
                },

                label: 'Vlož zde svůj soubor.',

                buttonConfirmLabel: 'Ok',
                meta: {
                    pathUpload: path.value,
                    seoName: sSeo,
                    type: typefile,
                    limg: limg.value
                }
            });
        }
        else
        {

            objSlim = new Slim(element, {
                load: image,
                ratio: ratio,
                maxFileSize: 10,
                minSize: {
                    width: w,
                    height: h,
                },
                forceSize: {
                    width: fw,
                    height: fh,
                },
                crop: {
                    x: 0,
                    y: 0,
                    width: cw,
                    height: ch
                },
                service: sUrlService,
                download: false,
                didUpload: function (data, ready, response) {
                    console.log(response);
                    console.log(response.status);
                    //debugger;
                    dirPhotos = '/home/www/www.handmade.net/www/www';
                    var fn = '';
                    if(response.repath != null)
                      fn = response.repath.replace(dirPhotos, '');
                    else
                      fn = response.path.replace(dirPhotos, '');
                    $('#' + type.value).val('' + fn);
                    valueTestField('#' + type.value);
                    $('.btnModalUplClose').trigger('click');

                    //alert(response.status);
                    
                    /*
                    if (response.status == 'success') {
                        Image2SQL('/' + path.value);
                        if ($('#selectIP').length > 0) {
                            var prd = $('#selectIP').val();
                            if (prd != '') {
                                saveImageToProduct('/' + fn, prd);
                            }
                        }
                        //$('#upl_body_new').html('');
                    } */

                    this.destroy(element);
                    
                    showToast('success', 'Uložení dat', '', 'Fotografie byla uploadnuta.'); 
                },
                label: 'Vlož zde svůj soubor.',

                buttonConfirmLabel: 'Ok',
                meta: {
                    pathUpload: path.value,
                    seoName: sSeo,
                    type: typefile,
                    limg: limg.value
                }
            });
        }
    }
    });
}

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function SlimInitBanner(action) {

    $(document).ready(function () 
    {

      $('#div_limg').css('display', 'none');
      document.querySelector('#uplact').value = action;
      var element = document.querySelector('#my-cropper');
      var path = document.querySelector('#async2dir');
      
      if (1==2 && gA)
          debugger;
          
      var type = document.querySelector('#typefile');
      var limg = '';
      var image = '';
      var sSeo = '';

      if (1 == 2 && gA)
          alert('SlimInitBanner - action: ' + action);
  
      try {
          objSlim.destroy(element);
      } catch (e) { }
  
      if (1==2 && gA)
          debugger;
  
      var w = 480;
      var h = 480;
      var cw = 100;
      var ch = 100;
      var fw = 480;//-- forced size - vynucena
      var fh = 480;
      var ratio = '1:1';
      var typefile = '-undefined';
      
      var nID = type.value.split('_')[1];
      var valType = $('#selAdvType_' + nID).val();
      
      if(valType == '0')
      {
          ModalWindow('danger', '', 'Před výběrem - uploadem banneru zvolte typ banneru!', 'Zavřít', '', '', '', 'false');
          return false;
      }
      else
      {
          $('#btn-modal-uploader').trigger('click');
      }
    
      if (type.value.indexOf("banner_") == 0) {
        switch(valType)
        {
          case '1': // -- 728x90
              typefile = '-banner';
              ratio = 'free';
              w = 728;  //-- minimalni sirka
              h = 90;  //-- minimalni vyska
              fw = 728; //-- vynucena sirka vystupu
              fh = 90; //-- vynucena vyska vystupu
              cw = 728; //-- sirka oriznuti
              ch = 90; //-- vyska oriznuti
              break;
         case '2': // -- 160x600
              typefile = '-banner';ratio = 'free'; w = 160; h = 600; fw = 160; fh = 600; cw = 160; ch = 600; 
              break;
         case '3': // -- 320x100
              typefile = '-banner';ratio = 'free'; w = 320; h = 100; fw = 320; fh = 100; cw = 320; ch = 100; 
              break;
         case '4': // -- 300x600
              typefile = '-banner';ratio = 'free'; w = 300; h = 600; fw = 300; fh = 600; cw = 300; ch = 600; 
              break;
          case '5': // -- 300x300
              typefile = '-banner';
              ratio = '1:1';
              w = 300;  //-- minimalni sirka
              h = 300;  //-- minimalni vyska
              fw = 300; //-- vynucena sirka vystupu
              fh = 300; //-- vynucena vyska vystupu
              cw = 300; //-- sirka oriznuti
              ch = 300; //-- vyska oriznuti
              break;
         case '6': // -- 300x250
              typefile = '-banner';ratio = 'free'; w = 300; h = 250; fw = 300; fh = 250; cw = 300; ch = 250; 
              break;
         case '7': // -- 970x310
              typefile = '-banner';ratio = 'free'; w = 970; h = 310; fw = 970; fh = 310; cw = 970; ch = 310; 
              break;
         case '8': // -- 480x300
              typefile = '-banner';ratio = 'free'; w = 480; h = 300; fw = 480; fh = 300; cw = 480; ch = 300; 
              break;
         case '9': // -- 480x480
              typefile = '-banner';ratio = '1:1'; w = 480; h = 480; fw = 480; fh = 480; cw = 480; ch = 480; 
              break;
        }
      }
  
      if (1 == 2 && gA){
          alert('type.value: ' + type.value);
          alert('typefile: ' + typefile);
          alert('valType: ' + valType);
      }
  
  
      if (typefile != '-undefined') {
          
          image = document.querySelector('#' + type.value).value;
          image = image.replace('/Photos', 'Photos');
  
          var sUrlService = '/_UPLOAD/server/async.php?dir=' + path.value + '&seo=' + sSeo + '&type=' + typefile;
  
          //--  || typefile.indexOf("-blur") > -1 || typefile.indexOf("-slide") > -1
  
          if (1 == 2 && gA) {
              alert(
                      'type.value: ' + type.value +
                      '\ntypefile: ' + typefile +
                      '\nsUrlService: ' + sUrlService +
                      '\nSEO: ' + sSeo +
                      '\nimage: ' + image +
                      '\nwidth: ' + w +
                      '\nheight: ' + h
  
                  );
  
          }
  
  
          
          objSlim = new Slim(element, {
              load: image,
              ratio: ratio,
              maxFileSize: 10,
              minSize: {
                  width: w,
                  height: h,
              },
              forceSize: {
                  width: fw,
                  height: fh,
              },
              crop: {
                  x: 0,
                  y: 0,
                  width: cw,
                  height: ch
              },
              service: sUrlService,
              download: false,
              didUpload: function (data, ready, response) {
                  console.log(response);
                  console.log(response.status);
                  
                  //debugger;
                  
                  dirPhotos = '/home/www/www.handmade.net/www/www';
                  var fn = '';
                  if(response.repath != null && response.repath != '')
                    fn = response.repath.replace(dirPhotos, '');
                  else
                    fn = response.path.replace(dirPhotos, '');

                  $('#' + type.value).val('' + fn);
                  $('#aBanner_' + nID).attr('href', fn);
                  valueTestField('#' + type.value);
                  $('.btnModalUplClose').trigger('click');
  
                  //alert(response.status);
                  
                  /*
                  if (response.status == 'success') {
                      Image2SQL('/' + path.value);
                      if ($('#selectIP').length > 0) {
                          var prd = $('#selectIP').val();
                          if (prd != '') {
                              saveImageToProduct('/' + fn, prd);
                          }
                      }
                      //$('#upl_body_new').html('');
                  } */
  
                  this.destroy(element);
                  
                  showToast('success', 'Uložení dat', '', 'Banner byl uploadnut.'); 
              },
              label: 'Vlož zde svůj banner.',
  
              buttonConfirmLabel: 'Ok',
              meta: {
                  pathUpload: path.value,
                  seoName: sSeo,
                  type: typefile,
                  limg: limg.value
              }
          });
      }
    });
}