// ###########################################################################
// AUTHOR:   Alan Turransky
// PURPOSE:  Pyramid Stack Core
// ###########################################################################

// ###########################################################################
// OBJECT DEFINITION
// ###########################################################################
var mngi = {

  uid : { version:'0', revision:'1', build:'1a', release:'alpha', codename:'asimov' },

  env : { loadTime         : 0,
          device           : 'unknown',
          width            : $(window).width(),
          height           : $(window).height(),
          geolocation      : (typeof navigator.geolocation   != undefined),
          localStorage     : (typeof window.localStorage     != undefined),
          sessionStorage   : (typeof window.sessionStorage   != undefined),
          openDatabase     : (typeof window.openDatabase     != undefined),
          applicationCache : (typeof window.applicationCache != undefined),
          isHtml5          : (typeof window.localStorage     != undefined) && 
                             (typeof navigator.geolocation   != undefined),
          isMobile         : $.browser.mobile,
          isComputer       : true,
          isTablet         : false,
          isSmartPhone     : false,
          isDumbPhone      : false
  },

  api : function(){
          try{
            if((arguments.length)&&(typeof mngi[arguments[0]]=='function')){
              for(var a=[],x=1; x<arguments.length; x++) a[x-1]=arguments[x];
              mngi[arguments[0]].apply(this,a);
            };
          } catch(err){ };
  },

  // EXAMPLES:
  //   mngi.api("load","component","nav.htm","#bottom-navigation-container");
  //   mngi.api("load","apt-ad",   "adPos1", "#ad-position-1", "#ad-position-1-container");
  //   mngi.api("load","script",   "foo.js");
  //   mngi.api("load","style",    "foo.css");
  load: function(){
          try{
            if(!mngi.env.isDumbPhone){
              switch(arguments[0]){
              case 'component': if((arguments.length==3)||(arguments[3])) $.loadAjax(arguments[1],arguments[2],function(data){ this.html(data); }); break;
              case 'style':     $.loadStyle(arguments[1]);  break;
              case 'script':    $.loadScript(arguments[1]); break;
              case 'apt-ad':    if(arguments[4]){
                                  yld_mgr.place_ad_here(arguments[1]);
                                  if(!$.browser.msie) $.moveNode($(arguments[2]),$(arguments[3]));
                                };
                                break;
              };
            };
          } catch(err){ };
  },

  dbug: function(){
    var d=$("#dbug"); if(!d.length) $('body').append('<div id="dbug"></div>');
    var h=''; for(var p in mngi.env) h+='['+p+' = '+mngi.env[p]+']<br />'; h+='</div>';
    $('#dbug').append(h);
  },

  done: function(){
    mngi.env.loadTime=((new Date().getTime())-(mngi.env.loadTime))+'ms';
  },

  init: function(){
    $('body').append('<div id="qed"></div>');
    mngi.env.loadTime     =  new Date();
    mngi.env.device       =  $('#qed').css('font-family');
    mngi.env.isTablet     =  mngi.env.isMobile &&  mngi.env.isHtml5 && (mngi.env.device=='tablet');
    mngi.env.isSmartPhone =  mngi.env.isMobile &&  mngi.env.isHtml5 && (mngi.env.device=='phone');
    mngi.env.isDumbPhone  =  mngi.env.isMobile && !mngi.env.isHtml5 && (mngi.env.device=='phone');
    mngi.env.isComputer   = !mngi.env.isMobile;
    $('#qed').remove();
    $(document).ready(mngi.done);
  }
};