/*********************************************************************************
toggle.js - this file is used for the expand/collapse functionality for
			div blocks

external variables - sGfxPath: if path to graphics folder is different than the
                               default, it can be specified here
					 bExpand: if defined, items will start in an expanded state
					 sMemory: sets the variable name for toggle memory via cookie 
					 		  if not defined memory feature is inactive 
**********************************************************************************/

var sLastItem = "";

if(typeof sGfxPath == "undefined"){
	sGfxPath = "../graphics/Buttons/Default/";
}

////////////////////////////////
///////      CORE      /////////
////////////////////////////////
/*
aTSToggle[x] = index toggle group
aTSToggle[x][0] = toggle type (false = one item open at a time)
aTSToggle[x][1] = toggle collapse (false = do not allow collapse)
aTSToggle[x][2] = toggle table structure (false = toggle on div not table rows)
aTSToggle[x][3] = id of current selection
aTSToggle[x][4] = id of next selection
aTSToggle[x][5] = item expanding (false = collapse)
aTSToggle[x][6] = height of item
aTSToggle[x][7] = increment speed of item
aTSToggle[x][8] = setinterval pointer
aTSToggle[x][9] = interval in use
*/

//init vars
if(typeof aTSToggle == "undefined"){
	initTS(0, false, true, false);
}
if(typeof bTSTween == "undefined"){
	bTSTween = true;
}

var sTSDisplay = "";
var sBrowser = navigator.appName;
var nTSBaseInc = 12;
var nTSInterval = 40;

if (sBrowser != "Microsoft Internet Explorer"){
	sTSDisplay = 'table-row';
}else{
	sTSDisplay = 'block';
}

function initTS(nIdx, bType, bCollapse, bTable){
	if(typeof aTSToggle == "undefined"){
		aTSToggle = new Array();
	}
	aTSToggle[nIdx] = [bType, bCollapse, bTable, "", "", "", "", "", "", false];
}

function tween(nGroup){
	aTSToggle[nGroup][9] = true;
	var nCurrHeight = parseInt(aTSToggle[nGroup][3].style.height.substring(0, aTSToggle[nGroup][3].style.height.indexOf('px')));
	if(aTSToggle[nGroup][5]){
		//expand item
		if(nCurrHeight >= (aTSToggle[nGroup][6] - aTSToggle[nGroup][7])){
			aTSToggle[nGroup][3].style.height = aTSToggle[nGroup][6];
			aTSToggle[nGroup][3].style.height = 'auto';
			clearInterval(aTSToggle[nGroup][8]);
			aTSToggle[nGroup][9] = false;
		}else{
			aTSToggle[nGroup][3].style.height = (nCurrHeight + aTSToggle[nGroup][7]);
		}
	}else{
		//collapse item
		if(nCurrHeight <= (0 + aTSToggle[nGroup][7])){
			toggleItemBullet(aTSToggle[nGroup][3].attributes.id.value);
			if(aTSToggle[nGroup][2]){
				document.getElementById('row_' + aTSToggle[nGroup][3].attributes.id.value).style.display = 'none';
			}
			aTSToggle[nGroup][3].style.display = 'none';
			clearInterval(aTSToggle[nGroup][8]);
			if(aTSToggle[nGroup][4].style != undefined){
				toggleItemBullet(aTSToggle[nGroup][4].attributes.id.value);
				if(aTSToggle[nGroup][2]){
					document.getElementById('row_' + aTSToggle[nGroup][4].attributes.id.value).style.display = sTSDisplay;
				}
				setParameters(aTSToggle[nGroup][4], nGroup);
				aTSToggle[nGroup][4] = "";
				aTSToggle[nGroup][5] = true;
				aTSToggle[nGroup][8] = setInterval('tween(' + nGroup + ')', nTSInterval);
			}else{
				aTSToggle[nGroup][3] = "";
				aTSToggle[nGroup][9] = false;
			}
		}else{
			aTSToggle[nGroup][3].style.height = (nCurrHeight - aTSToggle[nGroup][7]);
		}
	}
}  

function getHiddenTSHeight(o){
	//get offset height of hidden item
	var n = 0;
	o.style.position = 'absolute';
	o.style.visibility = 'hidden';
	o.style.display = 'block';
	n = o.offsetHeight;
	o.style.display = 'none';
	o.style.visibility = 'visible';
	o.style.position = 'static';
	return n;
}

