//**************************************************************************************
// Instantiate global CDA object
if(!window.CDA) {
	CDA=new Object();
} // end if

	CDA.getDocBody = function() {
		return document.getElementsByTagName("body")[0];
	} // end method	

	if(navigator.appName.indexOf("Netscape") != -1)
	{
		CDA.getMaxX		= function() {return(pageXOffset+innerWidth);}
		CDA.getMaxY		= function() {return(pageYOffset+innerHeight);}
	}
	else 	if(document.all) 	{
		CDA.getMaxX		= function() {return(document.body.scrollLeft+document.body.clientWidth);}
		CDA.getMaxY		= function() {return(document.body.scrollTop+document.body.clientHeight);}
	} // end if
	
	CDA.createActivity = function(parentWin) {
		var loadImg = new Image(); loadImg.src="images/loading.gif";
		loadImg.id="imgLoader";
		var textSpan = document.createElement("span");
		textSpan.appendChild(document.createTextNode("Loading..."))
		var outElem = document.createElement("div");		
		outElem.id="divActivity";
		outElem.className="activity"
		outElem.appendChild(loadImg);
		outElem.appendChild(textSpan);		
		return outElem;
	} // end method
	
	CDA.removeActivity = function(parentWin) {			
		var activity = CDA.find("divActivity");	
		if (activity) {			
			var parent = activity.parentNode;
			parent.removeChild(activity);
		}
	} // end method

	CDA.find = function(elemId) {
		return document.getElementById(elemId);		
	} // end method
	

	CDA.findHeight = function(elem) {
		var e = (typeof elem=="object") ? elem : CDA.find(elem);		
		return e.offsetHeight
	} // end function
	
	CDA.findWidth = function(elem) {
		var e = (typeof elem=="object") ? elem : CDA.find(elem);		
		return e.offsetWidth;
	} // end function

	CDA.findTop = function(elem) {
		var oNode = (typeof elem=="object") ? elem : CDA.find(elem);
		var iTop = 0;
   
   		while(oNode.tagName != 'HTML') {
			iTop += oNode.offsetTop || 0;
			if(oNode.offsetParent) { //i.e. the parent element is not hidden
				oNode = oNode.offsetParent;
  			}
  			else {
 				break;
  			}
   		}
   		return iTop;
	} // end function
	
	CDA.findLeft = function(elem)
	{
		var oNode = (typeof elem=="object") ? elem : CDA.find(elem);
   		var iLeft = 0;
   		while(oNode.tagName != 'HTML') {
      		iLeft += oNode.offsetLeft || 0;
      		if(oNode.offsetParent) { //i.e. the parent element is not hidden
         		oNode = oNode.offsetParent;
      		}
      		else {
         		break;
      		}
   		}
   		return iLeft;    	
	} // end function
	
	CDA.findBottom = function(elem)	{
		var curElem = (typeof elem=="object") ? elem : CDA.find(elem);
		var top = CDA.findTop(curElem);				
		var height = CDA.findHeight(curElem)
		return top + height;		
	} // end function
	
	
if(!window.CDA.Html) {
	CDA.Html=new Object();	
} // end if

	CDA.Html.makeAnchor = function(href, anchorChild, target) {
		var link = document.createElement("a");
		link.href=href;			
		if (typeof anchorChild=="string") {
			link.appendChild(document.createTextNode(anchorChild));
		} else {
			link.appendChild(anchorChild);
		} // end if				
		
		return link;
	} // end method

	CDA.Html.makeDiv = function(content, id, cssClass, style) {
		var div = document.createElement("div");
		div.id = id;
		div.className =cssClass;
		div.setAttribute("style",style);
		div.innerHTML= content;
		return div;
	} // end method

	CDA.Html.cssDisplay = function(elem, cssdisplay) {	
		curElem = (typeof elem=="object") ? elem : CDA.find(elem);
		curElem.style.display=cssdisplay;		
	} // end method

	CDA.Html.rowStart = "<tr>";
	CDA.Html.rowEnd = "</tr>";
	CDA.Html.tableEnd = "</table>";
	
//**************************************************************************************

// retrieves the last character in a string
function getLastChar(str) {
	return str.charAt(str.length-1);
} // end function


// checks if browser is FireFox version 1 or above
function isFireFox() {
	if(navigator.userAgent.indexOf("Firefox")!=-1){
		var versionindex=navigator.userAgent.indexOf("Firefox")+8
		if (parseInt(navigator.userAgent.charAt(versionindex))>=1)
			return true;
	} // end if
	return false;
} // end function

//**************************************************************************************
// Function:	expandDesc()
// Returns:		Null
function expandDesc(descDivId, gotoElemId, resizeFunc) {
	CDA.Html.cssDisplay(descDivId, "inline");		
} // end method

//**************************************************************************************
// Function:	collapseDesc()
// Returns:		Null
function collapseDesc(descDivId) {
	CDA.Html.cssDisplay(descDivId, "none");	
} // end method

//**************************************************************************************
// Function:	setRandomBook.  CMS Supplied.  Add book image to array through CMS
//				upon new entry.   
// Returns:		object type in string form
function setRandomBook(bookKey, src, title){
	
	var img = CDA.find("imgBookBucket");
	img.src = src;
	img.title = title;
	img.setAttribute("bookKey", bookKey);
	img.onclick = function() {
		var popup = new DivPopup(560, 350);
		popup.show();
		var selectedBook = new Book(null, "datatable_noborder", false, this.getAttribute("bookKey"))
		selectedBook.iframeId = "ifmBookPopup";
		selectedBook.parent = popup.contentDiv;
		selectedBook.show();
		popup.addContent();
	} // function	
} // end function



