/**
 * site.js
 * 
 * Set of Javascript helper functions used in various other javascripts.
 * 
 * @author Various
 * @author Collected/Contributed/Created by Aaron Berk (theberkoeffect.com) and David DiMaria info@collectivesessions.com
 */
function addLoadListener(fn) {
	if (typeof window.addEventListener != 'undefined') {
		window.addEventListener('load', fn, false);
	} else if (typeof document.addEventListener != 'undefined') {
		document.addEventListener('load', fn, false);
	} else if (typeof window.attachEvent != 'undefined') {
		window.attachEvent('onload', fn);
	} else {
		var oldfn = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = fn;
		} else {
			window.onload = function() {
				oldfn();
				fn();
			};
		}
	}
}

/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
    Usage: getElementsByClassName(document, 'img' ,'image');
*/
// Can replace all of these calls with Moo calls.
// used in 
// dashboard.js
// schedule.js
function getElementsByClassName(oElm, strTagName, strClassName) {
	var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];		
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}	
	}
	return (arrReturnElements)
}


// only used in this script:
// show()
// hide()
function showHide( divName, boolShow ) {
	
	if ( ( obj = $(divName) ) != null ) {
	
		if( boolShow ) {			 
			v = 'visible';	
			d = "inline-block";
		}	
		else {
			v = 'hidden'
			d = "none";
		}	
				
		obj.setStyle('visibility', v);	
		obj.setStyle('display', d);	
	}
}

function isVisible( divName ) {
	
	if ( ( obj = $(divName) ) != null ) {
	
		//obj = getStyle( obj );
		
		return !( obj.getStyle('visibility') == 'hidden' && obj.getStyle('display') == 'none' );
	}
}


// Also defined in dashboard.js
// dashboard.js
// schedule.js
function getScrollY() {
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}

// Used in schedule popups:
// dashboard.js
// schedule.js
// Overridden by moo-core?
function getHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function showArray( aryDivNames, strType ) {
	
	for( var i=0; i<aryDivNames.length; i++ ) {		
		show( aryDivNames[i], strType );
	}
}


function hideArray( aryDivNames, strType ) {
	
	for( var i=0; i<aryDivNames.length; i++ ) {		
		hide( aryDivNames[i], strType );
	}
}

function show( divName, strType ) {
	
	switch( strType ) {
		
		case "simple":
			showHide( divName, true );
			break;
	}
}


function hide( divName, strType ) {
	
	switch( strType ) {
		
		case "simple":
			showHide( divName, false );
			break;
	}
}

function getSubnavIDs() {
	
	return ['participants_nav','groups_nav','roles_nav','facilities_nav','resources_nav','categories_nav'];
}

function hasClass( obj, strClass ) {
	
	aryClass = obj.className.split(' ');
	
	for( var i=0; i<aryClass.length; i++) {
	
		if( aryClass[i] == strClass )
			return true;
	}
	
	return false;
}

function subnavSelected( obj, strClass ) {
	
	arySubnavIDs = getSubnavIDs();
	
	for( var i=0; i<arySubnavIDs.length; i++) {
	
		if( hasClass(document.body, arySubnavIDs[i].split('_')[0] ) )
			return true;
	}
	
	return false;
}


function toggleSubnav() {
	var manageElement = document.getElementById('manage');
	if( !!manageElement ) {
		if( !subnavSelected() && isVisible( getSubnavIDs()[0] ) ) {
			hideArray( getSubnavIDs(), 'simple' );
			document.getElementById('manage' ).className = 'has_children';
		}
		else {
			showArray( getSubnavIDs(), 'simple' );
			document.getElementById('manage').className = 'has_children_open';
		}
	}
	return false;
}

addLoadListener( toggleSubnav );