
if (document.all)
    document.write(
        '<style type="text/css" media="screen">@import "/wp-content/themes/apg1/sheet_ie.css";</style>');

function initializePage()
{
    Comments.init();

    // Need to do it this way for compatibility with Safari 1.3    
    //document.body.setAttribute("onclick", "onClickBody(event)");
    //document.body.onclick = onClickBody;
}

function onClickBody(evt)
{
    if (window.event) evt = event;
    
    var target = getEventTarget(evt);
    if (target.nodeName == "IMG" && hasClass(target, "blogImage"))
    {
        preventDefault(evt);
        ImageBubble.show(target);
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////

var ImageBubble =
{
    imageBubbleElt: null,
    
    show: function(thumbElt)
    {
        var imageBubbleElt = this.imageBubbleElt;
        
        if (!imageBubbleElt)
        {
            imageBubbleElt = document.createElement("div");
            this.imageBubbleElt = imageBubbleElt;
            
            imageBubbleElt.className = "imageBubble";
            imageBubbleElt.imageElt = document.createElement("img");
            imageBubbleElt.imageElt.className = "imageBubbleImage";
            imageBubbleElt.previewElt = document.createElement("img");
            imageBubbleElt.previewElt.className = "imageBubblePreview";
    
            imageBubbleElt.appendChild(imageBubbleElt.imageElt);
            imageBubbleElt.appendChild(imageBubbleElt.previewElt);

            RoundedBox.makeRoundedBox(imageBubbleElt);
            
            var captionElt = document.createElement("div");
            captionElt.className = "imageBubbleCaption";
            captionElt.appendChild(document.createTextNode("(Click to close)"));
            imageBubbleElt.appendChild(captionElt);
            
            document.body.appendChild(imageBubbleElt);
        }
        
        if (imageBubbleElt.style.visibility == "visible")
            return;
    
        var width = Number(thumbElt.getAttribute("originalWidth"));
        var height = Number(thumbElt.getAttribute("originalHeight"));
    
        if (height > width && height > window.innerHeight)
        {
            var newHeight = window.innerHeight - 140;
            width = (width/height) * newHeight;
            height = newHeight;
        }
        else if (width > height && width > window.innerWidth)
        {
            var newWidth = window.innerWidth - 140;
            height = newWidth / (width/height);
            width = newWidth;
        }
        
        var top = windowScrollY() + (windowInnerHeight()/2 - height/2)
        var left = windowInnerWidth()/2 - width/2;
        var previewElt = imageBubbleElt.previewElt;
        previewElt.src = thumbElt.src;
        previewElt.style.width = width + "px";
        previewElt.style.height = height + "px";
            
        var imageElt = imageBubbleElt.imageElt;
        imageElt.src = thumbElt.src;
        imageElt.style.width = width + "px";
        imageElt.style.height = height + "px";
        
        imageBubbleElt.style.top = top + "px";
        imageBubbleElt.style.left = left + "px";
        imageBubbleElt.style.width = (width + 102) + "px";
        
        imageBubbleElt.style.visibility = "visible";
    
        imageElt.src = thumbElt.parentNode.href;
        registerPopup(imageBubbleElt);
    }
};

////////////////////////////////////////////////////////////////////////////////////////////////////

function smoothScrollIntoView(element, onLastFrame)
{
    var padding = 40;

    var windowBottom = windowScrollY() + windowInnerHeight();

    var offset = getClientOffset(element);
    var elementTop = offset.y - padding;
    var elementBottom = offset.y + element.offsetHeight;

    if (elementTop < windowScrollY() || elementBottom > windowBottom)
    {
        var scrollAmount = (elementBottom - windowBottom) + padding;

        // If the top of the comments would go out of view, then scroll to anchor the
        // top of the comments to the top of the window instead
        if (elementTop < windowScrollY() + scrollAmount)
            scrollAmount = elementTop - windowScrollY();
        
        var keyframes =
        [{
            begin: 0, duration: 400, tween: easeOutQuad,
            onframe: onScrollFrame,
            onlastframe: function() { if (onLastFrame) setTimeout(onLastFrame, 0); }
        }];

        Timeline.play(keyframes, 1, { scrollMin: windowScrollY(), scrollAmount: scrollAmount });
    }
}

function onScrollFrame(keyframe, progress, params)
{
    window.scrollTo(0, params.scrollMin + (progress.tweenProgress * params.scrollAmount));
}

// Easing equations courtesy of http://www.robertpenner.com/easing_terms_of_use.html
function easeOutQuad(t, b, c, d)
{
	return -c *(t/=d)*(t-2) + b;
}
