/*********************************************************************************
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
**********************************************************************************/

if(typeof sGfxPath == "undefined"){
	sGfxPath = "../graphics/Buttons/Default/";
}

////////////////////////////////
///////      CORE      /////////
////////////////////////////////
/*
aTSToggle[x] = index toggle group
aTSToggle[x][0] = toggle type (true = 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
aTSToggle[x][10] = memory (a non empty string will save under that variable name in memory)
aTSToggle[x][11] = prevent cookie reading on page load (used when wanting to load manually)
*/

//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, sCookieName, bAutoLoad){
	var sFinalCookie = '';
	var bCookieAutoLoad = true;
	if(typeof aTSToggle == "undefined"){
		aTSToggle = new Array();
	}

	if(typeof sCookieName != "undefined" && sCookieName != ''){
		sFinalCookie = sUniqueCookiePrefix + "_" + sCookieName;
		if(typeof bAutoLoad != "undefined"){
			bCookieAutoLoad = bAutoLoad;
		}
	}
	
	aTSToggle[nIdx] = [bType, bCollapse, bTable, "", "", "", "", "", "", false, sFinalCookie, bCookieAutoLoad];
}

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(document.getElementById(sId).style.display == "block" || document.getElementById(sId).style.display == sTSDisplay){
		bBlock = true;
	}
	
	//remove previous selected item from the toggle cookie
	if(aTSToggle[nGroup][10] != "" && aTSToggle[nGroup][0] && typeof aTSToggle[nGroup][3].id != "undefined"){
		saveState(aTSToggle[nGroup][3].id, true, nGroup);
	}
	
	showItem(document.getElementById(sId), nGroup, bTween);

	if(aTSToggle[nGroup][10] != ""){
		saveState(sId, bBlock, nGroup);
	}
}	

//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(aTSToggle[nGroup][10] != ""){
						saveState(aToggleAll[i], false, nGroup);
					}
				}else{
					document.getElementById(aToggleAll[i]).style.display = 'none';
					if(aTSToggle[nGroup][10] != ""){
						saveState(aToggleAll[i], true, nGroup);
					}
				}
			}
		}
	}
}

//This function is used when saving toggle state is enabled
function saveState(sId, bBlock, nGroup){
	var sCookie = getCookie(aTSToggle[nGroup][10]);
	var aCookie = new Array();
	var bFound = false;

	bBlock = !bBlock;

	if(sCookie != '' && sCookie != "null"){
		if(sCookie != null){
			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;
		}
	}else if(bBlock){
		//add id to cookie (no other Ids have been added yet)
		sCookie = sId;
	}
	
	document.cookie = aTSToggle[nGroup][10] + "=" + escape(sCookie) + "; path=/";
}

function getCookie(sName){
  var results = document.cookie.match ( '(^|;) ?' + sName + '=([^;]*)(;|$)' );

  if (results)
    return (unescape (results[2]));
  else
    return null;
}

function deleteCookie(sName){
	document.cookie = sUniqueCookiePrefix + "_" + sName + "=; expires=Thu, 01-Jan-1970 00:00:01 GMT;path=/"
}

function toggleCookieLoad(){
	for(var nIdx = 0; nIdx < aTSToggle.length; nIdx++){
		//if memory variable is defined and autoload is enabled
		if(typeof aTSToggle[nIdx] != "undefined" && aTSToggle[nIdx][10] != "" && aTSToggle[nIdx][11]){
			loadToggleGroupCookie(nIdx);
		}
	}
}

function loadToggleGroupCookie(nIdx){
	sCookie = "";
	aCookie = new Array();
		
	sCookie = getCookie(aTSToggle[nIdx][10]);
	if(sCookie != null && sCookie != "null"){
		aCookie = sCookie.split('|');
		for(var i = 0; i < aCookie.length; i++){
			if(document.getElementById(aCookie[i]) != undefined){
				if(aTSToggle[nIdx][2]){
					document.getElementById('row_' + aCookie[i]).style.display = sTSDisplay
					document.getElementById(aCookie[i]).style.display = "block";			
				}else{
					document.getElementById(aCookie[i]).style.display = "block";
				}
				if(document.getElementById(aCookie[i] + "_img") != undefined){
					document.getElementById(aCookie[i] + "_img").src = sGfxPath + "close.gif";
				}
				
				try{
					if(typeof eval('cookieLoadFunction' + nIdx) != "undefined"){
						eval('cookieLoadFunction' + nIdx) (aCookie[i]);
					}
				}catch(e){}
			}
		}
		//need to set currently selected item if colapse is enabled and page is loading
		if(aTSToggle[nIdx][0] && aCookie.length == 1 && aCookie[0] != ''){
			aTSToggle[nIdx][3] = document.getElementById(aCookie[0]);
		}
	}
}

function windowOnload(s) {
	var sPreviousOnload = window.onload;
	window.onload = function(){ 
		if(sPreviousOnload){
			sPreviousOnload(); 
		}
		eval(s); 
	}
}

if(document.getElementById){
	windowOnload("toggleCookieLoad();")
	//window.onload = toggleCookieLoad;
	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>');
}




