var loadImages = function( parent )
{	
	var element = $('#' + parent + ' .detail_submenu_right .dynamicImage');
	
	// Check to make sure that this is the first time that the image will be loaded
	if( element.children('img').length <= 0 )
	{
		// Only convert the images for the tab that you are on
		element.prepend(function(index)
		{
			var thumbnail = $(this).attr('data-thumbnail');
			var title = $(this).attr('data-thumbnail-title');
			return '<img width="220" height="308" alt="' + title + '" src="' + thumbnail + '">';
		});
	}
}

// Make sure that every item and menu item has the correct ID
var assignGownIndex = function( item )
{
	var menuItems = $('.' + item + '_container .submenu_item_list li');
	var items = $('.' + item + '_container .detail_submenu_right .submenu_item');

	menuItems.each(function(key, value)
	{
		$(this).attr( 'id', 'item' + $(this).attr('data-id') );
	});
	
	items.each(function(key, value)
	{
		$(this).addClass( 'item' + $(this).attr('data-postid') + '_detail' );
	});
};

// show the selected menu item and mataching content
var show_item = function( item )
{
	var parent = item.parents('.detail_container');
	var lastSelectedItem = parent.find( ".selected_item" );
	var lastSelectedItemDetail = parent.find( ".selected_item_detail" );

	if ( lastSelectedItem ) {
		lastSelectedItem.removeClass("selected_item");
	}

	if ( lastSelectedItemDetail )
	{
		lastSelectedItemDetail.removeClass("selected_item_detail");
		lastSelectedItemDetail.hide();
	}

	item.addClass("selected_item");
	var id = "." + item.attr( "id" ) + "_detail";
	var itemDetail = parent.find( id );

	if ( itemDetail )
	{
		itemDetail.fadeIn( 'slow' );
		itemDetail.addClass("selected_item_detail");
	}
	
	loadImages( parent.attr('id') );
};

$(document).ready(function()
{
	$("#subnavigation").find(".subnavigation_element").each( function()
	{
		var subnavLink = $(this);
		
		subnavLink.click(function()
		{
			// make sure the first entry is always highlighted
			var subnavLinkId = subnavLink.attr("id"); 
			var subnavContainer = $( "." + subnavLinkId + "_container" );
			var firstItem = $('#' + subnavContainer.attr('id') + ' .submenu_item_list li').attr('id');
			show_item( $('#' + firstItem), subnavContainer );
			
			
			var subnavLinkId = $(this).attr("id");
			var lastInvoked = $(".selected_container");

			if ( lastInvoked )
			{
				lastInvoked.removeClass( "selected_container" );	
				lastInvoked.fadeOut( "slow" );
			}

			var subnavContainerId = subnavLinkId + "_container";
			var subnavContainer = $( "." + subnavContainerId );

			if ( subnavContainer )
			{
				subnavContainer.find(".submenu_item_list").find("li").click( function()
				{
					var item = $(this).attr('id');
					loadImages( item );
					show_item( $('#' + item ) );
				});
				if ( !subnavContainer.is(":visible") )
				{
					subnavContainer.animate({width:'toggle'},1500);
					subnavContainer.addClass( "selected_container" );
					$('#homePageContent').hide();
				}
				else
				{
					$('#homePageContent').show('fast');
				}
			}
		});			
			
	});
	
	assignGownIndex( 'gowns' );
	assignGownIndex( 'maidsflowergirls' );
	assignGownIndex( 'mothersspecialoccasion' );
	assignGownIndex( 'accessories' );

});
