/* ITEM SCROLLER */

var thumbItemWidth = 60;
var bodyItemWidth = 676;
var activeBodyIndex;
var viewedThumbIndex;

var itemCount;
var thumbContainerWidth;
var bodyContainerWidth;

/*function initScroller(){
	itemCount = $('.ui-tabs-show ul.thumbViewer li').length;
	activeBodyIndex = 0;
	viewedThumbIndex = 0;
	thumbContainerWidth = itemCount * thumbItemWidth;
	bodyContainerWidth = itemCount * bodyItemWidth;
	$('div.ui-tabs-show ul.thumbViewer a.itemIndex').click(function(){
		activeBodyIndex = $("div.ui-tabs-show ul.thumbViewer a.itemIndex").index(this);
		updateBodyScroller();		
		});
	updateBodyScroller();
    }*/
function initScroller(e, ui){
    activeBodyIndex = 0;
    viewedThumbIndex = 0;
    if(ui == null)
    {
        itemCount = $('.ui-tabs-show ul.thumbViewer li').length;
    }else{
        itemCount = $(ui.panel).find('ul.thumbViewer li').length;
        $(ui.panel).find('ul.thumbViewer li').removeClass('active');
        $(ui.panel).find('ul.thumbViewer li').eq(activeBodyIndex).addClass('active');
    }

    thumbContainerWidth = itemCount * thumbItemWidth;
    bodyContainerWidth = itemCount * bodyItemWidth;
    var items;
    if(ui == null)
    {
        items = $('div.ui-tabs-show ul.thumbViewer a.itemIndex');
    }
    else
    {
        items = $(ui.panel).find('ul.thumbViewer a.itemIndex');
    }
    items.unbind('click');
    items.click(function(){
        activeBodyIndex = $("div.ui-tabs-show ul.thumbViewer a.itemIndex").index(this);
        updateBodyScroller();        
        });
    updateThumbScroller();
    updateBodyScroller();
    }	
	
function updateActiveThumb(){
	$('.ui-tabs-show ul.thumbViewer li').removeClass('active');
	$('.ui-tabs-show ul.thumbViewer li').eq(activeBodyIndex).addClass('active');
	if( (activeBodyIndex - viewedThumbIndex) >= itemsToScroll)
	{
		thumbNext();
	}
	else if( viewedThumbIndex > activeBodyIndex)
	{
					thumbPrev();
	}
	}

function updateBodyScroller(){
				var marginLeftValue = String(-1 * bodyItemWidth * activeBodyIndex) + 'px';
				$('div.ui-tabs-show .portItemCon').animate({ marginLeft: marginLeftValue}, 'normal');
				$('li.portScrollDesc').html('<span>' + String(activeBodyIndex + 1) + '</span> / ' + String(itemCount));
				updateActiveThumb();
}

function ensureInitted(){
				if(itemCount == undefined){
								initScroller();
				}
}
function bodyNext(){
				ensureInitted();
				if(activeBodyIndex < itemCount - 1){
					activeBodyIndex += 1;
								updateBodyScroller();
				}
				return false;
}

function bodyPrev(){
				ensureInitted();
				if(activeBodyIndex > 0){
								activeBodyIndex -= 1;
								updateBodyScroller();
				}
				return false;
}

/* THUMBNAIL SCROLLER */
var itemsToScroll = 10;

function updateThumbScroller(){
				var marginLeftValue = String(-1 * thumbItemWidth * viewedThumbIndex) + 'px';
				$('.ui-tabs-show ul.thumbViewer').animate({ marginLeft: marginLeftValue }, 'normal');
}
		   
function thumbPrev() {
							ensureInitted();
	if(viewedThumbIndex >= itemsToScroll)
							{
											viewedThumbIndex -= itemsToScroll;
											updateThumbScroller();
							}
	return false;
}

function thumbNext() {
							ensureInitted();
							if(viewedThumbIndex < (itemCount - itemsToScroll))
							{
											viewedThumbIndex += itemsToScroll;
											updateThumbScroller();
							}
	return false;
}