// JavaScript Document


animAmount = 15;
animSpeed = 15;

justOpened = '';


function getElemHight(obj) {
	y = document.getElementById(obj);
	return y.offsetHeight;
}

function getElemScroll(obj) {
	y = document.getElementById(obj);
	return y.scrollHeight;
}

function setHeight(obj, h){
	x = document.getElementById(obj);
	x.style.height = h + 'px';
}


function animateShow(obj, ht, htChange, fSpeed, htAmount) {

  if (document.getElementById) {
  	
   // objID = document.getElementById(obj);
   	setHeight(obj, ht);
    if (ht <= htAmount) {
      
      ht += htChange;
      window.setTimeout("animateShow('" +obj+ "',"+ht+", "+htChange+", "+fSpeed+", "+htAmount+")", fSpeed);
    }
  }
}


function animateHide(obj, ht, htChange, fSpeed, htAmount) {

  if (document.getElementById) {
  	if(ht < 0){
		ht = 0;
	}
	
	
   // objID = document.getElementById(obj);
   	setHeight(obj, ht);
    if (ht > 0) {
      
      ht -= htChange;
      window.setTimeout("animateHide('" +obj+ "',"+ht+", "+htChange+", "+fSpeed+", "+htAmount+")", fSpeed);
    }
	
  }
}



function initalizeElements(){
	if (!document.getElementsByTagName){ 
		return; 
	}
	
	
	
	var btns = document.getElementsByTagName('h3');
	var divs = document.getElementsByTagName('div');
	var btnNum = 0;
	var contNum = 0;
	
	// loop through all div tags
	for (var i=0; i<divs.length; i++){
		var div = divs[i];
		
		var classAttribute = String(div.className);
			
		// use the string.match() method to catch 'expandContent' references in the rel attribute - IMPORTANT - can't have that value+more E.G. expandContentBig
		if (classAttribute.match('expandContent')){
			
			div.setAttribute('id','expander' + contNum);
			contNum++;
		}
	}
	
	
	
	// loop through all h3 tags
	for (var i=0; i<btns.length; i++){
		var btn = btns[i];
		
		var classAttribute = String(btn.className);
		// use the string.match() method to catch 'expandBtn' references in the rel attribute - IMPORTANT - can't have that value+more E.G. expandBtnBig
		if (classAttribute.match('expandBtn')){
			
			btn.setAttribute('rel', btnNum);
			btn.onclick = function () {
				
				clickedBtn = 'expander' + this.getAttribute('rel');
				
				if(!justOpened){
					animateShow(clickedBtn, 0, animSpeed, animAmount, getElemScroll(clickedBtn));
					justOpened = clickedBtn;
				} else {
					if(clickedBtn == justOpened){
						animateHide(clickedBtn, getElemHight(clickedBtn), animSpeed, animAmount, 0);
						justOpened = '';
					} else {
						animateShow(clickedBtn, 0, animSpeed, animAmount, getElemScroll(clickedBtn));
						animateHide(justOpened, getElemHight(justOpened), animSpeed, animAmount, 0);
						justOpened = clickedBtn;
					}
				}
				
				return false;
			}
			
			btnNum++;
		}
	}
}



window.onload = function(){
	initalizeElements();
} 