window.shop = {

    add2cart: function(id,link){
        $(link).parents(".add2CartLink").addClass("added");
        $("#basketBlock").load("/cart/add/" + id);
        return false;
    },
    
    add2compare: function(id,link){
        $(link).parents(".add2CompareLink").addClass("added");
        $("#comparerBlock").load("/compare/add/" + id);
        return false;
    },

    initSigns: function(selector, inputSelector){
        var input = $(inputSelector);
        $(selector).each(function(){
            var $this = $(this);
            var mark = 0;
            var tMark = 0;

            function addMouseOverCallback(i)
            {
                $this.find(".s" + i).mouseover(function(){
                    if(tMark != 0){
                        $this.removeClass("mark"+tMark);
                    }
                    $this.addClass("mark" + i);
                    tMark = i;
                }).click(function(){
                    mark = tMark;
                    input.attr('value',mark);
                }).css({cursor:'pointer'});
            }

            for(var i = 0; i <= 5; i++){
                addMouseOverCallback(i);
            }

            $this.mouseout(function(){
                if(tMark != 0){
                    $this.removeClass("mark"+tMark);
                }
                tMark = mark;
                $this.addClass("mark" + mark);
            });


        });
    },

    showCommentForm: function(good_id){
        if(!this.commentForm){
            this.commentForm = new App.ux.CommentForm();
            var _this = this;
        }
        this.commentForm.show(good_id);
        return false;
    },

    setUseful: function(comment_id, is_useful){
        $.post('/catalogue/setCommentUseful', {
            comment_id: comment_id,
            is_useful: is_useful
        }, function(data){
            $("#comment_useful_" + comment_id).html(data);
        });
        return false;
    },

    resetSearch: function()
    {
        location.href = location.href.split("?")[0];
    }

}

namespace("App.ux");
App.ux.CommentForm = newClass(App.superModalForm, {

    form: null,

    constructor: function(zIndex){
        var _this = this;
        this.constructor.prototype.constructor.call(this, zIndex);

        _this.setSize(200,200);
        _this.body.html('loading...');

        $.get("/catalogue/commentForm", null, function(data){
            _this.body.empty().append(data);
            _this.setSize(500,null);
            _this.form = _this.window.find('form');
            _this.initEvents();
        });
    },

    initEvents: function(){
        var _this = this;
        this.body.find("input[name=cancel]").click(function(){
            _this.hide();
        });

        this.form.validate({
            submitHandler: function (form) {
                var $form = $(form);
                $("input[type=submit]", $form).attr("disabled", "disabled").attr('value','Отправка...');
                $.get("/system/nospam_key",null,function(nospam_key){
                    $form.ajaxSubmit({
                        data:{'nospam_key':nospam_key},
                        success:function(){
                            location.href="#comments";
                            location.reload();
                        }
                    });
                })

            },
            focusInvalid: false,
            focusCleanup: true,
            rules: {
                name: { required: true, minlength: 2 },
                comment: {required: true, minlength: 5}
            },
            invalidHandler: function (form, validator) {
                console.log(validator);
                alert('Введены не все поля');
            },
            errorPlacement: function () { }
        });
    },

    submit: function(){
        this.form.submit();
        return false;
    },
    
    hide: function(){
        this.constructor.prototype.hide.call(this);
        return false;
    }


});
