
function printx(msg, parent) {
    if (msg == null) return;
    if (msg === "") return;
    if (!parent) parent = document.getElementById('text');
    var text = window.document.createTextNode(msg);
    parent.appendChild(text);
}
    
function println(msg, parent) {
    if (!parent) parent = document.getElementById('text');
    printx(msg, parent);
    var br = window.document.createElement("br");
    parent.appendChild(br);
}


function showStats() {
    
    var greyStuff = document.createElement("div");
    greyStuff.setAttribute("id", "greybackground");
    
    document.body.appendChild(greyStuff);
    
    var statScreen = document.createElement("div");
    
    
    statScreen.setAttribute("id", "stats");
    
    var barWidth = 150; /*px*/
    var brutalityWidth = barWidth / 100 * window.stats.brutality;
    var cunningWidth = barWidth / 100 * window.stats.cunning;
    var disdainWidth = barWidth / 100 * window.stats.disdain;
    var infamyWidth = barWidth / 100 * window.stats.infamy;
    window.stats.wealth = window.stats.wealth || 0;
    var woundText = [
      "Uninjured"
      ,"Battle-scarred"
      ,"Permanently wounded"
      ,"Permanently weakened"
      ,"At Death's door"
      ,"At Death's door"
      ,"At Death's door"
    ][window.stats.wounds*1];
    statScreen.innerHTML="  <table>"+
"    <tr><td class='leftStatName'>Brutality<div class='definition'>Strength and cruelty</div></td><td><div class='rightStatBar'><div style='width: "+brutalityWidth+"px;' class='leftStatBar'>&nbsp;</div></div></td><td class='rightStatName'>Finesse<div class='definition'>Precision and aerial maneuverability</div></td></tr>"+
"    <tr><td class='leftStatName'>Cunning<div class='definition'>Intelligence and trickery</div></td><td><div class='rightStatBar'><div style='width: "+cunningWidth+"px;' class='leftStatBar'>&nbsp;</div></div></td><td class='rightStatName'>Honor<div class='definition'>Honesty and trustworthiness</div></td></tr>"+
"    <tr><td class='leftStatName'>Disdain<div class='definition'>Patience and scorn</div></td><td><div class='rightStatBar'><div style='width: "+disdainWidth+"px;' class='leftStatBar'>&nbsp;</div></div></td><td class='rightStatName'>Vigilance<div class='definition'>Attention and impulsiveness</div></td></tr>"+
"    <tr><td class='leftStatName'>Infamy</td><td><div class='rightStatBar' style='background-color:grey'><div style='width: "+infamyWidth+"px; background-color:white' class='leftStatBar'>&nbsp;</div></div></td><td></td></tr>"+
"    <tr><td></td><td style='text-align: center'><b>Wealth</b>: "+window.stats.wealth+" gold coins</td></td><td></td></tr>"+
"    <tr><td></td><td style='text-align: center'><b>Wounds</b>: "+woundText+"</td></td><td></td></tr>"+
"  </table><div id='restart' style='float: left; text-decoration: underline; cursor: pointer'></div>";
    
    var restartLink = document.createElement("a");
    restartLink.innerHTML="Start Over from the Beginning";
    restartLink.onclick = function() {
        if (window.confirm("Restart your game?  Did you click that intentionally?")) {
            clearCookie();
            document.body.removeChild(greyStuff);
            document.body.removeChild(statScreen);
            clearScreen(restoreGame);
        }
        return false;
    }
    
    var button = document.createElement("button");
    button.innerHTML="OK";
    button.onclick = function() {
        document.body.removeChild(greyStuff);
        document.body.removeChild(statScreen);
    }
    statScreen.appendChild(button);    
    document.body.appendChild(statScreen);
    var ssWidth = statScreen.clientWidth;
    var bodyWidth = document.body.clientWidth;
    var bodyMarginLeft = 8; // getComputedStyle(document.body, null).marginLeft;
    var left = Math.floor((bodyWidth - ssWidth) / 2) + bodyMarginLeft;
    statScreen.style.left = left + "px";
    document.getElementById('restart').appendChild(restartLink);
    window.scrollTo(0,0);
}

