var catalogItem = function() {
    var options = {
        inputSheetsId  : 'sheets_quant',
        itemPriceId    : 'item_price',
        itemValueId    : 'item_value',
        basketButtonId : 'basket_button'
    };
    
    var prices = {
        retail    : null,
        fromStack : null,
        wholesale : null,
        action    : null
    };
    
    var mode = null;
    
    var isAction = null;
    
    var metersSheet = null;
    
    function obtainPriceTypeByMeters(meters)
    {
        if (meters < 50) {
            return 'retail';
        } else if (meters >= 50 && meters < 1000) {
            return 'fromStack';
        } else if (meters >= 1000 ) {
            return 'wholesale';
        }
        
        return 'retail';
    }
    
    function clearHtmlValues()
    {
    	if (mode) {
    		//$('#tr_' + mode + ' td.sp').text('');
        	//$('#tr_' + mode + ' td.mp').text('');
        	if (!metersRec) {	
        		$('#tr_' + mode + ' input.tm').attr("value", '0');
        	}
        	
        	$('#tr_' + mode + ' td.tp').text('0');
        	$('#tr_' + mode + ' td.tw').text('0');
		} else {
			document.getElementById('sheet').innerHTML = '&nbsp;';
	        $('#' + options.itemPriceId + ' em.meter').text('');
	        $('#' + options.itemValueId + ' strong').text('');
	        $('#' + options.itemValueId + ' em').text('');
		}
        //$('#' + options.itemPriceId + ' em.sheet').text('.');
        
    }
    
    return {
        init : function(priceRetail, priceFromStack, priceWholesale, priceAction, iIsAction, iMetersSheet, imode, imetersRec)
        {
    		//imode = 0 - для каталога
    		//imode > 0 - для корзины
    		mode = imode;
    		metersRec = imetersRec;
    		//alert(mode);	
            prices.retail = parseFloat(priceRetail);
            prices.fromStack = parseFloat(priceFromStack);
            prices.wholesale = parseFloat(priceWholesale);
            prices.action = parseFloat(priceAction);
            
            isAction = parseInt(iIsAction);
            
            metersSheet = parseFloat(iMetersSheet);
        },
        
        recalculate : function()
        {
        	if (mode) {
        		var sheets = parseInt($('#' + options.inputSheetsId + '_' + mode).attr('value'));
        	} else {
        		var sheets = parseInt($('#' + options.inputSheetsId).attr('value'));
        	}	
            
            
            if (isNaN(sheets)) {
                clearHtmlValues();
                $('#' + options.basketButtonId).attr({disabled:true});
                calculateRes();
                return;
            }
            
            var totalMeters = Math.round((sheets * metersSheet) * 100) / 100;
            
            var curPrice = null;

            if (isAction) {
                curPrice = prices.action;
            } else {
                curPrice = prices[obtainPriceTypeByMeters(totalMeters)];
            }

            var totalPrice = curPrice * sheets;

            var meterPrice = Math.round((curPrice / metersSheet) * 100) / 100;
            
            var totalWeight = $('#tr_' + mode + ' td.wp').text()*sheets;
            
            if (mode) {
            	$('#tr_' + mode + ' td.sp').text(curPrice);
            	$('#tr_' + mode + ' td.mp').text(meterPrice);
                if (!metersRec) {
                	$('#tr_' + mode + ' input.tm').attr("value", totalMeters);
                }
            	$('#tr_' + mode + ' td.tp').text(totalPrice);
            	
            	$('#tr_' + mode + ' td.tw').text(totalWeight);
            	
            	calculateRes();
            	
            } else {
            	$('#' + options.itemPriceId + ' em.sheet').text(curPrice + ' руб.');
                $('#' + options.itemPriceId + ' em.meter').text(meterPrice + ' руб.');
                
                $('#' + options.itemValueId + ' strong').text(totalMeters);
                $('#' + options.itemValueId + ' em').text(totalPrice + ' руб.');
                
                $('#' + options.basketButtonId).attr({disabled:false});
            }	
            
            
        }
    };
}();
