/**
 * Byte Image Rotator
 * @author Eric Butera <eric@bytepro.net>
 * @since 2007.02.16
 * $Id: byte_image_rotator.js 851 2007-02-16 22:34:57Z  $
 */

// setup namespace
if (typeof BYTE == "undefined") { var BYTE = {}; }
BYTE['imageRotator'] = {};

/**
 * Image Rotator constructor
 * @param {string} HTML element id to .innerHTML new rotations in
 * @param {string} Rotation mode
 * @param {int}    Rotation speed in seconds
 * @constructor
 * @class YAHOO
 */
BYTE.imageRotator = function(htmlId, mode, speed) {

    /**
     * HTML Element id
     * @property _htmlId
     * @type string
     * @private
     */
    this._htmlId = htmlId;

    /**
     * Rotation mode. Valid modes are 'random' 'rotate'
     * @property _mode
     * @type string
     * @private
     */
    this._mode = mode;

    /**
     * Rotation speed
     * @property _speed
     * @type int
     * @private
     */
    this._speed = speed;

    /**
     * Images to rotate through
     * @property _images
     * @type array
     * @private
     */
    this._images = new Array();

    /**
     * Internal array pointer
     * @property _imagePointer
     * @type int
     * @private
     */
    this._imagePointer = 0;

    /**
     * This will hold a reference to the refresher
     * @property _refreshInterval
     * @private
     */
    this._refreshInterval = null;

    /**
     * Image html object this rotator is working on
     * @property _img
     * @private
     */
    this._img;

};


/**
 * Add an image to the rotator.
 * @param {object}
 * @since 2007.02.16
 */
BYTE.imageRotator.prototype.addImage = function(imageObj) {

    if (typeof imageObj != "object")
        return false;

    if (imageObj.src == null || imageObj.src == "")
        return false;

    var defaults = {
        src:    null,
        width:  null,
        height: null,
        alt:    null,
        title:  null,
        border: null,
        link:   null,
        style:  null,
        target: null
    };

    // overwrite defaults with user defined parameters
    for (var param in imageObj)
        defaults[param] = imageObj[param];

    var result = this._images.push(defaults);

    return true;
};


/**
 * Return a random image from the image array
 * @return {object}
 * @since 2007.02.16
 */
BYTE.imageRotator.prototype._randomImage = function() {
    var key = Math.random() * this._images.length;
    key = Math.floor(key);
    return this._images[key];
};


/**
 * Return the next image from the current image array
 * @return {object}
 * @since 2007.02.16
 */
BYTE.imageRotator.prototype._nextImage = function() {
    // if we reach the max, reset to 0
    if (this._imagePointer >= this._images.length)
        this._imagePointer = 0;

    var pointer = this._imagePointer;
    this._imagePointer++;
    return this._images[pointer];
};


/**
 * Render the image rotator
 * @return bool
 * @since 2007.02.16
 * @private
 */
BYTE.imageRotator.prototype._render = function() {

    // dont render if there aren't any images
    if (this._images.length == 0)
        return true;

    switch (this._mode) {
        case 'random':
            imageObj = this._randomImage();
            break;
        default:
            imageObj = this._nextImage();
            break;
    }

    if (this._img == undefined)
        return false;

    var props = ['src', 'width', 'height', 'alt', 'title', 'border'];
    for (var key in props) {
        var propKey = props[key];
        if (imageObj[propKey] != null)
            this._img[propKey] = imageObj[propKey];
        else
            this._img[propKey] = "";
    }

    this.updateLink(imageObj);

    return true;
};


/**
 * Update the a href link for the image
 * @param {object} Image object
 * @since 2007.02.16
 */
BYTE.imageRotator.prototype.updateLink = function(imageObj) {

    var elAHref = document.getElementById('byteImageRotator-image-'+ this._htmlId);
    var target = '_self';

    if (imageObj.link != null) {

        elAHref.href = imageObj.link;
        if (imageObj.target != null) {
            target = imageObj.target;
        }

    } else {
        elAHref.href = 'javascript:void(null);';
    }

    elAHref.target = target;
};


/**
 * Create an image for the rotation to work with.
 * @return bool
 * @since 2007.02.16
 */
BYTE.imageRotator.prototype.createImage = function() {
    var htmlElement = document.getElementById(this._htmlId);
    if (htmlElement == undefined)
        return false;

    htmlElement.innerHTML = '<a id="byteImageRotator-image-'+ this._htmlId +'" href="javascript:void(null);"><img id="byteImageRotator-'+ this._htmlId +'" border="0"></a>';
    this._img = document.getElementById('byteImageRotator-'+ this._htmlId);
    if (this._img == undefined)
        return false;

    return true;
};


/**
 * Start the image rotation loop.  This is a bit of a hack as you can't pass an
 * object to setInterval
 * @return bool
 * @since 2007.02.16
 */
BYTE.imageRotator.prototype.start = function() {
	

    if (! this.createImage() )
        return false;

    this._render();

    var o = this;
    var f = function() { o._render(); };
    o.intervalcheck = f;
    this._refreshInterval = self.setInterval(f, this._speed);
};


BYTE.imageRotator.prototype.stop = function() {
    self.clearInterval(this._refreshInterval);
};
