/*
    File: voorb.js
*/
var debug = true;

function get_url(apiname, params)
{
    // config.js
    //   var apikey
    //   var fac_id
    //   var urlbase
    //
    var format  = "json";
    var pretty  = true;
    var logging = true;
    var param   = params || {};
    var url = urlbase + "/api2/" + apiname + "." + format;
    url += "?fac_id=" + fac_id;
    if (debug)
    {
      if (pretty)
          url += "&pretty=1";
      if (logging)
          url += "&logging=1";
    }
    if (param.filter)
        url += "&"+param.filter;

    var result = {url: url, apikey: apikey};
    result.method = (param.method ? param.method : "GET");

    if (param.callback_done || param.callback_fail)
    {
        result.callback = {};
        result.callback.done = (param.callback_done ? param.callback_done : ["api2.callbackstatusdone"]);
        result.callback.fail = (param.callback_fail ? param.callback_fail : ["api2.callbackstatusFail"]);
    }

    return result;
}


function splitFunctionname(fullname)
{
    var result = {fnmodule: "api2", fnname: "no_function"};
    var x = fullname.split(".");
    if (x.length == 2)
    {
        if (callbackFunctions[x[0]] && callbackFunctions[x[0]][x[1]])
        {
            result.fnmodule = x[0];
            result.fnname   = x[1];
        }
    }
    return result;
}


function call_api(io_data, params)
{
    var url_data = get_url(params.apiname, params);
    tm = new Date;

    $.ajax( url_data.url
          , { type: url_data.method
            , data: JSON.stringify(io_data)
            , headers: { "X-FACILITOR-API-Key": url_data.apikey }
            }
          ).done( function (data, textStatus, jqXHR)
                  {
                      var statusInfo = {jqXHR: jqXHR, textStatus: textStatus};
                      for (var i=0; i<url_data.callback.done.length; i++)
                      {
                          var splitfunction = splitFunctionname(url_data.callback.done[i]);
                          callbackFunctions[splitfunction.fnmodule][splitfunction.fnname](data, statusInfo, params);
                      }
                  }
                )
           .fail( function (data, textStatus, jqXHR, errorThrown)
                  {
                      var statusInfo = {jqXHR: jqXHR, textStatus: textStatus, errorThrown: errorThrown};
                      for (var i=0; i<url_data.callback.fail.length; i++)
                      {
                          var splitfunction = splitFunctionname(url_data.callback.fail[i]);
                          callbackFunctions[splitfunction.fnmodule][splitfunction.fnname](data, statusInfo, params);
                      }
                  }
                );

}


function call_optionlist(listname, apiname, params)
{
	  var param = params || {};
	  var data_optionlist = {};

    param.optionlist = listname;
	  param.apiname = apiname;
	  if (!param.callback_done)
	      param.callback_done = ["api2.createOptionList", "api2.callbackstatusDone"];
    call_api(data_optionlist, param);
}


var callbackFunctions_api =
    {   name: "api2",
        no_function: function _no_function(data, statusInfo, params)
            {
                var msg = "Onbekende callback functie";
                $("#Status").text(msg);
            },
        callbackstatusDone: function _callbackstatusDone(data, statusInfo, params)
            {
                $("#Status").text(statusInfo.jqXHR.status + ": " + statusInfo.textStatus);
                $("#btnSubmit").attr("disabled", false);
            },
        callbackstatusFail: function _callbackstatusFail(data, statusInfo, params)
            {
                var info = data.responseJSON.error;
                $("#Status").text(info.code + ": " + info.message);
                $("#Result").text("");
                $("#btnSubmit").attr("disabled", false);
            },

        createOptionList: function _createOptionList(data, statusInfo, params)
            {
                var listname = params.optionlist;
                var apiname  = params.apiname;
                $("#"+listname).empty();
                $.each( data[apiname]
                      , function (index, value)
                        {
                            apirecord = data[apiname][index];
                            $("#"+listname).append("<option value="+ apirecord.id +">" + apirecord.name + "</option>");
                        }
                      );
            },

        showAppointmentNr: function _showAppointmentNr(data, statusInfo, params)
            {
                var msg = "Afspraak gemaakt met nummer: "+ data.appointment.id;
                $("#Result").text(msg);
                $("#btnSubmit").attr("disabled", true);
            },

        b: function _ennuverder2(data, statusInfo, params)
        {
            var msg = $("#Status").text();
            msg = msg + "andere functie";
            $("#Result").text(msg);
        }
    }

var callbackFunctions = {};
callbackFunctions.api2 = callbackFunctions_api;
