////
//// NOTE THAT THIS INCLUDE CONTAINS FUNCTION NAMES USED ELSEWHERE
//// CARE SHOULD BE TAKEN TO NOT INCLUDE OTHER SCRIPTS WHEN THIS ONE IS USED
////   CURRENT KNOWN INCLUDES WITH SAME FUNCTION NAME(S):
////	/common/csIncludes/utils.js
////


////
// ImageButton Object 
////
function ImageButton(oDiv,sNormalImg,sOverImg,sDownImg,sDisabledImg,sNormalClass,sOverClass,sDownClass,sDisabledClass,fpOnClick,fpRefreshDisplay) {
	// Image button can have four states: normal, over, down, and disabled.
	// Button can have different images for each state, as well as different CSS classes for each state
	// Button also has methods: show, hide, enable, and disable, which are the same for all buttons.
	// Each individual button can also define custom handlers for onclick and refreshDisplay.
	
	// Expects a DIV object, with an inner IMG object with an id of 'imgButtonCaption'
	
	// Usage:
	// Define DIV in HTML: <div id='divMyImageButton'><img id='imgButtonCaption' src="imgButtonNormal.gif"></img></div>
	// Create Object in JS: var oMyButton = new ImageButton(divMyImagebutton,'imgButtonNormal.gif', etc etc);
	
	// define images. This creates image objects and pre-downloads the graphic files
	try {
		if (sNormalImg) {
			var oNormalImg;
			oNormalImg = oDiv.getElementsByTagName("img")["imgNormal"];
			if (!oNormalImg) {
				oNormalImg = document.createElement("img");
				oNormalImg.setAttribute("id", "imgNormal");
				oDiv.appendChild(oNormalImg);
			}
			oNormalImg.src = sNormalImg;
			oNormalImg.style.display = 'none';
					
			oDiv.normalImgSrc = sNormalImg;
		}
		
		if (sOverImg) {
			var oOverImg;
			oOverImg = oDiv.getElementsByTagName("img")["imgOver"];
			if (!oOverImg) {
				oOverImg = document.createElement("img");
				oOverImg.setAttribute("id", "imgOver");
				oDiv.appendChild(oOverImg);
			}
			oOverImg.src = sOverImg;
			oOverImg.style.display = 'none';		
			oDiv.overImgSrc = sOverImg;
		}
		
		if (sDownImg) {
			var oDownImg;
			oDownImg = oDiv.getElementsByTagName("img")["imgDown"];
			if (!oDownImg) {
				oDownImg = document.createElement("img");
				oDownImg.setAttribute("id", "imgDown");				
				oDiv.appendChild(oDownImg);
			}
			oDownImg.src = sDownImg;
			oDownImg.style.display = 'none';				
			oDiv.downImgSrc = sDownImg;
		}
		
		if (sDisabledImg) {
			var oDisabledImg;
			oDisabledImg = oDiv.getElementsByTagName("img")["imgDisabled"];
			if (!oDisabledImg) {
				oDisabledImg = document.createElement("img");
				oDisabledImg.setAttribute("id", "imgDisabled");					
				oDiv.appendChild(oDisabledImg);
			}
			oDisabledImg.src = sDisabledImg;
			oDisabledImg.style.display = 'none';			
			oDiv.disabledImgSrc = sDisabledImg;
		}
		
		// define classes
		if (sNormalClass) {
			oDiv.normalClass = sNormalClass;
		}
		
		if (sOverClass) {
			oDiv.overClass = sOverClass;
		}
		
		if (sDownClass) {
			oDiv.downClass = sDownClass;
		}
		
		if (sDisabledClass) {
			oDiv.disabledClass = sDisabledClass;
		}
		
		// bind event handlers	
		oDiv.onmouseover = ImageButton_onmouseover;
		oDiv.onmouseout = ImageButton_onmouseout;
		oDiv.onmousedown = ImageButton_onmousedown;
		oDiv.onmouseup = ImageButton_onmouseup;
		
		
		oDiv.show = ImageButton_show;
		oDiv.hide = ImageButton_hide;
		oDiv.enable = ImageButton_enable;
		oDiv.disable = ImageButton_disable;
		
		if (fpOnClick) {
			oDiv.onclick = fpOnClick;
			oDiv.origOnClick = fpOnClick; // safe keeping for the onclick handler 
		}
		
		if (fpRefreshDisplay) {
			oDiv.refreshDisplay = fpRefreshDisplay;
		}
		
		return oDiv;
	} catch (e) {
		handleException(ChainableException.fromGenericException(e,getCallContext()));
	}
}

