function onReadyThumbCarousel()
{
	$(document).mouseup(stopTween);
	$('.galleryblock_nav').each(
		function()
		{
			onResize($(this));
		}
	);
}

function onResize(div)
{
	if(div != null)
	{
		var ul = div.find('ul');
		if(ul.attr('childCount') == undefined)
		{
			//set child count
			var $kids = ul.children('li');
			ul.attr('childCount', $kids.length);
		}
		if(ul.attr('loadedChildren') == undefined)
		{
			debug("intialising carousel");
			if(ul.find('li img').length > 0)
			{
				debug("intialising image carousel");
				ul.css('padding',  '0');
				intialiseCarousel(div, ul);
			}
			else
			{
				debug("intialising number carousel");
				ul.attr('loadedChildren', ul.attr('childCount'));
				ul.attr('aH', 0);
				ul.children('li').each(
					function ()
					{
						var aH = (ul.attr('aH')*1) + $(this).width() + 5;
						ul.attr('aH', aH);
					}
				);
				
				onCarouselReady(ul);
			}
		}
		else if(ul.attr('childCount') == ul.attr('loadedChildren'))
		{
			//if carousel loaded
			debug("carousel ready");
			checkCarousel(div, ul);
		}
	}
}

/////////////////////////////////
// CAROUSEL INTIALISE FUCNTIONS
/////////////////////////////////

function intialiseCarousel(div, ul)
{
	div.css('overflow', 'hidden');
	div.height(60);
	//ul.find('li img').css('display', 'none');
	ul.find('li img').load(function(){onImageLoaded($(this), ul);});
	ul.find('li img').error(function(){onImageError($(this), ul);});
}

function onImageError(img, ul)
{
	onImageResponse(img, ul);
}

function onImageLoaded(img, ul)
{
	if(img != null)
	{
		if(ul.attr('aH') == undefined)
		{
			ul.attr('aH', 0);
		}
		var aH = (ul.attr('aH')*1) + img.width() + 5;
		ul.attr('aH', aH);
		img.css('display', 'inline');
		onImageResponse(img, ul);
	}
}

function onImageResponse(img, ul)
{
	if(img != null)
	{
		if(ul.attr('loadedChildren') == undefined)
		{
			ul.attr('loadedChildren', 0);
		}
		var loaded = (ul.attr('loadedChildren')*1) + 1;
		debug("loaded "+ loaded +" carousel images");
		ul.attr('loadedChildren', loaded);		
		onCarouselReady(ul);
	}
}

function onCarouselReady(ul)
{
	var div = ul.parent();
	setCarouselSettings(div, ul);
	debug("loaded "+ ul.attr('loadedChildren') +"/"+ul.attr('childCount'));
	if(ul.attr('loadedChildren') == ul.attr('childCount'))
	{
		debug("setting carousel, loaded "+ ul.attr('loadedChildren') +"/"+ul.attr('childCount'));
		ul.find('li img').css('display', 'inline');
		setListeners(div, ul);
		checkCarousel(div, ul);
	}
}

function setListeners(div, ul)
{
	if(div.width() < ul.width())
	{
		var height = ul.height();
		debug("nav line-height " + (height-8));
		div.parent().find('.disabled-navs').toggleClass('disabled-navs', false);
		//div.parent().find('.navs').css('line-height', (height-8));
		
		var prev = div.parent().find('.prev');
		if(prev.attr('onmousedown') == undefined)
		{
			prev.attr('onmousedown', true);			
			div.parent().find('.prev').mousedown(function(){startTween(this, 1);});
			div.parent().find('.next').mousedown(function(){startTween(this, -1);});
		}
	}
}

function setCarouselSettings(div, ul)
{
	div.css('overflow', 'hidden');
	div.height(60);
	if(ul.height() < div.height())
	{
		var height = ul.height()+4;
		div.height(height);
		div.parent().height(height);
		//ul.css('margin-top', div.height()-ul.height()-4);
	}
	ul.width(ul.attr('aH')*1);
	debug(div.width() + " compared to " + ul.attr('aH'));
	if(div.width() < ul.attr('aH')*1)
	{
		ul.animate({"left": 0}, 0);
		div.css('text-align', 'left');
	}
}

/////////////////////////////////
//CAROUSEL RESIZE FUCNTIONS
/////////////////////////////////

function checkCarousel(div, ul)
{
	var ulw = ul.width();
	if(ul.width() < div.width())
	{
		div.parent().find('.navs').toggleClass('disabled-navs', true);
	}
	else
	{
		div.parent().find('.navs').toggleClass('disabled-navs', false);
	}
	$('.galleryblock_nav .galleryblock_container_bg').width(ulw);
	$('.galleryblock_nav .galleryblock_container_bg').css('margin-left', -(ulw/2));
}

/////////////////////////////////
//CAROUSEL TWEEN FUCNTIONS
/////////////////////////////////

var tweenTimeFactor	= 75;
var intialScrollSpeed = 0.25;
var scrollSpeed = 0;

function stopTween()
{
	scrollSpeed = 0;
}

function startTween(a, value, tO)
{
	if(a != null && value != null)
	{
		if(tO == null)
		{
			tweenObject = $(a).parent().find('ul');
		}
		else
		{
			tweenObject = tO;
		}
		scrollSpeed = intialScrollSpeed*value;
		tween();
	}
}

function tween()
{
	if(tweenObject)
	{
		var currentPos 	= tweenObject.position().left*1;
		var newPos 		= currentPos + scrollSpeed;
		var maxPos		= tweenObject.parent().width() - tweenObject.width();

		if(newPos < maxPos)
		{
			newPos = maxPos;
		}
		else if(newPos > 0)
		{
			newPos = 0;
		}

		if(currentPos != newPos)
		{
			tweenObject.animate({"left": newPos}, 0);
			scrollSpeed+= scrollSpeed;
			setTimeout(tween, tweenTimeFactor);
		}
	}
}
