var ckeditor_config = new Object();
ckeditor_config.toolbar_LHL =
  [
   ['Source','-','Preview'],
   ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'],
   ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
   '/',
   ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
   ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote','CreateDiv'],
   ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
   ['Link','Unlink','Anchor'],
   ['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak','Iframe'],
   '/',
   ['Styles','Format','Font','FontSize'],
   ['TextColor','BGColor'],
   ['Maximize', 'ShowBlocks','-','About']
   ];
ckeditor_config.toolbar = 'LHL';


function log_error(xhr, status, error) {
  console.log('Error (' + status + '): ' + error);
  console.log(xhr);
}

jQuery.fn.center = function() {
  this.css("position","absolute");
  this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
  this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
  return this;
}

function message(text) {
  $('#message_content').append(text);
  $('#messages').center().fadeIn();
}

function clear_messages() {
  $('#messages').hide();
  $('#message_content').html('');
}

function basename(path, suffix) {
  // Returns the filename component of the path  
  // 
  // version: 1103.1210
  // discuss at: http://phpjs.org/functions/basename
  // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +   improved by: Ash Searle (http://hexmen.com/blog/)
  // +   improved by: Lincoln Ramsay
  // +   improved by: djmix
  // *     example 1: basename('/www/site/home.htm', '.htm');
  // *     returns 1: 'home'
  // *     example 2: basename('ecra.php?p=1');
  // *     returns 2: 'ecra.php?p=1'
  var b = path.replace(/^.*[\/\\]/g, '');
 
  if (typeof(suffix) == 'string' && b.substr(b.length - suffix.length) == suffix) {
    b = b.substr(0, b.length - suffix.length);
  }
 
  return b;
}

function format_file_size(size) {

  if (size < 1024)
    return size + ' B';

  if (size > 1024)
    return parseInt( size / 1024 ) + ' KB';

}

function fill_form_fields(data) {

  for (var i in data) {
    var field = $('input[name="' + i + '"]');

    if (field.attr('overwrite') == 0)
      continue;
    if (field.length)
      field.val(data[i]);

    var textarea = $('textarea[name="' + i + '"]');
    if (textarea.length)
      textarea.val(data[i]);

    var select = $('select[name="' + i + '"]');
    if (select.length) {
      select.val(data[i]);
      select.change();
    }
  }

}

$.extend($.expr[':'], {
  text_gt: function(a, i, m) {
             var text = $(a).text();
             var compare = m[3];
             return text > compare;
           },
  text_lt: function(a, i, m) {
             var text = $(a).text();
             var compare = m[3];
             return text < compare;
           },
  text_range: function(a, i, m) {
             var text = $(a).text();
             data = m[3].split(',');
             var lower = data[0];
             var upper = data[1];
             return text >= lower && text <= upper;          
           }
});

function format_date(date) {

  if (date == '')
    return '';

  var date = Date.parse(date);

  if (date)
    return date.toString('yyyy-MM-dd');
  else {
    message('Invalid date.');
    return '';
  }
}

function rawurlencode(str) {
  str = (str + '').toString();
 
  // Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
  // PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
  return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A');
}

