/*********
 * 
 * div_expand.js
 * 
 * author: James Stone, james.stone@sjsu.edu
 *
 * for use on a page like the student success stories where you
 * want a series of more... buttons that expand and collapse a 
 * div containing more text/html
 * 
 * use in html document
 *  
 * <div id="hide_text_button_10"></div>
 * this will be replaced with a button that says more... on page load/init
 * where 10 is a number ascending from 1 to 20
 *
 * <div class="sucess_text" id="hide_text_10">
 * the contents of this div will be hidden on page load and expanded by
 * the above button
 *
 * Revision History
 * 
 * 07-Jan-2009:  Created basic functionality and tested with student success stories
 * 
 ******/
if (window.addEventListener) // Firefox
{
	window.addEventListener('load', init, false);;
} 

else if (window.attachEvent) // IE
{   
	window['onload'] = init;
} 

function init() {
	initDivExpand();
}


// toggles the obj (div) open or closed (visible or hidden)
function switchMenu(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != "none" ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}

// toggles all text boxes
function switchMenuAll() {
	for (var x = 1; x < 21; x++) {
		switchMenu('hide_text_' + padDigits(x, 2));
	}
}

// to create leading zeros
function padDigits(n, totalDigits) { 
	n = n.toString(); 
	var pd = ''; 
	if (totalDigits > n.length) 
	{ 
		for (i=0; i < (totalDigits-n.length); i++) 
		{ 
			pd += '0'; 
		} 
	} 
	return pd + n.toString(); 
} 

// init code for the student success stories
function initDivExpand() {
	var tempD;
	var myNum;

	// build buttons

	tempD = document.getElementById('expand_all_hidden_text');
	if (tempD) {
		tempD.innerHTML='<p><a onclick=\"switchMenuAll();\" href=\"#expand_all\" title=\"Expand all...\">Expand all...</a></p>';
	}
    tempD = null;


// init each text box / button
	for (var x = 1; x < 21; x++) {
		myNum = padDigits(x, 2);
		tempD = document.getElementById('hide_text_button_' + myNum);
		// build buttons
		if (tempD) {
			tempD.innerHTML='<p><a onclick=\"switchMenu(\'hide_text_' + myNum + '\');\" href=\"#more\" title=\"Read more...\">Read more...</a></p>';
			switchMenu('hide_text_' + myNum);  // close the divs initially
		}
		tempD = null;
	}
	
}



