jQuery.fn.extend({
    disableSelection : function() {
            this.each(function() {
                    this.onselectstart = function() { return false; };
                    this.unselectable = "on";
                    jQuery(this).css('-moz-user-select', 'none');
            });
    },
    enableSelection : function() {
            this.each(function() {
                    this.onselectstart = function() {};
                    this.unselectable = "off";
                    jQuery(this).css('-moz-user-select', 'auto');
            });
    }
});

function resizeFilters() {
    var height = $("#manfilter-content").height();
    $("#manfilter").height(height+78);
}

function checkUncheck(flag, ref) {
    if (flag) {
        $("#"+ref+" input").attr('checked', 'checked');
    } else {
        $("#"+ref+" input").attr('checked', false);
    }
}

$(document).ready( function () {

    $('#manfilter, #topfilters').disableSelection();

    $(".block-mp-tovar").css("opacity", 0.95);
    $(".block-mp-tovar").hover(
        function() {
            $(this).css("opacity", 1);
        },
        function() {
            $(this).css("opacity", 0.95);
        }
    );

    $(".topfilters-manselect").click(function() {
        if ($(this).is('.topfilters-manselect-opened')) {
            $("#manfilter-slideup").trigger('click');
        } else {
            var height = $("#manfilter-content").height();
            $("#manfilter").animate({height: height+78+'px'}, 500, function() {
                $(".topfilters-manselect").addClass('topfilters-manselect-opened');
            });
        }
    });

    $("#manfilter-slideup, #manfilter-slideup-button, #manfilter-bottom a").click(function() {
        $("#manfilter").animate({height: '28px'}, 500, function () {
            $(".topfilters-manselect").removeClass('topfilters-manselect-opened');
        });
    });

    $("#leftMenu-filters ul li").click(function() {
       if (!$(this).is(".current")) {
           $(this).addClass("current")
       } else {
           $(this).removeClass("current");
       }
    });

    $("#topfilters-sorting a").click(function() {
       $("#topfilters-sorting a").removeClass("current");
       $(this).addClass("current");
    });

    $(".manfilter-checkbox input").click(function(event) {
        event.stopPropagation();
    });

    $(".manfilter-checkbox").click(function() {
        if ($("input", this).attr('checked')) {
            $("input", this).attr('checked', false);
        } else {
            $("input", this).attr('checked', 'checked');
        }
    });

    $(".manfilter-buttons-others").click(function() {
        $(this).addClass("current");
        $(".manfilter-buttons-popular").removeClass("current");
        $("#manfilter-others").css("display", "block");
        resizeFilters();
    });

    $(".manfilter-buttons-popular").click(function() {
        $(this).addClass("current");
        $(".manfilter-buttons-others").removeClass("current");
        $("#manfilter-others").css("display", "none");
        //$("#manfilter-others .manfilter-checkbox input").attr('checked', false);
        resizeFilters();
    });

    $("#topfilters-sort .open").click(function(event) {
        event.stopPropagation();
        if ($(this).is(".opened")) {
            $("#topfilters-sort-block").css("display", "none");
            $(this).removeClass("opened");
        } else {
            $("#topfilters-sort-block").css("display", "block");
            $("#topfilters-sort-block").css("opacity", 0.9);
            $(this).addClass("opened");
        }
    });

    $("#topfilters-sort-block a").click(function(event) {
        $("#topfilters-sort-block a").removeClass("current");
        $(this).addClass("current");
        $("#topfilters-sort .open a").html($(this).html());
    });

    $("body").click(function() {
        $("#topfilters-sort-block").css("display", "none");
        $("#topfilters-sort .open").removeClass("opened");
    });

    $(".catalog-entry a.info").click(function() {
        var parent = $(this).parents(".catalog-entry");
        if ($("div.desc", parent).css("display") == 'none') {
            $("div.desc", parent).css("display", "block");
            $("p.price span.short", parent).css("display", "none");
            $("p.price span.long", parent).css("display", "block");
            $("p.price a.whatis", parent).css("display", "block");
            $("textarea", parent).css("display", "inline");
            $("td.c2", parent).css("padding-bottom", "20px");
            $(this).html('Краткая информация');
        } else {
            $("div.desc", parent).css("display", "none");
            $("p.price span.short", parent).css("display", "block");
            $("p.price span.long", parent).css("display", "none");
            $("p.price a.whatis", parent).css("display", "none");
            $("textarea", parent).css("display", "none");
            $("td.c2", parent).css("padding-bottom", "0px");
            $(this).html('Полная информация');
        }
        return false;
    });

    $("#authblock").click(function() {
        $(this).fadeOut(500);
    });

    $("#block-enterOpt-enter").click(function() {
        $("#authblock").fadeIn(500);
    });

    $("#authblock-container").click(function(event) {
        event.stopPropagation();
    });
});
