﻿/*
 * post.js
 * Copyright (C) 2009
 * Author: Pascal Haakmat <pascal.haakmat@lightmaker.com>
 * Created: 2009-05-11
 */

var Post = {
    isScrolledIntoView: function(elem)
    {
        var docViewTop = $(window).scrollTop();
        var docViewBottom = docViewTop + $(window).height();

        var elemTop = $(elem).offset().top;
        // 300 is RespondBox element height, we need to add this because initially it will be 0 (because of slideUp).
        var elemBottom = elemTop + $(elem).height() + 300; 

        return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom)
          && (elemBottom <= docViewBottom) &&  (elemTop >= docViewTop) );
    }
}

$(document).ready(function() {

    $(".RespondOpenLink").click(function(e) {

        if ($("#RespondBox").is(":hidden")) {
            $("#RespondBox").slideDown("slow");
            
            if(!Post.isScrolledIntoView($("#RespondBox")))
                $('html,body').animate({ scrollTop: $("#RespondBox").offset().top - 300 }, 2000);

        }
        else {
            $("#RespondBox").slideUp("slow", function() {
                $(this).hide();
            });
        }
        return false;
    });
    
    

});
