$.fn.preload = function() {
    this.each(function(){
        $('<'+this.nodeName+'/>')[0].src = $(this).attr('src');
    });
}

$(document).ready(function(){
    $(".images").each(function() {
		root = this;
		
        items = $(root).find("img, object, iframe");
		
        /*
		items.each(function() {
			$(this).preload();
		});
        */
        
		numbers = $('<div class="numbers clearfix" />');
		
		$(items).wrapAll('<div />');
		
		for(i=1; i <= items.length; i++) {
			$(numbers).append($('<a href="#">'+i+'</a>'));
		}
		
		$(this).prepend(numbers);
		
		$(this).find('img, object, iframe').slice(1).hide();
		$(this).find('a:eq(0)').addClass('selected');
		
		if(items.length <= 1) {
			$(root).find('.numbers').hide();
		}
    });
	
	$('.numbers a').click(function() {
		images = $(this).closest('.images');
		numbers = $(this).closest('.numbers');
		
		n = $(this).index();
		
		//hide
		images.find('img, object, iframe:not(:eq('+n+'))').hide();
		numbers.find('a:not(:eq('+n+'))').removeClass('selected');
		
		//show
		$(images.find('img, object, iframe').get(n)).show();
		$(this).addClass('selected');
		return false;
	});
});
