var speed = 5;

function toggle_reveal(id, elem_id, final_height){

   // Get the element
   var elem = document.getElementById(elem_id);
   var anc = document.getElementById(id + "anchor");

   // Setup heights
   if(elem.status != 0 && elem.status != 1 && elem.status != -1){ 
	elem.style.height = 0;
        anc.style.backgroundImage="url(/images/" + id + "bakclose.gif)";
   }

   // Clear timeout if exists
   if (elem.movement){ clearTimeout(elem.movement); }

   // Get current height
   var height = parseInt(elem.style.height);

   // Get status
   var status;
   if(elem.status == 0 || elem.status == 1){ status = elem.status; }
   else{
      if(height == 0){
         status = 1;
         elem.status = status;
      }
      else{
         status = 0;
         elem.status = status;
      }
   }

   // Opening
   if(status == 1){
      // If the final height has been reached and we are not just starting
      if(height == final_height) {
         anc.style.backgroundImage = "url(/images/" + id + "bakopen.gif)";
         elem.status = -1;
         return;
      }
      var dist = Math.ceil( (final_height - height) / speed);
      height = height + dist;
      elem.style.height = height + "px";
   }
   // Closing
   else{
      // If the final height has been reached and we are not just starting
      if (height == 0){
         anc.style.backgroundImage="url(/images/" + id + "bakclose.gif)";
         elem.status = -1;
         return;
      }
      var dist = Math.ceil(height / speed);
      height = height - dist;
      elem.style.height = height + "px";
   }

   // Set timeout
   repeat = "toggle_reveal('" + id + "', '" + elem_id + "', '" + final_height + "')";
   elem.movement = setTimeout(repeat, 1);
}

function prepare_reveal() {
   var links = document.getElementsByTagName("a");
   for (var i=0; i < links.length; i++){
      links[i].onclick = function(){
         var destination = this.getAttribute("href");
         if (destination.indexOf("#subscribe") != -1){ toggle_reveal('sub', 'subprthide', 150); }
         if (destination.indexOf("#emailarticle") != -1){ toggle_reveal('eml', 'emlhide', 335); }
         if (destination.indexOf("#bookmark") != -1){ toggle_reveal('bok', 'bokhide', 80); }
      }
   }
}

add_load_event(prepare_reveal);


