﻿
$(function() {
    // Cartdetail tooltip
    $(".cartdetail").tooltip(
    {
        relative: true,
        offset: [30, 0],
        position: 'center left',
        effect: 'slide',
        predelay: 500
    });

    // Scalediscount tooltip
    $(".scale").tooltip(
    {
        relative: true,
        offset: [0, -50],
        position: 'bottom center',
        effect: 'slide'
    });

    $(".amountinfo").tooltip(
    {
        relative: true,
        offset: [0, -50],
        position: 'bottom center',
        effect: 'slide',
        predelay: 500
    });

    // Image Gallery
    var scroll = $(".scrollable");
    if (scroll) {
        scroll.scrollable();

        //if (scroll.scrollable().getSize() === 1) {
        if (scroll.size() === 1) {
            scroll.scrollable()
            .getNaviButtons()
            .addClass(
                scroll.scrollable()
                .getConf()
                .disabledClass);
        }
    }
    
    $(".scrollable .items img").click(function() {

        // see if same thumb is being clicked
        if ($(this).hasClass("active")) { return; }

        // calclulate large image's URL based on the thumbnail URL (flickr specific)
        var url = $(this).attr("alt");
        var orgUrl = $(this).attr("org");

        // get handle to element that wraps the image and make it semi-transparent
        var wrap = $(".gallery #image_wrap").fadeTo("fast", 0.0, function() {

            // the large image from www.flickr.com
            var img = new Image();


            // call this function after it's loaded
            img.onload = function() {

                // make wrapper fully visible
                wrap.fadeTo("fast", 1);

                // change the image
                wrap.find("img").attr("src", url);
            };

            // begin loading the image from www.flickr.com
            img.src = url;
        });


        // activate item
        $(".items img").removeClass("active");
        $(this).addClass("active");

        $("#image_overlay").find("img").attr("src", orgUrl);

        // when page loads simulate a "click" on the first image
    }).filter(":first").click();

    if ($(".items img").length <= 1) {
        $("#image_slider").remove();
    }

    $("#image_wrap img[rel]").overlay({
        effect: 'apple',
        target: '#image_overlay'
    });

    // Hide empty attribute rows
    if ($("#product_articles")) {

        $("#product_articles").removeEmptyCols();
    }

    hideAllMessages();


    $('.messageDelete').live('click', function(e) {
        currentMessage = $(this).closest('.messageItem');
        currentMessage.fadeOut('fast');
        e.preventDefault();
    });
});

function hideMessage(messageID) {
    NVShop.Services.NVShopService.HideMessage(messageID);
}

function hideAllMessages() {

    NVShop.Services.NVShopService.HideAllMessages();
}

function fadeOutMessages() {

    $('.messageItem').fadeOut();
}

function calcMeasurement(articleID, salesUnit, qtyFactors) {

    var quantity = NVShop.Services.NVShopService.CalcMeasurement(articleID, salesUnit, qtyFactors)

    return quantity;
}


function SetUniqueRadioButton(nameregex, current) {
    re = new RegExp(nameregex);
    for (i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i]
        if (elm.type == 'radio') {
            if (re.test(elm.name)) {
                elm.checked = false;
            }
        }
    }
    current.checked = true;
}

$.fn.removeEmptyCols = function() {

    var numberOfCols = $('tr:first > td, tr:first > th', this).length;
    var colsToRemove = Array.prototype;

    for (var col = numberOfCols; col > 0; col--) {

        var cells = $('tr > td:nth-child(' + col + '), tr > th:nth-child(' + col + ')', this);
        var emptyCells = cells.filter(":empty");

        if (cells.length > 0 && cells.length == emptyCells.length) {
            cells.remove(); ;
        }
    }
};
