
var alldivs = document.getElementsByTagName("DIV");

var showtimer = null;
var hidetimer = null;

var maxoffsetheight = 200;

for (var x=0; x<alldivs.length; x++)
{
	if(alldivs[x].className == 'commentsdiv')
	{
		targetdivID = alldivs[x].id.replace(/comments/,"inlinecomments");
		targetDivObj = document.getElementById(targetdivID);
		//alert(targetDivObj.scrollHeight);
		//alert(targetDivObj.offsetHeight);
		//targetDivObj.style.height = targetDivObj.offsetHeight.toString() + 'px';
		//targetDivObj.style.height = '150px';
		//alert('style: ' + targetDivObj.style.height + ' offsetheight: ' + targetDivObj.offsetHeight + ' scrollheight: ' + targetDivObj.scrollHeight);
		if(targetDivObj.scrollHeight >= maxoffsetheight)
		{
			targetDivObj.style.height = maxoffsetheight.toString() + 'px';
		}
		else
		{
			targetDivObj.style.height = targetDivObj.scrollHeight.toString() + 'px';
		}
		alldivs[x].onclick = function()
		{
			clearInterval(hidetimer);
			clearInterval(showtimer);
			//alert("test");
			targetdivID = this.id.replace(/comments/,"inlinecomments");
			
			targetDivObj = document.getElementById(targetdivID);
			
			//alert('style: ' + targetDivObj.style.height + ' offsetheight: ' + targetDivObj.offsetHeight + ' scrollheight: ' + targetDivObj.scrollHeight);
			if(targetDivObj.offsetHeight > 0)
			{
				//alert("hide");
				//alert(targetdivID);
				if(this.innerText)
				{
					this.innerText = "Show Comments: ";
				}
				if(this.textContent)
				{
					this.textContent = "Show Comments: ";
				}
				hidetimer = setInterval(function(){hidecomments(targetdivID);}, 50);
			}
			else if(targetDivObj.offsetHeight < maxoffsetheight)
			{
				//alert("show");
				if(this.innerText)
				{
					this.innerText = "Hide Comments: ";
				}
				if(this.textContent)
				{
					this.textContent = "Hide Comments: ";
				}
				showtimer = setInterval(function(){showcomments(targetdivID)}, 50);
			}
		}
		//alert(document.getElementById(targetdivID).id);		
	}
}

function hidecomments(targetcomments)
{
	//clearInterval(showtimer);
	//alert("inside hider");
	//clearInterval(hidetimer);
	//alert(targetcomments);
	var target = document.getElementById(targetcomments);
	if(parseInt(target.style.height) <= 15)
	{
		target.style.height = '0px';
	}
	if(parseInt(target.style.height) > 0)
	{
		target.style.height = (parseInt(target.style.height) - 15).toString() + 'px';
	}
	if(parseInt(target.style.height) <= 0)
	{
		target.style.height = '0px';
		clearInterval(hidetimer);
	}
}

function showcomments(targetcomments)
{
	//clearInterval(hidetimer);
	//clearInterval(showtimer);
	var target = document.getElementById(targetcomments);
	//var maxheight = 200;
	if(target.scrollHeight >= maxoffsetheight)
	{
		//alert(target.scrollHeight);
		maxheight = maxoffsetheight;
	}
	else
	{
		maxheight = target.scrollHeight;
	}
	
	if(parseInt(target.style.height) < maxheight)
	{
		target.style.height = (parseInt(target.style.height) + 15).toString() + 'px';
	}
	if(parseInt(target.style.height) >= maxheight)
	{
		target.style.height = maxheight.toString() + 'px';
		clearInterval(showtimer);
	}
}
