(function($){

jQuery.preloadImages = function() {
        var a = (typeof arguments[0] == 'object')? arguments[0] : arguments;
        for(var i = a.length -1; i > 0; i--) {
                jQuery("<img>").attr("src", a[i]);
        }
}

$.fn.simpleGallery = function(options) {

  var defaults = {
   image_container: this.attr('class'),
   gallery_links_container: 'gallery_links_container',
   selected_link_style: 'selected_link_style'
  };
  var options = $.extend(defaults, options);

  var preload = new Array();
  $('.'+options.gallery_links_container+' a').each(function(i){
	  preload[i] = $(this).attr('href');
  });
  $.preloadImages(preload);

  $('.'+options.gallery_links_container+' a').click(function() {

	  var image_address = $(this).attr('href');
	  if ($(this).attr('title')) var image_description = $(this).attr('title');
		else var image_description = '';

	  $('.'+options.image_container).html(
			'<img src="'+image_address+'" alt="_" title="'+image_description+'" />'
		);

	  if (options.selected_link_style) {
		  $('.'+options.gallery_links_container+' a').removeClass(options.selected_link_style);
		  $(this).addClass(options.selected_link_style);
	  }
	  if (options.inactive_image_link && options.active_image_link) {
		  $('.'+options.gallery_links_container+' a img').attr('src',options.inactive_image_link);
		  $(this).find('img').attr('src',options.active_image_link);
	  }
	  return false;
  });

  //first
  if ($('.'+options.gallery_links_container+' a:first').attr('title')) var first_description = $('.'+options.gallery_links_container+' a:first').attr('title');
		else var first_description = '';
  var first_image = $('.'+options.gallery_links_container+' a:first').attr('href');
  $('.'+options.image_container).html(
			'<img src="'+first_image+'" alt="_" title="'+first_description+'" />'
	);
	  if (options.selected_link_style) {
		  $('.'+options.gallery_links_container+' a:first').removeClass(options.selected_link_style);
		  $('.'+options.gallery_links_container+' a:first').addClass(options.selected_link_style);
	  }
	  if (options.inactive_image_link && options.active_image_link) {
		  $('.'+options.gallery_links_container+' a:first img').attr('src',options.active_image_link);
	  }
 };
})(jQuery);
