$(document).ready(function() {
    
    $('.b-hmenu__link, .b-vmenu__link').hover(
        function() {
            var className = $(this).parents('ul:first').get(0).className + "__item",
                classNameActive = className + "_active";
                if($(this).parent().hasClass(classNameActive)) {
                    return false;
                }
                $(this).parent().siblings('.' + classNameActive).removeClass(classNameActive);
                $(this).parent().addClass(classNameActive);
        },
        function() {}
    );

    if($('.b-product__view').length > 0) {
        $('.b-product__view__list a:first').addClass('active');
        $('.b-product__view__list a').click(function() {
            var link = $(this);
                if(link.hasClass('active')) {
                    return false;
                }

                $('.b-product__view__list .active').removeClass('active');

            var img = $('.b-product__view__area img').get(0);
                img.src = this.href;
                img.alt = this.title;

                link.addClass('active');

             return false;
        });
    }

    (function($) {
        
        $.fn.coverflow = function(settings) {

            var settings = jQuery.extend({
                    click: '',
                    activeClass: 'active',
                    hoverClass: 'hover',
                    styles: []
                }, settings);

            if(typeof(settings.styles) != 'object' || settings.styles.length < 2 || settings.styles.length % 2 != 1) {
                return false;
            }

            var min         = 0,
                max         = this.children().length - 1,
                center      = parseInt(max / 2),
                childrens   = this.children(),
                order       = [0, 1, 2, 3, 4];

                childrens.each(function(index) {

                    $(this).css(settings.styles[index]).get(0).index = index;

                    $(this).hover(
                        function() {
                            $(this).addClass(settings.hoverClass);
                        },
                        function() {
                            $(this).removeClass(settings.hoverClass);
                        }
                    )

                    $(this).children(settings.click).click(function() {

                        var offset = center - this.parentNode.index;
                            if(offset == 0) {
                                return false;
                            }

                        var oldOrder = order.slice(0);
                            for(var i = 0, total = oldOrder.length; i < total; i++) {
                                var x = i + offset;
                                    x = x < 0 ? total + x : x >= total ? x - total : x;
                                    order[x] = oldOrder[i];
                            }

                            rebase(offset);

                    });

                });

                function rebase(offset) {

                    var z = order.slice(0, center).reverse().concat(order.slice(center));
                        for(var i = 0; i < z.length; i++) {
                            $(childrens[z[i]]).css('z-index', z.length - i);
                        }

                    for(var i = 0, length = order.length; i < length; i++) {
                        var child = $(childrens[order[i]]);
                            child.removeClass(settings.activeClass).animate(settings.styles[i], 400).get(0).index = i;
                            if(i == center) {
                                child.css('z-index', '90').addClass(settings.activeClass);
                            }
                    }
                }

                rebase();

        }

    })(jQuery);

    if((coverflow = $('.b-coverflow')) && coverflow.length > 0) {
        coverflow.coverflow({
            click: 'img',
            activeClass: 'b-coverflow__item_active',
            hoverClass: 'b-coverflow__item_hover',
 /*           styles: [
                { width: '150px', height: '100px', marginLeft: '-320px', top: '55px' },
                { width: '210px', height: '140px', marginLeft: '-250px', top: '30px' },
                { width: '270px', height: '180px', marginLeft: '-135px', top: '0' },
                { width: '210px', height: '140px', marginLeft: '40px', top: '30px' },
                { width: '150px', height: '100px', marginLeft: '170px', top: '55px' }
            ]
   */     
            styles: [
                 { width: '150px', marginLeft: '-320px', top: '55px' },
                 { width: '210px', marginLeft: '-250px', top: '30px' },
//                { width: '270px', marginLeft: '-135px', top: '0' },
                //{ width: '270px', marginLeft: '-135px', top: '0' },
                { width: '320px', marginLeft: '-160px', top: '0' },
                 { width: '210px', marginLeft: '40px', top: '30px' },
                 { width: '150px', marginLeft: '170px', top: '55px' }
            ]
   });
    }

    if((feedback = $('.b-feedback__form')) && feedback.length > 0) {

        var profile = [
                { className: '.b-feedback__name', type: 'string', required: true },
                { className: '.b-feedback__phone', type: 'string', required: true },
                { className: '.b-feedback__email', type: 'regexp', required: true, pattern: /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/i },
                { className: '.b-feedback__message', type: 'text', required: true }
            ];

            feedback.submit(function() {
                var canProcess = true;
                    for(var i = 0; i < profile.length; i++) {
                        var field = $(profile[i].className);
                            if(profile[i].required && field.val().replace(/\s/g, '').length == 0) {
                                field.addClass('b-feedback__error');
                                canProcess = false;
                                continue;
                            }
                            if(profile[i].type == 'regexp' && field.val().search(profile[i].pattern) == -1) {
                                field.addClass('b-feedback__error');
                                canProcess = false;
                                continue;
                            }
                            $(field).removeClass('b-feedback__error');
                    }

                    return canProcess;
            });

 var map = null;


        $('.b-address-list__map_js').click(function() {
            $('.b-paranja').css('height', $(document).height()).show();
$(".b-popup__map").html("<img src=\"/web-files/map-olymp.jpg\" alt=\"\" />");
            $('.b-popup').show();
/*
if(!map) {
                var pnt = new YMaps.GeoPoint(37.626893, 55.781121);
                    map = new YMaps.Map($('.b-popup__map')[0]);
                    map.addControl(new YMaps.TypeControl());
                    map.addControl(new YMaps.Zoom());
                    map.setCenter(pnt, 15);

                var placemark = new YMaps.Placemark(pnt);
                    placemark.description = "Олимпийский пр-т, 16, стр. 1<br />т.: (495)933–8496, 937–2805";
                    map.addOverlay(placemark);
            }

*/
            return false;
        });


      $('#contacts_2').click(function() {
            $('.b-paranja').css('height', $(document).height()).show();
$(".b-popup__map").html("<img src=\"/web-files/map-frunz.jpg\" alt=\"\" />");
            $('.b-popup').show();

            return false;
        });


        $('.b-paranja, .b-popup-close').click(function() {
            $('.b-paranja, .b-popup').hide();
            return false;
        });

    }

});


