var docuMouseUp = false;
var imagePadding = 6;

function onReadyThumbCarousel()
{
	if(!docuMouseUp)
	{
		//if mouse up not added then add
		docuMouseUp = true;
		$(document).mouseup(function(){stopTween(); return false;});
	}
	
	//for each gallery nav do resize carousel
	$('.galleryblock_nav').each(
		function()
		{
			onResizeCarousel($(this));
		}
	);
}

function onResizeCarousel(div)
{
	if(div != null)
	{
		//initalise carousel width
		intialiseCarouselWidth(div);
		
		//finds ul
		var ul = div.find('ul');
		if(ul.size() > 0)
		{
			//if childCount not set, then set
			if(data(ul, 'data-childCount') == undefined)
			{
				//set child count
				var kids = ul.children('li');
				//ul.children('li').hide();
				data(ul, 'data-childCount', kids.length);
			}
			
			//if loaded children not set
			if(data(ul, 'data-loadedChildren') == undefined)
			{
				debug(carouselDebug, "intialising carousel check");
				intialiseCarousel(div, ul);
				if(ul.find('li img').length > 0)
				{
					//if thumb navigation
					debug(carouselDebug, "intialising image carousel");
					intialiseImageCarousel(div, ul);
				}
				else
				{
					//else if text navigation
					debug(carouselDebug, "intialising number carousel");
					ul.children('li').each(
						function ()
						{
							$(this).show();
							incrementLoadedChildren(ul);
							updateGalleryWidth(ul, $(this));
						}
					);
					//check to see if its still on one line
					checkCarouselHeight(ul, 21, 0);
					onCarouselReady(ul);
				}
				
			}
			else if(data(ul, 'data-childCount') == data(ul, 'data-loadedChildren'))
			{
				//if carousel loaded
				debug(carouselDebug, "carousel ready");
				checkCarousel(div, ul);
			}
		}
	}
}

/////////////////////////////////
// CAROUSEL INTIALISE FUCNTIONS
/////////////////////////////////

function intialiseCarouselWidth(div)
{
	//set actuall width
	var cw = getActualWidth(div);		
	
	cw = div.parent().width();
	cw -= 32;
	setActualWidth(div, cw);
	setActualWidth(div.find('.galleryblock_container'), cw);
	debug(carouselDebug, "set container width to:"+getActualWidth(div));
}

function intialiseCarousel(div, ul)
{
	setActualWidth(ul, 0);
	ul.css('padding',  '0');
	data(ul, 'data-loadedChildren', 0);
	updateGalleryWidth(ul);
	div.css('overflow', 'hidden');
	//ul.find('li img').css('display', 'none');
}

function intialiseImageCarousel(div, ul)
{
	ul.find('li img').attr('alt', '');
	ul.find('li img').load(function(){onImageLoaded($(this), div, ul);});
	ul.find('li img').error(function(){onImageError($(this), div, ul);});
}

/////////////////////////////////
//CAROUSEL IMAGE FUCNTIONS
/////////////////////////////////

function onImageError(img, div, ul)
{
	onImageResponse(img, ul);
}

function onImageLoaded(img, div, ul)
{
	//unbind listeners
	img.unbind('load').unbind('error');
	
	if(stillNeedToLoad(ul) && img != null)
	{
		//set the list item to invisible
		img.parent().parent().css('visibility', 'hidden').show();
				
		//update gallery list width
		updateGalleryWidth(ul, img);
		
		//amke visible
		img.parent().parent().css('visibility', '');
		img.css('display', '');
				
		//on image response
		onImageResponse(img, ul);
	}
}

function onImageResponse(img, ul)
{
	if(stillNeedToLoad(ul) && img != null)
	{
		incrementLoadedChildren(ul);
		onCarouselReady(ul);
	}
}

function incrementLoadedChildren(ul)
{
	var loaded = getNumber(ul, 'data-loadedChildren') + 1;
	//debug(carouselDebug, "loaded "+ loaded +" carousel images");
	data(ul, 'data-loadedChildren', loaded, true);
}

/////////////////////////////////
//CAROUSEL TEXT ITEM FUCNTIONS
/////////////////////////////////

//check to see if the list is only one line
function checkCarouselHeight(ul, oH, times)
{
	var uH = ul.height();
	debug(carouselDebug, "list two line check, ul height:"+uH+", against list item height:"+oH);
	if(uH > oH && times < 100)
	{
		updateGalleryWidth(ul, 1);
		checkCarouselHeight(ul, oH, times+1);
	}
}

/////////////////////////////////
//CAROUSEL QUEUE FUCNTIONS
/////////////////////////////////

//checks to see if we need to load any more children still
function stillNeedToLoad(ul)
{
	if(data(ul, 'data-loadedChildren') == undefined)
	{
		data(ul, 'data-loadedChildren', 0);
	}
	if(getNumber(ul, 'data-loadedChildren') < getNumber(ul, 'data-childCount'))
	{
		return true;
	}
	return false;
}

