﻿var $ = function(id){return document.getElementById(id)};
window.onload= function()
{
  var h2 = document.getElementsByTagName('h2');
  var hl = h2.length;
  for(var i=0; i<hl; i++)
  {
    h2[i].onclick = autoOC;
  }
}
function autoOC(){
  var obj = $(this.getAttribute('obj'));
  var status = obj.getAttribute("status")==null;
  var oh = parseInt(obj.offsetHeight);
  var h =  parseInt(obj.scrollHeight);
  obj.style.height = oh;
  obj.style.display = "block";
  obj.style.overflow = "hidden";
  if(obj.getAttribute("oldHeight") == null){
    obj.setAttribute("oldHeight", oh);
    this.style.background = "url(images/close.gif) right no-repeat";
	this.style.color="#7C5A24";
  }else{
    var oldH = Math.ceil(obj.getAttribute("oldHeight"));
    this.style.background = "url(images/open.gif) right no-repeat"
	this.style.color="#000";
  }
  var reSet = function(){
    if(status){
      if(oh < h){
        oh = Math.ceil(h-(h-oh)/1.2);
        obj.style.height = oh+"px";
      }else{
        obj.setAttribute("status",false);
        window.clearInterval(IntervalId);
      }
    }else{
      if(oh > 0){
        oh = Math.floor(oh-oh/3);
        obj.style.height = oh+"px";
      }else{
        obj.removeAttribute("status");
        obj.removeAttribute("oldHeight");
        window.clearInterval(IntervalId);
      }
    }
  }
  var IntervalId = window.setInterval(reSet, status==false?20:15);
  return status;
};
