﻿if ((typeof Prototype == 'undefined') || (typeof Element == 'undefined') || (typeof Element.Methods == 'undefined')) 
    alert("Toggle.js requires the Prototype JavaScript framework");

var Toggle = Class.create(
{
    initialize: function(target, controller, toggleState)
    {
        this.toggleState = toggleState;
        this.target = $(target);
        this.controller = $(controller);
        this.controller.style.cursor = "pointer";
        this.setState();
        
        Event.observe(this.controller, "click", this.controller_click.bind(this));
    },
    
    controller_click: function()
    {
        this.toggleState = !this.toggleState;
        this.setState();
    },
    
    setState: function()
    {
        if(this.toggleState)
        {
            this.controller.removeClassName("toggleOff");
            this.controller.addClassName("toggleOn");
            this.target.show();
        }
        else
        {
            this.controller.removeClassName("toggleOn");
            this.controller.addClassName("toggleOff");
            this.target.hide();
        }
    }
});