function ImageButton_onmouseover() {
	var oImage = this.getElementsByTagName("img")["imgButtonCaption"];
	if (oImage && this.overImgSrc) {
		oImage.src = this.overImgSrc;
	}
	
	if (this.overClass && (this.className != this.overClass)) {
		this.className = this.overClass;
	}
}

function ImageButton_onmouseout() {
	var oImage = this.getElementsByTagName("img")["imgButtonCaption"];
	if (oImage && this.normalImgSrc) {
		oImage.src = this.normalImgSrc;
	}
	
	if (this.normalClass && (this.className != this.normalClass)) {
		this.className = this.normalClass;
	}
}

function ImageButton_onmousedown() {
	var oImage = this.getElementsByTagName("img")["imgButtonCaption"];
	if (oImage && this.downImgSrc) {
		oImage.src = this.downImgSrc;
	}
	
	if (this.downClass && (this.className != this.downClass)) {
		this.className = this.downClass;
	}
}

function ImageButton_onmouseup() {
	var oImage = this.getElementsByTagName("img")["imgButtonCaption"];
	if (oImage && this.overImgSrc) {
		oImage.src = this.overImgSrc;
	}
	
	if (this.overClass && (this.className != this.overClass)) {
		this.className = this.overClass;
	}
}

function ImageButton_show() { this.style.display = 'block';}

function ImageButton_hide() { this.style.display = 'none';}

function ImageButton_enable() {
	var oImage = this.getElementsByTagName("img")["imgButtonCaption"];
	if (oImage && this.normalImgSrc) {
		oImage.src = this.normalImgSrc;
	}
	
	if (this.normalClass) {
		this.className = this.normalClass;
	}
	
	// return the onclick hander to its original state
	if (this.origOnClick) {
		this.onclick = this.origOnClick;
	}
	
	// return mouse event handlers
	this.onmouseover = ImageButton_onmouseover;
	this.onmouseout = ImageButton_onmouseout;
	this.onmousedown = ImageButton_onmousedown;
	this.onmouseup = ImageButton_onmouseup;
}

function ImageButton_disable() {
	
	var oImage = this.getElementsByTagName("img")["imgButtonCaption"];
	if (oImage && this.disabledImgSrc) {
		oImage.src = this.disabledImgSrc;
	}
	
	if (this.disabledClass) {
		this.className = this.disabledClass;
	}
	
	// set the onclick handler to null
	this.onclick = nullFunction;
	
	// set mouse event handlers to null
	this.onmouseover = nullFunction;
	this.onmouseout = nullFunction;
	this.onmousedown = nullFunction;
	this.onmouseup = nullFunction;
}

////
// DROP TABS - Mouse over
// Ramyar Jafarkhani 10/2/2007
////

function DropTabMouseOver(sElemSource, sElemTarget, sClass_Default, sClass_Ro, sClass_Dn)
{
	try {
		var oSource = document.getElementById(sElemSource);
		var oTarget = document.getElementById(sElemTarget);
		
		if (sClass_Default)
			oSource.classDefault = sClass_Default;
		
		if (sClass_Ro)
			oSource.classOver = sClass_Ro;
		
		if (sClass_Dn)
			oSource.classDown = sClass_Dn;
			
		oSource.elemTarget = oTarget;
		oSource.onmouseover = DropTabMouseOver_onmouseoverSource;
		oSource.onmouseout = DropTabMouseOver_onmouseoutSource;
		oSource.onmousedown = DropTabMouseOver_onmousedownSource;

		oTarget.elemSource = oSource;
		//oTarget.onmouseover = DropTabMouseOver_onmouseoverTarget;
		oTarget.onmouseout = DropTabMouseOver_onmouseoutTarget;
	
	} catch (ex) {
	
	}	

}

function DropTabMouseOver_onmouseoverSource()
{
	if (this.classOver && (this.className != this.classOver)) {
		this.className = this.classOver;
	}
	if (this.elemTarget)
	{
		this.elemTarget.style.display = "block";
	}
}

function DropTabMouseOver_onmouseoutSource()
{
	if (this.classDefault && (this.className != this.classDefault)) {
		this.className = this.classDefault;
	}
	if (this.elemTarget)
		this.elemTarget.style.display = "none";
}

function DropTabMouseOver_onmousedownSource()
{
	if (this.classDown && (this.className != this.classDown)) {
		this.className = this.classDown;
	}
	if (this.elemTarget)
		this.elemTarget.style.display = "block";
}

