/** 
* the new call becomes either
*
* 	new AC.Randinator('<p>test1','<p>test2','<p>test3');
*
* to render content inline with document.write or
*
* 	new AC.Randinator('<p>test1', '<p>test2', '<p>test3', {container: 'container_id'});
*
* to render the content into a specific container in the DOM
*
* we need to support old style calls as well for now so
* if no container is specified, it will document.write 
* as it did before.  we can plan to slowly transition calls where
* there isn't a clear container for the content.
*
* the old randinator used an if(arguments) convention which will ALWAYS eval
* true in most modern browsers (that is, arguments is always an object, even if empty)
* this uses the convention of checking for items in arguments
*
* @author: aubrey anderson
*
* TODO default content for non-js browsers / a JS mis-push etc?
*/

if (typeof(AC) == "undefined") AC = {};

AC.Randinator = Class.create();
AC.Randinator.prototype = {
	initialize: function() {	
		if (arguments.length > 0 && typeof(arguments[arguments.length-1]) == 'object') { // looking for a container object at the end
			window.onload = this.randomizeContent.bind(this, arguments);
		}
		else if (arguments.length > 0) {
			this.renderInline(arguments[Math.floor(Math.random() * arguments.length)]);
		}
	},
	randomizeContent: function(args) {	
		var randNum = Math.floor(Math.random() * (args.length-1)); // avoid the container arg at the end		
		this.renderToContainer($(args[args.length-1].container), args[randNum]);
	},
	renderToContainer: function(container, content) {
		container.innerHTML = content;
	},
	renderInline: function(content) {
		document.write(content);
	}
};

/** 
* support for deprecated calls in the old format
* this is still kind of dumb, there is some redundancy but 
* at least the call is always consistent
*/
function randinator() {
	if (arguments.length > 0) new AC.Randinator(arguments[Math.floor(Math.random() * arguments.length)]);
}
