/**
 * This class converts a list of companies to an interactive company viewer.
 * It slides a number of companies at a time through the screen, pausing when
 * the mouse cursor is over a company.
 * @constructor
 */
function BannerRotator(element, speed) {
	this._element = $(element);
	this._TIME_PER_BANNER = speed || 6000;

	//show the first item and then start the slider.
	var currentBanner = this._element.children()[0];
	$(currentBanner).show();
	this._startSlider();
}

BannerRotator.prototype = {
	/**
	 * The time to pause at each location.
	 * @private
	 */
	_TIME_PER_BANNER: null,
	
	/**
	 * The HTML List element containing the banners
	 * @private
	 */
	_element: null,
	
	/**
	 * A list of each banner's HTML List Item element.
	 * @private
	 */
	_banners: null,
	
	/**
	 * Contains a reference to the currently running {setInterval}, if any
	 * @private
	 */
	_interval: null,
	
	/**
	 * check if any movement is currently busy.
	 * @private
	 */
	_animation: null,
	
	/**
	 * Starts an interval that removes the first banner out of view, and the
	 * next one in line into view. Prevents starting twice.
	 * @private
	 */
	_startSlider: function () {
		if (this._interval) {
			// We are already running, so don't start again.
			return;
		}
		this._interval = setInterval(new Delegate(this, this._hideShow), this._TIME_PER_BANNER);
	},
			
	/**
	 * Allow new movement
	 * @private
	 */
	_checkAnimate: function () {	
		this._animation = false;
	},
	
	_hideShow: function() {
		if(this._animation)
		{
			return;
		}
		this._animation = true;
		var currentBanner = this._element.children()[0];
		var nextBanner = this._element.children()[1];
		if(currentBanner.animate){
			return;
		}
		$(currentBanner).fadeOut(500, function() {
			$(nextBanner).fadeIn(500);
		}).appendTo(this._element);
		
		setTimeout(new Delegate(this, this._checkAnimate), 1000);
	}
};


/**
 * @copyright EveryWeb Solutions 2007
 * @projectDescription JsUtils
 * 
 */
function Delegate(owner, method) {
    // check 'method' first, so when you forget the owner, this following error while be throw instead of
    // "Argument 'owner' must be of type 'object'" which is try because its a function, but not clear.    
    if(method == null) { 
       // give some indication what is involved, so the error can be located more easily .
        var extraInfo = "";
        if (owner != null && owner.toString) { // probably you left out the 'this', so owner = method
            extraInfo = " owner = " + owner.toString();
        }
        throw new IllegalArgumentException("Delegate: Argument 'method' is null'" + extraInfo);
    }
    
    if(typeof(method) != "function") {
        throw new IllegalArgumentException("Delegate: Argument 'method' must be of type 'function' ");
    }

    if(owner == null) {
        throw new IllegalArgumentException("Delegate: Argument 'owner' is null");
    }
    if(typeof(owner) != "object") {
        throw new IllegalArgumentException("Delegate: Argument 'owner' must be of type 'object'");
    }
    
    
    var delegate = function () {
        return method.apply(owner, arguments);
    };
    
    delegate.constructor = Delegate;
    
    delegate.getOwner = function() { return owner;};
    delegate.getMethod = function() { return method;};
    
    return delegate;
}