function showItem(o, nGroup, bTween){
	if(nGroup == undefined){
		var nGroup = 0;
	}
	if(bTween == undefined){
		var bTween = bTSTween;
	}	
	
	if(!aTSToggle[nGroup][9]){
		if(o.style.display == 'block'){
			//collapse item
			if(aTSToggle[nGroup][1]){
				if(bTween){
					aTSToggle[nGroup][3] = o;
					aTSToggle[nGroup][6] = aTSToggle[nGroup][3].offsetHeight;
					setInc(nGroup);
					aTSToggle[nGroup][3].style.height = aTSToggle[nGroup][6];
					aTSToggle[nGroup][5] = false;
					aTSToggle[nGroup][8] = setInterval('tween(' + nGroup + ')', nTSInterval);
				}else{
					//no tween effect
					if(aTSToggle[nGroup][2]){
						document.getElementById('row_' + o.attributes.id.value).style.display = 'none';
					}
					toggleItemBullet(o.attributes.id.value);
					aTSToggle[nGroup][3] = "";
					o.style.display = 'none';
				}
			}
			
		}else{
			if(aTSToggle[nGroup][0] && aTSToggle[nGroup][3] != ""){
				//expand item and collapse previous
				if(bTween){
					aTSToggle[nGroup][4] = o;
					aTSToggle[nGroup][6] = aTSToggle[nGroup][3].offsetHeight;
					setInc(nGroup);
					aTSToggle[nGroup][3].style.height = aTSToggle[nGroup][6];
					aTSToggle[nGroup][5] = false;
					aTSToggle[nGroup][8] = setInterval('tween(' + nGroup + ')', nTSInterval);
				}else{
					//no tween effect
					if(aTSToggle[nGroup][2]){
						document.getElementById('row_' + aTSToggle[nGroup][3].attributes.id.value).style.display = 'none';
					}
					toggleItemBullet(aTSToggle[nGroup][3].attributes.id.value);
					aTSToggle[nGroup][3].style.display = 'none'
					aTSToggle[nGroup][3] = o;
					if(aTSToggle[nGroup][2]){
						document.getElementById('row_' + aTSToggle[nGroup][3].attributes.id.value).style.display = sTSDisplay;
					}
					toggleItemBullet(aTSToggle[nGroup][3].attributes.id.value);
					aTSToggle[nGroup][3].style.display = 'block';
				}
			}else{
				//expand item
				if(aTSToggle[nGroup][2]){
					document.getElementById('row_' + o.attributes.id.value).style.display = sTSDisplay;
				}
				toggleItemBullet(o.attributes.id.value);
				if(bTween){
					setParameters(o, nGroup);
					aTSToggle[nGroup][5] = true;
					aTSToggle[nGroup][8] = setInterval('tween(' + nGroup + ')', nTSInterval);
				}else{
					//no tween effect
					aTSToggle[nGroup][3] = o;
					o.style.display = 'block';
				}
			}
		}
	}
}

function checkInterval(nGroup){
	return aTSToggle[nGroup][9];
}

function killCurrentItem(nGroup){
	if(nGroup == undefined){
		var nGroup = 0;
	}
	if(aTSToggle[nGroup][3] != ""){
		aTSToggle[nGroup][3].style.display = 'none';
		aTSToggle[nGroup][3] = "";
	}
}

function killAllIntervals(){
	for(var i = 0; i < aTSToggle.length; i++){
		clearInterval(aTSToggle[i][8]);
	}
}

function setParameters(o, nGroup){
	if(nGroup == undefined){
		var nGroup = 0;
	}
	aTSToggle[nGroup][3] = o;
	o.style.height = '';
	aTSToggle[nGroup][6] = getHiddenTSHeight(o);
	setInc(nGroup);
	o.style.height = 1;
	o.style.display = 'block';
}

function setInc(nGroup){
	if(aTSToggle[nGroup][6] > 80){
		if(aTSToggle[nGroup][6] > 80 && aTSToggle[nGroup][6] <= 150){
			aTSToggle[nGroup][7] = nTSBaseInc + 2;
		}else if(aTSToggle[nGroup][6] > 150 && aTSToggle[nGroup][6] <= 300){ 
			aTSToggle[nGroup][7] = nTSBaseInc + 5;
		}else if(aTSToggle[nGroup][6] > 300 && aTSToggle[nGroup][6] <= 500){ 
			aTSToggle[nGroup][7] = nTSBaseInc + 12;
		}else if(aTSToggle[nGroup][6] > 500 && aTSToggle[nGroup][6] <= 700){ 
			aTSToggle[nGroup][7] = nTSBaseInc + 22;
		}else if(aTSToggle[nGroup][6] > 700 && aTSToggle[nGroup][6] <= 1000){ 
			aTSToggle[nGroup][7] = nTSBaseInc + 28;
		}else if(aTSToggle[nGroup][6] > 1000 && aTSToggle[nGroup][6] <= 1500){ 
			aTSToggle[nGroup][7] = nTSBaseInc + 35;
		}else if(aTSToggle[nGroup][6] > 1500 && aTSToggle[nGroup][6] <= 2500){ 
			aTSToggle[nGroup][7] = nTSBaseInc + 45;
		}else if(aTSToggle[nGroup][6] > 2500 && aTSToggle[nGroup][6] <= 5000){ 
			aTSToggle[nGroup][7] = nTSBaseInc + 60;
		}else if(aTSToggle[nGroup][6] > 5000){ 
			aTSToggle[nGroup][7] = nTSBaseInc + 80;
		}
	}else{
		aTSToggle[nGroup][7] = nTSBaseInc;
	}
}

