/* 
 * Adimo Web Components
 * 
 * LICENSE
 * 
 * This source file is subject to the Adimo Source License that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://adimo.pl/licencja-oprogramowania.html
 */

var shop = {
    init: function () {
        
    },

    homePage: function () {
        $('#sliderc').nivoSlider({
            effect:'fade',
            slices:15,
            animSpeed:800,
            pauseTime:5000,
            directionNav:true,
            controlNav:true,
            pauseOnHover:false,
            captionOpacity:.7
        });
    },

    category: {
        tree: function () {
            $('li:not(.active)').find('ul').hide();
        }
    },

    product: function () {
        $('.productPreview').jqzoom({
            preloadText: 'ładowanie...'
        });

        if (isStockEnabled == 1) {
            shop.stock.addToBasket();
        }
    },

    order: {
        first: function () {
            $('input[name="userType"]').change(function () {
                if ($('#userType-b').is(':checked')) {
                    //$('#userFirstName-label, #userFirstName-element').hide();
                    //$('#userLastName-label, #userLastName-element').hide();

                    $('#userCompanyName-label, #userCompanyName-element').show();
                    $('#userNip-label, #userNip-element').show();
                } else {
                    $('#userCompanyName-label, #userCompanyName-element').hide();
                    $('#userNip-label, #userNip-element').hide();

                    //$('#userFirstName-label, #userFirstName-element').show();
                    //$('#userLastName-label, #userLastName-element').show();
                }
            }).change();

            $('input[name="metadataGift"]').change(function () {
                if ($('#metadataGift').is(':checked')) {
                    $('input[name="hasOtherAddress"]').attr('checked', true).change();
                } else {
                    $('input[name="hasOtherAddress"]').attr('checked', false).change();
                }
            }).change();
			
            $('input[name="hasOtherAddress"]').change(function () {
                if ($('#hasOtherAddress').is(':checked')) {
                    $('#otherAddressBox').show();
                } else {
                    $('#otherAddressBox').hide();
                }
            }).change();

            if ($('.errors').size()) {
                $('.errors').each(function () {
                    $(this).prevAll().addClass('hasError').parent().prev('dt').find('label').addClass('hasErrorLabel');
                });
            }
        }
    },

    payment: function () {
        $('#pay_type-element label input').hide();

        $('#pay_type-element label img').click(function () {
            $('#pay_type-element label img').css('border-color', '#fff');
            $(this).css('border-color', '#81CF33').prev('input').attr('checked', 'checked');
        });

        $('#js').val(1);

        $('#PaymentForm').submit(function (e) {
            if ($('input[name="pay_type"]:checked', '#PaymentForm').val() == undefined) {
                alert('wybierz formę płatności');

                e.preventDefault();
            }
        });
    },

    stock: {
        addToBasket: function () {
            var isSet = false, isUnavailable = false;

            if (!$('.productVariant').size()) {
                return;
            }

            $('.productVariant').change(function () {
                $('.productVariant').each(function () {
                    if ($(this).val() == 'unset') {
                        isSet = false;

                        return false;
                    }

                    isSet = true;
                });

                if (isSet) {
                    // sprawdzamy czy dany wariant jest dostępny i czy nie ma innej ceny
                    $('#loading').show();

                    $.ajax({
                        url: '/ajax/get-stock-info',
                        data: $('#addToBasketForm').serialize(),
                        type: 'post',

                        success: function (res) {
                            $('#flashMessage').remove();

                            if (res.available) {
                                $('#productPrice').html(res.price);

                                isUnavailable = false;
                            } else {
                                shop.showMessage('Niestety wybrany przez Ciebie wariant jest niedostępny');

                                isUnavailable = true;
                            }

                            if (res.mediaId) {
                                $('#mediaId_'+res.mediaId).click();
                            }

                            $('#loading').hide();
                        },

                        error: function () {
                            $('#loading').hide();

                            alert('Wystąpił błąd aplikacji');
                        }
                    });
                }
            });

            $('input[name="addToBasket"]').click(function (e) {
                if (!isSet) {
                    e.preventDefault();

                    shop.showMessage('Wybierz wariant produktu');
                }

                if (isUnavailable) {
                    e.preventDefault();

                    shop.showMessage('Niestety wybrany przez Ciebie wariant jest niedostępny');
                }
            });
        }
    },

    showMessage: function (msg) {
        if (!$('#flashMessage').size()) {
            $('#nav').after('<div id="flashMessage" class="kerror">'+msg+'</div>');
        } else {
            $('#flashMessage').fadeOut('fast', function () {$(this).html(msg).fadeIn();});
        }
    }
};



