// toggle divs and adds a given selction to a field
// divs has to be named as xx_div, where xx is the name of a given selectable field
//
// divs has to "register" using AddDiv

// Testet i Netscape 4.73, 6.2 og Exploder 5.0

var Adivs = new Array();

function AddDiv(Lname) {
  // append div id til array
  //alert("AddDiv" + Lname);
  Adivs[ Adivs.length ] = Lname;
  // ret top og left value ( med udgangspunkt i Netscape 4. )
  var TopValue = FindDiv(Lname).top;
  var LeftValue = FindDiv(Lname).left;
  if ( is_gecko) { // Gecko layout engine.
    var PxEnd = TopValue.indexOf("px");
    TopValue = TopValue.substring(0,PxEnd);
    TopValue = parseInt(TopValue) - 1;
    TopValue = TopValue + 'px';
    document.getElementById(Lname).style.top = TopValue;
    var PxEnd = LeftValue.indexOf("px");
    LeftValue = LeftValue.substring(0,PxEnd);
    LeftValue = parseInt(LeftValue) - 2;
    LeftValue = LeftValue + 'px';
    document.getElementById(Lname).style.left = LeftValue;
  }
  else if (is_ie5up || is_opera) { // IE 4 and later
    var PxEnd = TopValue.indexOf("px");
    TopValue = TopValue.substring(0,PxEnd);
    TopValue = parseInt(TopValue) + 6;
    TopValue = TopValue + 'px';
    document.getElementById(Lname).style.top = TopValue;
    var PxEnd = LeftValue.indexOf("px");
    LeftValue = LeftValue.substring(0,PxEnd);
    LeftValue = parseInt(LeftValue) + 0;
    LeftValue = LeftValue + 'px';
    document.getElementById(Lname).style.left = LeftValue;
  }
}

function FindDiv(divname) {
  if ( is_gecko || is_ie5up ) { // Gecko layout engine, IE 4 and later
     return document.getElementById(divname).style;
  } else { // earlier versions
     return document.layers[divname];
  }
}

function xFindDiv(divname) {
  return document.getElementById(divname);
}
function FlipDiv(on,off) {
  xFindDiv( on ).style.visibility = "visible";
  xFindDiv( on ).style.position = "relative";
  xFindDiv( off ).style.visibility = "hidden";
  xFindDiv( off ).style.position = "absolute";
}

function HideDivs() {
  for (i = 0; i < Adivs.length; i++) {
    FindDiv(Adivs[i]).visibility = "hidden";
  }
}

function FindDivName(FromField) {
  if (typeof FromField == "string") {
    for (i = 0; i < Adivs.length; i++)
      if (Adivs[i].indexOf(FromField) != -1) {
        return FindDiv(Adivs[i]);
      }
  } else {
    idx = FromField.selectedIndex;
    div = FromField[idx].value + '_div';
    if (FindDiv(div))
      return FindDiv(FromField[idx].value + '_div');
  }
}
function HideHelp(FromField) {
  FindDivName(FromField).visibility = "hidden";
}
function ShowHelp(FromField) {
  //HideDivs();
  FindDivName(FromField).visibility = "visible";
}
function findPosX(obj) {
  var curleft = 0;
  if (document.getElementById || document.all) {
    while (obj.offsetParent) {
      curleft += obj.offsetLeft
      obj = obj.offsetParent;
    }
  }
  else if (document.layers)
    curleft += obj.x;
  return curleft;
}

function findPosY(obj) {
  var curtop = 0;
  if (document.getElementById || document.all) {
    while (obj.offsetParent) {
      curtop += obj.offsetTop
      obj = obj.offsetParent;
    }
  }
  else if (document.layers)
    curtop += obj.y;
  return curtop;
}

function getScreenHeight() {
  if (document.body.clientHeight) return document.body.clientHeight;
  if (document.documentElement.clientHeight) return document.documentElement.clientHeight;
  return window.innerHeight;
}

function ShowHelpFloat(FromField, Anchor, Xoffset) {
  for (i = 0; i < Adivs.length; i++)
    if (Adivs[i].indexOf("default") == -1)
      FindDiv(Adivs[i]).visibility = "hidden";
  //alert(Anchor.offsetLeft+" "+Anchor.offsetTop);
  D = FindDivName(FromField);
  helpHeight = document.getElementById(FromField+"_div").clientHeight;
  screenHeight = getScreenHeight()+document.body.scrollTop+document.documentElement.scrollTop;
  //alert("Left:"+Anchor.offsetLeft+" Top:"+Anchor.offsetTop+" name:"+Anchor.name+" x:"+findPosX(Anchor)+" y:"+findPosY(Anchor));
  //D.left = Anchor.offsetLeft+80; D.top = Anchor.offsetTop+120;
  D.left = findPosX(Anchor)+Xoffset;
  D.top = findPosY(Anchor)+15;
  if (findPosY(Anchor) + helpHeight + 17 > screenHeight)
    D.top = screenHeight - helpHeight - 2;
  D.visibility = "visible";
}


function admin_set_action (form, action, what) {
    if ( confirm('Er du sikker?') ) {
        form.admin_action.value = action;
        form.what.value = what;
        form.submit();

        return false;
    } else {
        return false;
    }
}



function toggle_period_item (id) {
    var cell = document.getElementById(id);

    if ( !cell ) {
        alert('Could not find cell id: ' + id);
    }

    if ( cell.style.display == 'none' ) {
        var imgs = cell.getElementsByTagName('img');

        for (var i = 0; i < imgs.length; i++) {
            imgs[i].src = imgs[i].getAttribute('href');
        }

        cell.style.display = 'block';
    } else {
        cell.style.display = 'none';
    }

    return false;
}


function timeline_set_focus (id) {
    var a_id = 'anchor_title_' + id;
    var cell = document.getElementById(a_id);

    toggle_period_item(id);
    cell.focus();
    window.scrollBy(0,300);
}
