/**
 * This javascript is used to add the capability of hide/show parts of a document.
 *
 * $Rayge: rayge/js/toggler.js,v 1.0 2005/09/25 13:15:32 fhelly Exp $
 * $Id: Toggler.js 44 2006-02-27 18:25:31Z 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 (GPL). If you did not
 * receive this file, see http://www.fsf.org/copyleft/gpl.html.
 *
 * @version $Revision: 54 $
 * @package rayge
 */

var togglers = new Array();

function Toggler(id, mode, btag, ctag, otitle, ctitle, maxheight, minheight)
{
    if (!document.getElementById && !getElementsByTagName) {
        this.container = false;
        return this;
    }
    this.id         = id;
    this.container  = document.getElementById(id)||false;
    if (this.container) {
        var btags       = this.container.getElementsByTagName(btag);
        var ctags       = this.container.getElementsByTagName(ctag);
        // Localise the buttons and initialize its events
        if(typeof(btags[0]) != 'undefined' &&
            typeof(ctags[0]) != 'undefined') {

            this.button               = btags[0];
            this.button.content       = ctags[0];
            this.button.container     = this.container;
            this.button.mode          = mode;
            /* Create the button cookie */
            this.button.cookie        = new Cookie(this.id, 1000*60*60*24*2);
            /* Get the button status from its cookie,
               default 'o': the content is opened */
            this.button.status        = this.button.cookie.get('o');
            /* Make sure the container display mode is set */
            this.button.container.style.display = 'block';
            this.button.isimg         = (btag.toLowerCase() == 'img');
            this.button.maxheight     = (typeof(maxheight) == 'undefined') ? null : maxheight;
            this.button.minheight     = (typeof(minheight) == 'undefined') ? null : minheight;

            if (this.button.isimg) {
                var length = this.button.src.length;
                this.button.firstSrc = this.button.src.substring(0, this.button.src.lastIndexOf('.'));
                this.button.ext      = this.button.src.substr(this.button.firstSrc.length);
            }
            this.button.firstClassName = this.button.className;
            if (this.button.status == 'c') {
                /* Hide the content */
                this.button.content.style.display = 'none';
                /* Swap the button */
                if (this.button.isimg) {
                    /* use common suffix for image name */
                    this.button.src = this.button.firstSrc+'_c'+this.button.ext;
                }
                /* Swap the button */
                this.button.className = this.button.firstClassName+'Closed';
                if (this.button.minheight) {
                    this.button.container.style.height = this.button.minheight;
                }
            } else {
                /* Swap the button */
                if (this.button.isimg) {
                    /* use common suffix for image name */
                    this.button.src = this.button.firstSrc+'_o'+this.button.ext;
                }
                /* Swap the button */
                this.button.className = this.button.firstClassName+'Opened';
                if (this.button.maxheight) {
                    this.button.container.style.height = this.button.maxheight;
                }
            }
            /* Initialize the events */
            this.button.onclick       = clickOnToggler;
            this.button.onmouseover   = mouseOverToggler;
            this.button.onmouseout    = mouseOutToggler;
            this.button.otitle        = otitle;
            this.button.ctitle        = ctitle;
        }
    }
}
function mouseOutToggler(e)
{
    if (!e) {
        var e = window.event;
    }
    this.className = (this.cookie.get('o') == 'o') ?
        this.firstClassName+'Opened' :
            this.firstClassName+'Closed';
    this.title = (this.cookie.get('o') == 'o') ?
        this.ctitle : this.otitle;
    if (this.isimg) {
        var suffix = (this.cookie.get('o') == 'o') ?
            '_o' : '_c';
        /* use common suffix for image name */
        this.src = this.firstSrc+suffix+this.ext;
    }
}
function mouseOverToggler(e)
{
    if (!e) {
        var e = window.event;
    }
    if (document.all) {
        this.style.cursor = 'hand';
    } else {
        this.style.cursor = 'pointer';
    }
    this.title = (this.className == this.firstClassName+'Closed') ?
        this.otitle : this.ctitle;
    this.className = (this.className == this.firstClassName+'Closed') ?
        this.firstClassName+'Opened' :
            this.firstClassName+'Closed';
    if (this.isimg) {
        var suffix = (this.className == this.firstClassName+'Closed') ?
            '_o' : '_c';
        /* use common suffix for image name */
        this.src = this.firstSrc+suffix+'_omo'+this.ext;

    }
}
function clickOnToggler(e)
{
    if (!e) {
        var e = window.event;
    }
    if (this.content.style.display == 'none') {
        this.content.style.display = this.mode;
        this.className = this.firstClassName+'Opened';
        this.title = this.ctitle;
        if (this.isimg) {
            this.src = this.firstSrc+'_o'+this.ext;
        }
        if (this.maxheight) {
            this.container.style.height = this.maxheight;
        }
        this.cookie.set('o');
    } else {
        this.content.style.display = 'none';
        this.className = this.firstClassName+'Closed';
        this.title = this.otitle;
        if (this.isimg) {
            this.src = this.firstSrc+'_c'+this.ext;
        }
        if (this.minheight) {
            this.container.style.height = this.minheight;
        }
        this.cookie.set('c');
    }
}