/* 
Showcase widget. 
Replaces the first showcase image with a showcase gallery. All images
with the "showcase" CSS class will be included in this gallery, and will
be removed from their original location.

Depends on jQuery
*/
$(document).ready(function() {
	function insertShowcase(id, cls) {
		var sc_id = "scw" + id.toString()
		var orig = $("." + cls);
		orig.first().before("<div id=\"" + sc_id + "\" class=\"scw\"></div>");
		var sc = $("#" + sc_id);
	
		// Insert the HTML code for the gallery
		sc.append("<div class=\"full\"><img src=\"" + orig.first().attr("src") + "\"></div>");
		sc.append("<ol class=\"thumbs\"></ol>");
		sc.append("<div class=\"break\"></div>");
		
		orig.hide();
		orig.each(function() {
			sc.find(".thumbs").append("<li><a class=\"select\" href=\"#\"><img src=\"" + $(this).attr("src") + "\"></a></li>");
		});
		orig.remove();
		
		sc.find(".thumbs img").load(function() {
			var w = $(this).width();   
			var h = $(this).height();
			var padHor = Math.floor((73 - w) / 2);
			var padVer = Math.floor((78 - h) / 2);
			$(this).parent().parent().css("padding", padVer.toString() + "px " + padHor.toString() + "px");
			$(this).parent().parent().css("width", w.toString() + "px");
			$(this).parent().parent().css("height", h.toString() + "px");
		});
		
		sc.find(".select").click(function() {
			$(this).children("img").each(function() {
				sc.find(".full img").attr("src", $(this).attr("src"));
			});
			return false;
		});
	}

	// Find all images that have to be put in showcase galleries
	classes = $("[class^=showcase]").map(function() { return this.className});
	
	// Find unique image classes and insert separate galleries for each
	var showcases = [];
	for (var i = 0; i < classes.length; i++) {
		if ($.inArray(classes[i], showcases) == -1) {
			insertShowcase(i, classes[i]);
			showcases.push(classes[i]);
		}
	}
});