//If item has a bullet the bullet id must be appended by '_img'
function toggleItemBullet(sId){
	
	if(document.getElementById(sId + "_img") != undefined){
		//image is defined. change source accordingly
		if(document.getElementById(sId).style.display == "block"){
			document.getElementById(sId + "_img").src = sGfxPath + "open.gif";
		}else{
			document.getElementById(sId + "_img").src = sGfxPath + "close.gif";
		}
	}
}

////////////////////////////////
///////  WRAPPER CODE  /////////
////////////////////////////////

var aToggleAll = new Array(); //this is for if we are using the toggle all function

function expandcontent(sId, nGroup, bTween){
	var bBlock = false;
	if(nGroup == undefined){
		var nGroup = 0;
	}
	if(bTween == undefined){
		var bTween = bTSTween;
	}
	// if bCollapse exists we want to collapse the last open item
	if(typeof bCollapse != "undefined"){
		if(sLastItem != "" && sLastItem != sId){
			if(document.getElementById(sLastItem + "_img") != undefined){
				document.getElementById(sLastItem + "_img").src = sGfxPath + "open.gif";
			}
			if(typeof sMemory != "undefined"){
				saveState(sLastItem);
			}
		}
	}
	
	if(document.getElementById(sId).style.display == "block"){
		bBlock = true;
	}
	
	showItem(document.getElementById(sId), nGroup, bTween);

	if(typeof sMemory != "undefined"){
		saveState(sId, bBlock);
	}
		
	sLastItem = sId;
}	

//this function will expand or contract all elements in the aToggleAll array. It will NOT apply tween effect.
function toggleAll(bExpand, nGroup){
	if(nGroup == undefined){
		var nGroup = 0;
	}
	if(!aTSToggle[nGroup][9]){
		//reset pointers
		aTSToggle[nGroup][3] = "";
		aTSToggle[nGroup][4] = "";
		for(var i = 0; i < aToggleAll.length; i++){
			if((bExpand && document.getElementById(aToggleAll[i]).style.display != 'block') || (!bExpand && document.getElementById(aToggleAll[i]).style.display == 'block')){
				toggleItemBullet(aToggleAll[i]);
				document.getElementById(aToggleAll[i]).style.height = 'auto';
				if(bExpand){
					document.getElementById(aToggleAll[i]).style.display = 'block';
					if(typeof sMemory != "undefined"){
						saveState(aToggleAll[i], false);
					}
				}else{
					document.getElementById(aToggleAll[i]).style.display = 'none';
					if(typeof sMemory != "undefined"){
						saveState(aToggleAll[i], true);
					}
				}
			}
		}
	}
}

//This function is used when saving toggle state is enabled
function saveState(sId, bBlock){
	var nStartIdx = 0, nEndIdx = 0;
	var sCookie = "";
	var aCookie = new Array();
	var bFound = false;

	bBlock = !bBlock;
	nStartIdx = document.cookie.indexOf(sMemory + "=");

	if(nStartIdx > -1){
		nStartIdx = nStartIdx + sMemory.length + 1;
		nEndIdx = document.cookie.indexOf(";", nStartIdx);
		sCookie = document.cookie.substring(nStartIdx, nEndIdx);
		aCookie = sCookie.split('|');
		
		for(var i = 0; i < aCookie.length; i++){
			if(aCookie[i] == sId){
				if(!bBlock){
					//remove value from cookie
					aCookie.splice(i, 1);
				}else{
					bFound = true;
				}
				break;
			}
		}
		
		if(bBlock && !bFound){
			//id is expanded and not added to the cookie yet
			aCookie.push(sId);
		}
		
		sCookie = aCookie.join('|');
	}else if(bBlock){
		//add id to cookie (no other Ids have been added yet)
		sCookie = sId;
	}

	document.cookie = sMemory + "=" + sCookie;
}

function windowLoad(){
	var nStartIdx = 0, nEndIdx = 0;
	var sCookie = "";
	var aCookie = new Array();
	nStartIdx = document.cookie.indexOf(sMemory + "=");
	if(nStartIdx > -1){
		nStartIdx = nStartIdx + sMemory.length + 1;
		nEndIdx = document.cookie.indexOf(";", nStartIdx);
		sCookie = document.cookie.substring(nStartIdx, nEndIdx);
		aCookie = sCookie.split('|');
		for(var i = 0; i < aCookie.length; i++){
			if(document.getElementById(aCookie[i]) != undefined){
				document.getElementById(aCookie[i]).style.display = "block";
				if(document.getElementById(aCookie[i] + "_img") != undefined){
					document.getElementById(aCookie[i] + "_img").src = sGfxPath + "close.gif";
				}
			}
		}
	}
}

if(document.getElementById){
	if(typeof sMemory != "undefined"){
		window.onload = windowLoad;
	}
	document.write('<style type="text/css">');
	if(typeof bExpand != "undefined"){
		document.write('.switchcontent{display:block;overflow:hidden;}');
	}else{
		document.write('.switchcontent{display:none;overflow:hidden;}');
	}
	document.write('</style>');
}




