/*
* A YUI Animation Utility wrapper that toggles display properties of an element
*
* @author Hao Tan, Dan Langan
*/

/*
 !NB: when using displayToggler
 a) have the css to set the element (to be toggled) to have display none
 b) define .show css class if doesn't exist (just a css property of display: block)
 c) if setting up some custom animation, you will most likely have to override the showOnStart function in your config
    to fit your animation's needs (please see DEFAULT_CONFIG's showOnStart function)

Eample usage 1: instantiate a displayToggler object with some settings

var myToggler = SW.widget.displayToggler({
    // Required:
    element:yuiDom.get("formName")

    // optional:

    // showTriggers:,
    // hideTriggers:,
    // toggleTriggers:,

    // (see DEFAULT_CONFIG)
});

myToggler.show();
myToggler.hide();
myToggler.toggle();

yuiEvent.addListener("myShowButton", "click", myToggler.show, myToggler, true);
*/

/*
Example usage 2: defining custom event

yuiEvent.onDOMReady(function(){
    SW.widget.displayToggler({
        element:yuiDom.get("resFormContainer"),
        showTriggers:"bookNowBt",
        hideTriggers:"closewin",
        showAttributes: {
            height: {from: 0, to: 170},
            top: {from: 0, to: -175}
        },
        hideAttributes: {
            height: {from: 170, to: 0 },
            top: {from: -175, to: 0}
        },
        showOnStart: function (config) {
            yuiDom.setStyle(config.element,"height",0);
        }
    });
});

*/
YAHOO.namespace("SW.widget.displayToggler");

(function(){
    var yuiDom = YAHOO.util.Dom;
    var yuiEvent = YAHOO.util.Event;
    
    var DEFAULT_CONFIG = {
        element:null, // NOTE: this must be supplied

        showTriggers:null,
        hideTriggers:null,
        toggleTriggers:null,

        showClass:"show",
        animatingClass:"animating",
        showDelay:0.35,
        hideDelay:0.5,
        showDuration:.175,
        hideDuration:.25,
        showTransition:YAHOO.util.Easing.easeNone,
        hideTransition:YAHOO.util.Easing.easeNone,
        showAttributes:{
            opacity:{from:0,to:1}
        },
        hideAttributes:{
            opacity:{from:1,to:0}
        },
        showOnStart:function(config){
//            yuiDom.setStyle(config.element,"opacity",0);
        },
        hideOnStart:function(config){
            // do nothing
        },
        showOnComplete:function(config){
            // do nothing
        },
        hideOnComplete:function(config){
            // do nothing
        }
    };

    function Toggler(config){
        if(!config.element){
            throw("no element supplied to Toggler");
        }

        for(var prop in DEFAULT_CONFIG) {
            if(!config.hasOwnProperty(prop)){
                config[prop] = DEFAULT_CONFIG[prop];
            }
        }

        var isShowing = !!config.isShowing;
        var animIn = new yuiAnim(config.element, config.showAttributes, config.showDuration, config.showTransition);
        animIn.onStart.subscribe(function(){
            yuiDom.addClass(config.element,config.showClass);
            yuiDom.addClass(config.element,config.animatingClass);
            config.showOnStart(config);
        });
        animIn.onComplete.subscribe(function(){
            yuiDom.removeClass(config.element,config.animatingClass);
            config.showOnComplete(config);
        });
        var animOut = new yuiAnim(config.element, config.hideAttributes, config.hideDuration, config.hideTransition);
        animOut.onStart.subscribe(function(){
            yuiDom.addClass(config.element,config.animatingClass);
            config.hideOnStart(config);
        });
        animOut.onComplete.subscribe(function(){
            yuiDom.removeClass(config.element,config.animatingClass);
            yuiDom.removeClass(config.element,config.showClass);
            config.hideOnComplete(config);
        });


        var self = {
            show:function(){
                isShowing = true;
                animIn.animate();
            },
            hide:function(){
                isShowing = false;
                animOut.animate();
            },
            toggle:function(){
                if(isShowing){
                    self.hide();
                }else{
                    self.show();
                }
            },
            isShowing: function() {
                return isShowing;
            },
            setIsShowing: function(showStatus) {
                isShowing = showStatus;
            }
        };

        if(config.showTriggers){
            yuiEvent.addListener(config.showTriggers, "click", self.show, self, true);
        }
        if(config.hideTriggers){
            yuiEvent.addListener(config.hideTriggers, "click", self.hide, self, true);
        }
        if(config.toggleTriggers){
            yuiEvent.addListener(config.toggleTriggers, "click", self.toggle, self, true);
        }

        return self;
    }

    SW.widget.displayToggler = function(config){
        return new Toggler(config);
    };
    
})();


