  ///
  /// Script JavaSCript do Tema Principal
  /// NetForce Tecnologia
  ///

  $(document).ready(function() {

    // Confirmação dos Links
    if ($('body.body-main').size() > 0)
      window.name = 'principal';

    // Confirmação dos Links
    $('a[confirm]').click(function() {
      return window.confirm($(this).attr('confirm'));
    });

    // Alertar dos Links
    $('a[alert]').click(function() {
      alert($(this).attr('alert'));
    });

    // Gráficos de barras (Demonstrativos)
    $('div[grafico]').each(function() {
      var div = $(this);
      div.addClass('ui-grafico');
      div.html('<div class="ui-grafico-bar" style="width: ' + div.attr('grafico') + '%;">&nbsp;</div>');
    });

    // Sujestões
    $('a[sugest]').click(function() {
      var a = $(this);
      var fld = $('#' + a.attr('field'));
      if (fld.val() == a.attr('sugest'))
      {
        fld.val('0');
        fld.change();
      } else {
        fld.val(a.attr('sugest'));
        fld.change();
      }
    });

    // CheckGrid
    $('input.checkgrid').click(function() {
      var check = $(this);
      var hnd = check.attr('handle');
      theme_set_check(hnd, check.attr('checked'));
      /*

      var index = g_checkGrid.indexOf(hnd);

      // Verificar se deve adicionar
      if (check.attr('checked') && (index === false))
        g_checkGrid.add(hnd);

      // Verificar se deve remover
      if ((check.attr('checked') != true) && (index !== false))
        g_checkGrid.remove(index);
      */
    });

    // CheckGrid ALL
    $('input.checkgrid-all').click(function() {
      var check = $(this);

      $('input.checkgrid').each(function() {
        var item = $(this);
        var hnd  = item.attr('handle');
        item.attr('checked', check.attr('checked'));
        theme_set_check(hnd, check.attr('checked'));
      });        
    });

    // Link com CheckGrid
    $('a.com-checkgrid').click(function() {
      var a = $(this);
      if (g_checkGrid.count() == 0)
      {
        alert('Nenhum item foi selecionado');
        return false;
      }

      a.attr('href', a.attr('href') + '&hnds=' + g_checkGrid.implode(','));
    });

    // Link com DLG
    $('a[dlg]').click(function() {
      var dlg    = new NDialog();
      dlg.width  = 400;
      dlg.height = 500;
      dlg.url    = $(this).attr('dlg');
      dlg.show();
    });

    // Campos numéricos
    $('input.text-int').number(0);

    // Tutorial
    $('#tutorial').draggable({
      stop: function(e, ui) {
        var pos = $(this).position();
        $.cookie('jogo_tut_x', pos.left);
        $.cookie('jogo_tut_y', pos.top);
      }
    });
  });

  function sleep_close()
  {
    window.setTimeout('window.close();', 1000, 'JavaScript');
  }

  function theme_set_check(hnd, value)
  {
    var index = g_checkGrid.indexOf(hnd);

    // Verificar se deve adicionar
    if ((value == true) && (index === false))
      g_checkGrid.add(hnd);

    // Verificar se deve remover
    if ((value != true) && (index !== false))
      g_checkGrid.remove(index);
  }
