
/**
 * function alert_r
 *
 * php alias print_r
 * @author    b.hempel
 * @copyright 2007-2008 � seto GmbH
 * @version   1.0
 *
 * @param     object    obj   this obj will be printed
 * @param     boolean   ret   optionally parameter: if true the alert won't be displayed, but the function returns an string
 *
 * @return    string    if ret=true then a string of the given object will be returned
 *            boolean   if no ret is given or ret=false only a true is returned
 *
 */
function alert_r(obj, ret, level)
{
  var str_alert = '';
  var str_type  = typeof obj;

  if (obj && str_type.match(/object/)) {

    level = level && level>0 ? level : 0;

    var type_arr  = true;
    var counter   = 0;
    for (var id in obj) {
      var id_nr = id.match(/^[0-9]+$/) ? true : false;

      str_alert += str_alert ? '\n' : '';
      str_alert += str_repeat(' ', 4*(level+1))+(id_nr ? '' : '"')+id+(id_nr ? '' : '"')+': ';
      str_alert += alert_r(obj[id], true, level+1)+',';

      if (id!=counter) {
        type_arr = false;
      }
      counter++;
    }

    /* Korrektur object => array */
    str_type = str_type=='object' && type_arr ? 'array' : str_type;

    str_alert =
      str_type + '\n' +
      str_repeat(' ', 4*level) + '('+'\n'+
      str_alert+'\n'+
      str_repeat(' ', 4*level) + ')';

  } else {
    switch (typeof obj) {
      case 'object':      str_alert = 'null';                 break;
      case 'undefined':   str_alert = 'undefined';            break;
      case 'number':      str_alert = obj;                    break;
      case 'string':      str_alert = '"'+obj+'"';            break;
      case 'boolean':     str_alert = obj ? 'true' : 'false'; break;
      case 'function':    str_alert = 'function: '+id;        break;
      default:            alert(typeof obj);                  break;
    }
  }

  if (!ret) {
    alert(str_alert);
    return true;
  } else {
    return str_alert;
  }
}



/**
 * function vbl
 * console log function
 *
 */
function vbl(obj, description)
{
  if (window.console) {

    /* if ie */
    if (!!(window.attachEvent && !window.opera)) {
      obj = alert_r(obj, 1);
    }

    if (description) {
      window.console.log(obj, description);
    } else {
      window.console.log(obj);
    }
  }
}


/**
 * function str_repeat
 *
 * php alias str_repeat
 *
 */
function str_repeat(str, nr)
{
  var ret = '';

  for (var i=0; i<nr; i++) {
    ret += str;
  }

  return ret;
}

/**
 * limit decimal places
 *
 * @param {Object} num  number to limit
 */
function round(num){
  num = num.toString();
  var split = num.split(".");
  var dec = split[1].substr(0,3);

  var dec1 = dec.match(/\d{2,2}/);
  var dec2 = dec.match(/(\d$)/);

  var rounded = (dec1 + "." + dec2[0]);

  return split[0] + "." + Math.round(rounded);
}

/**
 * toggle container display
 *
 * @param {STRING} id           id_page of the container to show/hide
 * @param {OBJECT} editName     editorObject
 */
function switchContainerDisplay(id, actionIn, editName, json){

    var containerID          = 'texblockImageAdd_'+id
    var switchContainer      = $(containerID);
    var textActionLinks      = switchContainer.next(0);
    var fileActionLinks      = switchContainer.down('.actionlinks');
    var action               = (actionIn) ? actionIn : false;
    var editorObj            = window[editName];


    var uploadFunc           = 'initUploader'+id+'Func()';

    if( switchContainer.style.display == 'none' ) {

        /* slide-in the upload container */
        Effect.SlideDown(
          containerID,
          { afterFinish: function() {
                           textActionLinks.style.display = 'none';
                           fileActionLinks.style.display = 'block';
                           eval(uploadFunc);
                         },
            duration : 0.2
          }
        );

        /* show uploadEditor if hidden */
        if ($(containerID).style.display == 'none') {
            $(containerID).show();
        }

        /* show uploadOverlay if hidden */
        if ($('uploadOverlay'+id).style.display == 'none') {
            $('uploadOverlay'+id).show();
        }

    } else {

        Effect.SlideUp(
          containerID,
          { beforeStart: function() {
                           fileActionLinks.style.display = 'none';
                           textActionLinks.style.display = 'block';
                           /* hang the container back to original position */
                           $('inputUrl'+id).up(0).appendChild($('uploadOverlay'+id));
                           if(action) {
                             $('texblockImageAdd_'+id).next(0).down('.actionlink').innerHTML = 'Bild ersetzen';
                             editorObj.insertElement(json.component, json.id, json.data, json, false, true);
                             if(json.last_responses) {
                               var pictCommand = json.last_responses[0].add;
                               editorObj.insertElement(pictCommand.component, pictCommand.id, pictCommand.data, pictCommand);
                             }
                           }
                         },
            duration: 0.2
          }
        );

        $('uploadOverlay'+id).hide();

    }
}

