/**
 * Setup do ajax para adm
 */ 
$.ajaxSetup({
  type: "POST",
  url: 'actions/AjaxAction.php'
});


/**
 * Janela modal com iFrame
 */
function modalIframe(largura, altura, title, src, scrolling, jsclose) {
  
  var iframeAltura = altura -27;
  var modaltop;
  
  if (!scrolling) {
    scrolling = 'auto';
  }
  
  modaltop  = '<div id="modaltop">';
  modaltop += '  <div id="modaltitle" style="width: '+ (largura-68)  +'px">'+ title +'</div>';
  modaltop += '  <div id="modalclose"><a href="#" class="simplemodal-close"><img src="includes/img/bt_fechar.png" border="0" /></a></div>';
  modaltop += '</div>';
  
  $.modal('<iframe src="'+ src +'" height="'+ iframeAltura +'" width="'+ largura +'" scrolling="'+ scrolling +'" frameborder="0" marginheight="4" marginwidth="4" vspace="0" hspace="0">', {
    closeHTML: modaltop,
    containerCss: {
      backgroundColor: "#ffffff",
      height: altura,
      width: largura,
      padding: 0
    },
    
    opacity: 40,
    overlayClose: true,
    
    onClose: function () {
      if (jsclose) {
        eval(jsclose);
      }
      this.close();
    },
    
    onOpen: function (dialog) {
      dialog.overlay.fadeIn('fast', function() {
        dialog.data.hide();
        dialog.container.fadeIn('fast', function() {
          dialog.data.slideDown('fast');
        });
      });
    }
    
  });
  
}


function modalProcessing(msg) {
  
  var altura = 100;
  var tableAltura = altura-27;
  var largura = 250;
  var html;
  var title = 'Aguarde ...';
  var content = '<img src="includes/img/aguarde.gif" border="0" align="absmiddle" />&nbsp; <b>'+ msg +'</b>';
  
  //$('body').css({'overflow-x':'hidden','overflow-y':'hidden'});
  
  html  = '<div id="modaltop">';
  html += '  <div id="modaltitle" style="width: '+ (largura-68)  +'px">'+ title +'</div>';
  html += '</div>';
  html += '<div><table width="'+ largura +'" border="0" cellspacing="0" cellpadding="0">';
  html += '  <tr>';
  html += '    <td height="'+ tableAltura +'" align="center">'+ content +'</td>';
  html += '  </tr>';
  html += '</table></div>';

  $.modal(html, {
    containerCss: {
      backgroundColor: "#ffffff",
      height: altura,
      width: largura,
      padding: 0
    },
    opacity: 40
  });
}


function modal(largura, altura, title, content) {
  
  var tableAltura = altura -27;
  var html;
  
  html  = '<div id="modaltop">';
  html += '  <div id="modaltitle" style="width: '+ (largura-68)  +'px">'+ title +'</div>';
  html += '</div>';
  html += '<div><table width="'+ largura +'" border="0" cellspacing="0" cellpadding="0">';
  html += '  <tr>';
  html += '    <td height="'+ tableAltura +'" align="center">'+ content +'</td>';
  html += '  </tr>';
  html += '</table></div>';

  $.modal(html, {
    containerCss: {
      backgroundColor: "#ffffff",
      height: altura,
      width: largura,
      padding: 0
    },
    opacity: 40
  });
}


/**
 * Retira espaços em branco do fim e do inicio de uma string 
 */
function trim(str, chars) {
  return ltrim(rtrim(str, chars), chars);
}


/**
 * Retira espaços em branco a esquerda de uma string 
 */
function ltrim(str, chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}


/**
 * Retira espaços em branco a direira de uma string 
 */
function rtrim(str, chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function aguardeModal(op) {

  if (op) {
    
    var style1   = '';
    var boxmodal = '';
    
    style1 += 'background:#ffffff; opacity:0.5; -moz-opacity:0.5; filter: alpha(opacity=50);';
    style1 += 'position:absolute; width:100%; height:100%; left:0px; top:0px; border:1px none #000000; z-index:1;'
    
    boxmodal += '<table id="modalAguardeTexto" width="300" border="0" cellspacing="0" cellpadding="0" style="position:absolute; left:30%; top:35%; border:1px solid #000000; z-index:2; text-align: center;">';
    boxmodal += '  <tr>';
    boxmodal += '    <td height="150" align="center" bgcolor="#FFFFCC" class="v14">';
    boxmodal += '     <img src="includes/img/aguarde.gif" align="absmiddle" /> Aguarde ...';
    boxmodal += '    </tr>';
    boxmodal += '  </td>';
    boxmodal += '</table>';
    
    $("body").append("<div id='modalAguarde' style='"+ style1 +"'></div>"+ boxmodal);
    
  } else {

    $("#modalAguarde").remove();
    $("#modalAguardeTexto").remove();
    
  }
  
}
