$(document).ready(function()
{
	// Preload some images - begin
	jQuery('<img>').attr('src', '/assets/images/banners/01_cupcakes.jpg');
	jQuery('<img>').attr('src', '/assets/images/banners/02_snowmen.jpg');
	jQuery('<img>').attr('src', '/assets/images/banners/03_poodles.jpg');
	jQuery('<img>').attr('src', '/assets/images/banners/04_ladybugs.jpg');
	jQuery('<img>').attr('src', '/assets/images/banners/05_wedding.jpg');
	jQuery('<img>').attr('src', '/assets/images/banners/06_easter.jpg');
	
	// Cycle the banner images
	$("#banner").cycle
	({
	 	timeout: 5000,
		speed: 1000
	});
	
	// Determine the current anchor, if it represents a gallery id, display it!
	var current_anchor = window.location.hash;
	if(current_anchor.substr(0, 1) == "#" && $(current_anchor).length > 0 && $(current_anchor).hasClass("gallery"))
	{
		$(current_anchor).removeClass("gallery").addClass("gallery_active");
	}
	
	// Ensure that at least one gallery is open
	if($(".gallery_active").length <= 0 && $(".gallery").length > 0)
	{
		$(".gallery:first").removeClass("gallery").addClass("gallery_active");
		
		// Update the hash to reflect the open gallery item
		window.location.hash = $(".gallery:first").attr('id');
	}
	
	// Activate/deactivate galleries
	$(".activate_gallery").click(function()
	{
		// Determine what the target id is
		var link_id = $(this).attr("id");
		var target_id = "";
		if(link_id !== "")
		{
			target_id = link_id.replace("activate_", "");
		}
		
		// If we have a valid target id, activate/deactivate it
		if(target_id !== "" && $("#"+target_id).length > 0)
		{
			var activating = false;
			if($("#"+target_id).hasClass("gallery"))
			{
				$("#"+target_id).removeClass("gallery").addClass("gallery_active");
				activating = true;
			}
			else
			{
				$("#"+target_id).removeClass("gallery_active").addClass("gallery");
			}
			
			// If we are activating a gallery; update the current anchor to reflect which one we're looking at
			if(activating)
			{
				window.location.hash = target_id;
			}
			
			// Since we're deactivating a gallery, we may want to clear the hash if no galleries are open
			else if($(".gallery_active").length <= 0)
			{
				window.location.hash = "";
			}
			
			return false;
		}
		
		return true;
	});
	
	// Setup external links
	// Load the page in a new window
	$('a[rel=external], a.external_link').each(function()
	{
		// Set target, clear rel and update titles
		$(this).attr("target", "_blank").attr("rel", "");
		var old_title = $(this).attr("title");
		if(old_title !== "")
		{
			$(this).attr("title", old_title+" [Opens in new window]");
		}
		else
		{
			$(this).attr("title", "[Opens in new window]");
		}
	});
});

