/*

Copyright (c) 2011 Sam Giberson, http://www.samgiberson.com/

Licensed under the MIT license
http://en.wikipedia.org/wiki/MIT_License

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.

*/

jQuery.fn.addLoader = function(options){
	
	// merge passed settings with defaults
	var opts = jQuery.extend({}, jQuery.fn.addLoader.defaults, options);
	
	return this.each(function()
	{
		// add the loading bar markup to the container
		jQuery(this).prepend('<div class="loading-bar"><div class="progress"></div></div>');
		
		// grab total number of images the animation is dependent on
		var TOTAL_IMAGES = jQuery(this).find('img').length;
		
		// grab height and width of container for positioning the loading bar
		var container_width = jQuery(this).width();
		var container_height = jQuery(this).height();
		
		// set width of loading bar to be half of the total width
		var loader_width = Math.floor(container_width/2);
		
		// loading bar height
		var loader_height = opts.height;
		var half_loader_height = opts.height/2;
		
		// set width of each step in loader
		var loader_step_width = Math.ceil(loader_width/TOTAL_IMAGES);
		
		// set step at zero, for use in preload loop later on
		var step = 0;
		
		// for vertical center in container, set top position at half of the container height, less half of the loading bar height
		var loader_position_top = Math.floor(container_height/2) - half_loader_height;
		
		// loader width is half of the container, so set left position to 1/4 of total width for horizontal center
		var loader_position_left = Math.floor(container_width/4);
		
		// position loader to center of container
		jQuery('.loading-bar').css({height: ''+ opts.height +'px', top: ''+ loader_position_top +'px', left: ''+ loader_position_left +'px', width: ''+ loader_width +'px'});
		jQuery('.progress').css({height: ''+ opts.height +'px'})
		// create array for all images that need to be loaded prior to starting the animation
		var preload_images = [];
		
		// loop through all the images, grab their source and stuff them into the preload_images array
		for ( pre=0; pre < TOTAL_IMAGES; pre++  )
		{
			preload_images[pre] = jQuery(this).find('img:eq(' + pre + ')').attr('src');
		}
		
		// add preload_images array to image preloader
		jQuery.imgpreload(preload_images,
		{
			each: function()
			{
				// as each image loads, decrease the count of the remaining images by one, add one to the step count
				// and then animate the progress bar to the appropriate width
				TOTAL_IMAGES--;
				step++;
				jQuery('.progress').animate(
					{width: '' + (loader_step_width * step) + 'px'},
					{
						queue: false,
						duration: opts.speed,
						easing: ''+ opts.ease +'',
						complete: function()
						{
							// once .progress has reached it's final width, remove the loading bar
							// letting the .progress bar reach its final width allows for animations
							// to run longer than the time it takes to preload each image	
							var progress_w = jQuery(this).width();
							if ( progress_w >= loader_width )
							{
								hideLoader();
							}
							
						}
					}
				);
			},
			all: function()
			{
				// anything here will run as soon as all images have been preloaded
			}
		});
	});
	
}

// defaul options
jQuery.fn.addLoader.defaults = {
	height: 4,
	speed: 2000,
	ease: 'easeInCirc'
};



// remove the loading bar from view and fire a callback function to display your glorious content
function hideLoader()
{
	jQuery('.loading-bar').animate(
		{opacity: 0},
		{
			queue: false,
			duration: 750,
			easing: 'easeInSine',
			complete: function()
			{
				// your callback function here
				homePageSlider();
			}
		}
	);
	
}
