(function($){
    $.fn.thefold = function(){
        return this.each(function(){
            var p = $(this);
            var element = {};
            element.id = $(this).attr('id');
            element.position = p.position();
            element.top = element.position.top;
            element.offscreen = element.position.top + $(this).height();
            $(window).bind("scroll", function(){
                isOffscreen(element);
            });
            isOffscreen(element);
        });
    };
    
    function isOffscreen(element){
   		//alert ('off : ' + element.id);
        if (($(window).scrollTop()) > element.offscreen || element.top > ($(window).scrollTop() + $(window).height())) {
            $('body').trigger({
                type: "offscreen",
                id: element.id
            });
            return true;
        }
        else {
            $('body').trigger({
                type: "onscreen",
                id: element.id
            });
            return false;
        }
    };
    
    //public
    $.fn.thefold.format = function(){
    };
    $.fn.thefold.defaults = {};
    
    
})(jQuery);
