var Main = {
  filter:function(pattern) {
    $(".search").parent().parent().hide()
    $(".search:[value*='"+pattern+"']").parent().parent().show();
   
  },

  rewriteRichtext:function () {
    for ( var ins in CKEDITOR.instances ) {
      if ( $('#'+ins+'_hidden') ) {
        $('#'+ins+'_hidden').val(CKEDITOR.instances[ins].getData());
      }
      delete CKEDITOR.instances[ins];
      
    }
  },
  
  deleteRichtext:function () {
    for ( var ins in CKEDITOR.instances ) {
      
      delete CKEDITOR.instances[ins];

    }
  }


  
}

var Tree = {
  toggle:function(className) {
    $('dl.'+className).toggle();
    if ( !$('dl.'+className).is(':hidden') ) {
      $('img.'+className).attr('src', '/images/icons/22/down.png');
    }
    else {
      $('img.'+className).attr('src', '/images/icons/22/right.png');
    }
    
  }
}

var Passwd = {
  confirmCheck:function() {
    if ( $('#new_password').val() == $('#confirm_password').val() ) {
      $('#confirm_password_check').css('color', 'green');
      $('#confirm_password_check').html('OK');
      return true;
    }
    else {
      $('#confirm_password_check').css('color', 'red');
      $('#confirm_password_check').html('Neshoduje se');
      return false;
    }
  },

  strengthCheck:function() {
    if ( $('#new_password').val().length >= 5 ) {
      $('#strength_password_check').css('color', 'green');
      $('#strength_password_check').html('OK');
      return true;
    }
    else {
      $('#strength_password_check').css('color', 'red');
      $('#strength_password_check').html('Slabé heslo');
      return false;
    }
  },

  enable:function(id) {
    strchck = Passwd.strengthCheck();
    cnfrmchck = Passwd.confirmCheck();
    if ( strchck && cnfrmchck ) {
      $('#submit').removeAttr("disabled");
    }
    else {
      $('#submit').attr("disabled","disabled");
    }
  }
}

var Ajax = {

  deleteUser:function(id) {
    $.ajax({
       type: 'GET',
       cache:false,
       url: '/frontend_dev.php/usermanagement/deleteuser?id='+id,
       error: function(){alert('Error')},
       success: function(html){$('#user_' + id).hide('slow');}
     });
  },
  
  setHtml:function( divId, scriptName ) {
    $.ajax({
       type: 'GET',
       cache:false,
       url: scriptName,
       error: function(){alert('Error')},
       success: function(html){$('#' + divId).html(html);}
     });
  },

  setHtmlSlow:function( divId, scriptName ) {
    $('#' + divId).slideUp('slow');
    $.ajax({
       type: 'GET',
       cache:false,
       url: scriptName,
       error: function(){alert('Error');},
       success: function(html){$('#' + divId).html(html);}
     });
     $('#' + divId).slideDown('slow');
  },

  addHtml:function( divId, scriptName ) {
    $.ajax({
       type: 'GET',
       cache:false,
       url: scriptName,
       error: function(){alert('Error')},
       success: function(html){$('#' + divId).append(html);}
     });
  },

  newPassword:function( idUser ) {
    
    $.ajax({
       type: 'GET',
       url: '/usermanagement/generatepassword?iduser=' + idUser,
       error: function(){alert('Error')},
       success: function(psswd){
         if ( window.confirm('Nové heslo je: ' + psswd + "\nZaslat uživateli emailem?") ) {
           $.ajax({
             type: 'GET',
             url: 'usermanagement/sendpassword?iduser=' + idUser + '&psswd=' + psswd,
             error: function(){alert('Error: email se nezda\u0159ilo odeslat');},
             success: function(newpsswd){alert('Email úsp\u011bšně odeslán');}
           });
         }
       }
     });
  },

  pageToExternalLink:function( idpage ) {
    if ( window.confirm("Stránka k této položce menu bude smazána") ) {
      $.ajax ({
        type: 'Get',
        url: '/amdmin/deletepage?idpage=' + idpage,
        error: alert('Nezdařilo se smazání stránky')
      });
    }
  },

  setSortableMenu:function(id) {
    $("#"+id).sortable({
      delay: 300,
      stop: function(event, ui) {
        arr = $("#"+id).sortable('toArray');
        for ( var i in arr ) {
          $.ajax ({
            type: 'Get',
            url: '/frontend_dev.php/admin/setmenuposition?idmenu='+arr[i]+'&position='+(i)
          });
        }
      }

    });
  },

  setSortableComponent:function(id) {
    $("#"+id).sortable({
      delay: 300,
      stop: function(event, ui) {
        var arr = $("#"+id).sortable('toArray');
        for ( var i in arr ) {
          $.ajax ({
            type: 'Get',
            url: '/frontend_dev.php/admin/setcomponentposition?params='+arr[i]+'&position='+(i)
          });
        }
      }

    });
  },

  setSortableComponentItem:function(id) {
    $("#"+id).sortable({
      delay: 300,
      stop: function(event, ui) {
        var arr = $("#"+id).sortable('toArray');
        for ( var i in arr ) {
          $.ajax ({
            type: 'Get',
            url: '/frontend_dev.php/admin/setcomponentitemposition?params='+arr[i]+'&position='+(i)
          });
        }
      }

    });
  },

  setForm:function(selector, divId, scriptName) {
    var options = {
        //target:        '#output2'//,   // target element(s) to be updated with server response
        //beforeSubmit: ,  // pre-submit callback
        success: function(){Ajax.setHtmlSlow(divId, scriptName);}

        // other available options:
        //url:       url         // override for form's 'action' attribute
        //type:      type        // 'get' or 'post', override for form's 'method' attribute
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type)
        //clearForm: true        // clear all form fields after successful submit
        //resetForm: true        // reset the form after successful submit

        // $.ajax options can be used here too, for example:
        //timeout:   3000
    };

    // bind to the form's submit event
    $(selector).submit(function() {
        // inside event callbacks 'this' is the DOM element so we first
        // wrap it in a jQuery object and then invoke ajaxSubmit
        $(this).ajaxSubmit(options);

        // !!! Important !!!
        // always return false to prevent standard browser submit and page navigation
        return false;
    });
  }
}
function showRequest(formData, jqForm, options) {
    // formData is an array; here we use $.param to convert it to a string to display it
    // but the form plugin does this for you automatically when it submits the data
    var queryString = $.param(formData);

    // jqForm is a jQuery object encapsulating the form element.  To access the
    // DOM element for the form do this:
    // var formElement = jqForm[0];

    alert('About to submit: \n\n' + queryString);

    // here we could return false to prevent the form from being submitted;
    // returning anything other than false will allow the form submit to continue
    return true;
} 

