/*
 * jQuery Preloader v1.0 - http://softmachine.co.il/devtools/jquery/preload/
 *
 * Uses the built in timer capabilities in JQuery
 * to offer image preloading onload / on demand
 *
 * TERMS OF USE - jQuery Preloader
 * 
 * Open source under the BSD License. 
 * 
 * Copyright © 2011 Softmachine.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * Neither the name of the author nor the names of contributors may be used to endorse 
 * or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 *
*/

/********************************************************
timer : load images on timed interval t/f ; 
quota : number of images to load per call ;
delay : time interval between calls

load_bw( q[optional] ) : load q images from the last to download to last - q
load_fw( q[optional] ) : load q images from the first to download to first + q
   ++when q is unspecified, the above quota is used as default.++   
********************************************************/

(function($){
	/* image preloader by Soft Machine */
    $.preload = function(el, timer, quota, delay, options){
        // To avoid scope issues, use 'base' instead of 'this'
        // to reference this class from internal events and functions.
        var base = this;

        // Access to jQuery and DOM versions of element
        base.$el = $(el);
        base.el = el;

        // Add a reverse reference to the DOM object
        base.$el.data("preload", base);

        base.init = function(){			
            if( typeof( quota ) === "undefined" || quota === null ) quota = 10;
            if( typeof( delay ) === "undefined" || delay === null ) delay = 1000;
			if( typeof( timer ) === "undefined" || timer === null ) timer = false;
            base.quota = quota;
            base.delay = delay;
			base.timer = timer;
			
            base.options = $.extend({},$.preload.defaultOptions, options);
			
            base.img_set = base.$el.find('img');   
			base.fl = 0
			base.ll = base.img_set.length - 1;	
			var q = base.quota / 2;
			if (base.quota % 2)	q += 1;						
		
			base.load_bw(base.quota - q);
			base.load_fw(q);
			if (base.timer)
			{				
				setInterval(function(){
					 base.load_fw(q);
					 base.load_bw(base.quota - q);
			          },  base.delay);
			}
			
        };
        
		

		 base.load_bw = function(q) {				
					var i, j ;					
					if (base.ll > base.fl)
					{						
						if( typeof( q ) === "undefined" || q === null ) q = base.quota;						
				        i = parseInt(parseInt(base.ll) - parseInt(base.fl));						
						i = i > q ? q : i;						
						j = parseInt(base.ll - i);						
						while (base.ll > j)
						{
						  if ($(base.img_set[base.ll]).attr('asrc')) {
						      $(base.img_set[base.ll]).attr('src', $(base.img_set[base.ll]).attr('asrc'));
						      }	 
						 base.ll--;  		  
						}						
					}
				//	alert("base.ll="+base.ll);
           // alert("base.fl="+base.fl);
				}

        base.load_fw = function(q) {							
					var i, j ;
					if (base.ll > base.fl)
					{
					   if( typeof( q ) === "undefined" || q === null ) q = base.quota;
					   i = base.ll - base.fl + 1;
					   i = i > q ? q : i;
					   j = base.fl + i;					   
					   while (base.fl < j)
					   {
						 if ($(base.img_set[base.fl]).attr('asrc')) {
						     $(base.img_set[base.fl]).attr('src', $(base.img_set[base.fl]).attr('asrc'));
							  }	 
						 base.fl++;  		  
					   }									   
					}
					//alert("base.ll="+base.ll);
            //alert("base.fl="+base.fl);
				}
        // Run initializer
        base.init();
    };

    $.preload.defaultOptions = {       
    };

    $.fn.preload = function(timer, quota, delay, options){
        return this.each(function(){
            (new $.preload(this, timer, quota, delay, options));
              
                   // HAVE YOUR PLUGIN DO STUFF HERE

                   // END DOING STUFF

        });
    };

})(jQuery);
