/**
 * @author aalucas
 */
function RandLogoPicker(config){
	this.config = {
		maxItems: config.maxItems||2,
		logoId: config.logoId,
		randClass: config.randClass||"logo-${num}",
		randFlashPath: config.randFlashPath||null,
		_replaceRegex: config._replaceRegex||/(.*)(?:\$\{num\})(.*)$/
	}
}

RandLogoPicker.prototype.getFlashPath = function(rNum){
	var returnValue = "";
	if (!rNum) var rNum = (1 + Math.round(Math.random()*(this.config.maxItems-1))); 
	var flashPath = this.config.randFlashPath;
	if (flashPath) returnValue = flashPath.replace(this.config._replaceRegex, ("$1" + rNum + "$2"));
	return returnValue;
}

RandLogoPicker.prototype.addSWFObject = function(swfObject){
	this._swfObject = swfObject;
}

RandLogoPicker.prototype.updateLogo = function(rNum){
	this.updateImageLogo(rNum);
	if (this._swfObject) this.updateFlashLogo(rNum);
}

RandLogoPicker.prototype.updateFlashLogo = function(rNum){
	var flashPath = this.getFlashPath(rNum);
	if (flashPath) {
		this._swfObject.attributes.swf = this.getFlashPath(rNum);
		this._swfObject.write(this.config.logoId);
	}
}

RandLogoPicker.prototype.updateImageLogo = function(rNum){
	var logo = document.getElementById(this.config.logoId);
	if ((logo)&&(typeof logo.className !== "undefined")) {
		if (!rNum) var rNum = (1 + Math.round(Math.random()*(this.config.maxItems-1))); 
		logo.className += (" " + 
			this.config.randClass.replace(this.config._replaceRegex, ("$1" + rNum + "$2"))
		);
	} else {
		var self = this;
		window.setTimeout(function(){self.updateLogo()}, 2000);
	}
}