﻿/* 
This class allows panels to slide out but ensures the user has time to mouse over.
This also ensures that panels that have another panel with a class of .dropDown inside of them
that when moused over, the panel doesn't collapse.
This also allows the developer to add handlers for onshow, onhide.
*/

/* TODO: Make a jquery plugin out of this!! */

function SlideOut(target, onShow, onHide, onBeforeShow, dropDownSelector, showSpeed, delayClose, closeSpeed) {
    this.jObj = target;
    this.onShow = onShow;
    this.onHide = onHide;
    this.showSpeed = showSpeed;
    this.delayClose = delayClose;
    this.closeSpeed = closeSpeed;
    this.dropDownSelector = (dropDownSelector == null ? ".dropDown" : dropDownSelector);
    this._started = false;
    this.onBeforeShow = onBeforeShow;
};

SlideOut.prototype.SetHandlers = function(onShow, onHide, onBeforeShow) {
    this.onShow = onShow;
    this.onHide = onHide;
    this.onBeforeShow = onBeforeShow;
}

SlideOut.prototype.EnsureOpen = function() {
    this.jObj.find(this.dropDownSelector);
    jDropDown.data("isShowing", true);
}

//binds all of the events to work
SlideOut.prototype.Start = function() {
    var _me = this;
    //console.log("STARTING");

    //Manages the filter drop downs for the grids
    this.jObj.hover(function() {
        //console.log("hovering over target");

        //get the drop down
        var jDropDown = $(this).find(_me.dropDownSelector);

        //set the drop down flag
        jDropDown.data("isShowing", true);

        //only bind the hover methods if they haven't been done yet
        if (!_me._started) {
            //set the drop down hover methods        
            jDropDown.hover(function() {
                //console.log("hovering over drop down");
                //when the expanded drop down is hovered, set it showing flag to true
                $(this).data("isShowing", true);
                //clear any timeout associated with the drop down     
                clearTimeout(jDropDown.data("currTimeout"));
                jDropDown.data("currTimeout", null);
            }, function() {
                //ignore the out hover event for the drop down control
            })
            _me._started = true;
        }
        //do showing check
        if (_me.onBeforeShow) {
            _me.onBeforeShow.call(_me, $(this));
        }
        //do the showing
        jDropDown.slideDown(_me.showSpeed);

        if (_me.onShow != null) {
            _me.onShow.call(_me, $(this));
        }

    },
    function() {
        //console.log("blurring from target");
        var jDropDown = $(this).find(_me.dropDownSelector);
        jDropDown.data("isShowing", false);
        var _this = $(this);
        //console.log("timeout = " + _me._currTimeout);
        //set a timeout (only if there is currently no pending timeout)
        //to hide the drop down list after the delayClose time if
        //the user is no longer viewing the slide out control.
        if (jDropDown.data("currTimeout") == null) {
            jDropDown.data("currTimeout", setTimeout(function() {
                if (!jDropDown.data("isShowing")) {
                    jDropDown.slideUp(_me.closeSpeed);
                    if (_me.onHide != null) {
                        _me.onHide.call(_me, _this);
                    }
                }
                clearTimeout(jDropDown.data("currTimeout"));
                jDropDown.data("currTimeout", null);
            }, _me.delayClose));
        }
    });
}