/*
 *	'Figleaf'
 *  =========
 *
 *	A __really__ basic library for putting a invisible overlay on top of any(?) flash movie
 *
 *	To use, include this file/javascript and call Figleaf.addOverlay to add an overlay to any flash movie.
 *
 *	NOTE:
 *		This will not work on any flash movie that is NOT windowless!
 *		In other words, unless there is a "wmode" parameter and it is __not__ "window",
 *		there will be no overlay.  It only works when the movie's "wmode" parameter is "opaque" or "transparent".
 *
 *		If I am wrong, please enlighten me.
 *
 *	Author: 	brainphat@garvinggarvin.com
 *	Site:		http://garvinggarvin.com
 *
 *  Copyright (c) 2008, C.M. Garvin and/or brainPHAT Designs.  Creative Commons Attribution 3.0 licence.
 *
 *	Free to alter or distribute.  Just give me credit where/if due.
 */

if (typeof Figleaf == "undefined") {
	Figleaf = {};	// reserve namespace
}

fig = Figleaf;

fig.DEBUG = false;				// whether/not debugging is on; if on, won't work unless there's a 'dbdiv' DIV on the page
fig.USE_IMAGE = false;			// whether to use an image as the overlay, or merely a transparent DIV
fig.OVERLAY_COLOR = '#00b';		// the overlay DIV's background color
fig.OPAC = .4;					// set the default opacity; .0 = completely transparent, 1 = completely opaque
fig.COVERIMG = 'img/cover.png';	// the image to use; NEEDS TO BE SET

fig.addOverlay = function (f) {
	/*
	 * function addOverlay ([element reference])
	 *
	 * DESCRIPTION: overlays a flash movie with a transparent image
	 *
	 * 		TAKES:
	 *			f - a reference to the flash movie's EMBED element (REQUIRED)
	 *
	 * 		RETURNS: reference to the overlay element
	 */

 	var d = document;

	// get coords of flash movie
	var mPos = fig.findPos(f);
	var mX = mPos[0];
	var mY = mPos[1];
	fig.dbug('movie coords: ( ' + mX + ', ' + mY + ' )');

	// get height & width of movie
	var mW = f.offsetWidth;
	var mH = f.offsetHeight;
	fig.dbug('width: ' + mW + "<br />height: " + mH);

	// create a transparent div of that height & width
	var cdiv = d.createElement('div');

	cdiv.style.width = mW + 'px';
	cdiv.style.height = mH + 'px';
	cdiv.style.zIndex = '5000'; 	// unneccesary?
	cdiv.style.position = 'absolute';
	cdiv.style.top = mY + 'px';
	cdiv.style.left = mX + 'px';
	cdiv.style.backgroundColor = fig.OVERLAY_COLOR;
	cdiv.style.opacity = '' + fig.OPAC;
	cdiv.style.filter = 'alpha (opacity=' + (fig.OPAC * 100) + ')';	// for IE

	// an "the image" to it, filling up the entire length & width
	var cimg = d.createElement('img');

	cimg.src = Figleaf.COVERIMG;
	cimg.style.width = mW + 'px';
	cimg.style.height = mH + 'px';
	/* cimg.style.zIndex = '5000'; */	// unneccesary?
	cimg.style.position = 'absolute';
	cimg.style.top = mY + 'px';
	cimg.style.left = mX + 'px';
	cimg.style.opacity = '' + fig.OPAC;
	cimg.style.filter = 'alpha (opacity=' + (fig.OPAC * 100) + ')';	// for IE

	// place it within the same container as the movie
	// but don't place it inside an OBJECT tag
	var fp = f.parentNode;

	if (fp.tagName == 'OBJECT') {
		fp = fp.parentNode;	// then get the OBJECT's parent
	}

	if (fig.USE_IMAGE) {
		fp.appendChild(cimg);	// returns a reference to the overlay
		return cimg;
	}

	fp.appendChild(cdiv);	// returns a reference to the overlay
	return cdiv;

}	// end addOverlay

fig.doFig = function(e) {
	/*
	 * function doFig (e)
	 *
	 * DESCRIPTION: finds all of a page's flash movies, calls addOverlay for each
	 *
	 * 		RETURNS: nothing
	 */
	// really only useful for testing
	// finds all EMBED/Flash OBJECT tags, puts a figleaf on 'em
	var d = document;

	// I __think__ this will catch all Flash movies.  If you know better, please let me know.
	// One way it might not (probably won't) work is when there are Flash movies inside of (i)frames.
	// Not sure how to handle that just yet
	
	var fs = d.getElementsByTagName('object');
	
	for (var i = 0; i < fs.length; i++) {
		var f = fs[i];

		if (f.type == "application/x-shockwave-flash") {	// going to look for correct markup; pointless, otherwise
			var oLay = fig.addOverlay(f);
			fig.dbug('overlay' + i + ' == ' + oLay);
		}
	}

	fs = d.getElementsByTagName('embed');

	fig.dbug('# of flash movies found: ' + fs.length);

	for (var i = 0; i < fs.length; i++) {
		var f = fs[i];

		var oLay = fig.addOverlay(f);
		fig.dbug('overlay' + i + ' == ' + oLay);
	}	
}

/* UTILITY FUNCTIONS */

fig.dbug = function (str) {
	if (Figleaf.DEBUG) {
		var dbdiv = document.getElementById('dbdiv');	// this function looks for a DIV with an id of 'dbDiv'
		if (dbdiv) {
			dbdiv.innerHTML += "<p>" + str + "</p>";
		}
	}
}

/* got this from Quirksmode @
 * 	http://www.quirksmode.org/js/findpos.html
 */
fig.findPos = function (obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