/////////////////////////////////
//CAROUSEL READY FUCNTIONS
/////////////////////////////////

function onCarouselReady(ul)
{
	//update list container
	var wrap = ul.parent();
	setCarouselSettings(wrap, ul);
	
	debug(carouselDebug, "loaded "+ getNumber(ul, 'data-loadedChildren') +"/"+getNumber(ul, 'data-childCount'));
	if(!stillNeedToLoad(ul))
	{
		if(ul.find('li img').size() > 0)
		{
			//make sure theres no image listeners
			ul.find('li img').unbind('load').unbind('error');
			ul.find('li img').css('display', 'inline');
			preciseCarouselWidth(ul, 0);
		}
		debug(carouselDebug, "setting carousel, loaded "+ data(ul, 'data-loadedChildren') +"/"+data(ul, 'data-childCount')+", list height:"+ul.height()+", width: "+getActualWidth(ul));
		setListeners(wrap, ul);
	}
}

function setListeners(wrap, ul)
{
	//make sure all navs dont do anything
	var div = wrap.parent();
	div.find('.navs').click(function(){return false;});
	
	//if list is greater than its container
	if(ul.width() > wrap.width())
	{
		var height = ul.height();
		debug(carouselDebug, "set navigation tween listeners");
		
		if(div.find('.gcarousel').size() > 0)
		{
			//enable buttons
			div.find('.disabled-navs').toggleClass('disabled-navs', false);
			
			var prev = div.find('.prev');
			if(data(prev, 'data-onmousedown') == undefined)
			{
				//set navigation listeners
				data(prev, 'data-onmousedown', true);	
				div.find('.prev').mousedown(function(){startTween(this, 1); return false;});
				div.find('.next').mousedown(function(){startTween(this, -1); return false;});
			}
		}
	}
}

function setCarouselSettings(wrap, ul)
{
	wrap.css('overflow', 'hidden');
	wrap.height(60);
	if(ul.height() < wrap.height())
	{
		var height = ul.height()+4;
		wrap.height(height);
		wrap.parent().height(height);
		//ul.css('margin-top', wrap.height()-ul.height()-4);
	}
	
	debug(carouselDebug, "container width "+getActualWidth(wrap)+ " compared to list width " + getActualWidth(ul));
	if(wrap.width() < actualWidth(ul))
	{
		ul.animate({"left": 0}, 0);
		wrap.css('text-align', 'left');
	}
}

function preciseCarouselWidth(ul, times)
{
	var uH = ul.height();
	var lp = ul.find('li:first').position();
	if(times < 100)
	{
		updateGalleryWidth(ul, -1);
		var np = ul.find('li:last').position();
		debug(carouselDebug, "last list item offset :"+lp.top+", compare to new offset:"+np.top);
		if(lp.top == np.top)
		{
			preciseCarouselWidth(ul, times+1);
		}
		else
		{
			updateGalleryWidth(ul, 1);
		}
	}
}


/////////////////////////////////
//CAROUSEL RESIZE FUCNTIONS
/////////////////////////////////

function checkCarousel(wrap, ul)
{
	var ulw = ul.width();
	var div = wrap.parent();
	if(div.find('.gcarousel').size() > 0)
	{
		if(ul.width() < wrap.width())
		{
			div.find('.navs').toggleClass('disabled-navs', true);
		}
		else
		{
			div.find('.navs').toggleClass('disabled-navs', false);
		}
	}
	updateGalleryWidth(ul);
}


function updateGalleryWidth(ul, ele)
{
	//set width od list
	var ulw = getActualWidth(ul);
	
	if(ele != null)
	{
		if(!isNaN(ele))
		{
			//else if number
			ulw+=ele;
		}
		else
		{
			//if image was passed then update list
			var elew = ele.width();
			if(ele.is('img'))
			{
				debug(carouselDebug, "loaded image, with width "+elew);
			}
			else
			{
				debug(carouselDebug, "number width: "+elew);
			}
			ulw+=elew;
			if((getNumber(ul, 'data-loadedChildren')+1) < getNumber(ul, 'data-childCount'))
			{
				var padding = 4;
				if(ele.is('img'))
				{
					padding = imagePadding;
				}
				ulw+=padding;
				debug(carouselDebug, "add padding: "+padding);
			}
		}
		setActualWidth(ul, ulw);
		debug(carouselDebug, "set list width to: "+ulw);
		ulw = getActualWidth(ul);
		debug(carouselDebug, "list width: "+ulw);
	}
	
	//set css width
	ul.width(ulw);
	
	//list holder
	//this makes allows the list to scroll
	var pulw = getActualWidth(ul.parent());
	
	if(pulw < ulw)
	{
		ulw = pulw;
	}
	
	//set width of nav container
	//plus padding for corners
	//gallery-nav
	ul.parent().parent().width(ulw+32);
	
	//set width of background
	ul.parent().parent().find('.galleryblock_container_bg').width(ulw);
}


