var UNANSWERED = -2;
var UNDECIDED  = -1;
var INCORRECT  =  0;
var CORRECT    =  1;

var SCORE2IMAGE = {};
SCORE2IMAGE[UNANSWERED] = 'nix.gif',
SCORE2IMAGE[UNDECIDED] = 'curl_orange.gif',
SCORE2IMAGE[INCORRECT] = 'cross_red.gif',
SCORE2IMAGE[CORRECT] = 'curl_green.gif'

var SCORE2TYPE = {}
SCORE2TYPE[UNANSWERED] = 'unanswered';
SCORE2TYPE[UNDECIDED] = 'undecided';
SCORE2TYPE[INCORRECT] = 'incorrect';
SCORE2TYPE[CORRECT] = 'correct';

function Utils()
{
}

if (self == top)
{
  Array.prototype.clone = function()
  {
    //alert("clone" + "\nlength: " + this.length);
    var b = [];
    for (var i=0; i<this.length; i++)  
      b[i] = this[i];
    return b;
  };

  Array.prototype.randomize = function()
  {
    //alert("randomize" + "\nlength: " + this.length);  
    for (var i=0; i<this.length; i++)
    {
      var rnd = utils.getRandomNumber(0, this.length);

      var tmp = this[rnd];
      this[rnd] = this[i];
      this[i] = tmp;
    }
  };
}
else
{
  //alert("self != top");
  Array.prototype.clone = top.Array.prototype.clone;
  Array.prototype.randomize = top.Array.prototype.randomize;
}

Utils.prototype = {
  goTo: function(url, isRedirect, doc)
  {
    if ((url == 'inloggen.asp' || url == 'aanmelden.asp') && top.document.body.clientHeight < 620)
    {
      var w = window.open('../?c=' + url, 'taalklasPlayer', 'fullscreen=yes');
      if(w != null) return;
    }
    
    if (!isRedirect)
      top.prevContentLocation = document.location.href;

    doc = doc || document;
    doc.location.href = url;
  },
  writeButton: function(msg, href, className)
  {
    var s = ''
    + '<button onclick="document.location.href = \'' + href + '\';" class="' + className + '" unselectable="on" onmouseenter="top.hoverEl(event)" onmouseleave="top.unhoverEl(event)">'
    + msg
    + '</button>';
    document.writeln(s);
  },
  writeButtonText: function(msg, action, centerMsg, top, left, imgsrc, width, height, id)
  {
    var s = this.getButtonText(msg, action, centerMsg, top, left, imgsrc, width, height, id);

    document.writeln(s);
  },
  getButtonText: function(msg, action, centerMsg, top, left, imgsrc, width, height, id)
  {
    var s = '<div ' + (id?'id="' + id + '" ':'') + 'class="buttonTextXLdiv" onclick="' + action + '" onmouseenter="top.hoverEl(event)" onmouseleave="top.unhoverEl(event)" style="' + ((typeof(top) != 'undefined')?' top:' + top + 'px;':'') + ((typeof(left) != 'undefined')?' left:' + left + 'px;':'') + '" unselectable="on">'
    + '<div class="buttonTextXLmsg' + (centerMsg?' center':'') + '" unselectable="on" style="width:' + (width?(width-5):240) + 'px;' + (height?(' padding-top:' + Math.max(0, (height/2-13)) + 'px'):'') + '">' + msg + '</div>'
    + '<img src="' + (imgsrc?imgsrc:'media/buttons/buttonTextXL_pas.gif') + '" width="' + (width?width:245) + '" height="' + (height?height:60) + '" class="buttonTextXLimg">'
    + '</div>';

    return s;
  },
  setInnerHTML: function(el, html, keepSingleLine)
  {
    el.runtimeStyle.removeAttribute('fontSize');

    if (!keepSingleLine)
      el.innerHTML = html;
    else
    {
      el.innerHTML = '&nbsp;';
      var preHeight = el.offsetHeight;
      el.innerHTML = html;
      var postHeight = el.offsetHeight;
      while (postHeight > preHeight)
      {
        var fs = 1*el.currentStyle.fontSize.replace(/\D/g, '');
        fs--;
        el.runtimeStyle.fontSize = fs + 'px';
        postHeight = el.offsetHeight;
      }
    }
  },
  getRandomNumber: function(min, max)
  {
    var num;
    do
    {
      num = Math.floor(min + (max-min)*Math.random());
    } while (num < min || num >= max);
    return num;
  },
  getSemiRandomNumber: function(min, max, excludeds)
  {
    excludeds = excludeds || [];
    var rnd;
    do
    {
      rnd = this.getRandomNumber(min, max);
    } while (this.contains(excludeds, rnd));
    
    return rnd;
  },
  extendListRandomly: function(list, newLen, nrsMax)
  {
    var num;
    while (list.length < newLen)
    {
      num = this.getSemiRandomNumber(0, nrsMax, list);
      list = list.concat([num]);
    }
    list.randomize();  
    return list;
  },
  contains: function(arr, v)
  {
    for (var i=0; i<arr.length; i++)  
      if (arr[i] == v)
        return true;
    return false;
  }
  
};

document.location.getParameter = function(name)
{
  var nvs = document.location.search.substring(1).split('&');
  for (var i=0; i<nvs.length; i++)
  {
    var nv = nvs[i].split('=');
    if (nv[0] == name)
      return nv[1];
  }
  
  return null;
};

var utils = new Utils();

