// This file was generated by Dashcode from Apple Inc.
// You may edit this file to customize your Dashboard widget.

function CreateGrabber(grabberId, frontId)
{
    var grabberElement = document.getElementById(grabberId);
    if (!grabberElement.loaded) {
        grabberElement.loaded = true;
        grabberElement.object = new Grabber(grabberElement, document.getElementById(frontId));
        return grabberElement.object;
    }
}

function Grabber(grabberImg, front)
{
    this._front = front;
    this._grabberImg = grabberImg;

    var _self = this;

    this._updateOpacity = function(animation, now, first, done)
    {
        _self._grabberImg.style.opacity = now * 0.75;
    }

    this._animationComplete = function()
    {
        delete _self._animation;
        delete _self._animator;
    }

    this._frontMove = function(event)
    {
        if (_self._outdelay !== undefined) {
            clearTimeout(_self._outdelay);
            delete _self._outdelay;
        }
        if (_self._shown) {
            return;
        }

        var from = 0.0;
        var duration = 500;
        if (_self._animation !== undefined) {
            from = _self._animation.now;
            duration = (new Date).getTime() - _self._animator.startTime;
            _self._animator.stop();
        }

        _self._shown = true;

        var animator = new AppleAnimator(duration, 13);
        animator.oncomplete = _self._animationComplete;
        _self._animator = animator;

        _self._animation = new AppleAnimation(from, 1.0, _self._updateOpacity);
        animator.addAnimation(_self._animation);
        animator.start();
    }

    this._frontOutDelay = function(event)
    {
        if (_self._outdelay === undefined) {
            _self._outdelay = setTimeout(_self._frontOut, 0, _self);
        }
    }

    this._frontOut = function(_self)
    {
        if (_self._outdelay !== undefined) {
            clearTimeout(_self._outdelay);
            delete _self._outdelay;
        }
        if (!_self._shown) {
            return;
        }

        var from = 1.0;
        var duration = 500;
        if (_self._animation !== undefined) {
            from = _self._animation.now;
            duration = (new Date).getTime() - _self._animator.startTime;
            _self._animator.stop();
        }

        var animator = new AppleAnimator(duration, 13);
        animator.oncomplete = _self._animationComplete;
        _self._animator = animator;

        _self._animation = new AppleAnimation(from, 0.0, _self._updateOpacity);
        animator.addAnimation(_self._animation);
        animator.start();

        _self._shown = false;
    }

    this._grabberImg.style.opacity = 0.0;
    this._grabberImg.setAttribute("onclick", "event.stopPropagation(); event.preventDefault();");
    this._front.addEventListener("mousemove", this._frontMove, true);
    this._front.addEventListener("mouseout", this._frontOutDelay, true);
}

Grabber.prototype.hide = function()
{
    this._grabberImg.style.display = "none";
}

Grabber.prototype.show = function()
{
    this._grabberImg.style.display = "block";
}
