(function($){
    function checkAllAction(items)
    {
        items.each(function(){
            var el = $(this);
            el.attr('checked', 'checked').get(0).checked = true;
            rechangeCheckboxes(el.attr('rel').replace('index-', ''));
        });
    }
    $(document).ready(function(){
        $('a.action-to-cart').live('click', function(event){
            event.preventDefault();
            $('form#' + $(this).attr('rel')).submit();
            return false;
        });
        $('a.action-delete-item').live('click', function(event){
            event.preventDefault();
            if(confirm('Удалить этот товар из корзины?'))
            {
                var form = $('form#' + $(this).attr('rel'));
                $.post(form.attr('href'), form.serialize() + '&shk_action=del&ajax=cart', function(data){
                    var content = $(data);
                    if($('form[id^="cart-item-"]', content).length == 0)
                    {
                        document.location.href = document.location.href;
                        document.location.reload();
                    }
                    else
                    {
                        form.fadeOut(function(){
                            form.remove();
                        });
                        $('div.container-frame > div.block').replaceWith($('div.container-frame > div.block', content));
                        $('form[id^="cart-item-"]', content).each(function(){
                            var el = $(this);
                            $('form#' + el.attr('id')).replaceWith(el);
                        });
                    }
                });
            }
            return false;
        });
        $('a.btn-minus').live('click', function(event){
            event.preventDefault();
            var el = $(this);
            var form = $('form#' + el.attr('rel'));
            var countField = $('input.hidden-count', form);
            var count = parseInt(countField.val());
            count = isNaN(count) ? 1 : count;
            if(count == 1)
            {
                return;
            }
            count--;
            if(count == 1)
            {
                el.addClass('btn-disable');
            }
            countField.val(count);
            $('strong.display-count', form).html(count);
            $.post(form.attr('href'), form.serialize() + '&shk_recount=Y&ajax=cart', function(data){
                var content = $(data);
                if($('form[id^="cart-item-"]', content).length == 0)
                {
                    document.location.href = document.location.href;
                    document.location.reload();
                }
                else
                {
                    $('div.container-frame > div.block').replaceWith($('div.container-frame > div.block', content));
                    $('form[id^="cart-item-"]', content).each(function(){
                        var el = $(this);
                        $('form#' + el.attr('id')).replaceWith(el);
                    });
                }
            });
            return false;
        });
        $('a.btn-plus').live('click', function(event){
            event.preventDefault();
            var el = $(this);
            var form = $('form#' + el.attr('rel'));
            var countField = $('input.hidden-count', form);
            var count = parseInt(countField.val());
            count = isNaN(count) ? 1 : count;
            count++;
            if(count > 1)
            {
                $('a.btn-plus', form).removeClass('btn-disable');
            }
            countField.val(count);
            $('strong.display-count', form).html(count);
            $.post(form.attr('href'), form.serialize() + '&shk_recount=Y&ajax=cart', function(data){
                var content = $(data);
                if($('form[id^="cart-item-"]', content).length == 0)
                {
                    document.location.href = document.location.href;
                    document.location.reload();
                }
                else
                {
                    $('div.container-frame > div.block').replaceWith($('div.container-frame > div.block', content));
                    $('form[id^="cart-item-"]', content).each(function(){
                        var el = $(this);
                        $('form#' + el.attr('id')).replaceWith(el);
                    });
                }
            });
            return false;
        });
        $('ul#sizes-tree li.tree-header > a').click(function(event){
            event.preventDefault();
			var el = $(this).parent('li');
			if(el.hasClass('list-open'))
			{
				$('ul input[type="checkbox"]', $(this).parent('li'))
				checkAllAction($('ul input[type="checkbox"]', el));
			}
            el.toggleClass('list-close').toggleClass('list-open');
            return false;
        });
        var sizeCheckboxes = $('input.size-filter').change(function(){
            var checked = $('input.size-filter:checked');
            if(checked.length == 0 || sizeCheckboxes.length == checked.length)
            {
                if(checked.length > 0)
                {
                    checkAllAction(sizeCheckboxes);
                }
                $('a#size-all').addClass('checked-all');
            }
            else
            {
                $('a#size-all').removeClass('checked-all');
            }
        });
        sizeCheckboxes.eq(0).change();
        var materialCheckboxes = $('input.material-filter').change(function(){
            var checked = $('input.material-filter:checked');
            if(checked.length == 0 || materialCheckboxes.length == checked.length)
            {
                if(checked.length > 0)
                {
                    checkAllAction(materialCheckboxes);
                }
                $('a#material-all').addClass('checked-all');
            }
            else
            {
                $('a#material-all').removeClass('checked-all');
            }
        });
        materialCheckboxes.eq(0).change();
        var producerCheckboxes = $('input.producer-filter').change(function(){
            var checked = $('input.producer-filter:checked');
            if(checked.length == 0 || producerCheckboxes.length == checked.length)
            {
                if(checked.length > 0)
                {
                    checkAllAction(producerCheckboxes);
                }
                $('a#producer-all').addClass('checked-all');
            }
            else
            {
                $('a#producer-all').removeClass('checked-all');
            }
        });
        producerCheckboxes.eq(0).change();
        var pillowcaseCheckboxes = $('input.pillowcase-filter').change(function(){
            var checked = $('input.pillowcase-filter:checked');
            if(checked.length == 0 || pillowcaseCheckboxes.length == checked.length)
            {
                if(checked.length > 0)
                {
                    checkAllAction(pillowcaseCheckboxes);
                }
                $('a#pillowcase-all').addClass('checked-all');
            }
            else
            {
                $('a#pillowcase-all').removeClass('checked-all');
            }
        });
        pillowcaseCheckboxes.eq(0).change();
        $('a#size-all').click(function(event){
            event.preventDefault();
            checkAllAction(sizeCheckboxes);
            $(this).addClass('checked-all');
        });
        $('a#material-all').click(function(event){
            event.preventDefault();
            checkAllAction(materialCheckboxes);
            $(this).addClass('checked-all');
        });
        $('a#producer-all').click(function(event){
            event.preventDefault();
            checkAllAction(producerCheckboxes);
            $(this).addClass('checked-all');
        });
        $('a#pillowcase-all').click(function(event){
            event.preventDefault();
            checkAllAction(pillowcaseCheckboxes);
            $(this).addClass('checked-all');
        });
        $('div.box-close > a').live('click', function(event){
            event.preventDefault();
            $(this).parents('div.popup1').eq(0).remove();
            $('#popup-overlay').remove();
            $('embed, object, select').css({
                visibility: 'visible'
            });
            return false;
        });
        var popupShowTimeout;
        var popupHideTimeout;
        $('div.product-box[rel^="product"] h2 > a').hover(function(){
			var el = $(this);
            var popupContent = $('div#popup-content-' + el.parents('div.product-box').eq(0).attr('rel').replace('product-', ''));
            if(popupContent.length == 1)
            {
                var offset = el.offset();
				var popup = $('div.popup2').hide();
                $('div.popup-content', popup).html(popupContent.html());
                clearTimeout(popupHideTimeout);
                clearTimeout(popupShowTimeout);
                popupShowTimeout = setTimeout(function(){
                    popup
                        .css({
                            top: (offset.top - 45) + 'px',
                            left: offset.left + el.width() + 'px'
                        })
                        .fadeIn();
                }, 100);
            }
        }, function(){
			clearTimeout(popupHideTimeout);
			clearTimeout(popupShowTimeout);
			popupHideTimeout = setTimeout(function(){
				$('div.popup2').fadeOut();
			}, 100);
        });
		$('div.product-box[rel^="product"] > a').hover(function(){
			var el = $('h2 > a', $(this).parent());
            var popupContent = $('div#popup-content-' + el.parents('div.product-box').eq(0).attr('rel').replace('product-', ''));
            if(popupContent.length == 1)
            {
                var offset = el.offset();
				var popup = $('div.popup2').hide();
                $('div.popup-content', popup).html(popupContent.html());
                clearTimeout(popupHideTimeout);
                clearTimeout(popupShowTimeout);
                popupShowTimeout = setTimeout(function(){
                    popup
                        .css({
                            top: (offset.top - 45) + 'px',
                            left: offset.left + el.width() + 'px'
                        })
                        .fadeIn();
                }, 100);
            }
        }, function(){
			clearTimeout(popupHideTimeout);
			clearTimeout(popupShowTimeout);
			popupHideTimeout = setTimeout(function(){
				$('div.popup2').fadeOut();
			}, 100);
        });
        $('div.visual a[rel^="small-image"]').click(function(event){
            event.preventDefault();
            var el = $(this);
            var visible = $('div.visual a[rel^="large-image"]:visible');
            visible.hide();
            $('div.visual a[rel^="' + el.attr('rel').replace('small', 'large') + '"]').fadeIn();
            el.hide();
            $('div.visual a[rel^="' + visible.attr('rel').replace('large', 'small') + '"]').fadeIn();
            return false;
        });
        $('form.catalog-form a[rel^="size-container"], form.catalog-form label[for^="material"], form.catalog-form label[for^="producer"]').hover(function(){
            var el = $(this);
            var name = el.is('a') ? 'size' : el.attr('for').replace(/\-\d+$/, '');
            var id = name == 'size' ? el.attr('rel').replace('size-container-', '') : el.attr('for').replace(name + '-', '');
            if(tooltips[name] && tooltips[name][id])
            {
                var offset = el.offset();
                var popup = $('div.popup2').hide();
                $('div.popup-content', popup).html('<div class="tooltip-content">' + tooltips[name][id] + '</div>');
                clearTimeout(popupHideTimeout);
                clearTimeout(popupShowTimeout);
                popupShowTimeout = setTimeout(function(){
                    popup
                        .css({
                            top: (offset.top - 45) + 'px',
                            left: offset.left + el.width() + 'px'
                        })
                        .fadeIn();
                }, 100);
            }
        }, function(){
            clearTimeout(popupHideTimeout);
			clearTimeout(popupShowTimeout);
			popupHideTimeout = setTimeout(function(){
				$('div.popup2').fadeOut();
			}, 100);
        });
        var location = window.location.href.replace(/#(.*)$/, '');
        $('a[href^="#"]').each(function(){
            var el = $(this);
            var href = location + el.attr('href').replace('/#.*/i', '');
            el.attr('href', href);
        });
    });
})(jQuery);

