/*
Name:       Image Loader
Version:    1.2 (February 20th, 2010)
Author:     Bryan T. Smith
Support:    http://orionseven.com/imageloader

Licence:    Image Loader is licensed under the MIT license
			http://www.opensource.org/licenses/mit-license.php
            
Credits:    The great jQuery community producing numerous tutorials 
			on creating jQuery plugins. 
*/

// Image Loader Initalization Function Declaration
jQuery.fn.imageLoader = function(options, callBackFunction) {

    var opts = $.extend({}, $.fn.imageLoader.defaults, options);
    var loading = 1;
    var appendToID = this;
    var images = [];
    var params = [];
    var imgWidth = '';
    var imgHeight = '';    
    var imagesIsObjects = (typeof opts.images[0] == 'object') ? true : false;

    insertLoading();

    // Add Images
    for (var i = 0; i < opts.images.length; i++) {
        images['img' + i] = new Image();        
		$(appendToID).append(function(index, html) {
			var img = $(images['img' + i]).load(function() {
				updateLoading();
			});
			
			if (opts.hideImages) {
				$(img).hide();
			}			
			img.attr((imagesIsObjects ? opts.images[i] : { src: opts.images[i] }));            
			return img;
		});           
        if (opts.imgWidth){
            $(images['img' + i]).attr('width', opts.imgWidth);
        }
        if (opts.imgHeight){
            $(images['img' + i]).attr('height', opts.imgHeight);
        }
        /*if (opts.imgClass){
            $(images['img' + i]).attr('class', opts.imgClass);
        }*/
        $(images['img' + i]).wrap('<div class="slide">').css('display', 'block');
        if (opts.params[i][0] != 'null'){
            var target;
            var link = document.createElement('a');
            link.setAttribute('href', opts.params[i][0]);
            if (opts.params[i][1] != 'null'){
                target = opts.params[i][1];
                link.setAttribute('target', target);
            }
            //$(images['img' + i]).wrap('<a href="'+opts.params[i][0]+'" '+(target?' target="'+target+'"':'')+' >');
            //$(images['img' + i]).wrap('<a href="#">');
            $(images['img' + i]).wrap(link);
        }
    }

    return $(this);

    // Private Function: Update Loading Screen
    function updateLoading() {
        loading++;

        if ((opts.images.length + 1) == loading) {
            if (opts.endableSplashScreen) {
                $('#imageLoaderSplashArea').remove();
            }

            if (typeof callBackFunction == 'function') {
                callBackFunction.call();
            }
        }
        else {
            if (opts.endableSplashScreen && opts.enableSplashScreenText)
                $('#imageLoaderSplashArea p.loadingText').html("Loading " + loading + " of " + opts.images.length);
        }
    };

    // Private Function: Insert Loading Screen
    function insertLoading() {
        if (opts.endableSplashScreen) {
            var preloadArea = '<div id="imageLoaderSplashArea">'
				+ ((opts.enableSplashScreenImage) ? '<img src="' + opts.splashScreenImage + '" class="splashScreenImage" />' : '')
				+ ((opts.enableSplashScreenText) ? '<p class="loadingText">Loading ' + loading + ' of ' + opts.images.length + '</p>' : '')
				+ '</div>';

            $(appendToID).append(preloadArea);
        }
    }
};

// Default Options
jQuery.fn.imageLoader.defaults = {
	images: [],
    params: [],
	splashScreenImage: 'images/splashScreenImage.gif',
	enableSplashScreenImage: 1,
	enableSplashScreenText: 1,
	endableSplashScreen: 1,
	hideImages: 1
};
