function Toggle (el) {

    Finder.call(this, el);

    this.nodes = getNodesWithAttr(el, "toggleParam");
    var _this = this;
    
    loop(this.nodes, 
         function(node){
             if(node.getAttribute('toggleSelected') != null)
                 _this.prevState = node.getAttribute("toggleParam");
         });
            

    function handleEvent(e, callback){

        var evt = getEvent(e);
        var src = evtSrc(evt);

        //check if src or parent is the target toggle button
        var toggleParamNode = src.getAttribute("toggleParam") ? src : null;
        function findParam(node){
            if(node.getAttribute("toggleParam")) {
                toggleParamNode = node;
                return false;
            }
        }

        if(toggleParamNode === null) {
            forAncestors(src, findParam);
            if(toggleParamNode === null)
                return;
        }


        function select(){

            var selectCallbackTest; 
            eval("selectCallbackTest = " + _this.selectCallback);

            if(typeof selectCallbackTest == "function") {
                eval(_this.selectCallback + '(toggleParamNode.getAttribute("toggleParam"))');
            }
        }

        function deselect(){

            var deselectCallbackTest; 
            eval("deselectCallbackTest = " + _this.deselectCallback);

            if(typeof deselectCallbackTest == "function") {
                eval(_this.deselectCallback + '(toggleParamNode.getAttribute("toggleParam"))');
            }
        }


        if(Boolean(_this.multi)) {

            if(_this.isSelected(toggleParamNode)) {
                _this.uiDeselect(toggleParamNode);
                if(toggleParamNode.getAttribute("deselectAction"))
                    eval(toggleParamNode.getAttribute("deselectAction"));
            } else {
                _this.uiSelect(toggleParamNode);
                eval(toggleParamNode.getAttribute("selectAction") );
            }

        } else {

            for(var x = 0; x < _this.nodes.length; x++){
                if(_this.nodes[x] != toggleParamNode) {
                    _this.uiDeselect(_this.nodes[x]);
                } else {
                    _this.uiSelect(_this.nodes[x]);
                    select();
                }
            }

        }


    }


    addEventListener(el, _this.eventLaunch, function(e){handleEvent(e);});

}


extend(Toggle, Finder);


Toggle.prototype.baseattr = "toggle";

Toggle.prototype.attributes = {
    "multi" : {},
    "selectCallback" : {},
    "unselectCallback" : {},
    "selectClass" : {},
    "eventLaunch" : {"default" : "click"}
}

Toggle.prototype.uiDeselect = function(node){
    removeClass(node, this.selectClass);
}

Toggle.prototype.uiSelect = function(node){
    addClass(node, this.selectClass);
}

Toggle.prototype.isSelected = function(node){
    return hasClass(node, this.selectClass);
}

Toggle.prototype.invoke("Toggle");