var curHoverEl;
var curHoverImg;
function hoverEl(evt)
{
  var el = evt.toElement; if (!el) return;
  if (el.nodeName == "IMG")
  {
    if (el.className != 'manual' && el.src.indexOf('_pas') != -1) {
      if (top.unhoverTO)
      {
        clearTO();
        top.doUnhover();
      }
      el.src = el.src.replace('_pas', '_over');
      top.curHoverEl = evt.srcElement;
      top.curHoverImg = el;
    }
  }
  else
  {
    if (el.nodeName == "DIV" && el.currentStyle.backgroundImage.indexOf('_pas')) {
      if (top.unhoverTO)
      {
        clearTO();
        top.doUnhover();
      }
      el.style.backgroundImage = el.currentStyle.backgroundImage.replace('_pas', '_over');
      top.curHoverEl = evt.srcElement;
      top.curHoverImg = el;
    }
  }
};

function unhoverEl(evt)
{
  /*var el = evt.fromElement; if (!el) return;
  if (el.nodeName == "IMG")
  {
    if (el.src.indexOf('_over')) el.src = el.src.replace('_over', '_pas');
  }
  else
  {
    if (el.nodeName == "DIV" && el.currentStyle.backgroundImage.indexOf('_over')) el.style.backgroundImage = el.currentStyle.backgroundImage.replace('_over', '_pas');
  }*/
};

var unhoverTO;
function hover()
{
  if (!top.unhoverTO) return;
  if (!top.curHoverEl) return;
  var el = event.srcElement; if (!el) return;
  
  try {
    if (top.curHoverEl.contains(el))
    {
      top.clearTimeout(top.unhoverTO);
      top.unhoverTO = false;
    }
  }catch(e){}
};

function unhover()
{
  if (!top.curHoverEl)
    return;
  var el = event.srcElement; if (!el) return;
  
  clearTO();
  top.unhoverTO = top.setTimeout("top.doUnhover()", 50);
}  

function clearTO()
{
  if (top.unhoverTO)
    try {top.clearTimeout(top.unhoverTO);} catch(e) {}
}

function doUnhover()
{
  top.unhoverTO = false;
  top.curHoverEl = null;
  el = top.curHoverImg; 
  if (!el) return;
  try{
  if (el.nodeName == "IMG")
    el.src = el.src.replace('_over', '_pas');
  else
    if (el.nodeName == "DIV") 
      el.style.backgroundImage = el.currentStyle.backgroundImage.replace('_over', '_pas');
  }catch(e){}
}

// attaches an event on an element (ie. <body onclick="" />)  
function attachEventHandler(theEl, theEvent, theHandler, theObject)
{

  var isIE = (navigator.appName.toLowerCase().indexOf('internet explorer') != -1);

  theEvent = theEvent.toLowerCase();
  if (isIE)
  {
    if (theObject)
      theEl.attachEvent(theEvent, function() { theHandler.call(theObject) } );
    else
      theEl.attachEvent(theEvent, theHandler);
  }
  else
  {
    if (theEl == document.body)
      theEl = document;
    if (theEvent == "ondragstart")
      theEvent = "ondraggesture";
    theEl.addEventListener(theEvent.substring(2), theHandler, true);
  }
}


attachEventHandler(window, 'onload', function() {
  attachEventHandler(document.body, 'onmouseover', hover);
  attachEventHandler(document.body, 'onmouseout', unhover);
  for (var i=0; i<document.images.length; i++)
  {
    var img = document.images[i];
    if (img.className == "buttonTextXLimg")
      continue;
    attachEventHandler(img, 'onmouseenter', hoverEl);
    attachEventHandler(img, 'onmouseleave', unhoverEl);
  }
});

function doOK()
{
  
  var reg = /^[\w]+$/;
  if(!reg.test(document.f.name.value)){
    document.getElementById("errMsg").style.visibility = "visible";
    document.getElementById("errMsg").innerHTML = "Gebruik letters, getallen of een combinatie ervan.";
    return false;
  }
  document.f.submit();
}

function doFocus(id)
{
  document.getElementById(id).select();
}

function getXPosRect(el) 
{
  var x;

  if (document.getBoxObjectFor) 
  {   
    var bo = document.getBoxObjectFor(el);   
    x = bo.x;   
  }
  else if (el.getBoundingClientRect) 
  {   
    var rect = el.getBoundingClientRect();   
    x = rect.left;   
  }

  el.x = x;
  
  return el.x;
}

function getElementsByTagNameAttributeValue(ancestorEl, tagName, attrName, attrValue) {
  if (typeof(attrValue) == "undefined")
    attrValue = null;

  var els = [];
  var a = ancestorEl.getElementsByTagName(tagName);
  for (var i=0; i<a.length; i++)
  {
    var el = a[i];
    switch (attrName)
    {
      case "className":
      case "class":
        if (el.className.indexOf(attrValue) != -1)
          els.push(el);
        break;
      default:
        var val = el.getAttribute(attrName);
        if ((val != null) && ((attrValue == null) || (val == attrValue)))
          els.push(el);
    }
  }
  return els;
};

function getParentElementByTagNameAttributeValue(el, tagName, attrName, attrValue) {
  if (typeof(attrValue) == 'undefined')
    attrValue = null;

  tagName = tagName.toLowerCase();

  while (el && el.getAttribute && el.parentNode)
  {
    if (el.tagName && (el.tagName.toLowerCase() == tagName))
    {
      switch (attrName)
      {
        case "className":
        case "class":
          if (el.className.indexOf(attrValue) != -1)
            return el;
        default:
          var val = el.getAttribute(attrName);
          if ((val != null) && ((attrValue == null) || (val == attrValue)))
            return el;
      }
    }
    el = el.parentNode;
  }

  return null;
};

function addToClass(el, s)
{
  el.className += ' ' + s;
}

function removeFromClass(el, s)
{
  el.className = el.className.replace(new RegExp('\s*' + s, 'g'), '');
}

function trim(s)
{
    return (s+"").replace(/^\s+|\s+$/g, "");
}
