/**
 * Javascript file for Th. Marthinsen web presence
 *
 * @author bitExpert AG
 * @since  2009-05-13
 * @version 1.0
 *
 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * This file is devided into sections:
 *
 * 1. jQuery carousel functions
 *
 *
 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 */


/**
 * ############################## SECTION 1 ####################################
 * ###################### jQuery carousel functions ############################
 */


/**
 *
 * Callback function for the carousel gallery to get the items for a specific
 * category and load them into the gallery
 *
 * @param carousel
 * @param state
 *
 */
function marthinsen_itemLoadCallback(carousel, state)
{
	// does the requested item already exist?
	if(carousel.has(carousel.first, carousel.last))
	{
		return;
	}

	// get the items for the carousel gallery via JSON
	$.getJSON(
		'index.php5?sModule=ajax.LoadCarouselImages&d=cutlery',
		{
			first: carousel.first,
			last: carousel.last
		},
		function(json)
		{
			marthinsen_itemAddCallback(carousel, carousel.first, carousel.last, json);
		}
	);
}


/**
 *
 * Callback function to add the images out of the json string to the gallery
 *
 * @param carousel
 * @param first
 * @param last
 * @param json
 *
 */
function marthinsen_itemAddCallback(carousel, first, last, json)
{
	// Set the size of the carousel
	carousel.size(parseInt(json.items));

	// iterate over the images and add them sequentially to the carousel
	$.each(json.images, function(i)
		{
			carousel.add(first + i, marthinsen_getItemHTML(json.images[i]));
		}
	);

	// add hover effect to carousel images
	$('#carousel li a').hover(
		function()
		{
			var $oImage = $(this).children('img');
			$oImage.attr('src', $oImage.attr('src').replace(/\.jpg/, 'a.jpg'));
		},
		function()
		{
			var $oImage = $(this).children('img');
			$oImage.attr('src', $oImage.attr('src').replace(/a\.jpg/, '.jpg'));
		}
	);
}


/**
 * generates the HTML Code for one image object out of the JSON String
 *
 * @param img
 */
function marthinsen_getItemHTML(img)
{
	return "<a href='#'><img src='" + img.path + "' alt='' width='241' height='124' /><div><p><span>" + img.name + "</span></p></div></a>";
}
