
// To display the cart for the first time the page is loaded
function getLastVisitedArticles() {
    if (isLastVisitedAvailable() == true && LastVisitedArticles) {
        // Get the cart content - asynchronously
        LastVisitedArticles.getLastVisitedArticles({
                            callback:function(articles) {
                                refreshLastVisitedLayer(articles);
                            },
                            timeout:15000
                         });
    }
}

function insertLastVisitedArticle(articleId) {
    if (articleId && LastVisitedArticles) {
        LastVisitedArticles.insertLastVisitedArticle(articleId);
    }
}

// A callback function when last visited articles are obtained from the server side
function refreshLastVisitedLayer(articles) {
    if (articles && articles.length > 0) {
        for (var i = 0; i < articles.length; i++) {
            refreshSingleArticle(articles[i], i);
        }

        // After all articles is refreshed now show the last visited articles layer
        changeLayerDisplay('lastVisitedArticles', 'inline');
    }
}

function isLastVisitedAvailable() {
    var element = getElementById('lastVisitedArticles');
    if (element) {
        return true;
    }
    return false;
}

function refreshSingleArticle(article, idx) {

    refreshArticleRating(article, idx);
    refreshArticleDescription(article, idx);
    refreshArticlePrice(article, idx);
    refreshArticleCount(article, idx);
    refreshArticleImage(article, idx);
    changeLayerDisplay('emptyGalleryIdx'+idx, 'inline');
}

// To refresh the article image part
function refreshArticleImage(article, idx) {
    var articleLink = article.productGroupFullShortLink + "/+" + article.productShopIdentifier;

    // Refresh the image's a href link
    var imgLink = getFirstElementByName('lastVisitedImgLink' + idx);
    if (imgLink && imgLink.href) {
        imgLink.href = articleLink;
    }

    // Refresh the image's src
    var imgObj = getElementById('lastVisitedImgId' + idx);
    if (imgObj && article.imageUrl) {
        imgObj.src = article.imageUrl;
    }
}

// To refresh the article count part
function refreshArticleCount(article, idx) {
    // Refresh article count
    if (article.quantityInCart == 0) {
        changeLayerDisplay('itemEmpty_'+idx, 'inline');
        changeLayerDisplay('itemFilled_'+idx, 'none');
    }
    else {
        changeLayerDisplay('itemEmpty_'+idx, 'none');
        changeLayerDisplay('itemFilled_'+idx, 'inline');

        // change the count element
        setInnerHTML('itemFilledCount_' + idx, article.quantityInCart);
    }

    // change the count id - from index to article id
    // so adding shopping cart works.
    var element = getElementById('itemFilledCount_' + idx);
    if (element) {
        element.id = 'itemFilledCount_' + article.id;
    }

    element = getElementById('itemEmpty_' + idx);
    if (element) {
        element.id = 'itemEmpty_' + article.id;
    }

    element = getElementById('itemFilled_' + idx);
    if (element) {
        element.id = 'itemFilled_' + article.id;
    }    

    // fill necessary form
    setValue('emptyArticleId'+idx, article.id);
    setValue('emptyFullPath'+idx, article.productGroupFullShortLink);
    setValue('incArticleId'+idx, article.id);
    setValue('incFullPath'+idx, article.productGroupFullShortLink);
    setValue('decArticleId'+idx, article.id);
    setValue('decFullPath'+idx, article.productGroupFullShortLink);
}

// To refresh the description part
function refreshArticleDescription(article, idx) {
    var articleLink = article.productGroupFullShortLink + "/+" + article.productShopIdentifier;
    setInnerHTML('lastVisitedDesc'+idx, article.productName);
    setInnerHTML('lastVisitedVariantText'+idx, article.filteredVariantText);
    var descLink = getFirstElementByName('lastVisitedDescLink' + idx);
    if (descLink && descLink.href) {
        descLink.href = articleLink;
    }
}

function refreshArticleRating(article, idx) {
    // Update the image - only if the article does have rating
    if (article && article.rating && article.rating > 0) {
        var imgLink = getElementById('lastVisitedRating' + idx);
        if (imgLink && imgLink.src) {
            imgLink.src = '/images/bewertung_' + article.rating + ".gif";
        }

        var aRefLink = getElementById('crallastVisitedRating' + idx);
        if (aRefLink) {
            aRefLink.href= '/bewertung/ansicht/+' + article.id;
        }

        aRefLink = getElementById('cralelastVisitedRating' + idx);
        if (aRefLink) {
            aRefLink.href= '/bewertung/formular/+' + article.id;
        }
        setInnerHTML('crlastVisitedRating'+idx, '(' + article.countRating + ')');
    }
}

function refreshArticlePrice(article, idx) {
    // Update before price
    var beforePrice = false;
    if (article.beforePriceMessage) {
        changeLayerDisplay('beforePriceMessage'+idx+article.beforePriceMessage, 'inline');
        setInnerHTML('beforePrice' + idx, article.formattedDisplayBeforePrice + "<br/>");
        beforePrice = true;
    }

    // Update after price
    if (article.afterPriceMessage) {
        var element = getElementById('afterPriceMain' + idx);
        if (element) {
            if (beforePrice) {
                element.className = "specialPrices";
            }
            else {
                element.className = "standardPrices";
            }
        }

        changeLayerDisplay('afterPriceMessage'+idx+article.afterPriceMessage, 'inline');
    }
    setInnerHTML('afterPrice' + idx, article.formattedDisplayAfterPrice);
}