﻿
// Bauer Media Limited
//Copyright 2008 Bauer Media Limited
//
// Created 10/11/2008 by Duncan Gossage
// 
//
// Revision History
//===============================================
//
// Job Reference    Developer          Date
//-----------------------------------------------
// CR-016           Kaz Rudys          10/11/2008

var scrollObjectList = null;

function AddNewScrollObject( baseIdContainer )
{
	// Initialisation
	if( scrollObjectList == null )
	{
	    scrollObjectList = new Array();
		setInterval("goTheFrame()", 20);
	}

	var mainContainer = $(baseIdContainer);

	var innerContainer = $(baseIdContainer + " > .carouselWrapper > #innerContainer");

	var scrollObject = $( baseIdContainer + " > .carouselWrapper > #innerContainer > .scroller");

	var item = $(baseIdContainer + " > .carouselWrapper > #innerContainer > .scroller > li");
	var itemCount = item.length;
	
	if (itemCount > 0 && mainContainer.length > 0 && innerContainer.length > 0 && scrollObject.length > 0) 
	{

	    var itemMargin = parseInt(removePx(item.css("margin-left")));
	    var itemWidth = parseInt(removePx(item.css("width")));
		
	    var innerContainerWidth = parseInt(removePx(innerContainer.css("width")));
	    var scrollerWidth = itemCount * (itemWidth + itemMargin) + itemMargin;
	    var itemsInView = Math.floor(innerContainerWidth / (itemWidth + itemMargin));
		
	    var maxCurrentItem = 1;

	    if (itemCount >= itemsInView)
	    {
	        maxCurrentItem = itemCount - (itemsInView - 1);
	    }

	    scrollObject.css("width", scrollerWidth);


	    var newObj = {
	        scroller: scrollObject,
	        scrollContainer: innerContainer,
	        Container: mainContainer,
	        marginWidth: itemMargin,
	        itemWidth: itemWidth,
	        totalItems: itemCount,
	        currentItem: 1,
	        totalViewableItems: maxCurrentItem,
	        targetXPos: 0,
	        Xpos: 0,
	        panelSpeed: .3,
	        scrollButtons: true,
	        MoveMe: function() { PositionChecker(this) }
	    };

	    var listCount = scrollObjectList.length;
	    scrollObjectList[listCount] = newObj;
	}
}


function PositionChecker(scrollObj) 
{
    if (scrollObj != null) 
    {
        if (scrollObj.Xpos > scrollObj.targetXPos || scrollObj.Xpos < scrollObj.targetXPos) 
        {
            var newXpos = scrollObj.Xpos - ((scrollObj.Xpos - scrollObj.targetXPos) * scrollObj.panelSpeed);

            if (Math.abs(scrollObj.targetXPos - newXpos) < 0.5) 
            {
                newXpos = scrollObj.targetXPos;
            }

            scrollObj.scroller.css("left", newXpos);
            scrollObj.Xpos = newXpos;
        }
    }
}



function Scroll(scrollAmount, scrollBoolean, idName)
{
    var CurrentListObject;

    if (scrollObjectList == null)
    {
        return;
    }

	for (iCount = 0; iCount <= scrollObjectList.length-1; iCount++) 
	{
		if( scrollObjectList[iCount].Container.attr("id") == idName )
		{
			CurrentListObject = scrollObjectList[iCount];
			break;
		}
	}
	
	if( CurrentListObject == null )
	{
		return;
	}
    
    if(scrollBoolean)
    {
		CurrentListObject.currentItem += scrollAmount;
	    
		if( CurrentListObject.currentItem > CurrentListObject.totalViewableItems )
		{
			CurrentListObject.currentItem = 1;
		}
	    
		if( CurrentListObject.currentItem < 1 )
		{
			CurrentListObject.currentItem = CurrentListObject.totalViewableItems;
		}
	}
	else
	{
		if( scrollAmount > CurrentListObject.totalViewableItems)
		{
			scrollAmount = CurrentListObject.totalViewableItems;
		}
		
		CurrentListObject.currentItem = scrollAmount;
	}
	
	newLeftXpos = -(( CurrentListObject.currentItem - 1 ) * ( CurrentListObject.itemWidth + CurrentListObject.marginWidth ));
	CurrentListObject.targetXPos = newLeftXpos;
}



function removePx(strParam)
{
    var result = strParam.replace("px", "");
    
    return result;
}

function goTheFrame() 
{
	if( scrollObjectList.length > 0 )
	{
		for( iCount = 0; iCount <= (scrollObjectList.length-1); iCount++ )
		{
			scrollObjectList[iCount].MoveMe();
		}
	}
}