/*****************************************************************
 *
 * inspired upon lazierLoad 0.4 - by Bramus! - http://www.bram.us/
 * inspired upon http://www.appelsiini.net/projects/lazyload/
 *
 *****************************************************************/
         
    /**
     * JS_BRAMUS2 Object
     * -------------------------------------------------------------
     */
     
    if (!JS_BRAMUS2) { 
        var JS_BRAMUS2 = new Object(); 
    }
        
        
    /**
     * lazierLoad Class
     * -------------------------------------------------------------
     */

        JS_BRAMUS2.lazierLoad                = Class.create();
        JS_BRAMUS2.lazierLoad.prototype      = {
            elements                : new Hash(),
            lazyScroller        : null,

            initialize          : function() {              
            },

            add             : function(ele,func) {

                var position = $(ele).cumulativeOffset();
                var viewportHeight = z_getHeight();
                var viewportScroll = z_getScroll();    

                // "above the fold" already
                if ((viewportScroll + viewportHeight) > position[1]) {
                    //load now
                    func();
                } else {
                    //setup to watch
                    this.elements.set(ele,func);
                    
                    if (!this.lazyScroller) {
                        Event.observe(window, 'scroll', this.lazyScroll.bind(this));  
                        this.lazyScroller = 1;
                    }
                }  
            },

            remove          : function (ele) {
                this.elements.unset(ele);           
            },      
            
            lazyScroll          : function() {
                var viewportHeight = z_getHeight();
                var viewportScroll = z_getScroll();
               
                var process = new Array();
                var elekeys = this.elements.keys();
                for(var x = 0; x < elekeys.size(); ++x) {
                    var i = elekeys[x];
                    var position = $(i).cumulativeOffset();

                    if ((viewportScroll + viewportHeight) > position[1]) {
                        //load now
                        this.elements.get(i)();
                        process.push(i);
                    }
                }
               
                for(var x = 0; x < process.size(); ++x) { 
                    this.elements.unset(process[x]);
                }

                if (this.elements.size() == 0) {
                    Event.stopObserving(window, 'scroll', this.lazyScroll);
                    this.lazyScroller = 0;
                }             
            }

        }
        
                
    /**
     * Hook lazierLoad to the dom:loaded event
     * -------------------------------------------------------------
     */
    
    myDivLL = new JS_BRAMUS2.lazierLoad();
        
