function AccountState(o){
	
	/**reference for the instance*/
	var app = this;
	
	var total = 0;// sum of banknotes
	var money = 0;//value of money field
	var diff = 0;//diff of total and money
	var id_account;//selected account
	var last_state;//last state of selected account
	var last_money;//last moeny on selected account
	var last_diff;// diff on last money and money
	
	var notes ;
	var moneyInput ;
	var ddAccount;
	
	this.defaults = {
			'open' : false,
			'notes' : '.note-input',
			'last_states' : [],
			'selector_money' :  '#accountstate-money',
			'selector_dd_account' : '#accountstate-id_account',
	}
	init();
	
	function init(){
		$.extend(app.defaults, o );
		notes = $(app.defaults.notes);
		moneyInput = $(app.defaults.selector_money);
		notes.change(runNote);
		moneyInput.change(run);
		moneyInput.focus(function(){
			 $(this).select().one('mouseup', function (e) {
				    $(this).off('keyup');
				    e.preventDefault();
				  }).one('keyup', function () {
				    $(this).select().off('mouseup');
				  });
			});
		if ( app.defaults.open ){
			ddAccount = $(app.defaults.selector_dd_account);
			ddAccount.change(run);
		}
		
		run();
	}
	
	
	
	  function runNote(){
		run("note");
	}
	
	function run(what ){
		calcTotal();
		if ( what == 'note' ){
			setMoneyAsTotal();
		}else{
			readMoney();
		}
		calcDiff();
		calcAccount();
		calcLastDiff();
		updateTotal();
		updateDiff();
		updateMoney();
		updateLastMoney();
		updateLastDiff();
		updatePrevState();
		
	}
	
	function calcAccount(){
		if ( app.defaults.open){
			app.last_money = 0;
			app.id_account = ddAccount.val();
			app.last_state = findLastState();
			if ( app.last_state != null){
				app.last_money = app.last_state['money'];
			}
		}
	}
	
	function calcLastDiff(){
		app.diff = Math.abs(app.last_money - app.money );
		
	}
	
	function setMoneyAsTotal(){
		app.money = app.total;
	}
	
	function readMoney(){
		app.money = 0;
		app.money = +moneyInput.val();
		if ( isNaN(money)){
			app.money = 0;
		}
	}
	
	function calcDiff(){
		
		app.diff = Math.abs(app.total - app.money );
		
	}
	function calcTotal(){
		app.total = 0;
		notes.each(function(i,e){
			var value, count;
			value = +$(e).data('value');
			count = +$(e).val();
			if ( isNaN(count))
				count = 0;
			app.total +=  value * count;
		});
	}
	
	function updateTotal(){		
		var money;
		money = accounting.formatNumber(app.total, 0, " "); // 9 876 543.210
		$('.notes-total').html(money);
	}
	function updateDiff(){		
		var money;
		money = accounting.formatNumber(app.diff, 0, " "); // 9 876 543.210
		$('.diff-total').html(money);
	}
	function updateMoney(){		
		var money;
		money = accounting.formatNumber(app.money, 0, " "); // 9 876 543.210
		$('.money').html(money);
		$('#accountstate-money').val(app.money +"");
	}
	
	function updateLastMoney(){		
		var money;
		money = accounting.formatNumber(app.last_money, 0, " "); // 9 876 543 
		$('.last-closing-money ').html(money);
	}
	
	function updateLastDiff(){		
		var money;
		money = accounting.formatNumber(app.diff, 0, " "); // 9 876 543.210
		$('.diff-closing').html(money);
	}
	
	function updatePrevState(){
		
		if ( app.defaults.open){
				$("#accountstate-prev_state").val(  app.last_state == null  ? '' : app.last_state.id_account_state);
		}
		
		
	}
	
	function findLastState(){
		var i,result;
		result = null;
		for ( var i = 0; i < app.defaults.last_states.length; i++){
			if ( app.defaults.last_states[i].id_account ==  app.id_account){
				result = app.defaults.last_states[i];
				break;
			}
		}
		return result;
	}
	
	
}