if ( typeof Dianna == "undefined" )
{
	Dianna = {};
}

if ( typeof Dianna.Catalog == "undefined" )
{
	Dianna.Catalog = {};
}

Dianna.Catalog = function()
{
	var _items = new Array();

	function _addItem( id, title, price, previewWidth, previewHeight )
	{
		var details = "";

		for ( var argumentIndex = 5; argumentIndex < arguments.length; argumentIndex++ )
		{
			details += "<div>" + arguments[ argumentIndex ] + "</div>";
		}

		_items[ id ] = {
			'id' : id,
			'title' : title,
			'price' : price,
			'details' : details,
			'previewWidth' : previewWidth,
			'previewHeight' : previewHeight
		};
	}

	function _initialize( initialItemId )
	{
		var previewImg = $( "table.photogrid td.preview img#preview" )[ 0 ];

		$( "table.photogrid td.thumbnail" ).hover( function()
		{
			$( this ).addClass( "hover" );
		}, function()
		{
			$( this ).removeClass( "hover" );
		} );

		$( "table.photogrid td.thumbnail" ).each( function()
		{
			$( "a img", this ).appendTo( this );
			$( "a", this ).remove();
		} );

		$( "table.photogrid td.thumbnail img" ).click( function()
		{
			$( "table.photogrid td.thumbnail" ).each( function()
			{
				$( this ).removeClass( "selected" );
			} );

			var item = _items[ this.id ];

			if ( item )
			{
				$( this ).parents( "td.thumbnail" ).addClass( "selected" ).append( this );

				var thumbnailSrc = this.src;
				var newPreviewSrc = thumbnailSrc.replace( "thumbnail", "preview" );

				$( previewImg ).hide();
				$( previewImg ).removeAttr( "src" );

				previewImg.width = item.previewWidth;
				previewImg.height = item.previewHeight;

				previewImg.src = newPreviewSrc;
				previewImg.alt = item.title;
				previewImg.title = item.title;

				$( previewImg ).show();

				$( "div#description div#heading" ).html( item.title );
				$( "div#description div#details" ).html( item.details );
				$( "div#description div#price" ).html( item.price );

				var href = "/" + this.id.replace( "-", "/" );

				var v = pageTracker;
				pageTracker._trackPageview( href );
			}
			else
			{
				$( previewImg ).hide();
				$( previewImg ).removeAttr( "height" );
				$( previewImg ).removeAttr( "width" );

				previewImg.src = "";
				previewImg.alt = "";
				previewImg.title = "";

				$( "div#description div#heading" ).html( "" );
				$( "div#description div#details" ).html( "" );
				$( "div#description div#price" ).html( "" );
			}
		} );

		var initialItem = initialItemId ? _items[ initialItemId ] : null;

		if ( initialItem )
		{
			$( "table.photogrid td.thumbnail img#" + initialItem.id ).click();
		}
		else
		{
			$( "table.photogrid td.thumbnail:first img" ).click();
		}
	}

	return {

		initialize : _initialize,

		addItem : _addItem

	};
}();