// JavaScript Document

var speed = 0.3;


var divs = new Array(); 
var openheights = new Array(); 
var ints = new Array(); 


function initialiseRM () {
	
	
	 var tags = document.getElementsByTagName('div');
	  
	 for (var i = 0 ; i < tags.length ; i++) {
		  
		  
		if (tags[i].className.indexOf("readmore") > -1)
		{
		
		   var rm_div = tags[i]; // get the read more div
		   divs.push(rm_div); // save the div
		   rm_div.style.height="auto";
		   openheights.push(rm_div.offsetHeight+20); // save its open height
		   
		   // set the height and other style properties to hide the readmore
		   rm_div.style.height = '1px';
		   rm_div.style.overflow = 'hidden';
		   
		   // add the link to readmore
		   var divInt = divs.length - 1
		   var readmorelink = document.createElement("a");
		   readmorelink.appendChild(document.createTextNode('read more . . . '));
		   readmorelink.href = "#";
		   readmorelink.setAttribute('id', 'rml'+divInt);
		   readmorelink.onclick = function () {
			   toggleRM(divInt);
			   return false;
		   }
		   rm_div.parentNode.appendChild(readmorelink);
		
		
		} 
	  }
}

function toggleRM (divInt) {
	
	 var h = parseInt(divs[divInt].style.height);
	 
	
	 if (h == 1) {
		 // closed
		 ints[divInt] = setInterval("animateDivOpen("+divInt+");", 20);
	 } else if (h == openheights[divInt]) {
		 // open
		 ints[divInt] = setInterval("animateDivClose("+divInt+");", 20);
	 }
	
}


function animateDivOpen (divInt) {
	 var h = parseInt(divs[divInt].style.height);
	 
	var newHeight = h + ((openheights[divInt] - h )*speed) + 1;
	
	newHeight = Math.ceil(newHeight);
	if (newHeight >= openheights[divInt]) {
		divs[divInt].style.height = openheights[divInt] + "px";
		clearInterval(ints[divInt]);
		document.getElementById('rml'+divInt).innerHTML = 'read less . . . ';
	} else {
		divs[divInt].style.height = newHeight + "px";
			//alert(divs[divInt].style.height);

	}
	
	
}

function animateDivClose (divInt) {
	 var h = parseInt(divs[divInt].style.height);
	var newHeight = (h * (1-speed)) - 1;
	newHeight = Math.floor(newHeight);
	
	if (newHeight <= 1) {
		divs[divInt].style.height = "1px";
		clearInterval(ints[divInt]);
		document.getElementById('rml'+divInt).innerHTML = 'read more . . . ';
	} else {
		divs[divInt].style.height = newHeight + "px"
	}
	
	
}


