﻿$(document).ready(function() {
    ChangeLatestDevice(null);
    $("#LatestDevice_Previous").hide();
    $("#LatestDevice_Next").click(function() {
        ChangeLatestDevice("forward");
    }
                ).mouseover(function() {
                    //mouseover
                    $(this).attr("src", "Images/ContentArrow_hover.gif");
                }).mouseout(function() {
                    //mouseout
                    $(this).attr("src", "Images/ContentArrow.gif");
                });
    $("#LatestDevice_Previous").click(function() {
        ChangeLatestDevice("backward");
    }
                ).mouseover(function() {
                    //mouseover
                    $(this).attr("src", "Images/ContentArrow_left_hover.gif");
                }).mouseout(function() {
                    //mouseout
                    $(this).attr("src", "Images/ContentArrow_left.gif");
                });
});

function ChangeLatestDevice(direction) {
    var Image = $("#LatestDeviceImg");
    var Link = $("#LatestDeviceLink");
    var NextLink = $("#LatestDevice_Next")
    var PreviousLink = $("#LatestDevice_Previous")
    var animationWidth = "100%";
    Image.css("behavior", "none");

    if (direction == "forward") { //fremad
        //Hæver index med 1
        CurrentDeviceIndex = CurrentDeviceIndex + 1;
        animationWidth = "150%"

        //Fjerner NextLink hvis det er sidste index
        if (CurrentDeviceIndex + 1 == manuals.length) {
            NextLink.hide();
        };

        PreviousLink.show();
    } else if (direction == "backward") {
        //sænker index med 1
        CurrentDeviceIndex = CurrentDeviceIndex - 1;
        animationWidth = "50%"

        //Fjerner PreviousLink hvis det er første index
        if (CurrentDeviceIndex - 1 < 0) {
            PreviousLink.hide();
        };
        NextLink.show();
    };

    var Container = $("#LatestDeviceContainer");
    Container.stop(true, true);
    if (direction != null) {
        Container.animate(
            { "width": animationWidth },
            100,
            function() {
            Image.attr("src", "/ml/" + manuals[CurrentDeviceIndex].ThumbPictId + ".png");
                if (manuals[CurrentDeviceIndex].ManualId == 0) {
                    Link.attr("href", 'javascript:showDatacardSelector(' + manuals[CurrentDeviceIndex].DatacardGroupId + ', ' + manuals[CurrentDeviceIndex].ThumbPictId + ', "' + manuals[CurrentDeviceIndex].ManufacturerName + '", "' + manuals[CurrentDeviceIndex].ProductName + '");');
                } else {
                    Link.attr("href", "Home.aspx?Id=" + manuals[CurrentDeviceIndex].ManualId);
                };
                Link.text(manuals[CurrentDeviceIndex].ManufacturerName + " " + manuals[CurrentDeviceIndex].ProductName);
            }
        );

        Container.animate({ width: "100%" }, 100);
    } else { // Håndtere førstegangs load.
        Image.attr("src", "/ml/" + manuals[CurrentDeviceIndex].ThumbPictId + ".png");
        
        if (manuals[CurrentDeviceIndex].ManualId == 0) {
            Link.attr("href", 'javascript:showDatacardSelector(' + manuals[CurrentDeviceIndex].DatacardGroupId + ', ' + manuals[CurrentDeviceIndex].ThumbPictId + ', "' + manuals[CurrentDeviceIndex].ManufacturerName + '", "' + manuals[CurrentDeviceIndex].ProductName + '");');
        } else {
            Link.attr("href", "Home.aspx?Id=" + manuals[CurrentDeviceIndex].ManualId);
        };

        Link.text(manuals[CurrentDeviceIndex].ManufacturerName + " " + manuals[CurrentDeviceIndex].ProductName);
        
    };   
};
