﻿function slideTopListLeft() {
    $("#TopListSliderLeftButton").hide();
    $("#TopListSliderRightButton").hide();    
    var newCssPosition = ($("#TopListSlider").position().left + 513) + "px";
    $("#TopListSlider").animate({
        left: newCssPosition
    }, 700, function() { checkTopListSliderButtons() });
}

function slideTopListRight() {
    $("#TopListSliderLeftButton").hide();
    $("#TopListSliderRightButton").hide();   
    var newCssPosition = ($("#TopListSlider").position().left - 513) + "px";
    $("#TopListSlider").animate({
        left: newCssPosition
    }, 700, function() { checkTopListSliderButtons() });
}

// Kolla vilka knappar som ska släckas
function checkTopListSliderButtons() {
    if ($("#TopListSlider").position().left >= 0) {
        $("#TopListSliderLeftButton").hide();
    }
    else {
        $("#TopListSliderLeftButton").show();
    }
    if ($("#TopListSlider").position().left <= ($("#TopListSlider").width() * -1) + $("#TopListSliderViewPort").width()) {
        $("#TopListSliderRightButton").hide();
    }
    else {
        $("#TopListSliderRightButton").show();
    }
}


$(document).ready(function() {


    if ($("#TopListSliderContainer").length) {

        // GENERERA INNEHÅLLET FÖR CONTAINERN
        var sliderBaseCode = '<div id="TopListSliderViewPort"><div id="TopListSlider"></div></div>';
        sliderBaseCode += '<img id="TopListSliderLeftButtonBg" src="/App_Themes/FrameworkBasic/images/custom/top-list-slider/top-list-slider-left-button-bg.png" alt="" />';
        sliderBaseCode += '<img id="TopListSliderLeftButton" onclick="slideTopListLeft();"  src="/App_Themes/FrameworkBasic/images/custom/top-list-slider/top-list-slider-left-button.png" alt="">';
        sliderBaseCode += '<img id="TopListSliderRightButtonBg" src="/App_Themes/FrameworkBasic/images/custom/top-list-slider/top-list-slider-right-button-bg.png" alt="" />';
        sliderBaseCode += '<img id="TopListSliderRightButton" onclick="slideTopListRight();" src="/App_Themes/FrameworkBasic/images/custom/top-list-slider/top-list-slider-right-button.png">';
        $("#TopListSliderContainer").html(sliderBaseCode);

        // GENERERA DIVARNA MED KOD
        $(".SimpleProductListItem").each(function() {
            var productCode = "";
            productCode += "<div class='TopListItem'><a href='";
            productCode += $(this).find(".SimpleProductListItemImage a").attr("href");
            productCode += "' style='background: #fff url(";
            productCode += $(this).find(".SimpleProductListItemImage img").attr("src");
            productCode += ") center center no-repeat' title='";
            productCode += $(this).find(".SimpleProductListItemName").html();
            productCode += "'></a>";
            productCode += "<div>";

            var nameToDisplay = $(this).find(".SimpleProductListItemName").html();
            if (nameToDisplay.length > 20) {
                nameToDisplay = nameToDisplay.substring(0, 18) + "..."
            }
            productCode += nameToDisplay;
            productCode += "</div>";
            productCode += "<span>";
            // IF NO PRICE DEFINED, DO NOT WRITE PRICE
            if ($(this).find(".PriceStandardPriceAmount").html() != null) {
                productCode += $(this).find(".PriceStandardPriceAmount").html();
            }
            productCode += "</span>";
            productCode += "</div>";

            // Sätt in divarna i slidern
            $("#TopListSlider").append(productCode);

            // Sätt sliderns bredd
            $("#TopListSlider").width($("#TopListSlider").width() + 171);

        });

        // Kolla vilka knappar som ska vara aktiva
        checkTopListSliderButtons();

        // BUTTON BACKGROUND FADING
        $("#TopListSliderLeftButton").bind("mouseenter", function() {
            $("#TopListSliderLeftButtonBg").stop(true, true);
            $("#TopListSliderLeftButtonBg").fadeTo(0, 0.8);
            $(this).stop(true, true);
            $(this).fadeTo(80, 1.0);
        });

        $("#TopListSliderLeftButton").bind("mouseleave", function() {
            $("#TopListSliderLeftButtonBg").stop(true, true);
            $("#TopListSliderLeftButtonBg").fadeTo(0, 1.0);
            $(this).stop(true, true);
            $(this).fadeTo(80, 0.5);
        });

        $("#TopListSliderRightButton").bind("mouseenter", function() {
            $("#TopListSliderRightButtonBg").stop(true, true);
            $("#TopListSliderRightButtonBg").fadeTo(0, 0.8);
            $(this).stop(true, true);
            $(this).fadeTo(80, 1.0);
        });

        $("#TopListSliderRightButton").bind("mouseleave", function() {
            $("#TopListSliderRightButtonBg").stop(true, true);
            $("#TopListSliderRightButtonBg").fadeTo(0, 1.0);
            $(this).stop(true, true);
            $(this).fadeTo(80, 0.5);
        });



        $(".ProductListWrapper").hide(); // DÖLJ PRODUKTVISNINGEN PÅ STARTSIDAN
    }

});