// JavaScript Document

var FADE_DURATION=0.2;
var FADE_IN=0.4;
var FADE_OUT=1;

function fadeEl(el, bFade) {
	el=$(el); // Ensure the element is "safe"
	
	if (bFade) { // If we're fading
		fadeTo=FADE_IN;
		indent=0;
	} else { // If we're not
		fadeTo=FADE_OUT;
		if (el.parentNode.hasClassName("hide_title")) {
			indent=-9999;
		}
	}
	
	// Clear any existing effects on the element - we don't want them clashing.
	if (el.currentEffect!=null) {
		el.currentEffect.cancel();
	}
	
	// If there is an info_bar created for the element, set its text-indent to display or hide the text
	if (typeof el.parentNode.info_bar!="undefined") {
		el.parentNode.info_bar.style.textIndent=indent+"px";
	}
	
	// Start the animation effect
	el.currentEffect=new Effect.Opacity(el, { to: fadeTo, duration: FADE_DURATION, queue: 'parallel' } );
}

document.observe('dom:loaded', function() {
	// Build an object to store default faux alts for duplication
	fauxAlt=document.createElement("div");
	fauxAlt.className="faux_alt";
	
	// Build an object to store default infobars for duplication
	infoBar=document.createElement("div");
	infoBar.className="info_bar";
	
	// Go through every a tag with glass fader
	$$("a.fader").each(function(fader) {
		// Get the child images
		childImgs=fader.select("img");
		
		// If there were any
		if (childImgs.length>0) {
			// We're only interested in the first image.
			fader.childImg=childImgs[0];
			
			// Lock the dimensions of the a tag to the image.
			fader.style.height=fader.childImg.offsetHeight+"px";
			fader.style.width=fader.childImg.offsetWidth+"px";
			
			// Get the title attribute from the link - or "" if there isn't one.
			title="";
			if ( (fader.getAttribute("title")!= null) && (fader.getAttribute("title")!="") ) {
				title=fader.getAttribute("title");
			}
			
			// If there is a title, set up the info bar
			if (title!="") {
				fader.info_bar=fader.appendChild(infoBar.cloneNode(false));
				fader.info_bar.innerHTML="<span>"+title+"</span>";
				if (fader.hasClassName("hide_title")) {
					fader.info_bar.style.textIndent="-9999px";
				}
			}
			
			// Set up the faux alt text
			fader.faux_alt=fader.appendChild(fauxAlt.cloneNode(false));
			fader.faux_alt.style.display="none";
			fader.faux_alt.innerHTML=fader.childImg.alt;
			
			// Bind the mouseover
			fader.onmouseover=function() {
				this.faux_alt.style.display="block";
				fadeEl(this.childImg, true);
			}
			
			// Bind the mouseout
			fader.onmouseout=function() {
				this.faux_alt.style.display="none";
				fadeEl(this.childImg, false);
			}
		}
	});
});