// Javascript Utilities for biophily
// =================================

var avatar = "";
var score = "";
var noten = 0;

// which browser ?
var ns4 = (document.layers) ? true : false;         // Mozilla 4+
var ie4 = (document.all) ? true : false;            // Explorer 4+
var ns6 = (document.getElementById) ? true : false  // Mozilla 5+


// C O O K I E S
// =============

// reading and storing
// ===================

function get_cookie(name)
{
  var search = name + "=";
  if (document.cookie.length > 0) // if there are any cookies
  {
    offset = document.cookie.indexOf(search);
    if (offset != -1) // if cookie exists
    {
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1)
        end = document.cookie.length;
      return unescape(document.cookie.substring(offset, end));
    }
    else
    {
      return "";
    }
  }
}

function set_cookie(name, value)
{
  var now = new Date();
  var exp = new Date(now.getTime() + 1000*60*60*24*365); // gilt ein Jahr
  document.cookie = name + "=" + escape(value)
                    + "; expires=" + exp.toGMTString() + ";";
}

function check_cookie()
{
  if ((document.cookie.indexOf("biop_avatar") >= 0)
      && (document.cookie.indexOf("biop_score") >= 0))
  {
    check_avatar();
    check_score();
  }
  else
  {
    window.location.href = "erstmal.html";
  }
}

// the avatar
// ==========

function check_avatar()
{
  avatar = get_cookie("biop_avatar");
  if (ns4)
  {
    document.stamp.document.avatar.src = avatar;
  }
  else if (ie4)
  {
    document.all.tags("img")["avatar"].src = avatar;
  }  
  else if (ns6)
  {
    document.getElementById("avatar").src = avatar;
  }  
  noten = 0;
}

// the score
// =========

score_a = new Array;
score_a = ["score_a1", "score_a2", "score_a3", "score_a4", "score_a5", "score_a6", "score_a7", "score_a8", "score_a9", "score_a10", "score_a11", "score_a12", "score_a13"];

function set_link(num, str)
{
  if (ns4 && document.score)
  {
    document.score.document.links[num].href = str;
  }
  else if (ie4 && document.score)
  {
    document.all.tags("A")[num].href = str;
  }
  else if (ns6 && document.getElementById("score"))
  {
    document.getElementById(score_a[num]).href = str;
  }
}

function set_score(num, str)
{
  if (ns4 && document.score)
  {
    document.score.document.images[num].src = "symbole/" + str + ".gif";
  }
  else if (ie4 && document.score)
  {
    document.all.tags("IMG")[num+1].src = "symbole/" + str + ".gif";
  }
  else if (ns6 && document.getElementById("score"))
  {
    document.images[num+1].src = "symbole/" + str + ".gif";
  }
}

function check_score()
{
  score = get_cookie("biop_score");
  if (score.charAt(0) == "1")
  {
    set_score(0, "avatar_w");
    set_link(0, "wurfel_eq.html");
  }
  if (score.charAt(1) == "1")
  {
    set_score(1, "biohazard_w");
    set_link(1, "kueche_eq.html");
  }
  if (score.charAt(2) == "1")
  {
    set_score(2, "harmful_w");
    set_link(2, "schlafzimmer_eq.html");
  }
  if (score.charAt(3) == "1")
  {
    set_score(3, "reizend_w");
    set_link(3, "bad_eq.html");
  }
  if (score.charAt(4) == "1")
  {
    set_score(4, "radioactive_w");
    set_link(4, "diele_eq.html");
  }
  if (score.charAt(5) == "1")
  {
    set_score(5, "oxygen_w");
    set_link(5, "wohnzimmer_eq.html");
  }
  if (score.charAt(6) == "1")
  {
    set_score(6, "platoni_s");
    set_link(6, "kueche_pl.html");
  }
  if (score.charAt(7) == "1")
  {
    set_score(7, "biohazard_b");
    set_link(7, "kueche_me.html");
  }
  if (score.charAt(8) == "1")
  {
    set_score(8, "harmful_b");
    set_link(8, "schlafzimmer_me.html");
  }
  if (score.charAt(9) == "1")
  {
    set_score(9, "reizend_b");
    set_link(9, "bad_me.html");
  }
  if (score.charAt(10) == "1")
  {
    set_score(10, "radioactive_b");
    set_link(10, "diele_me.html");
  }
  if (score.charAt(11) == "1")
  {
    set_score(11, "oxygen_b");
    set_link(11, "wohnzimmer_me.html");
  }
  if (score.charAt(12) == "1")
  {
    set_score(12, "map_b");
    set_link(12, "map.html");
  }
}

