// LCGENCLIENT.JS

var puzzle_id = 0;

var callSlotChangedFromApplet = base_callSlotChangedFromApplet;

function start_puzzle() {
  save_str = getQuery("save");
  if (save_str) load_data();
}

function load_data() {
  var grid = getElement("Grid");
  if (grid && grid.isActive()) {
    grid.setSyncString(save_str);
    grid.repaint();
  }
  else
    window.setTimeout("load_data()", 100);
}

function getElement(id) {
  var ns4 = (document.layers)? true:false;
  var ns6 = (document.getElementById)? true:false;
  var ie = (document.all)? true:false;

  if (ns4) return document.layers[id];
  if (ns6) return document.getElementById(id);
  if (ie) return document.all[id];
  return null;
}


function getQuery(nameToLookFor) {
  var retQuery = '';
  var query = window.location.
    href.substring(window.location.href.indexOf('?') + 1, 
		   window.location.href.length);

  var queries = query.split('&');
  if (arguments.length >= 1) {
    for (var i = 0; i < queries.length; i++) {
      if (queries[i].indexOf(nameToLookFor + "=") != -1)
	retQuery = queries[i].substring(nameToLookFor.length + 1, queries[i].length);
    }
  }
  else
    retQuery = query;

  return retQuery;
} 

function open_popup(url, options, ie_size, ns_size, title) {
  var size = "";
  if (navigator.appName=="Netscape") 
    size = ns_size;
  else
    size = ie_size;

  if (! title) title = "_blank";

  if (title == "_self") {
    window.location = url;
    return self;
  }

  var Splitted = size.split('=');
  var Width = parseInt(Splitted[1]);
  var Height = parseInt(Splitted[2]);
  var LeftPos = screen.width?(screen.width - Width)/2:0;
  var TopPos = screen.height?(screen.height - Height)/2:0;
  var OptionString = size + ",top=" + TopPos + ",left=" + 
    LeftPos + "," + options;

  my_popUp = window.open(url, title, OptionString);
  if (my_popUp) {
    my_popUp.location.href = url; 
    //workaround fuer Macs und einige UNIX-Versionen
    my_popUp.focus();
  }
  return my_popUp;
}

function release_session(url, session) {
  var img = new Image();
  img.src = (url + 
	     "?puzzle_id=" + escape(puzzle_id) + 
	     "&session=" + escape(session));
}

function callSubclassFromApplet(code) {
  window.setTimeout(code, 0);
}

function call_action(action) {
}

function callLinkFromApplet(action, time, argument) {
  call_action(action, time, argument);
}

function call_default_action(action, time, argument) {
  wid = 640;
  hgh = 480;
  leftPos = screen.width ? (screen.width - wid) / 2 : 0;
  topPos = screen.height ? (screen.height - wid) / 2 : 0;
  window.open(action, "newwin", 
	      "width=" + wid + ",height=" + hgh + ",top=" + topPos + 
	      ",left=" + leftPos + 
	      ",scrollbars=yes,location=yes,directories=no," +
	      "status=yes,menubar=yes,toolbar=yes,resizable=yes");
}

function panic(prepath) {
  if (! prepath) prepath = "";
  var PanicPath = prepath + "../../client/boss_page.html";

  x=screen.availWidth;
  y=screen.availHeight;
  target = parseFloat(navigator.appVersion.
		      substring(navigator.appVersion.indexOf('.')-1,
				navigator.appVersion.length));
    
  if((navigator.appVersion.indexOf("Mac")!=-1) &&(navigator.userAgent.indexOf("MSIE")!=-1) &&(parseInt(navigator.appVersion)==4))
    window.open(PanicPath,"sub",'scrollbars=yes');
  else if (target >= 4){
    if (navigator.appName=="Netscape"){
      var panic=window.open(PanicPath,"panic",'scrollbars=yes','width='+x+',height='+y+',top=0,left=0');
      panic.moveTo(0,0);
      panic.resizeTo(x,y);}
    if (navigator.appName=="Microsoft Internet Explorer")
      window.open(PanicPath,"panic","fullscreen=yes");
  }
  else
    window.open(PanicPath,"sub",'scrollbars=yes');
}

function base_callSlotChangedFromApplet() {
  call_ivwtag(11); 
}



//-------- VARS FUER IVW-ROUTINEN --------//

var ivw_token_number = 1;

var ivw_tag = "";  //der ivw tag wird im speicher gehalten
var ivw_tag_exists = false;

var get_ivwtag;
function trivial_getivwtag_handler() {return "";}
get_ivwtag = trivial_getivwtag_handler;


//-------- IVW-ROUTINEN --------// 
function presetIVWVars() {
  // Is Tok in the FileName?
  var DocumentPath = window.location.href + "";
  var TokStart = DocumentPath.indexOf("tok");
    
  if (TokStart != -1) {
    s = DocumentPath.substr(TokStart + 3);
    ivw_token_number = parseInt(s);
  }

  var tag = "x";
  tag = get_ivwtag() + "";
  if (tag.length > 1) {
    ivw_tag_exists=true;
    ivw_tag = tag ; 
  }
}

function do_ivwtag_withcheck() {
  var img = new Image();
  if(ivw_tag_exists)
    img.src=ivw_tag;
}


function advanced_call_ivwtag(select) {
  if (select > 1) {       //grid=11, //wordboxes=12
    for(i = 0; i < ivw_token_number; i++) 
      do_ivwtag_withcheck();
  }
  else {
    do_ivwtag_withcheck();
  }
}

var call_ivwtag = advanced_call_ivwtag;
