$(document).ready(function() {

    /**
    * Applying a mouse over effect on images
    * Requires cdiscount.ui.js
    */
    if (typeof $.fn.picHover != 'undefined') {
        $('.ps-associatedproductsaddtobasket').picHover();
        $('.jq-picHoverCss').picHover({ cssMode: true });
    };

    /**
    * Main Product Image carousel
    */
    $('.ps-thumbzoom a img').mouseover(function() {
        var $chosenImgSrc = $(this).attr('src');
        var $zoomPopupHref = $(this).parent().attr('href');
        //main image
        $('img[id*=mainProductImage]').attr('src', $chosenImgSrc.replace('/a/', '/d/'));
        //zoom image
        $('.cdsZoom').attr('href', $chosenImgSrc.replace('/a/', '/f/'));
        $('.cdsZoom').attr('onClick', $zoomPopupHref + ";return false;");
    });

    /**
    * Applying cdsZoom
    * 
    * @requires cdiscount.ui.js
    */
    if (typeof $.fn.cdsZoom != 'undefined') {
        $('.cdsZoom').cdsZoom({ selectorZoomWindow: $('.ps-zoomarea') });
    }

    /**
    * @requires cdiscount.ui.js
    * LinkAnchors : go to anchor with slide effect
    */
    if (typeof $.fn.slideToAnchor != 'undefined') {
        // links to tabs group
        $('.ps-animatedanchor').slideToAnchor({ anchorLink: '.ps-menutabsarea' });
        // links to top of the page
        $('.ps-backtotop').slideToAnchor();
    }

    /**
    * LinkAchors : Displays the associated tab
    */
    $('.ps-animatedanchor').click(function() {
        var $href = $(this).attr('href');
        if (/^#.{1,}$/.test($href)) {
            $('li:has(a[href^=' + $href + '].ps-contentareatabs)').trigger('click');
        }
    });

    /**
    * Tabs hover
    */
    $('a.ps-contentareatabs').hover(
        function() {
            $(this).parent().addClass('ps-contentareatabselected');
        },
        function() {
            $(this).parent().removeClass('ps-contentareatabselected');
        }
    );

    //shipping services
    $('.jq-shippingPartnersLink').click(function() {
        $('.jq-shippingPartnersLink').toggle();
        $('.ps-shippingservicesabsolutecontent').slideToggle('fast');
    });

    //Slide carousel of Associated products
    if (typeof $.fn.slideCarousel != 'undefined'
		&& $('.ps-commarealeftcontent .ps-commarealargecontentitem').slideCarousel($('a.ps-slideButtonLeft'), $('a.ps-slideButtonRight'))) {
        // show previous and next buttons
        $('a.ps-slideButtonLeft, a.ps-slideButtonRight').show();
    }

    /**
    * Add Product To Basket
    * checks variant
    */
    $("[id*=AddButton]").click(function() {
        var $this = $(this);
        var variantList = $("[id*=listBoxVariants]");
        var hasVariantError = false;
        //check product main variant
        if (variantList.length > 0) {
            variantId = variantList.attr("value");
            hasVariantError = (variantId == -1);
        }
        //check associated product variants
        if (!hasVariantError) {
            $("[id*=ProductCheckBox]").each(function() {
                var $this = $(this);
                hasVariantError = hasVariantError || ($this.attr("checked") == true && $this.parent().parent().find("[id*=varListAssoList]").attr("selectedIndex") == 0);
            });
        }
        if (hasVariantError) {
            $("div.popin-page-background").show();
            $("#divPopInNoSize").show();
        }
        return !hasVariantError;
    });

    /**
    * Pop-In error (addToBasket)
    */
    $("div.popin-wrapper input.button , div.popin-wrapper .popin-close").live('click', function() {
        var $this = $(this);
        //close popin
        $this.parents("div.popin-container").hide();
        if ($("div.popin-wrapper  > div.popin-container:visible").length == 0) { // there is no popin
            $("div.popin-page-background").hide();
        }
    });

});


