/* 
 * Copyright (c) 1995-2009, Index Data
 * All rights reserved.
 * See the file LICENSE for details.
 */

/*
 * This file will include and initialize all necessary components.
 */
var MK2_UI_CORE_BASE="/mk2-ui-core";

mk_main();

function mk_main() {
  mk_addLoadListener(mk_onLoad);
  mk_include("/pazpar2/js/pz2.js");
  mk_include(MK2_UI_CORE_BASE+"/js/split.js");
  mk_include(MK2_UI_CORE_BASE+"/js/helper.js");
  mk_include(MK2_UI_CORE_BASE+"/js/images.js");
  mk_include(MK2_UI_CORE_BASE+"/js/templates.js");
  mk_include(MK2_UI_CORE_BASE+"/js/components.js");
  mk_include(MK2_UI_CORE_BASE+"/js/controls.js");
  mk_include(MK2_UI_CORE_BASE+"/js/settings_client.js");
  mk_include(MK2_UI_CORE_BASE+"/js/cookies.js"); 
  mk_setWindowName();
}


function mk_onLoad() {
    //hide the page since we might need to redirect to login
    mk_hidePage();    
    //FIXME disabling the following for now, since all pages do it on their own
    // parse HTTP GET params (!!!)
    //window.location.parameters = parseQueryString(window.location.search);
    // load templating components
    //loadComponents();   
}

function mk_setWindowName () {
  var d = new Date();    
  if (!window.name || window.name.indexOf("mkid_")==-1) {      
    window.name = "mkid_" + d.getTime();
  }    
}

function mk_showPage() {
  document.body.style.display = "";
  document.body.style.visibility = "visible";
}

function mk_hidePage() {
  document.body.style.display = "none";
  document.body.style.visibility = "hidden";
}


/**
 * Alters the dom to include scipts
 */
function mk_include(file) {
  document.write('<script type="text/javascript" src="'+ file +'"></script>');
}


//more reliable version of the prependOnLoad
function mk_addLoadListener(fn) {  
  if (typeof window.addEventListener != 'undefined') {  
    window.addEventListener('load', fn, false);  
  } else if (typeof document.addEventListener != 'undefined') {  
    document.addEventListener('load', fn, false); 
  /* } else if (typeof window.attachEvent != 'undefined')  {
    var oldfun = null;
    if (typeof window.onload == 'function') {  
      oldfun = window.onload;
      window.onload = null;
    }
    window.attachEvent('onload', fn);
    window.attachEvent('onload', oldfun); */
  } else {  
    if (typeof window.onload != 'function') {  
      window.onload = fn;
    } else {  
      var oldfn = window.onload;  
      window.onload = function() {  
        oldfn();  
        fn();  
      }  
    }
  }  
}

// check if running on a mobile device or touch screen
function isTouchScreen() {
    var userAgent = window.navigator.userAgent;
    return userAgent.match(/iPhone|Android|iPod|iPad|Nokia|Symbian|BlackBerry|SonyEricsson|Samsung/) ? 1 : 0;
}


