﻿
var RecentProductViews_outerDivForSlider;
var RecentProductViews_innerDivForSlider;
var RecentProductViewsOuterDiv;
var navRecentProductViews_Prev;
var navRecentProductViews_Next;
var navRecentProductViews_ViewThisItem;
var navRecentProductViews_AddToCart;

var CurrentContentIndex;

var recentprods_glider;
var recentproducttimer;
var AnimationIsLocked = false; //start AnimationIsLocked in the unlocked state

function GetCurrentTextLinkUrl()
{
    if (((typeof $) != 'function') || isNaN(CurrentContentIndex))
        return null;
        
    var CurrentTextLink = $('RecentProducts_TextLink_'+CurrentContentIndex);
    if ((!CurrentTextLink) || (CurrentTextLink == null))
        return null;
    
    return CurrentTextLink.href;
}

function GetCurrentHiddenProductId()
{
    if (((typeof $) != 'function') || isNaN(CurrentContentIndex))
        return null;
        
    var CurrentHiddenProductId = $('RecentProducts_HiddenProductId_'+CurrentContentIndex);
    if ((!CurrentHiddenProductId) || (CurrentHiddenProductId == null))
        return null;
    
    return CurrentHiddenProductId.value;
}

function GetNextIndex(currentIndex)
{
    if ((isNaN(NumberProductsFilled)) || (NumberProductsFilled <= 1))
        return 0;

    //there are at least 2 NumberProductsFilled
    var nextIndexTemp = ((currentIndex+1) % NumberProductsFilled);
    
    return nextIndexTemp;
}

function GetPreviousIndex(currentIndex)
{
    if ((isNaN(NumberProductsFilled)) || (NumberProductsFilled <= 1))
        return 0;

    //there are at least 2 NumberProductsFilled
    var prevIndexTemp = (currentIndex-1);
    if (prevIndexTemp<0)
    {
        prevIndexTemp = (NumberProductsFilled - 1);
    }
    
    return prevIndexTemp;
}


function AnimateScroller(PushElementLeft)
{
    if (((typeof $) != 'function') || (!RecentProductViews_outerDivForSlider) || (!RecentProductViews_innerDivForSlider))
        return;
        
    //try animate, do nothing on error.
    try
    {
        if (recentprods_glider && (recentprods_glider != null))
        {
            if (PushElementLeft)
            {
                recentprods_glider.next();
            }
            else
            {
                recentprods_glider.previous();
            }
        }
    }
    catch (exc)
    {
    throw exc;
        return;
    }
}

function AnimateAndUpdateScrollerControls(PushElementLeft)
{
    
    //while animating the scroll, unset all the nav buttons
    UnsetScrollerNav_All();
    
    //lock AnimationIsLocked
    AnimationIsLocked = true;

    AnimateScroller(PushElementLeft);

}

function SetAndGoToPrevious()
{
    if (recentproducttimer)
        clearTimeout(recentproducttimer);
    
    if (AnimationIsLocked)
        return false;
        
    if (isNaN(CurrentContentIndex))
        return false;

    //set current index to previous
    CurrentContentIndex = GetPreviousIndex(CurrentContentIndex);

    AnimateAndUpdateScrollerControls(false);
    if (!isNaN(RecentProductLoopWaitTime))
    {
        recentproducttimer = setTimeout("SetAndGoToNext();", RecentProductLoopWaitTime);
    }
    
    //return false so the link isn't followed when it has been clicked.
    return false;

}

function SetAndGoToNext()
{
    if (recentproducttimer)
        clearTimeout(recentproducttimer);
    
    if (AnimationIsLocked)
        return false;
        
    if (isNaN(CurrentContentIndex))
        return false;
        
    //set current index to next
    CurrentContentIndex = GetNextIndex(CurrentContentIndex);

    AnimateAndUpdateScrollerControls(true);
    if (!isNaN(RecentProductLoopWaitTime))
    {
        recentproducttimer = setTimeout("SetAndGoToNext();", RecentProductLoopWaitTime);
    }

    //return false so the link isn't followed when it has been clicked.
    return false;

}

function SetScrollerNav_All()
{
    if (((typeof $) != 'function') || (!navRecentProductViews_Prev) || (!navRecentProductViews_Next))
        return


    navRecentProductViews_Prev.onclick = SetAndGoToPrevious;
    navRecentProductViews_Next.onclick = SetAndGoToNext;

    SetScrollerNav_ViewThisItem();
    SetScrollerNav_AddToCart();

    //unlock AnimationIsLocked
    AnimationIsLocked = false;
}