function DropTabMouseOver_onmouseoverTarget()
{
}

function DropTabMouseOver_onmouseoutTarget()
{
	this.style.display = "none";
}



////
// Scroll Divs
////
var ScrollDiv_hasScrolled = false;
function ScrollDiv(divTarget, iOpenSizePx) // ** TODO: add numFrames and speed as optional params
{
	this.div = divTarget;	

	if (this.div.parentNode.getAttribute("expanded") && this.div.parentNode.getAttribute("expanded") == "true")
	{
		this.div.style.display = "block";
		return;
	}

	this.div.parentNode.setAttribute("expanded", "true");
	
	this.iterate = ScrollDiv_iteration;
	this.hasScrolled = ScrollDiv_hasScrolled;

	this.numFrames = 3; // **TODO: add as function param
	var speed = 50; // **TODO: add as function param
	this.iterations = iOpenSizePx / this.numFrames;
	this.incrementSize = this.iterations;
	this.size = 0;
	this.count = 0;

	this.div.style.display = "block";
	
	this.intervalId = setInterval("this.iterate()", speed);
}

function ScrollDiv_iteration()
{
	if (this.numFrames > this.count)
	{
		this.size += this.incrementSize;
		this.div.style.height = this.size + "px";
		this.count++;
	}
	else
	{
		clearInterval(this.intervalId);
		this.div.style.display = "block";
		//this.hasScrolled = true;
		//fadeIn(this.div.parentNode.style, 2, 10) ;
		//this.div.style.display = "block";
	}
}

//fades layer in -- TEST/EXPERIMENTAL 
var fdOpac = 0; 
var fdDiv;
var fdFade;
var fdSpeed;
var fdCount = 0;
function fadeIn(oDivStyle, iFadeAmt, iSpeed) 
{
	if(fdOpac < 100)
	{ 
		fdDiv = fdDiv ? fdDiv : oDivStyle;
		fdFade = fdFade ? fdFade : iFadeAmt;
		fdSpeed = fdSpeed ? fdSpeed : iSpeed;

		fdOpac += fdFade; 
		fdDiv.style.opacity = fdOpac/100; 
		fdDiv.style.MozOpacity = fdOpac/100; 
		fdDiv.style.KhtmlOpacity = fdOpac/100;
		fdDiv.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + fdOpac + ")";
		setTimeout(fadeIn, fdSpeed); 
	} 
	else //finish up
	{
		fdDiv.style.filter = "";
		//fdOpac = 0;
	}
} 

////
// Accordion
////
var _accordion_isScrolling = false;
function Accordion(oDivOpen, oDivClose, iOpenSizePx, iFrames, iSpeed)
{
	if (_accordion_isScrolling == true) return;
	_accordion_isScrolling = true;
	
	this.divOpen = oDivOpen;
	this.divClose = oDivClose;
	if (this.divOpen.style.display == "block") return;

	//private method
	this.iterate = Accordion_iteration;
	
	//member vars
	this.openSize = iOpenSizePx;
	
	this.numFrames = (iFrames ? iFrames : 5);
	var speed = (iSpeed ? iSpeed : 50);
	this.iterations = iOpenSizePx / this.numFrames;
	this.incrementSize = this.iterations;
	this.sizeOpen = 0;
	this.sizeClose = this.openSize;
	this.count = 0;	
	
	this.divOpen.style.height = "0px";
	this.divOpen.style.display = "block";
	
	this.intervalId = setInterval("this.iterate()", speed);
}

function Accordion_iteration()
{
	if (this.numFrames > this.count)
	{
		this.sizeOpen += this.incrementSize;
		this.divOpen.style.height = this.sizeOpen + "px";
		
		this.sizeClose -= this.incrementSize;
		this.divClose.style.height = this.sizeClose + "px";
		
		this.count++;
		
		//see if we can keep scrolling
		if (this.sizeOpen + this.incrementSize > this.openSize || this.sizeClose - this.incrementSize < 0){
			this.divClose.style.height = this.openSize + "px";
			this.divClose.style.height = "0px";
			this.count = this.numFrames; //set to finish
		}
	}
	else
	{
		clearInterval(this.intervalId);
		this.divOpen.style.display = "block";
		this.divOpen.style.height = this.openSize + "px";
		
		this.divClose.style.height = "0px";
		this.divClose.style.display = "none";
		
		_accordion_isScrolling = false;
		_isToggling = false; //hack
	}
}