var GetFileName = function(str){
	var regexp = /(\w|[-.])+$/;
	return regexp.exec(str);
}

var mycarousel_itemList = new Array();


function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt)
{
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, mycarousel_itemList.length);
    carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1]));	
};

function mycarousel_galleryItemVisibleOutCallback(carousel, item, i, state, evt)
{
	if(state == 'next'){
		src = GetFileName($(item).next().next().find('img').attr('src'));
		$('#galleryShow img').hide();
		$('#galleryShow img').fadeIn().attr('src', 'images/gallery/l/'+src[0]);
		$('#galleryShow p').text($(item).next().next().find('img').attr('alt'));
	}
	else {
		src = GetFileName($(item).prev().prev().find('img').attr('src'));
		$('#galleryShow img').hide();
		$('#galleryShow img').fadeIn().attr('src', 'images/gallery/l/'+src[0]);
		$('#galleryShow p').text($(item).prev().prev().find('img').attr('alt'));
	}
	carousel.remove(i);
};

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt)
{
	carousel.remove(i);
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(item)
{
	return '<img src="' + item.url + '" width="96" height="65" alt="' + item.title + '" />';
};


$(document).ready(function(){
	
	
	$("#galleryList img").each(function(){
		mycarousel_itemList.push({url: $(this).attr('src'), title: $(this).attr('alt')});						
									
	});
	
	$("#galleryList").jcarousel({
		scroll: 1,
        wrap: 'circular',
		itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
		itemVisibleOutCallback: {onAfterAnimation: mycarousel_galleryItemVisibleOutCallback}

	});
	
	
});















