/****
Author: Jerome Mouneyrac
Bug Reference: http://tracker.moodle.org/browse/MDL-14439
IE and Opera fire the onchange when ever you move into a dropdwown list with the keyboard.
These functions fix this problem.
****/

/*
global variables

Note:
if I didn't use global variables, we would need to pass them as parameter:  
=> in initSelect(): 
   I would write "theSelect.onchange = selectChanged(...);"
   This code causes a javascript error on IE. (not firefox)
so I had to write theSelect.onchange = selectChanged; It's why I use global variables .
Because I use global variables, I didn't put this code in javascript-static.js.
This file is loaded in javascript.php.
*/ 
var select_formid;
var select_targetwindow;

//we redefine all user actions on the dropdown list
//onfocus, onchange, onkeydown, and onclick
function initSelect(formId,targetWindow)
{
    //initialise global variables
    select_formid=formId;
    select_targetwindow=targetWindow;

    var theSelect = document.getElementById(select_formid+"_jump");

    theSelect.changed = false;

    selectFocussed();

    theSelect.onchange = selectChanged;
    theSelect.onkeydown = selectKeyed;
    theSelect.onclick = selectClicked;
    
    return true;
}

function selectChanged(theElement)
{
    var theSelect;
    
    if (theElement && theElement.value)
    {
        theSelect = theElement;
    }
    else
    {
        theSelect = this;
    }
    
    if (!theSelect.changed)
    {
        return false;
    }

    //here is the onchange redirection
    select_targetwindow.location=document.getElementById(select_formid).jump.options[document.getElementById(select_formid).jump.selectedIndex].value;                                
    
    return true;
}

function selectClicked()
{
    this.changed = true;
}

function selectFocussed()
{
    this.initValue = this.value;
    
    return true;
}

//we keep Firefox behaviors: onchange is fired when we press "Enter", "Esc", or "Tab"" keys.
//note that is probably not working on Mac (keyCode could be different)
function selectKeyed(e)
{
    var theEvent;
    var keyCodeTab = "9";
    var keyCodeEnter = "13";
    var keyCodeEsc = "27";
    
    if (e)
    {
        theEvent = e;
    }
    else
    {
        theEvent = event;
    }

    if ((theEvent.keyCode == keyCodeEnter || theEvent.keyCode == keyCodeTab) && this.value != this.initValue)
    {
        this.changed = true;
        selectChanged(this);
    }
    else if (theEvent.keyCode == keyCodeEsc)
    {
        this.value = this.initValue;
    }
    else
    {
        this.changed = false;
    }
    
    return true;
}

function at_show_aux(parent, child)
{
  var p = document.getElementById(parent);
  var c = document.getElementById(child );

  var top  = (c["at_position"] == "y") ? p.offsetHeight+2 : 0;
  var left = (c["at_position"] == "x") ? p.offsetWidth +2 : 0;

  for (; p; p = p.offsetParent)
  {
    top  += p.offsetTop;
    left += p.offsetLeft;
  }

  c.style.position   = "absolute";
  c.style.top        = top +'px';
  c.style.left       = left+'px';
  c.style.visibility = "visible";
}

// ***** at_show *****

function at_show()
{
  var p = document.getElementById(this["at_parent"]);
  var c = document.getElementById(this["at_child" ]);

  at_show_aux(p.id, c.id);
  clearTimeout(c["at_timeout"]);
}

// ***** at_hide *****

function at_hide()
{
  var p = document.getElementById(this["at_parent"]);
  var c = document.getElementById(this["at_child" ]);

  c["at_timeout"] = setTimeout("document.getElementById('"+c.id+"').style.visibility = 'hidden'", 333);
}

// ***** at_click *****

function at_click()
{
  var p = document.getElementById(this["at_parent"]);
  var c = document.getElementById(this["at_child" ]);

  if (c.style.visibility != "visible") at_show_aux(p.id, c.id); else c.style.visibility = "hidden";
  return false;
}

// ***** at_attach *****

// PARAMETERS:
// parent   - id of the parent html element
// child    - id of the child  html element that should be droped down
// showtype - "click" = drop down child html element on mouse click
//            "hover" = drop down child html element on mouse over
// position - "x" = display the child html element to the right
//            "y" = display the child html element below
// cursor   - omit to use default cursor or specify CSS cursor name

function at_attach(parent, child, showtype, position, cursor)
{
  var p = document.getElementById(parent);
  var c = document.getElementById(child);

  p["at_parent"]     = p.id;
  c["at_parent"]     = p.id;
  p["at_child"]      = c.id;
  c["at_child"]      = c.id;
  p["at_position"]   = position;
  c["at_position"]   = position;

  c.style.position   = "absolute";
  c.style.visibility = "hidden";

  if (cursor != undefined) p.style.cursor = cursor;

  switch (showtype)
  {
    case "click":
      p.onclick     = at_click;
      p.onmouseout  = at_hide;
      c.onmouseover = at_show;
      c.onmouseout  = at_hide;
      break;
    case "hover":
      p.onmouseover = at_show;
      p.onmouseout  = at_hide;
      c.onmouseover = at_show;
      c.onmouseout  = at_hide;
      break;
  }
}
