﻿var FADE_SPEED = 450;
var TIP_OFFSET = 80;
var INITIAL_RANDOM_WAIT = 5;

var divs = new Array();
var containers = new Array();
var popDivs = new Array();

window.onscroll = DoScroll;

var lastUpdate = new Date();
var initialScroll = true;
var hasInteract = false;

function DoScroll()
{
    //Throttle this to half-second updates
    if (new Date() - lastUpdate > 150 || initialScroll)
    {
        for (var i = divs.length - 1; i >= 0; i--)
        {
            if (divs[i].style.display == "none")
            {
                var oT = parseFloat(containers[i].offsetTop);
                var oH = parseFloat(containers[i].clientHeight);

                var wO = GetScroll();
                var wH = GetHeight();

                if (oT + oH < wO + wH)
                {
                    if (initialScroll)
                        divs[i].style.display = "";
                    else
                        $(divs[i]).fadeIn("slow");
                }
            }
        }

        lastUpdate = new Date().getTime();
    }

    initialScroll = false;
}

function StartRandom()
{
    setTimeout("ShowRandomTip()", INITIAL_RANDOM_WAIT * 1000);
}

function ShowRandomTip()
{
    if (hasInteract)
        return;
    var rnd = Rand(0, popDivs.length - 1);

    var container = document.getElementById(popDivs[rnd]);



    var wrapper = getFirstChild(container);

    var child = getFirstChild(wrapper);

    var sib = getNexSibling(child);

    while (child.style.display == "none")
    {
        rnd = Rand(0, popDivs.length - 1);

        container = document.getElementById(popDivs[rnd]);
        wrapper = getFirstChild(container);
        child = getFirstChild(wrapper);
        sib = getNexSibling(child);
    }

    sib.style.display = "";

    $(sib).animate(
            {
                "top": "-=10px",
                "opacity": "+=.9"
            }, FADE_SPEED).delay(10000);

    $(sib).animate(
            {
                "top": "+=10px",
                "opacity": "-=.9"
            }, FADE_SPEED, 0, function ()
            {
                if (!hasInteract)
                    ShowRandomTip();
            });


}

function Register(div, type)
{
    switch (type)
    {
        case "Image":
            RegisterImage(div);
            break;
        case "Color":
            RegisterColor(div);
            break;
    }
}
function RegisterColor(div)
{
    var container = document.getElementById(div);
    var wrapper = getFirstChild(container);

    wrapper.style.display = "none";

    containers.push(container);

    divs.push(wrapper);
}

function RegisterImage(div)
{
    var container = document.getElementById(div);
    var wrapper = getFirstChild(container);
    var child = getFirstChild(wrapper);
    var sib = getNexSibling(child);

    $(sib).css({ opacity: 0 });

    child.style.display = "none";

    popDivs.push(div);

    containers.push(container);

    divs.push(child);

    var origHeight = sib.clientHeight;


    $(child).load(
            function ()
            {
                sib.style.top = container.clientHeight - origHeight + TIP_OFFSET + "px";
                sib.style.left = "0px";

                sib.style.height = origHeight - TIP_OFFSET + "px";

                sib.style.display = "none";
            }
            );

    $(wrapper).hover(
            function ()
            {
                hasInteract = true;

                //Don't show tips of elements you can't see!
                if (child.style.display == "")
                {
                    sib.style.top = container.clientHeight - origHeight + TIP_OFFSET + "px";
                    sib.style.height = origHeight - TIP_OFFSET + "px";
                    sib.style.display = "";

                    $(sib).stop().animate(
                       {
                           "top": "-=80px",
                           "height": "+=80px",
                           "opacity": "+=.9"
                       }, FADE_SPEED
                       );
                }
            },
            function ()
            {
                //Don't show tips of elements you can't see!
                if (child.style.display == "")
                {
                    $(sib).animate(
                    {
                        "top": "+=80px",
                        "height": "-=80px",
                        "opacity": "-=.9"
                    }, FADE_SPEED,
                    function ()
                    {

                        //We sometimes lose our tip.  I don't know why.  Reset it!
                        sib.style.top = container.clientHeight - origHeight + TIP_OFFSET + "px";
                        sib.style.height = origHeight - TIP_OFFSET + "px";
                        sib.style.display = "none";

                    }
                    );
                }
            }
           );

    $(wrapper).click(
            function ()
            {
                if (child.style.display == "")
                {
                    if (sib.style.display == "")
                    {
                        window.location = "http://www.cnn.com";
                    }
                    else
                    {
                        sib.style.display = "";

                        $(sib).animate(
                       {
                           "top": "-=10px",
                           "opacity": "+=.9"
                       }, FADE_SPEED
                       );
                    }
                }

            }
            );
}
