//
//
/////////////////////////////////  I N I T   B O D Y  ////////////////////////////////////////////////////////////
//
//
//
//  from here linenumbers matter !! We expect console, mootools, core, clientcide
//
//
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

tim.step(" OK - TIM running");

// first of all Error Handling

function showError(msg, fn, ln){
  fn = fn.split("/");
  var msp = "showError: " + fn[fn.length -1] + " / " + msg + " / line:" + ln;
  console.error(msp);
}

function catchError(foo, e, more){
  var fnm = "EOP", msg = "strange error", lnn = "0/0";
  if (e.fileName)   {fnm = e.fileName.split("/"); fnm = fnm[fnm.length -1];}
  if (e.message)    {msg = e.message;}
  if (e.lineNumber) {lnn = e.lineNumber;}
  msg = "CATCH " + foo + ": " + msg + " in " + fnm + " at " + lnn;
  console.error(msg, more);
  console.error(foo, e, more);
}

function switchConsole(){
  if(EOP.Debug && !Browser.Engine.gecko){
    console.log = function(){
      GLog.write($A(arguments).join(" |-> "));
      // TODO: REMEMEBR DEBUG
    };
  }
}

function $F(ret) {return new Function("{" + ret + "}");}

//noiv, Cologne, 2006, http://ExploreOurPla.net
function $I(){
  if (!arguments[0] && arguments[1]){return document.createTextNode(arguments[1]);}
  var el = document.createElement(arguments[0]);
  for (var i = 1; i < arguments.length; i++){
    var a = arguments[i];
    switch (typeof a){
      case "undefined" : /* do nothing */ break;
      case "string" :  el.appendChild(document.createTextNode(a)); break;
      case "object" :  if (a.nodeType) {el.appendChild(a);}
                       else {for (var v in a) {el[v] = a[v];}}}}
  return el;
}


window.addEvent('onerror', showError);

var cEOP = new Class({
  activate : function(){}
});

// the globals

var EOP;

//
/////////////////////////////////    S T A R T   ////////////////////////////////////////////////////////////
//

function initEOP() {

  try {

    tim.step(" OK - DOM loaded");

    EOP            = new cEOP();
    EOP            = $merge(EOP, eopDATA)
    EOP.key        = "EOP";
    EOP.host       = location.host;      // "localhost:8080"
    EOP.Debug      = false;               // if debugging in non gecko needed
    EOP.Disable    = {tiles: false, markers: true, polylines: true};
    EOP.updateName = function (){
      var name = "arctic.io - " + eopDATA.map.title;
      if (eopDATA.map.doesTime) {name += " - (" + eopDATA.yir + "/" + eopDATA.doy + ")";}
      document.title = name;
    }

    tim.step(" OK - EOP loaded");

    EOP.GEO = new cGeoDesktop(EOP);
    EOP.GEO.initStructure();
    EOP.GEO.initBehaviour();
    EOP.activate();
    EOP.GEO.activate();
    tim.step(" OK - EOP.GEO activated");

    // Controling windows events
    onresize = function () {EOP.GEO.MAP.checkResize();};
    onunload = function () {tim.step("NOK - unloading... bye"); if ($("mapFull")){GUnload();}};
    onload   = function () {
      var welcome = " OK - Welcome " + eopDATA.ipu + " using " + Browser.Engine.name + "(" + Browser.Engine.version + ") on " + Browser.Platform.name;
      tim.step(welcome);
    };

  } catch (e){catchError("initEOP", e) ;}
}


window.addEvent('domready', initEOP);
window.addEvent('mapready', switchConsole);         // provides GLog if needed


