﻿(function($) {
    $.fn.latestMembers = function(members) {
        //must pass in an array of member data 
        if (!members) { return this; }

        return this.each(function() {
            var me = this, timer = setTimeout(nextImage, 15000), indexCount = 0, images = [], img = $(me).find('dd img'), init = false;

            function nextImage() {
                if (!init) {
                    $(me).find('dt').text('You can also get found by:');
                    init = true;
                }

                //check for same
                if (img.get(0).src.indexOf(images[indexCount].attr('src')) != -1) {
                    indexCount += 1;
                    if (indexCount == images.length) { indexCount = 0; }
                }

                img = images[indexCount].clone().css('visibility', 'hidden').replaceAll(img).parents('a:first').attr('href', members[indexCount].url).end();

                var newWidth = img.width(), newHeight = img.height();

                img.css({ width: 0, height: 0, opacity: 0, visibility: 'visible' }).animate({ opacity: 1, width: newWidth, height: newHeight }, 1500, '', function() {
                    indexCount += 1;
                    if (indexCount == members.length) { indexCount = 0; }
                    timer = setTimeout(nextImage, 5000);
                });
            }

            //preload the images
            $.each(members, function(e, itm) {
                images.push($('<img src="' + itm.path + '" alt="' + itm.title + '" />'));
            });
        });
    }; //latestmember function

    $.fn.scrollTo = function(pad) {
        //to keep in line with jquery, return the jquery object
        return this.each(function() {
            var top = $(this).offset().top;
            if (pad && (top > pad)) { top = top - pad; }
            $('html, body').animate({ scrollTop: top }, "slow");
        });
    };
})(jQuery);

$('document').ready(function() {
	// This function makes any links marked with rel=external pop up in a new window
	$('a[rel=external]').click(function() {window.open(this.href);return false;});
});

