/*
** Peroni Image Managers
*/

// Static object for avatar selection on settings page

var CAvatarSelectionManager = 
{
	// Private methods
	_deselect: function(oSubject)
	{
		oSubject.className="";
	},
	
	_select: function(oSubject)
	{
		oSubject.className = "extSelected";
	},
	
	_getContainer: function(oPeer)
	{
		return oPeer.parentNode.parentNode;
	},
	
	// The form element is a grandparent
	// FIXME!  neaten this up
	_getFormEl: function(oPeer)
	{
		return oPeer.parentNode.parentNode.parentNode.parentNode;
	},
	
	// Return an array of icons in a container
	_getIcons: function(oContainer)
	{
		aIcons = new Array();
		iItr = 0;
		
		for (oCurrent = oContainer.firstChild; oCurrent; oCurrent = oCurrent.nextSibling)
		{
			// ignore non-anchor tags
			if (oCurrent.tagName != "A")
				continue;
								
			for (oItr2 = oCurrent.firstChild; oItr2; oItr2 = oItr2.nextSibling)
			{
				if (oItr2.tagName=="IMG")
				{
					aIcons[iItr] = oItr2;
					iItr++;
				}
			}
		}
		
		return aIcons;
	},
	
	_deselectPeers: function(oParent)
	{
		// Get the avatar container
		aIcons = this._getIcons(this._getContainer(oParent));
		
		for (i = 0; i < aIcons.length; i++)
		{
			this._deselect(aIcons[i]);
		}
	},
	
	// Public methods
	onMouseDown: function(oParent)
	{	
		this._deselectPeers(oParent);
		this._select(oParent);
		
		oParent.blur();
		
		// Set the form field
		document.getElementById("dynImageName").value = oParent.title;
		
		this._getFormEl(oParent).submit();
	},
	
	onRollover: function(oParent)
	{		
		if (oParent.className != "extSelected")		
			oParent.className='extOver';						
			
		oAvatarPL = document.getElementById("dynAvatarPreloader");
		oAvatarRL = document.getElementById("imgMyAvatar");	
		
		// IE workaround for onload bug				
		// -> append a count of milliseconds to the src
		
		if (!this.notFirstTime)
		{
			var tmp = new Date();
			var suffix = tmp.getTime();
			szPrefix = "?" + suffix;
			
			this.notFirstTime=true;				
		}
		else
		{
			szPrefix = "";
		}
		
		oAvatarRL.src = "../images/ajax-loader.gif";	
		oAvatarPL.src = oParent.title + szPrefix;
		
		oAvatarPL.onload = function()
		{ 
			//alert("(simulate loading time)");
			oAvatar = document.getElementById("imgMyAvatar");				
			oAvatar.src = this.src;	
		}		
	},	
	
	/*
	onRollout: function(oParent)
	{		
		if (oParent.className != "extSelected")	
			oParent.className='';			
			
		// reset the avatar image
		aIcons = this._getIcons(this._getContainer(oParent));
		
		for (i = 0; i < aIcons.length; i++)
		{
			if (aIcons[i].className == "extSelected")
			{									
				this.onRollover(aIcons[i]);
			}
		}
	}
	*/
	
	// Browse rollover behaviour for find other cognoscenti
	
	onBrowseRollover: function(oParent, szDivID)
	{
		oParent.className='extOver';
		
		oTarget = document.getElementById(szDivID);	
		
		oTarget.innerHTML = oParent.title;
	},
	
	onRollout: function(oParent, szDivID)
	{		
		// handle browse rollouts
		if (szDivID)
		{
			oTarget = document.getElementById(szDivID);	
		
			oTarget.innerHTML = "";
		}
		
		if (oParent.className != "extSelected")	
			oParent.className='';			
			
		// reset the avatar image
		aIcons = this._getIcons(this._getContainer(oParent));
		
		for (i = 0; i < aIcons.length; i++)
		{
			if (aIcons[i].className == "extSelected")
			{									
				this.onRollover(aIcons[i]);
			}
		}
	}
}

// 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;
	},
	
	// React to mouse over event
	mouseOver:function(oParent)
	{			
		aComponents = this.refinePaths(oParent);			
		
		// Only if 'active' is not set
		if (aComponents[3]==false)
		{
			oParent.src = aComponents[0] + aComponents[1] + "_over." + aComponents[2];
		}
	},
	
	// and mouse out
	mouseOut:function(oParent)
	{			
		aComponents = this.refinePaths(oParent);
		
		// Only if 'active' is not set
		if (aComponents[3]==false)
		{
			oParent.src = aComponents[0] + aComponents[1] + "." + aComponents[2];
		}
	},
	
	// 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 )
	{		
	    aVals = CRolloverManager.refinePaths(oParent);
		
		oForm = document.getElementById( szID );
		if (!aVals[3])
		{
			CRolloverManager.activate(oParent);
			oForm.value = "checked";
		}
		else
		{
			CRolloverManager.deactivate(oParent);
			oForm.value = "";
		}
	}
}

var CRadioboxManager = 
{
	findParent: function( oParent )
	{
		var oCur;
		
		for (oCur = oParent.parentNode; oCur; oCur = oCur.parentNode)
		{
			if (oCur.className == "cRadioPanel")
				break;
		}
		
		return oCur;
	},
	
	// Deactivate the images for all radioboxes 
	// in a group (value set later)
	deactivatePeers: function( oParent )
	{
		oParent = this.findParent( oParent );		
		
		var oCur;
		
		for (oCur = oParent.firstChild; oCur; oCur = oCur.nextSibling)
		{
			if (oCur.className == "cRadioButton")
			{
				for (oItr = oCur.firstChild; oItr; oItr = oItr.nextSibling)
				{
					if (oItr.className == "cIcon")
					{
						for (oItr2 = oItr.firstChild; oItr2; oItr2 = oItr2.nextSibling)
						{
							if (oItr2.nodeType == 1)
							{
								CRolloverManager.deactivate(oItr2);
							}
						}
					}
				}
			}
		}
	},
	
	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];}
}


