/*
  DC-Thera Directory - Base functions
*/
$(document).ready(function () {
  
  $.blockUI.defaults.fadeIn = false;
  $.blockUI.defaults.fadeOut = false;
  $.blockUI.defaults.constrainTabKey = true;
  $.blockUI.defaults.css.top = '10%';
  $.blockUI.defaults.css.color = '#000';
  $.blockUI.defaults.css.border = '';
  $.blockUI.defaults.css.backgroundColor = '';
  $.blockUI.defaults.css.cursor = 'pointer';
  // use yellow overlay 
  $.blockUI.defaults.overlayCSS.backgroundColor = '#120000'; 
  // make overlay more transparent 
  $.blockUI.defaults.overlayCSS.opacity = .8;

});

var rest = {
  getUrl: null,
  putUrl: null,
  delUrl: null,
  get: function(object, id, success, error) {
    if ((!id) || (!object)) return false;
    if (!success) {
      success = function(data) { }
    }
    if (!error) {
      error = function(text) { }
    }
    $.ajax({
      url: this.getUrl,
      type: 'POST',
      dataType: 'json',
      data: { id: id, object: object },
      success: function(data) { success(data.id); },
      error: function(xhr, text) { error(xhr.status + ' ' + text); }
      //complete:
    });
    return true;
  },
  put: function(object, id, success, error) {
    if ((!id) || (!object)) return false;
    if (!success) {
      success = function(data) { alert(data); }
    }
    if (!error) {
      error = function(text) { alert(text); }
    }
    $.ajax({
      url: this.getUrl,
      type: 'POST',
      dataType: 'json',
      data: { id: id, object: object },
      success: function(data) { success(data.id); },
      error: function(xhr, text) { error(xhr.status + ' ' + text); }
      //complete:
    });
    return true;
  },
  del: function(object, id, success, error) {
    if ((!id) || (!object)) return false;
    if (!success) {
      success = function(data) { alert(data); }
    }
    if (!error) {
      error = function(text) { alert(text); }
    }
    $.ajax({
      url: this.delUrl,
      type: 'POST',
      dataType: 'json',
      data: { id: id, object: object },
      success: function(data) { success(data.id); },
      error: function(xhr, text) { error(xhr.status + ' ' + text); }
      //complete:
    });
    return true;
  }
}

function updateQtip() {
  $('span.help').qtip({
    content: {
      url: helpUrl(),
      data: { id: $('span.help').attr('rel') },
      method: 'get'
    },
    position: {
      corner: {
        target: 'bottomMiddle',
        tooltip: 'topRight'
      }
    },
    style: {
      border: {
        width: 1,
        radius: 1
      },
      padding: 5,
      textAlign: 'left',
      tip: true,
      name: 'light'
    }
  });

  $('.tip').each(function() {
    $(this).qtip({
      overwrite: false,
      style: {
        'overflow': 'auto',
        padding: 5,
        background: '#EDEDED',
        color: 'black',
        textAlign: 'left',
        border: {
           width: 1,
           radius: 2,
           color: '#5F0000'
        },
        title: {
          padding: 2,
          background: '#FAE98E'
        },
        tip: true,
        'max-height': 150,
        width: 250
      },
      content: {
        title: { text: $(this).text() + ' ' + $(this).attr('title') },
        url: tipUrl(),
        data: { rel: $(this).attr('rel')  },
        method: 'get'
      },
      position: {
        corner: {
           target: 'rightBottom',
           tooltip: 'leftTop'
        },
        adjust: { screen: true }
      },
      hide: { when: 'mouseout', fixed: true }
    });
  });
}