function SetScrollerNav_ViewThisItem()
{
    if (((typeof $) != 'function') || (!navRecentProductViews_ViewThisItem))
        return
    
    navRecentProductViews_ViewThisItem.href = GetCurrentTextLinkUrl();
    navRecentProductViews_ViewThisItem.onclick = ""; //allow the href to be clicked

}

function SetScrollerNav_AddToCart()
{
    if (((typeof $) != 'function') || (!navRecentProductViews_AddToCart))
        return
    
    var CurrentHiddenProductId = GetCurrentHiddenProductId();
    var AddToCartForm_RecentProducts_ProductIDHiddenField = $('ProductID');
    
    if ((CurrentHiddenProductId != null) && (CurrentHiddenProductId > 0) && (AddToCartForm_RecentProducts_ProductIDHiddenField))
    {
        AddToCartForm_RecentProducts_ProductIDHiddenField.value = CurrentHiddenProductId;
        navRecentProductViews_AddToCart.href = '#';
        navRecentProductViews_AddToCart.onclick = function() { document.AddToCartForm_RecentProducts.submit(); return false; };
    }
    else
    {
        navRecentProductViews_AddToCart.href = GetCurrentTextLinkUrl(); //error with product and variantid, so use the same link as GetCurrentTextLinkUrl()
        navRecentProductViews_AddToCart.onclick = "";
    }

}

//scroller nav should be unset while animation is in progress, and reset when animation has finished
function UnsetScrollerNav_All()
{
    if (((typeof $) != 'function') || (!navRecentProductViews_Prev) || (!navRecentProductViews_Next) || (!navRecentProductViews_ViewThisItem))
        return

    navRecentProductViews_Prev.onclick = function() { return false; }; //disable a tag
    navRecentProductViews_Next.onclick = function() { return false; }; //disable a tag
    navRecentProductViews_ViewThisItem.href = "#";
    navRecentProductViews_ViewThisItem.onclick = function() { return false; }; //disable a tag
    
}

if (((typeof $) == 'function') && (!isNaN(NumberProductsFilled)) && NumberProductsFilled > 0)
{
    RecentProductViews_outerDivForSlider = $('RecentProductViews_outerDivForSlider');
    RecentProductViews_innerDivForSlider = $('RecentProductViews_innerDivForSlider');
    RecentProductViewsOuterDiv = $('RecentProductViewsOuterDiv');
    navRecentProductViews_Prev = $('navRecentProductViews_Prev');
    navRecentProductViews_Next = $('navRecentProductViews_Next');
    navRecentProductViews_ViewThisItem = $('navRecentProductViews_ViewThisItem');
    navRecentProductViews_AddToCart = $('navRecentProductViews_AddToCart');
    
    try
    {
        if (RecentProductViewsOuterDiv)
        {
            var GilderAfterFinishFunctionPtr = SetScrollerNav_All;

            recentprods_glider = new Glider('RecentProductViewsOuterDiv', {duration:0.5, afterFinish:GilderAfterFinishFunctionPtr});
        }
    }
    catch (exc)
    {
        recentprods_glider = null;
    }
    
    if (RecentProductViews_outerDivForSlider && RecentProductViews_innerDivForSlider && RecentProductViewsOuterDiv && navRecentProductViews_Prev && navRecentProductViews_Next)
    {
        RecentProductViewsOuterDiv.style.display = "block";
        
        var AddToCartForm_RecentProducts = $('AddToCartForm_RecentProducts');

        if (AddToCartForm_RecentProducts && encodeURIComponent && window && window.location && window.location.href)
        {
            AddToCartForm_RecentProducts.action="addtocart.aspx?returnurl=" + encodeURIComponent(window.location.href);
        }
        
        //first initialise CurrentContentIndex
        CurrentContentIndex = 0;
        
        if (NumberProductsFilled > 1)
        {
            SetScrollerNav_All();
            if (!isNaN(RecentProductLoopWaitTime))
            {
                recentproducttimer = setTimeout("SetAndGoToNext();", RecentProductLoopWaitTime);
            }
            navRecentProductViews_Prev.style.display = "block";
            navRecentProductViews_Next.style.display = "block";

        }
        else if (NumberProductsFilled == 1)
        {
            SetScrollerNav_ViewThisItem();
            SetScrollerNav_AddToCart();
        }
        
    }
}


