/*
** Image managers
*/

// Static class object for image rollovers
// Will toggle between XXX.ext and XXX_over.ext (XXX_active.ext)

var CRolloverManager = 
{
	removeTrails: function(szFld)
	{
		var res = "";
		var c = 0;
		for (i=0; i<szFld.length; i++) {
		  if (szFld.charAt(i) != "_" || c > 0) {
			res += szFld.charAt(i);
			if (szFld.charAt(i) != "_") c = res.length;
			}
		  }
		return res.substr(0,c);
	},
	
	concatenateUntil: function(aArr, iMax, szDiv)
	{		
		var szRet="";
		
		for (i=0; i < iMax; i++)
		{			
			if (!aArr[i])
				continue;			
			
			// Sometimes path is repeated
			// FIXME:  More elegant solution?
			if (i>0 && aArr[i] == aArr[i-1])
			{
				continue;
			}
				
			szRet += aArr[i];		
			
			if (szDiv)
			{
				// Dirty hack to ensure that
				// there are two slashes after http://
				if (aArr[i] == "http:" || aArr[i] == "https:")
				{
					szRet += szDiv;
				}
				
				szRet += szDiv;
			}
		}
		
		return szRet;
	},
	
	refinePaths: function(oParent)
	{
		// Get the filename
		aPaths = oParent.src.split("/");
		szFullName = aPaths[aPaths.length-1];
		
		// And the file extention
		aParts = szFullName.split(".");
		szFileExt = aParts[aParts.length-1];				
		
		szFileName = this.concatenateUntil(aParts, aParts.length-1);
		szPath = this.concatenateUntil(aPaths, aPaths.length-1,"/");	
		
		// Split up the filename
		aSegments = szFileName.split("_");		
		
		// see if the 'active' flag is set
		bActive=false;		
		if (aSegments[aSegments.length-1]=="active")
		{
			bActive=true;
			szFileName = this.removeTrails(this.concatenateUntil(aSegments,aSegments.length-1,"_"));
		}
		// Strip off _over extention, if present
		else if (aSegments[aSegments.length-1]=="over")
		{
			szFileName = this.removeTrails(this.concatenateUntil(aSegments,aSegments.length-1,"_"));
		}

		aRet = new Array(szPath, szFileName, szFileExt, bActive);
		
		return aRet;
	},
	
	isActive: function(oParent)
	{
		return this.refinePaths(oParent)[3];
	},
	
	toggleActivation: function(oParent)
	{
		if (this.isActive(oParent))
		{
			this.deactivate(oParent);
		}
		else
		{
			this.activate(oParent);
		}
	},
	
	// React to mouse over event
	mouseOver:function(oParent, szLabelID,szDefaultID)
	{			
		aComponents = this.refinePaths(oParent);			
		
		// Only if 'active' is not set
		if (aComponents[3]==false)
		{
			oParent.src = aComponents[0] + aComponents[1] + "_over." + aComponents[2];
		}
		
		if (szLabelID)
		{
			document.getElementById(szLabelID).style.display="inline";
		}
		if (szDefaultID)
		{
			document.getElementById(szDefaultID).style.display="none";
		}
	},
	
	// and mouse out
	mouseOut:function(oParent, szLabelID, szDefaultID)
	{			
		aComponents = this.refinePaths(oParent);
		
		// Only if 'active' is not set
		if (aComponents[3]==false)
		{
			oParent.src = aComponents[0] + aComponents[1] + "." + aComponents[2];
		}
		
		if (szLabelID)
		{
			document.getElementById(szLabelID).style.display="none";
		}
		if (szDefaultID)
		{
			document.getElementById(szDefaultID).style.display="inline";
		}
	},
	
	// activate the image
	activate:function(oParent)
	{			
		aComponents = this.refinePaths(oParent);
		
		oParent.src = aComponents[0] + aComponents[1] + "_active." + aComponents[2];
	},
	
	deactivate:function(oParent)
	{			
		aComponents = this.refinePaths(oParent);
		
		oParent.src = aComponents[0] + aComponents[1] + "." + aComponents[2];
	},
	
	deactivateAll:function(szID)
	{
		// Step through the given id and turn off all _active flags
		oParent = document.getElementById(szID);
		
		for (oStep = oParent.firstChild; oStep; oStep = oStep.nextSibling)
		{
			// Ensure that the whitespace is bypassed
			if (oStep.className == "cIcon")
			{
				for (oStep2 = oStep.firstChild; oStep2; oStep2 = oStep2.nextSibling)
				{
					if (oStep2.tagName == "IMG")
					{
						CRolloverManager.deactivate(oStep2);
					}
				}								
			}
		}
	},
	
	mouseOverByID:function(szID)
	{
		oID = document.getElementById(szID);
		
		CRolloverManager.mouseOver(oID);
	},
	
	mouseOutByID:function(szID)
	{
		oID = document.getElementById(szID);
		
		CRolloverManager.mouseOut(oID);		
	}
}

var CCheckboxManager = 
{
	onClick: function( oParent, szID, szShowOnClear )
	{		
		aVals = CRolloverManager.refinePaths(oParent);
		
		oForm = document.getElementById( szID );
		
		if (szShowOnClear)
			oSOC = document.getElementById( szShowOnClear );
		
		if (!aVals[3])
		{
			CRolloverManager.activate(oParent);
			oForm.value = "checked";
			
			if (szShowOnClear)
				oSOC.style.display="none";
		}
		else
		{
			CRolloverManager.deactivate(oParent);
			oForm.value = "";
			
			if (szShowOnClear)
				oSOC.style.display="block";
		}
	}
}

var CRadioboxManager = 
{
	// Deactivate the images for all radioboxes 
	// -> defined as all other images in scope
	// in a group (value set later)
	deactivatePeers: function( oParent )
	{
		for (oCur = oParent.parentNode.firstChild; oCur; oCur = oCur.nextSibling)
		{
			if (oCur.tagName == "IMG")
			{
				CRolloverManager.deactivate(oCur);
			}
		}	
	},
	
	onClick: function( oParent, szValue, szID )
	{		
		aVals = CRolloverManager.refinePaths(oParent);

		oForm = document.getElementById( szID );		

		// only take action if the box is not active
		if (!aVals[3])
		{					
			this.deactivatePeers(oParent);
			CRolloverManager.activate(oParent);
			
			if (oForm)
				oForm.value = szValue;
		}
	},
	
	// Activate radio box in parent container
	// Used for links next to radioboxes on click
	activateSibling: function( oParent, szValue, szID )
	{
		var		oItr, oItr2;
		
		for (oItr = oParent.parentNode.firstChild; oItr; oItr = oItr.nextSibling)
		{
			if (oItr.className == "cIcon")
			{
				for (oItr2 = oItr.firstChild; oItr2; oItr2 = oItr2.nextSibling)
				{
					if (oItr2.nodeType == 1)
					{
						this.onClick( oItr2, szValue, szID );
						
						// Deselect the link
						oParent.blur();
						
						return;
					}
				}
			}
		}
	}
}