// JavaScript Document
/*
Author: Alex Boyce
Copyright: 2009
Date: 9/29/09

Creative Commons License
*/

(function($) {
	$.fn.imagelist = function(options) {
		var defaultSettings = {
			divToLoad: "#preview",
			imageBorder: 0,
			linkImage: false,
			linkTag: "div",
			linkText: true,
			linkDeliminator: ","
		},
		settings = $.extend({}, defaultSettings, options);
		
		$(this).each(function(i) {
			var pl = new Image();
			pl.src = $(this).attr("href");
			});
		
		$(this).click(function(event) {
			event.preventDefault();
			
			$(document.body).css({cursor: "wait"});
			$(settings.divToLoad).html("Loading...");
			var src = $(this).attr("href"),
			img = new Image(),
			//alt = "";
			alt = ($(this).attr("title").length > 0) ? $(this).attr("title") : $(this).text();
			img.src = src;
			
			var imageStr = "<img src=\"" + img.src + "\" border=\"" + settings.imageBorder + "\" height=\"" + img.height + "\" width=\"" + img.width + "\" alt=\"" + alt + "\" title=\"" + alt + "\" />";
			
			if (settings.linkImage) {
				if (settings.linkText) {
					var parts = $(this).siblings(settings.linkTag).text().split(settings.linkDeliminator),
					l = parts[0],
					t = parts[1];
					imageStr = "<a href=\"" + l + "\" title=\"" + t + "\">" + imageStr + "</a>";
					imageStr += "<p align=\"center\"><a href=\"" + l + "\" title=\"" + t + "\">" + t + "</a></p>";
				}
				else {
					imageStr = "<a href=\"" + $(this).siblings(settings.linkTag).text() + "\">" + imageStr + "</a>";
				}
			}
			
			$(settings.divToLoad).html(imageStr);
			$(document.body).css({cursor: "default"});
			});
		
		return this;
		}
	})(jQuery);