var timerlen = 5;
var slideAniLen = 250;

var timerID;
var startTime;
var obj;
var endHeight;
var endWidth;
var moving;

var ugly_selectorText_workaround_flag = false;
var allStyleRules;

function slideDiv() {
  if(moving) return;

  moving = true;
  startslide();
}

function startslide(){
  obj = document.getElementById("DIV_MENU");

  endHeight = parseInt(obj.offsetHeight);
  endWidth = parseInt(obj.offsetWidth);
  startTime = (new Date()).getTime();

  obj.style.height = "1px";
  var iconmenu = document.getElementById("iconmenu");
  obj.style.top = eval(curTop(iconmenu) + iconmenu.offsetHeight)

  timerID = setInterval('slidetick();',timerlen);
}

function slidetick(){
  var elapsed = (new Date()).getTime() - startTime;

  if (elapsed > slideAniLen) {
    endSlide()
  } else {
    var d = Math.round(elapsed / slideAniLen * endHeight);
    var w = Math.round(elapsed / slideAniLen * endWidth);
    obj.style.height = d + "px";
    obj.style.width = w + "px";
  }

  return;
}

function endSlide(){
  clearInterval(timerID);

  obj.style.height = originalHeight;
  obj.style.width = originalWidth;

  moving = false;
  timerID = null;
  startTime = null;
  endHeight = null;
  obj = null;

  return;
}

function curTop(obj) {
  var curtop = 0;

  if (obj.offsetParent) {
    do {
      curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);
  }

  return curtop;
}

function curLeft(obj) {
  var curleft = 0;

  if (obj.offsetParent) {
    do {
      curleft += obj.offsetLeft;
    } while (obj = obj.offsetParent);
  }

  return curleft;
}

// code developed using the following workaround (CVS v1.15) as an example.
// http://lxr.mozilla.org/seamonkey/source/extensions/xmlterm/ui/content/XMLTermCommands.js
function ugly_selectorText_workaround() {
  if((navigator.userAgent.indexOf("Gecko") == -1) || (ugly_selectorText_workaround_flag)) {
    return; // we've already been here or shouldn't be here
  }
//  var styleElements = document.getElementsByTagName("style");
//
//  for(var i = 0; i < styleElements.length; i++) {
//    var styleText = styleElements[i].firstChild.data;
//    // this should be using match(/\b[\w-.]+(?=\s*\{)/g but ?= causes an
//    // error in IE5, so we include the open brace and then strip it
//    var regex = new RegExp("\b[\w-.]+(\s*\{)", "g");
//    allStyleRules = styleText.match(regex);
//  }
//
//  if (allStyleRules) {
//    for(var i = 0; i < allStyleRules.length; i++) {
//      // probably insufficient for people who like random gobs of
//      // whitespace in their styles
//      allStyleRules[i] = allStyleRules[i].substr(0, (allStyleRules[i].length - 2));
//    }
//  }
  ugly_selectorText_workaround_flag = true;
}

// getStyleById: given an element ID and style property
// return the current setting for that property, or null.
// args:
//  i - element id
//  p - property
function getStyleById(i, p) {
  var n = document.getElementById(i);
  var s = eval("n.style." + p);

  // try inline
  if((s != "") && (s != null)) {
    return s;
  }

  // try currentStyle
  if(n.currentStyle) {
    var s = eval("n.currentStyle." + p);
    if((s != "") && (s != null)) {
      return s;
    }
  }

  // try styleSheets
  var sheets = document.styleSheets;
  if(sheets.length > 0) {
    // loop over each sheet
    for(var x = 0; x < sheets.length; x++) {
      // grab stylesheet rules
      var rules = sheets[x].cssRules;
      if(rules.length > 0) {
        // check each rule
        for(var y = 0; y < rules.length; y++) {
          var z = rules[y].style;
          // selectorText broken in NS 6/Mozilla: see
          // http://bugzilla.mozilla.org/show_bug.cgi?id=51944
          ugly_selectorText_workaround();
          if(allStyleRules) {
            if(allStyleRules[y] == i) {
              return z[p];
            }
          } else {
            // use the native selectorText and style stuff
            if(((z[p] != "") && (z[p] != null)) || (rules[y].selectorText == i)) {
              return z[p];
            }
          }
        }
      }
    }
  }
  return null;
}
