(function() {

	var initCalc = false,
		str_Calc = ".calc",
		str_Help = "#help_box",
		str_Iclc = "#i_calc",
		str_IAdd = str_Iclc + " .add",
		str_Chcl = '.ch_calc',
		
		str_Disp = "display",
		str_Blck = "block",
		str_None = "none",
		
		_pVar = 0,
		_rVar = 0,
		_eVar = false;
	
	window.$calc = {
		
		data : {},
		
		curs : 1,
		
		init : function()
		{
			if( initCalc ) return;
			
			var self = this;
			
			// acitvate select
			$select.init();
			$select.change(function(e)
			{
				var id = $(e).attr('id'), val = $(e).val();
				
				switch( id )
				{
					case 'ip1' : 
						self.attach.call( self, $( str_Iclc + " input[id=ip1]" ), true );
						break;
					case 'ip2' : 
						val = parseInt(val);
						if( val == 0 || val == 1 || val == 7 ) 
						{
							$( str_IAdd + " > div[id]" ).css(str_Disp,str_None);
							$( str_IAdd + " div[id=add_box_" + val + "]" ).css(str_Disp,str_Blck);
							$( str_IAdd ).slideDown();
							
							var del = parseInt( $( str_IAdd + " div[id=add_box_" + val + "] input:checked" ).val() );
							if( isNaN( del ) ) del = 0;
							_rVar = del;
						}
						else
						{
							$( str_IAdd ).slideUp();
							_rVar = 0;
						}
						
						self.update.call( self );
						break;
					default :
						self.update.call( self );
						break;
				}
			});
			
			// activate data action
			$( str_Iclc + " input[id=ip1]" )
				.bind( 'keyup change', function(e) 
				{
					if( e.which == 13 || e.which == 27 )
					{
						$(this).blur(), $select.close();
						return false;
					}
					
					self.attach.call( self, this );
				})
				.blur( function()
				{
					$(this).val( $(this).val().replace(/\.$/i, "" ) );
					
					if( $(this).val() == '' ) 
					{
						$(this).val("0");
					}
					
					self.attach.call( self, this );
				})
				.val("0");
			
			var b = jQuery.browser.msie ? 'change click' : 'change';
			
			$( str_Iclc + " :checkbox" ).bind( b, function() { self.update.call( self ) });
			
			$( str_IAdd + " :radio" ).bind( b, function() 
			{
				var del = parseInt( $( this ).val() );
				if( isNaN( del ) ) del = 0;
				
				_rVar = del;
				self.update.call( self );
			});
			
			// create hint
			
			$('.field label[xtitle]').after( '<img src="/design/images/help.png" class="help" />' );
			$('.field .help')
				.mouseenter( function()
				{
					$(str_Help)
						.css('top', ($(this).parent().position().top + 23) + 'px')
						.find('.help_text')
						.text( $(this).prev().attr('xtitle') );
						
					if( jQuery.browser.msie )
					{
						$(str_Help).css( str_Disp, str_Blck );
						return;
					}
					
					if( _eVar ) 
					{
						$(str_Help).stop( false, true );
					}
					
					$(str_Help).fadeIn(1);
				})
				.mouseleave( function()
				{
					if( jQuery.browser.msie )
					{
						$(str_Help).css( str_Disp, str_None );
						return;
					}
					
					_eVar = true;
					$(str_Help).fadeOut( function() { _eVar = false; });
				});
			
			// create big hint for calc
			
			if( jQuery.browser.msie )
			{
				$( str_Calc ).hover(
					function() { $( str_Chcl ).css( str_Disp, str_Blck ); },
					function() { $( str_Chcl ).css( str_Disp, str_None ); });
			}
			else
			{
				$( str_Calc ).hover(
					function() { $( str_Chcl ).fadeIn(); },
					function() { $( str_Chcl ).fadeOut(); });
			}
			
			//
			initCalc = true;
		},
		
		attach : function( o, ignore )
		{
			var 
				t = ( /\.0?$/.test( $( o ).val() )  ? '.' : '' ),
				n = parseFloat( $( o ).val().replace(/^[^0-9]+(\d+)|^0(\d+)|(\.\d).+/i, "$1$2$3" ) );
			
			if( isNaN( n ) || n < 0 ) 
			{
				n = 0, 
				$( o ).val( "0" );
			}
			else if( n > 10000 )
			{
				n = 10000;
				$( o ).val( "10000" );
			}
			
			$( o ).val( n + t );
			
			if( n != _pVar || ignore == true )
			{
				_pVar = n, 
				this.update();
			}
		},
		
		update : function()
		{
			var sum = 0, tmp, tmp2, self = this;
			
			if( _pVar != 0 ) 
			{
				for( var i in self.data )
				{
					switch( self.data[i][0] )
					{
						case 'check' :
							
							if( $( 'input[id=' + i + ']:checked' ).length == 1 )
							{
								if( self.data[i][1] == 1 ) sum += self.data[i][2] * _pVar;
								else if( self.data[i][1] == 3 ) sum += self.data[i][2];
							}
							break;
						
						case 'select' :
							
							tmp = parseInt( $( 'input[id=' + i + ']' ).val() );
							
							if( typeof( self.data[i][2][tmp] ) == 'number' )
							{
								if( self.data[i][1] == 2 ) 
								{
									//sum += parseInt( Math.sqrt( _pVar ) * 4 / 2.5 ) * self.data[i][2][tmp];
									//sum += parseInt( Math.sqrt( _pVar ) * 4 ) * self.data[i][2][tmp];
									sum += _pVar * self.data[i][2][tmp];
								}
								else if( self.data[i][1] == 1 ) 
								{
									tmp2 = ( tmp == 0 || tmp == 1 || tmp == 7 ) ? (self.data[i][2][tmp] + _rVar) : self.data[i][2][tmp];
									sum += tmp2 * _pVar;
								}
							}
							break;
					}
				}
			}
			
			sum *= self.curs;
			tmp = sum.toString().replace(/(\d{1,3})?(\d{3})?(\d{3})(?:(\.\d{1,2})\d*)?$/ig, "$1 $2 $3$4" );
			
			if( sum > 0 )
			{
				$('.itogo').css( str_Disp, str_Blck ).find('.sum').html( tmp );
			}
			else
			{
				$('.itogo').css( str_Disp, str_None );
			}
			
			$(str_Calc + ' span:first').html( tmp );
		}
	}
})();
