/*
** Image managers
*/

// Icon highlighter for multiple line links
var CIconRolloverManager = 
{
	init: function( szClassName, szScreenID )
	{
		this.szContainerClassName = (szClassName) ? szClassName : "cSection";
		this.szScreenID = (szScreenID) ? szScreenID : "dynScreen";
	},
	
	compareClassNames: function( szClassList, szClassName )
	{
		aWords = szClassList.split(" ");
		
		if (aWords.length > 1)
		{
			for (i = 0; i < aWords.length; i++)
			{
				if (szClassName == aWords[i])
					return true;
			}
		}
		else if (aWords.length == 1)
		{								
			if (szClassName == szClassList)
				return true;
		}
		
		return false;
	},
	
	getIconHandle: function( oInterface )
	{
		// Find the parent node
		for (oParent=null, oCur = oInterface; oCur; oCur = oCur.parentNode)
		{
			if (this.compareClassNames(oCur.className, this.szContainerClassName))
			{
				oParent = oCur;
				break;
			}
		}
		
		if (!oParent)
			return null;
			
		// Get an array of images, and return the first one (should be the icon)
		return oParent.getElementsByTagName("IMG")[0];
	},
	
	getScreenHandle: function()
	{
		return document.getElementById(this.szScreenID);
	},
	
	getScreenImg: function( oScreenContainer )
	{
		return oScreenContainer.getElementsByTagName("IMG")[0];
	},
	
	mouseOver: function( oInterface )
	{
		oParent = this.getIconHandle(oInterface);
		
		if (oParent)
			CRolloverManager.mouseOver(oParent);		
		
		// The image source for the screen piggybacks on lowsrc
		if (oParent.lowsrc == "#")
			return;
			
		oScreenContainer = this.getScreenHandle();		
		
		oScreen = this.getScreenImg( oScreenContainer );
		
		oScreenContainer.style.display = "block";
		
		CRolloverManager.switchTo(oScreen, oParent.lowsrc);
	},
	
	mouseOut: function( oInterface )
	{
		oParent = this.getIconHandle(oInterface);
		
		if (oParent)
			CRolloverManager.mouseOut(oParent);
			
		// The image source for the screen piggybacks on lowsrc
		oScreenContainer = this.getScreenHandle();
		oScreenContainer.style.display = "none";
	}
}

// Override this if the container has a different name
CIconRolloverManager.init();

  /////////////////////
 // Support classes //
/////////////////////

// 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:")
				{
					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";
		}
	},
	
	// Switch to a prescribed image
	switchTo:function( oParent, szFilename )
	{			
		aComponents = this.refinePaths(oParent);			
		
		oParent.src = aComponents[0] + szFilename + "." + aComponents[2];
	},
	
	// 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;
					}
				}
			}
		}
	}
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function pop(url, h) {
  var x = window.open(url,"xx",'top=15,left=15,width=291,height=418,status=no,scrollbars=yes,resizable=yes');
  x.focus();
}

