function tcountForm(type, boxParams, productParams, syncParams, basket_form_id, add_to_basket_form_object) {

    this.formId = boxParams.formId;
    this.summPriceId = boxParams.summPriceId;
    this.productPriceId = boxParams.productPriceId;
    this.productArticleId = boxParams.productArticleId;
    this.curCountId = boxParams.curCountId;
    this.discont = parseInt(boxParams.discont);

    this.maxCount = parseInt(productParams.maxCount);
    this.productId = parseInt(productParams.productId);
    this.price = parseInt(productParams.price);

    this.syncUrl = syncParams.syncUrl;
    this.syncParams = syncParams.syncParams;
    this.syncCallback = syncParams.syncCallback;
    
    this.basket_form_id = basket_form_id;
    this.basket_form = $('#' + basket_form_id);
    
    this.add_to_basket_form_object = add_to_basket_form_object;

    this.init(parseInt(productParams.curCount), type);
}

tcountForm.prototype = {

    formId: null,
    formBox: null,

    minCount: 1,
    maxCount: null,
    curCount: null,

    productId: null,

    summPriceId: null,
    summPriceBox: null,
    summPrice: null,
    prePrice: null,
    
    productPriceId: null,
    productPriceBox: null,
    
    productArticleId: null,
    productArticleBox: null,
    
    price: null,
    discont: null,

    curCountId: null,
    curCountBox: null,

    syncUrl: null,
    syncParams: null,
    syncCallback: null,
    beforeSyncEvent: null,
    
    basket_form_id: null,
    basket_form: null,
    
    add_to_basket_form_object: null,


    init: function(curCount) {
        
        this.formBox = $("#" + this.formId);
        this.summPriceBox = $("#" + this.summPriceId);
        this.productPriceBox = $("#" + this.productPriceId);
        this.productArticleBox = $("#" + this.productArticleId);
        
        this.basket_form = $('#' + this.basket_form_id);
        
        if (this.curCountId)
            this.curCountBox = $("#" + this.curCountId);

        this.setCurrentCount(curCount, 1);
        this.bindButtons();
        
    },

    bindButtons: function() {
        var self = this;
        
        $("select.curCount", this.formBox)[0].onchange = function()
        {
            self.changeCurrentCount();
        };
        
        $('#castors_variants_' + self.productId).change(function()
        {
            var params = {
                product_id: self.productId,
                castors_id: this.value
            };
            
            var parent = $('#castors_variant_' + this.value + '_' + self.productId).parent('.model_params_pic');
            
            $('img', parent).css('display', 'none');
            
            $('#castors_variant_' + this.value + '_' + self.productId).css('display', 'block');
            
            $.post('/module/Catalog/Index/change-product-castors', params, function(data)
            {
                if (data)
                {
                    self.price = data.price;
                    self.showCurrentPrice();
                    self.showCurrentCount();
                    
                    self.productArticleBox.html(data.human_article);
                    
                    if (data.article_changed == true)
                        self.basket_form[0].changed_article.value = data.article;
                    else
                        self.basket_form[0].changed_article.value = '';
                }
                else
                {
                    alert('data is empty');
                }
                
            }, 'json');
        });
    },


    incCurrentCount: function() {
        this.setCurrentCount(parseInt(this.curCount) + 1);
    },


    decCurrentCount: function() {
        this.setCurrentCount(parseInt(this.curCount) - 1);
    },
    
    changeCurrentCount: function() {
        this.setCurrentCount(parseInt($("select.curCount", this.formBox)[0].value));
    },

    setCurrentCount: function(value, nosync) {
        if (!value)
            value = this.minCount;
        if (value >= this.minCount && value <= this.maxCount) {
            this.curCount = value;

            this.showCurrentCount();
            this.showCurrentPrice();

            if (this.beforeSyncEvent)
                this.beforeSyncEvent();

            if (!nosync)
                this.setSyncTimeout();
            
            if (this.onSetCurrentCount)
                this.onSetCurrentCount();
        }
    },


    
   interval     : 100,
   maxDelay     : 3000,

   delay        : 3000,
   delayStep    : 1000,
   delayId      : 0,


    setSyncTimeout: function() {
        var self = this;

        var upd = function() {
            if (self.delay > 0) {
                self.delay = self.delay - self.delayStep;
                self.delayId = setTimeout(upd, self.interval);
            }
            else {
                self.delay = self.maxDelay;
                self.delayId = 0;
                self.syncAction();
            }
        }
        
        self.delay = self.maxDelay;
        
        if (self.delayId == 0)
            upd();
    },


    syncAction: function() {
        var self = this;
        if (self.syncUrl == null) {
            return;
        }

        self.syncParams["selectedCount"] = self.curCount;

        $.post(self.syncUrl, self.syncParams, function(data) {
            if (data && data.length)
                alert(data);
            else
                if (self.syncCallback)
                    self.syncCallback(self);
        });
    },


    showCurrentCount: function() {
        if (this.curCountBox)
            this.curCountBox.html(this.curCount);
        
        $(".curCount", this.formBox)[0].value = this.curCount;
    },


    showCurrentPrice: function() {        
        if (this.discont)
        {
            this.prePrice = this.price - Math.round(this.discont / 100 * this.price);
            this.summPrice = this.prePrice * this.curCount;
        }
        else
        {
            this.summPrice = this.curCount * this.price;
        }
        
        this.productPriceBox.html(this.price);
        this.summPriceBox.html(this.summPrice);
        
    },

    /** callback **/
    onSetCurrentCount: function() {
        
    }

}