//**************************************************************************************
// Add prototype methods to Array object


// Array.concat() - Join two arrays
if( typeof Array.prototype.concat==='undefined' ) {
 Array.prototype.concat = function( a ) {
  for( var i = 0, b = this.copy(); i<a.length; i++ ) {
   b[b.length] = a[i];
  }
  return b;
  };
}

// Array.copy() - Copy an array
if( typeof Array.prototype.copy==='undefined' ) {
 Array.prototype.copy = function() {
  var a = [], i = this.length;
  while( i-- ) {
   a[i] = typeof this[i].copy!=='undefined' ? this[i].copy() : this[i];
  }
  return a;
 };
}

// Array.pop() - Remove and return the last element of an array
if( typeof Array.prototype.pop==='undefined' ) {
 Array.prototype.pop = function() {
  var b = this[this.length-1];
  this.length--;
  return b;
 };
}

// Array.push() - Add an element to the end of an array, return the new length
if( typeof Array.prototype.push==='undefined' ) {
 Array.prototype.push = function() {
  for( var i = 0, b = this.length, a = arguments, l = a.length; i<l; i++ ) {
   this[b+i] = a[i];
  }
  return this.length;
 };
}

// Array.shift() - Remove and return the first element
if( typeof Array.prototype.shift==='undefined' ) {
 Array.prototype.shift = function() {
  for( var i = 0, b = this[0], l = this.length-1; i<l; i++ ) {
   this[i] = this[i+1];
  }
  this.length--;
  return b;
 };
}

// Array.slice() - Copy and return several elements
if( typeof Array.prototype.slice==='undefined' ) {
 Array.prototype.slice = function( a, c ) {
  var i, l = this.length, r = [];
  if( !c ) { c = l; }
  if( c<0 ) { c = l + c; }
  if( a<0 ) { a = l - a; }
  if( c<a ) { i = a; a = c; c = i; }
  for( i = 0; i < c - a; i++ ) { r[i] = this[a+i]; }
  return r;
 };
}

// Array.splice() - Remove or replace several elements and return any deleted elements
if( typeof Array.prototype.splice==='undefined' ) {
 Array.prototype.splice = function( a, c ) {
  var i = 0, e = arguments, d = this.copy(), f = a, l = this.length;
  if( !c ) { c = l - a; }
  for( i; i < e.length - 2; i++ ) { this[a + i] = e[i + 2]; }
  for( a; a < l - c; a++ ) { this[a + e.length - 2] = d[a - c]; }
  this.length -= c - e.length + 2;
  return d.slice( f, f + c );
 };
}

// Array.unshift() - Add an element to the beginning of an array
if( typeof Array.prototype.unshift==='undefined' ) {
 Array.prototype.unshift = function() {
  this.reverse();
  var a = arguments, i = a.length;
  while(i--) { this.push(a[i]); }
  this.reverse();
  return this.length;
 };
}

// -- 4umi additional functions

// Array.forEach( function ) - Apply a function to each element
Array.prototype.forEach = function( f ) {
 var i = this.length, j, l = this.length;
 for( i=0; i<l; i++ ) { if( ( j = this[i] ) ) { f( j ); } }
};

// Array.indexOf( value, begin, strict ) - Return index of the first element that matches value
Array.prototype.indexOf = function( v, b, s ) {
 for( var i = +b || 0, l = this.length; i < l; i++ ) {
  if( this[i]===v || s && this[i]==v ) { return i; }
 }
 return -1;
};

// Array.insert( index, value ) - Insert value at index, without overwriting existing keys
Array.prototype.insert = function( i, v ) {
 if( i>=0 ) {
  var a = this.slice(), b = a.splice( i );
  a[i] = v;
  return a.concat( b );
 }
};

// Array.lastIndexOf( value, begin, strict ) - Return index of the last element that matches value
Array.prototype.lastIndexOf = function( v, b, s ) {
 b = +b || 0;
 var i = this.length; while(i-->b) {
  if( this[i]===v || s && this[i]==v ) { return i; }
 }
 return -1;
};

// Array.random( range ) - Return a random element, optionally up to or from range
Array.prototype.random = function( r ) {
 var i = 0, l = this.length;
 if( !r ) { r = this.length; }
 else if( r > 0 ) { r = r % l; }
 else { i = r; r = l + r % l; }
 return this[ Math.floor( r * Math.random() - i ) ];
};

// Array.shuffle( deep ) - Randomly interchange elements
Array.prototype.shuffle = function( b ) {
 var i = this.length, j, t;
 while( i ) {
  j = Math.floor( ( i-- ) * Math.random() );
  t = b && typeof this[i].shuffle!=='undefined' ? this[i].shuffle() : this[i];
  this[i] = this[j];
  this[j] = t;
 }
 return this;
};

// Array.unique( strict ) - Remove duplicate values
Array.prototype.unique = function( b ) {
 var a = [], i, l = this.length;
 for( i=0; i<l; i++ ) {
  if( a.indexOf( this[i], 0, b ) < 0 ) { a.push( this[i] ); }
 }
 return a;
};

// Array.walk() - Change each value according to a callback function
Array.prototype.walk = function( f ) {
 var a = [], i = this.length;
 while(i--) { a.push( f( this[i] ) ); }
 return a.reverse();
};
