// JavaScript Document

function($) {

/**
 * A page loading progress object. Initialized by passing a "loader" element in
 * which the loading progress will be displayed by a Drupal.progressBar (or not)
 *
 * e.g. Insert <div id="loader"></div> inside your page.tpl.php (or html...)
 *      then follow usage :)
 *
 * Usage:
 *    $('#loader').loader({options});
 *
 * Options:
 *    'wrapper': $(document.body),
 *        The element in which we check for the images to load
 *    'container': $('#container'),
 *        Container for your content which will get hidden then faded-in when
 *        the page has finished loading
 *    'class': 'loading',
 *        CSS class added to the loader and container for extra theming
 *    'useDrupalProgress': true
 *        Use Drupal.progressBar or not, in which case a simple
 *        <div><span>0</span></div> will be inserted in $(loader) and the
 *        span element will get updated with the progress in percentage
 *    'callback': function(){}
 *        Callback function that gets called after $('#container') finished
 *        fading in
 *
 *
 * Examples and documentation at: http://www.caktux.ca/
 * Version: 1.1 (28/04/2009)
 * Requires: jQuery v1.2.6+
 * Copyright (c) 2009 Vincent Gariepy
 * Dual licensed under the MIT (http://en.wikipedia.org/wiki/MIT_License)
 * and GPL (http://en.wikipedia.org/wiki/GNU_General_Public_License) licenses
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.

 */

  $.fn.loader = function(options) {
    return $.fn.loader.init(this, options);
  };

  $.fn.loader.init = function(loader, options) {

    if ($(loader).length <= 0) { return false; }

    var settings = {
      'wrapper': $(document.body),
      'container': $('#container'),
      'class': 'loading',
      'useDrupalProgress': true,
      'callback': function(){}
    };
    if (options) $.extend(settings, options);

    if (settings.useDrupalProgress) {
      if (!Drupal.jsEnabled) return false;
      var pb = new Drupal.progressBar('loader-progress');
      var $pb = $(pb.element);
      var $loader = $(loader).hide().append($pb).fadeIn('fast');
    } else {
      var $percent = $('<span>0</span>');
      var $loadel = $('<div></div>').text('%').prepend($percent);
      var $loader = $(loader).append($loadel);
    }

    var $wrapper = $(settings.wrapper);
    var $container = $(settings.container);

    imgLoaded = 0;
    imgTotal = $('img:visible', $wrapper).length;

    if (imgTotal <= 0) { initLoaded(); return false; }

    // We can go ahead now, hide the content in $container and attach
    // behaviors
    $container.hide();

    $(loader).addClass(settings.class);
    $wrapper.addClass(settings.class).css('cursor','wait');

    $('img:visible', $wrapper).each(function(img){
      $(this).load(function(e){
        imgLoaded++;
        var percentLoaded = parseInt((parseInt(imgLoaded) * 100) / parseInt(imgTotal));
        if (settings.useDrupalProgress) {
          pb.setProgress(percentLoaded);
        } else {
          $percent.text(percentLoaded);
        }
        if (imgLoaded == imgTotal) {
          initLoaded();
        }
      });
    });
  
    initLoaded = function() {
      $container.fadeIn('normal',function() {
        $loader.removeClass(settings.class).stop().fadeOut('normal');
        $wrapper.removeClass(settings.class).css('cursor','auto');
        if (typeof(settings.callback) == 'function') $(settings.callback({'loaded': true}));
        if ($.browser.msie) {
          fixIEfilter(this);
        }
      });
    };

    $(window).load( function() { // Force initLoaded() when images don't fire .load()
      if (settings.useDrupalProgress) {
        pb.setProgress(100);
      } else {
        $percent.text('100');
      }
      initLoaded();
    });

    return loader;
  };

  fixIEfilter = function(els) {
    $(els).each(function(i){
      if ($(this).length > 0) {
        if (this.style.filter && this.style.removeAttribute) {
          this.style.removeAttribute('filter');
        }
      }
    });
  };

}(jQuery);


