/*
** jQuery Text Expander
**
** Usage:	Div must have REL set to unique value
**			Div must have ID set to unique value
**			any anchor link with same REL as div will control div
**			Use name field of div to designate custom height by number of lines of text visible (defaults to 4)
**			any item with class vtile will expand based on the height of largest exCol Div			
**
**			Div Structure Example:
**			<div class="excopy">              								OUTER DIV CONTAINER
**				<div class="exCol">											PER TEXT COLUMN CONTAINER (used for height measure)
**					<div class="expandable">TEXT BLOCK GOES HERE</div>		EXPANDABLE TEXT
**					<div class="expandable">TEXT BLOCK GOES HERE</div>
**				</div>
**			</div>
**
**	WARNING: do not end text block with a <br> as it will make getLineHeight return an incorrect value
**/

//setup expansion on document.ready
$(document).ready(function() {
	 $('div.expandable').expander();
});

var exAnimRunning=false; //is expander animating?
var exColLargest=0; //used to keep track of the largest text column for divider size
var vDivOffset=140; //offset between height of divider and column height (divider height = exCol - vDivOffset)
(function($) {
	$.fn.expander = function(options) {
    	return this.each(function(i) {
			var $this = $(this);
			$this.id=$this.attr('id');
			var LineHeight=getLineHeight($this.id);  //get text line height
			if($this.attr('name')>0)
				var startHeight=$this.attr('name')*LineHeight;
			else
				var startHeight=4*LineHeight;
				
			$this.attr('lh',LineHeight);
			$this.attr('name',$this.height());
			if($this.height() > startHeight){
				$this.height(startHeight);
			$("a[@rel="+$this.attr('rel')+"]").click(function(){
				var dcl="div[@rel="+$this.attr('rel')+"_changeTxt]";
				var newLtxt=$(dcl).attr('name');
				$(dcl).attr('name',$(dcl).attr('innerHTML'));
				$(dcl).attr('innerHTML',newLtxt);
				ExpanderScaleDiv($this.id);
				return false;
			});
			expNum=i; //to ensure divs resize after all text is scaled down
			setTimeout("expanderSetDivs("+i+")",30)
			}else{
				$("div[@rel="+$this.attr('rel')+"_changeTxt]").hide();
			}
		});
	};
	function ExpanderScaleDiv(dObj){ //opens/closes divs
		if(!exAnimRunning){
			exAnimRunning=true;
			if(!$('#'+dObj).hasClass('opened')){
				var dOldH=$('#'+dObj).attr('name');
				$('#'+dObj).attr('name',$('#'+dObj).height());
				expanderExpandTo(dObj,dOldH);
			}else{
				var dOldH=$('#'+dObj).attr('name');
				$('#'+dObj).attr('name',$('#'+dObj).height());
				expanderSnapTo(dObj,dOldH);       //snap closed
//				expanderCollapseTo(dObj,dOldH);   //slide closed
 			}
		}
	}
})(jQuery);

var expNum=0;
//sets up the initial size of the verticle dividers
function expanderSetDivs(i){ 
	if(i==expNum){
		var DivHeight=$("#exCopy").innerHeight()-vDivOffset;
		expanderGetMaxHeight();
		$(".vtile").height(exColLargest-vDivOffset);
	}
}

//gets largest column height for divider height
function expanderGetMaxHeight(){ 
	$("div.exCol").each(function(i){
		if(i==0)
			exColLargest=0;
		if(exColLargest < $(this).height())
			exColLargest=$(this).height();
	});
}

// sets text column height without animation
//   @ dObj = object to effect
//   @ tHeight = target height
function expanderSnapTo(dObj,tHeight){
	$('#'+dObj).css('height',tHeight+'px');
	expanderGetMaxHeight();
	$(".vtile").height(exColLargest-vDivOffset);
	$('#'+dObj).removeClass('opened');
	exAnimRunning=false;
}

// animates text column collapse (choppy if the page is scrolled)
//   @ dObj = object to effect
//   @ tHeight = target height
function expanderCollapseTo(dObj,tHeight){
	var pObj=$('#'+dObj).attr('id');
	if($('#'+dObj).height() > tHeight){
		$('#'+dObj).height(parseInt($('#'+dObj).height())-parseInt($('#'+dObj).attr('lh')));
		expanderGetMaxHeight();
		$(".vtile").height(exColLargest-vDivOffset);
		setTimeout('expanderCollapseTo("'+pObj+'",'+tHeight+')',1);
	}else{
		$('#'+dObj).height(tHeight);
		$('#'+dObj).removeClass('opened');
		exAnimRunning=false;
	}
}

// animates text column expansion
//   @ dObj = object to effect
//   @ tHeight = target height
function expanderExpandTo(dObj,tHeight){
	var pObj=$('#'+dObj).attr('id');
	if($('#'+dObj).height() < tHeight){
		$('#'+dObj).height(parseInt($('#'+dObj).height())+(parseInt($('#'+dObj).attr('lh'))*2));
		expanderGetMaxHeight();
		$(".vtile").height(exColLargest-vDivOffset);
		setTimeout('expanderExpandTo("'+pObj+'",'+tHeight+')',1);
	}else{
		$('#'+dObj).addClass('opened');
		exAnimRunning=false;
	}
}

// returns text line height of an element
//   @ dObj = object to effect
function getLineHeight(dObj) {
	var addendum = "<br clear=\"both\">X";
	var h0 = $('#'+dObj).height();
	var oldHTML=$('#'+dObj).attr('innerHTML')
	$('#'+dObj).attr('innerHTML',oldHTML+addendum);
	var h1 = $('#'+dObj).height();
	$('#'+dObj).attr('innerHTML',oldHTML);
	return h1-h0;
}