﻿var _popoutId = "expand";
var _popout = null;
var _xOpen = -161;
var _xClosed = 0;
var _tweening = false;
var _tweenInfo;
var _tweenInterval;
var _refreshRate = 25;
var _autoShow = true;
var _expandInterval = 0;
var _closeInterval = 0;

function expandPopout() {
    clearInterval(_expandInterval);
    if (_popout != null) {
        _popout.style.display = "block";
        tweenX(_xOpen, 0.6, "out", expandComplete);
    }
}

function expandComplete() {
    _tweening = false;
}

function closePopout() {
    clearInterval(_closeInterval);
    if (_popout != null) {
        tweenX(_xClosed, 0.4, "in", closeComplete);
    }
}

function closeComplete() {
    _tweening = false;
    _popout.style.display = "none";
}

function initiateExpandingBox() {
    _popout = document.getElementById(_popoutId);
    if (_popout != null) {
        _popout.style.left = _xClosed + "px";
        if (_autoShow) {
            if (navigator.cookieEnabled) {
                var cookieVal = readCookie("popoutShown");
                var popoutShown = (cookieVal != null && cookieVal.toLowerCase() == "true");
                if (!popoutShown) {
                    createCookie("popoutShown", "true", null);
                    _expandInterval = setInterval("expandPopout()", 1000);
                    _closeInterval = setInterval("closePopout()", 10000);
                }
            }
        }
    }
}

function tweenX(val, time, ease, func) {
    if (!_tweening) {
        _tweening = true;
        _tweenInfo = { obj: _popout, val: val, time: (time * _refreshRate), count: 0, ease: ease, onComplete: func };
        _tweenInterval = setInterval("drawTween()", Math.round(1000 / _refreshRate));
    }
}

function drawTween() {

    _tweenInfo.count++;

    var t = _tweenInfo.count;
    var b = parseInt(String(_tweenInfo.obj.style.left).toLowerCase().replace("px", ""), 10);
    var c = _tweenInfo.val - b;
    var d = _tweenInfo.time;

    if (_tweenInfo.count == _tweenInfo.time) {
        _tweenInfo.obj.style.left = _tweenInfo.val + "px";
        clearInterval(_tweenInterval);
        _tweenInfo.onComplete();
    }
    else {

        var newVal;
        switch (_tweenInfo.ease.toLowerCase()) {
            case "in":
                newVal = Math.round(easeInQuad(t, b, c, d));
                break;
            case "out":
                newVal = Math.round(easeOutQuad(t, b, c, d));
                break;
        }
       
        _tweenInfo.obj.style.left = newVal + "px";
        if (newVal == _tweenInfo.val) {
            clearInterval(_tweenInterval);
            _tweenInfo.onComplete();
        }
    }
}

// t: current time, b: beginning value, c: change in position, d: duration
// quadratic easing in - accelerating from zero velocity
function easeInQuad(t, b, c, d) {
    return c * (t /= d) * t + b;
}

// quadratic easing out - decelerating to zero velocity
function easeOutQuad(t, b, c, d) {
    return -c * (t /= d) * (t - 2) + b;
}

addLoadEvent(initiateExpandingBox);

