/**
 *
 * Written specifically for removing loading indicators with
 * parent.hideFBProgressDiv for non-flash community galleries (flash can do
 * this on its own.)
 *
 * Note that this prototype requires jQuery.
 *
 * @ author Charles Bunata
 *
 */

DRHorton = DRHorton || {};

DRHorton.community = DRHorton.community || {};

DRHorton.community.imageGalleryHelper = DRHorton.community.imageGalleryHelper || {};
    
;(function($){
    
    // PRIVATE
    
    var _imgCount = 0, _loaded = 0, _timeout = 5000,
    
        _onGalleryImageLoad = function(){
            _loaded++;
        }
        
        _success = function(){},
        _failure = function(){}
    ;
    
    // PUBLIC Methods
    
    /**
     * Tests to see if all specified images are loaded and, if they are, invokes
     * the param.success funciton. If the timeout is reached, the param.failure
     * fucntion is called.
     * 
     * @param params (object) {
     * 
     *    images (array) array of the all images to test.
     *
     *    success (optional:function) handler to call when all images have
     *    loaded
     *
     *    failure (optional:function) handler to call when all images fail
     *    to load in _timeout milliseconds.
     *    
     * }
     *
     * @return void
     *
     * Example:
     * 
     * DRHorton.ImageGalleryHelper.test({
     *      images: $('.imageGalleryContainer:last img'),
     *      success: successFunction,
     *      failure: failureFunction;
     *      }
     *  });
     */
    DRHorton.community.imageGalleryHelper.test = function(params){
        
        if(params.success != undefined) _success = params.success;
        if(params.failure != undefined) _failure = params.failure;
        
        _imgCount = params.images.length;
        
        params.images.each(function(){
            $(this).attr('onload', _onGalleryImageLoad);
        });
        
        // temporary fix
        if($('.imageGalleryContainer:last object').length == 0){
            DRHorton.utils.testConditionOverTime({
                expression: _loaded + " == " + _imgCount,
                interval: 250,
                timeLimit: _timeout,
                success: function(){
                    _success()
                },
                failure: _failure
            });
        }
    }
    
    
})(jQuery);


