/*

pgrowl Version 1.00

Requires Prototype > 1.6
http://www.prototypejs.org/

Version History:

Apr 17th 2009 - 1.00 - Initial release

Usage:

<script>
pgrowl(ptitle,ptext,popts);
</script>

By default, the growl will remain open for 5 seconds, then close, and new growls will appear below any visible ones (change the "growlpos" var to "above/below")

Additional options (add to popts, above):

	pg_notifyonly	:	the growl is notification only - any existing visible growls will not be closed
	pg_close		:	the growl must be clicked to close - use for growls without hrefs only
	pg_innerclose	:	the growl will remain open until the 'close' link is clicked
	pg_noclose		:	the growl has no method to be closed - use when functionality in the growl is required for what is happening on the page
	pg_lastclose	:	the new growl will close the previous growl (if any)

*/

// above or below

var growlpos = 'below';

function growlin(what) {

	Effect.Appear('gr_'+what+'');

}

function growlout(what) {

	Effect.Fade('gr_'+what);

}

function pg_cleanup() {

	// clean up growls that are stuck

	var growls = document.getElementsByClassName('growl');

	var nowts = Number(new Date());

	for (i=0;i<growls.length;i++) {

		g_item = growls[i].id;
		g_class = growls[i].className;

		var g_classes = g_class.split(" ");
		for (g = 0; g < g_classes.length; g++) {

			thisclass = g_classes[g];

			if (thisclass != '') {

				if (thisclass.indexOf("ts:") != -1) {
					var g_bits = thisclass.split(":");
					var gts = g_bits[1]*1;

					if (nowts - gts > 2000 && document.getElementById(g_item).style.display != 'none' && g_class.indexOf("pg_close") == -1) {
						var grid_bits = g_item.split("_");
						var grid_id = grid_bits[1]*1;
						setTimeout("growlout('"+grid_id+"')",50);
					}

				}

			}

		}

	}

}

function pgrowl(ptitle,ptext,popts) {

	if (popts.indexOf("pg_notifyonly") == -1) {
		pg_cleanup();
	}

	var cgrowls = document.getElementById('growls').innerHTML;

	var grid = document.getElementById('growlid').innerHTML*1;

	var ts = Number(new Date());

	// options

	var gclose = '';
	var gstyle = '';

	if (popts.indexOf("pg_close") != -1) {
		gclose = 'onclick="javascript:growlout(\''+grid+'\')" title="click to close"';
		gstyle = 'cursor:pointer;';
	} else {

		gstyle = '';

		if (popts.indexOf("pg_innerclose") == -1 && popts.indexOf("pg_noclose") == -1) {
			setTimeout("growlout('"+grid+"')",5000);
		}

	}

	if (popts.indexOf("pg_lastclose") != -1) {
		var lastid = grid - 1;
		setTimeout("growlout('"+lastid+"')",5);
	}

	var thisgrowl = '<div id="gr_'+grid+'" style="display:none;'+gstyle+'" class="growl g_ts:'+ts+'';

	if (popts.indexOf("pg_close") != -1) {
		thisgrowl += ' pg_close';
	}
	thisgrowl += '" '+gclose+'><div class="growl_title">'+ptitle+'</div><div class="growl_text">'+ptext+'';

	if (popts.indexOf("pg_innerclose") != -1) {
		thisgrowl += '<br/><br/><a href="javascript:growlout(\''+grid+'\')">close</a>';
	}

	thisgrowl += '</div></div>';

	var newgrid = grid + 1;

	document.getElementById('growlid').innerHTML = newgrid;

	if (growlpos == 'above') {

		document.getElementById('growls').innerHTML = ''+thisgrowl+''+cgrowls+'';

	} else {

		document.getElementById('growls').innerHTML = ''+cgrowls+''+thisgrowl+'';

	}

	setTimeout("growlin('"+grid+"')",5);	


}
