﻿var rootUrl = 'http://' + window.location.hostname + '/';

function AddProduct(productid, units, image) {
    if(units != parseInt(units)) {
        alert("Antal skal være et heltal");
        return;
       }
    $(image).attr('src','images/webshop/ajax-loader-small_24x13.gif');
    var myUrl = rootUrl + "/ajax/oxygenAjaxVer300.asp?WebShopAction=AddProduct&ProductID=" + productid + "&Units=" + units;
    $.ajax({
        type: 'POST',
        url: myUrl,
        cache: false,
        success: function() {
            AddProductItemAdded(image);
        },
        error: function(xhr) {
            alert(xhr.status + '\n' + xhr.responseText);
        }
    });

}

function AddProductItemAdded(image) {
    $(image).attr('src', 'images/webshop/basket_ok.gif');
    $('#basket_icon').attr('src', 'images/webshop/ajax-loader-small.gif');
    UpdateBasket();
}
function UpdateBasket() {
    var myUrl = rootUrl + "/ajax/oxygenAjaxVer300.asp?BasketTotals=Show&Url=basket.asp"
    $.ajax({
        type: 'POST',
        url: myUrl,
        cache: false,
        success: function(data) {
            $("#basket").html(data);
        },
        error: function(xhr) {
            alert(xhr.status + '\n' + xhr.responseText);
        }
    });
}
function EmptyBasket() {
    if (confirm('Ønsker du at tømme indkøbskurven?')) {
        $('#basket_icon').attr('src','images/webshop/ajax-loader-small.gif');
        var myUrl = rootUrl + "/ajax/oxygenAjaxVer300.asp?WebShopAction=EmptyBasket"
        $.ajax({
            type: 'POST',
            url: myUrl,
            cache: false,
            success: function() {
                UpdateBasket();
            },
            error: function(xhr) {
                alert(xhr.status + '\n' + xhr.responseText);
            }
        });
    }
}


$(document).ready(function() {
    //    if ($('#divSubDescription') && $('#divAppartmentChooser')) {
    //        $('#divSubDescription').html($('#divAppartmentChooser').html());

    //        $('#divSubDescription').css('display', 'block');
    //        $('#divSubDescription').css('height', '100px');
    //    }
    var productList = $('#skin5-productlist');
    if (productList) {
        var highestHeight = 0;
        jQuery.each($('.skin5-product-image', productList), function() {
            var imageElement = $(this)[0];
            if (highestHeight < imageElement.offsetHeight)
                highestHeight = imageElement.offsetHeight;

        });
        jQuery.each($('.skin5-product-image', productList), function() {
            var imageElement = $(this)[0];
            $(imageElement).height(highestHeight);

        });
    }
    var skin8Productlist = $("#skin8-productlist");
    if (skin8Productlist) {
        var highestHeight = 0;
        jQuery.each($('.skin8-productlist-row', skin8Productlist), function() {
            highestHeight = 0;
            var rowElement = $(this)[0];
            jQuery.each($('.skin8-product-image', rowElement), function() {
                var imageElement = $(this)[0];
                if (highestHeight < imageElement.offsetHeight)
                    highestHeight = imageElement.offsetHeight;

            });
            jQuery.each($('.skin8-product-image-td', rowElement), function() {
                var imageElement = $(this)[0];
                $(imageElement).css("height", highestHeight+10 + "px");

            });
        });
    }
});