﻿/*!
 * K-Supermarket
 * Main JavaScript-file for K-Supermarket
 *
 */

$(document).ready(function () {
    $(".mainMenuItem").hover(

        function () {
            if (!$(this).hasClass('selected')) {
                if (!$.browser.msie) {
                    $(".mainMenuIcon > .nonHoverIcon", this).stop().fadeTo('normal', 0);
                }
                else {
                    $(".mainMenuIcon > .nonHoverIcon", this).stop().attr("style", "display:none");
                }

            }
        },
        function () {
            if (!$(this).hasClass('selected')) {
                if (!$.browser.msie) {
                    $(".mainMenuIcon > .nonHoverIcon", this).stop().fadeTo('normal', 1);
                }
                else {
                    $(".mainMenuIcon > .nonHoverIcon", this).stop().attr("style", "display: normal");
                }
            }
        }

    );

    $('.service').hover(function () {
        $(this).children('.tooltip').toggle();
    });


    $('.phoneInfo').hover(function () {
        $(this).children('.tooltip').toggle();
    });


    $('.highlight ul').carousel({
        fluid: false,
        transition: 'fade',
        slideshow: 4,
        showArrows: false,
        showNavigation: true
    });
    
    
    $('.productCarousel .products ul').carousel({
        fluid: false,
        transition: 'scroll'
    });
    

    $('.offerListRecipe').hover(function () {
        $(this).children('.offerListTooltip').removeClass('hiddenTooltip');
    }, function () {
        $(this).children('.offerListTooltip').addClass('hiddenTooltip');
    });

    tooltippingBargains();


    $("a[rel=gallery]").fancybox(
        {
            'cyclic': 'true',
            'titlePosition': 'outside',
            'overlayColor': '#003366',
            'overlayOpacity': 0.8
        });


    var showBeforeCut = 100;

    if ($(".storeMessage").parent().hasClass("narrow")) {
        showBeforeCut = 60;
    }

    $(".storeMessage").readmore({
        substr_len: showBeforeCut
    });

});




/* -- Bargain tooltips -- */
function tooltippingBargains() {

    // shared, static tooltip element
    var tooltip = $('#products-tooltip');

    if (!tooltip.length) {
        // no tooltip! abort!
        return;
    }

    tooltip.hover(function () { tooltip.show(); }, function () { tooltip.hide(); });

    $('.products .carouselWrapper li').hover(function () {
        if ($(this).hasClass('empty')) {
            return;
        }

        var numberOfItemsPerPage = 3;

        var $this = $(this),
            tempTooltip = $this.find('.carouselTooltip'),
            width = $this.width();

        var contents = tempTooltip.html();

        tooltip.html(contents);

        if (tempTooltip.hasClass('wideTooltip')) {
            tooltip.addClass('wideTooltip').removeClass('narrowTooltip');
        }
        else {
            tooltip.removeClass('wideTooltip').addClass('narrowTooltip');
        }

        var position = $this.position(),
            left = position.left % (width * numberOfItemsPerPage) - tooltip.width() / 2 + width;

        tooltip.css('left', left);
        tooltip.show();

    }, function () {
        tooltip.hide();
    });
}