function clearScreen(code) {
    main.innerHTML = "<div id='text'></div>";
    var useAjax = true;
    
    if (useAjax) {
      doneLoading();
      setTimeout(function() { window.scrollTo(0,0); }, 0);
      code.call();
    } else {
      if (!initStore()) alert("Your browser has disabled cookies; this game requires cookies to work properly.  Please re-enable cookies and refresh this page to continue.");
      startLoading();
      var form = window.document.createElement("form");
      var axn = window.location.protocol + "//" + window.location.host + window.location.pathname;
      form.setAttribute("action", axn);
      form.setAttribute("method", "POST");
      main.appendChild(form);
      form.submit();
    }
}

function safeSubmit(code) {
    return function safelySubmitted() {
        safeCall(code);
        return false;
    }
}

function startLoading() {
    var loading = document.getElementById('loading');
    if (!loading) {
      loading = document.createElement('div');
      loading.setAttribute("id", "loading");
      loading.innerHTML = "<p>Loading...</p><p><img src=\"../loader.gif\"></p>";
      main.appendChild(loading);
    }
}

function doneLoading() {
    var loading = document.getElementById('loading');
    if (loading) loading.parentNode.removeChild(loading);
    var header = document.getElementById('header');
    if (!header) {
      header = document.createElement('div');
      header.setAttribute("id", "header");
      header.innerHTML = "<h1 class='gameTitle'>Choice of the Dragon</h1><p><a class='spacedLink' href='../about.html'>About</a> <a class='spacedLink' href='credits.html'>Credits</a> <a class='spacedLink' href='../why.html'>Why?</a> <a class='spacedLink' href='http://www.choiceofgames.com/blog'>Blog</a></p>";
      document.body.appendChild(header);
      println();
    }
}

function setClass(element, classString) {
  element.setAttribute("class", classString);
  element.setAttribute("className", classString);
}
  
function printFooter() {
  var footer = document.getElementById('footer');
  var body = document.body;
  if (footer) {
    footer.parentNode.removeChild(footer);
    body.appendChild(footer);
  } else {
    footer = document.createElement("div");
    footer.setAttribute("id", "footer");
    footer.innerHTML=""
    +(isWeb?"<div style='float: left;'><a class='spacedLink' href='http://itunes.apple.com/us/app/choice-of-the-dragon/id348940932?mt=8'><img src='../icons/appstore.png' style='height: 3em;'></a> <a href='market://details?id=com.choiceofgames.dragon'><img class='spacedLink' src='../icons/droid.png' style='height: 3em;'></a></div><p>Now available for <a href='http://itunes.apple.com/us/app/choice-of-the-dragon/id348940932?mt=8'>iPhone App Store</a> and <a href='market://details?id=com.choiceofgames.dragon'>Android market!</a></p>":"")
    +"<p>Love it?  Hate it?  Write us at <a href='mailto:support@choiceofgames.com'>support@choiceofgames.com</a></p>"
    ;
    body.appendChild(footer);
  }
  if (window.fancyFont) Cufon.replace(document.getElementById('text'));
  scrollify();
}

function scrollify() {
  var h = main.offsetHeight;
  if (!h) {
    setTimeout(scrollify, 0);
    return;
  }
  var background = document.getElementById("background");
  if (!background) return;
  h -= 200;
  var minimum = 225;
  if (h < minimum) h = minimum;
  background.style.height = "" + h + "px";
}

function printButton(name, parent, isSubmit, code, className) {
  var button;
  if (isSubmit) {
    button = document.createElement("input");
    button.type = "submit";
    button.value = name;
    button.name = name;
  } else {
    button = document.createElement("div");
    var imageName = {
      "Next": "Next"
      ,"My Dragon": "MyDragon"
      ,"Next Chapter": "NextChapter"
      ,"Begin the Adventure":"Begin"
      ,"On with the show!":"OnShow"
      ,"Play Again":"PlayAgain"}[name];
    button.innerHTML = "<img src='design/"+imageName+".png'>";
  }
  if (!className) className = "next";
  setClass(button, className);
  if (code) button.onclick = code;
  if (!isMobile) try { button.focus(); } catch (e) {}
  parent.appendChild(button);
}

function printSeparator(parent) {
  var separator = document.createElement("img");
  separator.src = "design/sep.png";
  setClass(separator, "separator");
  parent.appendChild(separator);
}
  

window.isMobile = /Mobile/.test(navigator.userAgent);
window.isFile = /^file:/.test(window.location.href);
window.isWeb = /^https?:/.test(window.location.href);