function parse_score(num)
{
  temp = new Array;
  for (i=0; i<=12; i++)
  {
    temp[i] = score.charAt(i);
  }
  temp[num]="1";
  return temp.join("");
}

// D I S P L A Y
// =============

// give some feedback
// ==================

function set_title(str)
{
  if (ns4)
  {
    document.title.document.write("<H1>" + str + "</H1>");
    document.title.document.close();
  }
  else if (ie4)
  {
    document.all.title.innerHTML = "<H1>" + str + "</H1>";
  }
  else if (ns6)
  {
    document.getElementById("title").innerHTML = "<H1>" + str + "</H1>";
  }
  window.status=str;
}

// shake it
// ========
function shake(n)
{
  for (i = 10; i > 0; i--)
  {
    for (j = n; j > 0; j--)
    {
      self.moveBy(0,i);
      self.moveBy(i,0);
      self.moveBy(0,-i);
      self.moveBy(-i,0);
    }
  }
} 

// J A V A
// =======

var java_on = (navigator.javaEnabled());

// no live connect in ie on the apple macintosh
if (ie4)
  if(navigator.platform.indexOf("Mac") >= 0)
    java_on = false;

// give a second chance
if (document.cookie.indexOf("biop_java") >= 0)
{
  var temp = get_cookie("biop_java");
  java_on = (temp.indexOf("on") >= 0 ? true : false);
}
if (java_on == false && window.location.href.indexOf("java.html") < 0)
  window.location.href = "java.html";

function turn_java(str)
{
  if (str == "on")
  {
    java_on = true;
  }
  else
  {
    java_on = false;
  }
  set_cookie ("biop_java", str);
}

// K H
// ===

function kompleted ()
{
  var temp = !java_on;
  if (java_on && document.kh2.document.applets.length
      && document.kh2.document.Graph.isActive())
  {
    if (document.kh2.document.Graph.completed())
	temp = true;
  }
  return (temp);
}

// S O U N D
// =========

var organ_win = false;


// Create a Window for the Applet
// ==============================

function start_organ()
{
  return;
  if (java_on)
  {
    organ_win = window.open("organ.html", "organ",
                            "dependent,hotkeys,width=200,height=100");
    self.focus();
  }
}

function restart_organ()
{
  return;
  if (java_on)
    organ_win = window.open("", "organ",
                            "dependent,hotkeys,width=200,height=100");
}

restart_organ();

// Applet Control
// ==============

function connected()
{
  if (!java_on)
    return(0);
  if (!organ_win || organ_win.closed)
    return(1);
  if (!organ_win.document.Organ)
    return(1);
  if (organ_win.document.Organ.isActive())
    return(2);
  return(3);
}

function pconn()
{
  alert(connected());
}

function pplay(num, str)
{
  switch(connected())
  {
    case 1:
      start_organ();
      break;
    case 2:
      organ_win.document.Organ.pplay(eval(num), str + ".au");
      break;
    case 3:
      turn_java("off");
      alert("No java - No Music!");
    default:
      break;
  }
}

function prand()
{
  switch(connected())
  {
    case 1:
      start_organ();
      break;
    case 2:
      organ_win.document.Organ.prand();
      break;
    case 3:
      turn_java("off");
      alert("No java - No Music!");
    default:
      break;
  }
}

function psort()
{
  switch(connected())
  {
    case 1:
      start_organ();
      break;
    case 2:
      organ_win.document.Organ.psort();
      break;
    case 3:
      turn_java("off");
      alert("No java - No Music!");
    default:
      break;
  }
}

function pprint()
{
  switch(connected())
  {
    case 1:
      start_organ();
      break;
    case 2:
      organ_win.document.Organ.pprint();
      break;
    case 3:
      turn_java("off");
      alert("No java - No Music!");
    default:
      break;
  }
}

// Netscape styles loss on resize
// ==============================
// http://www.webreview.com/pub/1999/05/07/style

if (ns4)
{
  origWidth = innerWidth;
  origHeight = innerHeight;
}

function re_do()
{
   if (innerWidth != origWidth || innerHeight != origHeight)
     location.reload();
}

if (ns4)
  onresize = re_do;

// Prevent open in new window
// ==========================

if (window.name != "biop" && window.location.href.indexOf("maint.html") < 0)
{
  document.location.href="../index.html";
  alert("You will be transferred to the start now.");
}
