var app = {
   start : function () {
      oContent = $('#content');
      oContentData = $('#post-380');
      setTimeout(function () {
         $('#primary').remove();
         $('.entry-content').attr("style","width:800px;");
         var w = $('#topbar').width() + "px;";
         $('#topbarInner').attr("style","width:" + w);
         $('#realContentBox>table tr td').attr("style","border: 0px;");
         $('#contentBox>table tr td').attr("style","border: 0px; none");
      }, 500);
      $('[id^=toolbar]')
         .add('[id^=topbar]')
         .add('[id^=contentBox]')
         .add('#realContentBox')
         .attr("style","padding: 0px; margin: 0px; border: 0px none;");
/*
      .each(function () {
         this.style.paddingLeft   = '0px';
         this.style.paddingRight  = '0px';
         this.style.paddingTop    = '0px';
         this.style.paddingBottom = '0px';
         this.style.marginLeft    = '0px';
         this.style.marginRight   = '0px';
         this.style.marginTop     = '0px';
         this.style.marginBottom  = '0px';
         this.style.borderLeftWidth    = '0px';
         this.style.borderRightWidth   = '0px';
         this.style.borderTopWidth     = '0px';
         this.style.borderBottomWidth  = '0px';
         this.style.borderLeftStyle    = 'none';
         this.style.borderRightStyle   = 'none';
         this.style.borderTopStyle     = 'none';
         this.style.borderBottomStyle  = 'none';
      });
*/
      $('#contentBoxUpper').add('#contentBoxLower').attr("style","font-size: 4px; line-height: 4px; height: 4px; padding: 0px; margin: 0px; border: 0px none;");
/*
      .each(function () {
         this.style.fontSize     = '4px';
         this.style.lineHeight   = '4px';
         this.style.height       = '4px';
      });
*/
      app.login();
   },

   show_menu: function(menu) {
      var item, first;
      $('#newmenu>*').remove();
      for(m in appmenu[menu]) {
         if ((appmenu[menu][m].ico) && (appmenu[menu][m].all || (app.usr==0))) {
            item = $("<a>");
            item[0].href = "javascript:" + appmenu[menu][m].fn + "()";
            item.html("<img src='app/" + appmenu[menu][m].ico + "9.png' style='margin-left: 6px;' />");
            item.appendTo('#newmenu');
//            if (!first) {
//               first = item;
//            }
         }
      }
   },

   login: function() {
      $('#toolbar').hide();
      $('#topbar').attr("style","border: 0px none;").show();
      $('#realContentBox').html(
         "<table cellpadding='0' cellspacing='0' style='border: 0px none;'>" +
         "<tr><td colspan='2' id='title'>Login</td></tr>" +
         "<tr><td>&nbsp;</td><td>&nbsp;</td></tr>" +
         "<tr><td>Usuário:</td><td><input type='text' id='usr' size='70' /></td></tr>" +
         "<tr><td>Senha:</td><td><input type='password' id='pw' size='70' /></td></tr>" +
         "<tr><td colspan='2'><button id='loginbut'>&nbsp;&nbsp;&nbsp;Ok&nbsp;&nbsp;&nbsp;</button></td></tr>" +
         "</table>"
      );

      $('#realContentBox table').add('#realContentBox tr').attr(
         "style",
         "border: 0px none; " +
         "border-collapse: collapse; "
      );

      setTimeout(function () {
         $('#realContentBox td').attr(
            "style",
            "font-size: 15pt; " +
            "font-family: sans-serif;"
         );

         $('#title').attr(
            "style",
            "text-align: center; " +
            "font-size: 18pt; " +
            "font-weight: bolder; " +
            "border: 0px none; " +
            "border-collapse: collapse; " +
            "background: url(app/bk-dark.jpg); " +
            "color: white; " +
            "text-shadow: 2px 2px 2px #000; " +
            "filter: dropshadow(color=#rgba(0,0,0,0.6), offx=2, offy=2); " +
            "-webkit-box-shadow: 0 8px 6px -6px black; " +
            "-moz-box-shadow: 0 8px 6px -6px black; " +
            "box-shadow: 0 8px 6px -6px black; " +
            "font-family: sans-serif;"
         );

         $('#loginbut').attr(
            "style",
            "font-size: 18pt; " +
            "font-weight: bolder; " +
            "text-shadow: 2px 2px 2px #888; " +
            "filter: dropshadow(color=#rgba(0,0,0,0.6), offx=2, offy=2); " +
            "font-family: sans-serif;" +
            ""
         ).parent().attr(
            "style",
            "text-align: center;"
         );

         $('#loginbut').click(function() {
            var data = {};
            $('#realContentBox input').each(function () {
               data[this.id] = this.value;
            });
            $.ajax({
               type: "POST",
               url:  "app/login.php",
               data: data.toJSONString(),
               dataType: "json",
               success: function (json) {
                  $('#topbar').hide();
                  $('#toolbar').show();
                  app.show_menu("main");
               }
               ,
               error: function(xhr, textStatus, errorThrown) {
                  alert("Erro: " + (errorThrown?errorThrown:xhr.status));
               }
            });
         });
      },700);
   },

   show_emp: function () {
   }
};

/*
function expand()
{
   var i;
   var sizes = [ '100%', '75%', '50%', '40%', '40%' ];
   var icons = [ $(this) ];
   var next  = $(this).parent().parent().nextAll();
   i         = 0;
   $(next[i++] || []).animate({width:sizes[i],height:sizes[i]},"fast",arguments.callee);
   var prev  = $(this).parent().parent().prevAll().first();
   i         = 1;
   $(prev[i++] || []).animate({width:sizes[i],height:sizes[i]},"fast",arguments.callee);
}
*/

var appmenu = {
   main: {
      sair: {
         all: true,
         ico: "sair",
         fn:  "app.sair"
      },
      incluir: {
         all: false,
         ico: "incluir",
         fn:  "app.add_emp"
      },
      users: {
         all: false,
         ico: "user",
         fn:  "app.edit_dir"
      },
      cidades: {
         all: false,
         ico: "cidade",
         fn:  "app.edit_cid"
      },
      procurar: {
         all: true,
         ico: "buscar",
         fn:  "app.search_emp"
      }
   },
   usuarios: {
      sair: {
         all: true,
         ico: "sair",
         fn:  "app.start"
      },
   },
   cidades: {
      sair: {
         all: true,
         ico: "sair",
         fn:  "app.start"
      },
   },
   procurar: {
      sair: {
         all: true,
         ico: "sair",
         fn:  "app.start"
      },
      cadastro: {
         all: true,
         ico: "cad",
         fn:  "app.search_emp_cad"
      },
      sistemas: {
         all: true,
         ico: "sistemas",
         fn:  "app.search_emp_sist"
      },
      beneficios: {
         all: true,
         ico: "benef",
         fn:  "app.search_emp_benef"
      },
      tarifas: {
         all: true,
         ico: "tarif",
         fn:  "app.search_emp_tarif"
      }
   }
};

