/**
 * jQuery plugin.
 * Written by Tony R Quilkey
 * 21.05.2010
 */
/**
 * A simple image pre-loader.
 */
(function($) {
    $.fn.extend({
        preLoadImages: function(settings) {
            var defaults = {
                src: 'src'
            }

            var options = $.extend(defaults, settings);

            var cache = [];

            this.each(function() {
                var cacheImage = document.createElement('img');
                cacheImage.src = $(this).attr(options.src);
                cache.push(cacheImage);
            });
            return this;
        }
    });
})(jQuery);
/**
 * Hover over link to preview a thumb.
 */
(function($) {
    $.fn.extend({
        hoverImg: function(settings) {

            var defaults = {
                stage: '#hover-stage',
                attr: 'href'
            }

            var options = $.extend(defaults, settings);

            this.each(function() {
                $(this).find('li a').hover(
                    function() {
                        $(options.stage).html(
                            '<img src="' + $(this).attr(options.attr) + '" />'
                        );

                        var $img = $(options.stage).find('img');

                        var maxWidth = $(options.stage).css('width').replace('px','');
                        var maxHeight = $(options.stage).css('height').replace('px','');
                        var ratio = 0;
                        var width = $img.width();
                        var height = $img.height();

                        $(options.stage).hide();

                        if (width > maxWidth) {
                            ratio = maxWidth / width;
                            $img.css('width', maxWidth);
                            $img.css('height', height * ratio);
                            height = height * ratio;
                            width = width * ratio;
                        }

                        if (height > maxHeight) {
                            ratio = maxHeight / height
                            $img.css('height', maxHeight);
                            $img.css('width', width * ratio);
                            width = width * ratio;
                        }
                        $(options.stage).show();
                    },
                    function() {
                        $(options.stage).html('');
                    }
                );
            });
            return this;
        }
    });
})(jQuery);
