window.onload = initAll;

// global variables
var adImages = new Array( "images/ads/kryolan-black-white.png", 
"images/ads/repelsteeltje.png", "images/ads/xhd-logo.png");
var adURL = new Array( "arcadedekorf.nl", "kryolan.nl", "xhd.nl");
var thisAd = 0;

function initAll() {
  var host = "desktop";
  var isAndroid = navigator.userAgent.match(/Android/i) != null;
  var isiPad = navigator.userAgent.match(/iPad/i) != null;
  var isiPhone = navigator.userAgent.match(/iPhone/i) != null;
  
  if (isAndroid || isiPhone) {
    getNode( 'css/android-iphone.css', 'only screen and (max-width: 480px)');
  }
  else if (isiPad) {
    // two links to a specific stylesheet are required otherwise desktop screens in 768x1024
    // mode will get the ipad stylesheet
    getNode( 'css/ipad-portrait.css', 'only screen and (min-device-width: 1024px) and (orientation:portrait)');
    getNode( 'css/ipad-landscape.css', 'only screen and (min-device-width: 1024px) and (orientation:landscape)');
  }
  else {
    self.resizeTo(1024, 768);      
    getNode( 'css/desktop.css', 'screen and (min-width: 481px)');      
  } 
  
  if (isiPad || isiPhone) {
    setMeta( 'viewport', 'user-scalable=no, width=device-width');
    getScript( 'javascript/iphone.js');
    $('#korf0910').click(function(event) {
      mobilePhotoLib();
    });
  } 

  getScript( 'javascript/monsterscript.js');
  initBannerLink();
}


function adBanner() {
  thisAd++;
  if (thisAd == adImages.length) {
    thisAd = 0;
  }
  
  document.getElementById( "adBanner").src = adImages[thisAd];
  
  setTimeout( "adBanner()", 3 * 1000);
}

function newLocation() {
  document.location.href = "http://www." + adURL[thisAd];
  return false;
}

function initBannerLink() {
  if (document.getElementById( "adBanner").parentNode.tagName == "A") {
    document.getElementById( "adBanner").parentNode.onclick = newLocation;
  }
  adBanner();
}

function setMeta( name, content) {
    var metaNode = document.createElement( 'meta');
    metaNode.name = name;
    metaNode.content = content;
    document.getElementsByTagName('head')[0].appendChild( metaNode); 
}

function getNode( href, media) {
  var cssNode = document.createElement( 'link'); 
  cssNode.rel = 'stylesheet';
  cssNode.type = 'text/css';
  cssNode.title = 'dynamicLoadedSheet';
  cssNode.href = href;
  cssNode.media = media;
  document.getElementsByTagName("head")[0].appendChild( cssNode);
}

function getScript( script) {
  var scripttag = document.createElement( 'script');
    scripttag.setAttribute( 'src', script);
    scripttag.setAttribute( 'type', 'text/javascript');
    scripttag.setAttribute( 'language', 'javascript');
    document.getElementsByTagName( 'head')[0].appendChild( scripttag);
}

