/**
 * This javascript class adds an image-swap mouseover/mouseout
 * effect to each icon in a part of a document.
 *
 * Synopsis:
 * var rollover = new Rollovers('menu', 'graphics/menu', 'png');
 * cook0.set('my_value');
 * cook0.get('my_default_value');
 *
 * $Rayge: rayge/js/Rollovers.js,v 1.4 2005/12/14 15:36:22 fhelly Exp $
 * $Id: Rollovers.js 66 2006-03-21 01:19:29Z François Helly $
 *
 * $Revision: 54 $
 * $Date: 2006-04-07 15:24:11 +0200 (Fri, 07 Apr 2006) $
 * $Author: $
 *
 * Copyright 2004-2006 Francois Helly <fhelly@bebop-design.net>
 *
 * See the enclosed file COPYING for license information (LGPL). If you did not
 * receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @version $Revision: 54 $
 * @package rayge
 */

var rollovers = new Array();

/**
 * Constructs the object and initialize its events.
 *
 * @param string id        The container ID
 * @param string path      Path to graphics repository
 * @param string ext       The icon file extension
 * @param string position  The position and, if required, the repeat options
 *                         to apply to the background icon
 *                         default to '0px 0px no-repeat'
 * @param string padding   The padding to apply to the button,
 *                         depending on where the icon should appear,
 *                         default to '0 8px 0 21px', a right positioned
 *                         icon of 21px width and an horizontal white space
 *                         of 8px between each menu item
 */
function Rollovers(id, path, ext, position, padding)
{
    if (!document.getElementById && !getElementsByTagName) {
        this.container = false;
        return this;
    }
    this.id         = id;
    this.container  = document.getElementById(id);
    if(typeof(this.container) == 'undefined' ||
        this.container == null) {
        return;
    }

    this._buttons   = this.container.getElementsByTagName('INPUT');
    this._lns       = this.container.getElementsByTagName('A');

    if (typeof(padding) == 'undefined') {
        padding = '0 8px 0 22px';
    }
    if (typeof(position) == 'undefined') {
        position = '0px 0px no-repeat';
    }
    var serial = -1;
    var suffix;
    for (var i=0;i<this._lns.length;i++)
    {
        this._lns[i].basename  = this._lns[i].id;
        this._lns[i].index     = i;
        this._lns[i].path      = path;
        this._lns[i].ext       = ext;
        this._lns[i].selected  = (this._lns[i].className == 'selected');
        this._lns[i].inactive  = (this._lns[i].className == 'inactive');
        serial = this._lns[i].id.search(/\d+/);
        if (serial != -1) {
            this._lns[i].basename = this._lns[i].id.substring(0, serial);
        }
        suffix = (this._lns[i].inactive) ?
           '_ina' : ((this._lns[i].selected) ? '_hi' : '');
        if (typeof(this._lns[i].id) != 'undefined' &&
            this._lns[i].basename.substr(this._lns[i].basename.lastIndexOf('_')-1) !== suffix) {
            this._lns[i].imgs      = this._lns[i].getElementsByTagName('IMG');
            if (this._lns[i].imgs.length) {
                this._lns[i].img     = this._lns[i].imgs[0];
                this._lns[i].img.src = this._lns[i].path+'/'+this._lns[i].basename+suffix+'.'+ext;
            } else {
                this._lns[i].style.background = 'url('+this._lns[i].path+'/'+this._lns[i].basename+suffix+'.'+ext+') '+position;
            }
            this._lns[i].style.padding = padding;
            if (!this._lns[i].selected && !this._lns[i].inactive) {
                this._lns[i].position    = position;
                this._lns[i].onmouseover = mouseOverRollover;
                this._lns[i].onmouseout  = mouseOutRollover;
            }
        }
    }
    for (var j=0;j<this._buttons.length;j++)
    {
        if (this._buttons[j].className == 'icon' &&
            typeof(this._buttons[j].id) != 'undefined' &&
            this._buttons[j].id != '') {
            this._buttons[j].basename  = this._buttons[j].id;
            serial = this._buttons[j].id.search(/\d+/);
            if (serial != -1) {
                this._buttons[j].basename = this._buttons[j].basename.substring(0, serial);
            }
            this._buttons[j].index        = j;
            this._buttons[j].path         = path;
            this._buttons[j].ext          = ext;
            this._buttons[j].onmouseover  = mouseOverRollover;
            this._buttons[j].onmouseout   = mouseOutRollover;

        }
    }

}
function mouseOverRollover(e)
{
    if (!e) {
        var e = window.event;
    }
    e.cancelBubble = true;
    if (e.stopPropagation) {
        e.stopPropagation();
    }
    var suffix = '_omo';
    if (typeof(this.src) != 'undefined') {
        this.src = this.path+'/'+this.basename+suffix+'.'+this.ext;
    } else if (typeof(this.img) == 'undefined') {
        this.style.background = 'url('+this.path+'/'+this.basename+suffix+'.'+this.ext+') '+this.position;
    } else {
        this.img.src = this.path+'/'+this.basename+suffix+'.'+this.ext;
        if (document.all) {
            this.style.cursor = 'hand';
        } else {
            this.style.cursor = 'pointer';
        }
    }
}
function mouseOutRollover(e)
{
    if (!e) {
        var e = window.event;
    }
    e.cancelBubble = true;
    if (e.stopPropagation) {
        e.stopPropagation();
    }
    if (typeof(this.src) != 'undefined') {
        this.src = this.path+'/'+this.basename+'.'+this.ext;
    } else if (typeof(this.img) == 'undefined') {
        this.style.background = 'url('+this.path+'/'+this.basename+'.'+this.ext+') '+this.position;
    } else {
        this.img.src = this.path+'/'+this.basename+'.'+this.ext;
    }
}