function TicketSell(o){
	
	/**reference for the instance*/
	var app = this;
	
	this.defaults = {
			default_type: 0,
			selected_type: 1,
			time_unit_day : 10,
			time_unit_month : 20,
			time_unit_month_reference : 30,
			/**id of filter text*/
			types: [{
				name :'Bérlet',
				id_ticket_type: 0,
				max_usage_count:0,
				time_unit_type:1,
				time_unit_count:1,
				id_account:0,
				price_brutto:1000,
			},
		],
		selector_type: '#ticketcreate-id_ticket_type',
		selector_discount: '#ticketcreate-id_discount',
		selector_start: '#ticketcreate-start',
		selector_end: '#ticketcreate-end',
		selector_account: '#ticketcreate-id_account',
		selector_price: '#ticketcreate-price_brutto',
		selector_max_usage_count: '#ticketcreate-max_usage_count',
		date_format_moment: 'YYYY.MM.DD',
		ticket_type: null,
		start_date: null,
		end_date: null,
		max_usage_count: null,
		id_account: null,
		price: null,
		clear_cart_url: '',
		cart_paid_message:  'Tételek fizetve',
		user_cart: [],
		customer_cart: [],
		/**selector for customer cart container*/
		selector_customer_cart: '.customer-cart',
		selector_user_cart: '.user-cart',
		selector_btn_pay_customer_cart: '#btn_pay_customer_cart',
		selector_btn_pay_user_cart: '#btn_pay_user_cart',
		/**mark list paid url*/
		url_pay_customer_card: '-',
		url_pay_user_cart: '',
		url_delete_transaction: '',
		url_pay_transaction: '',
		discounts : [],
		discount: null,
		//if user cart module is on or off
		user_cart_on: true
		
	};
	
	init();
	
	function init(){
		$.extend(app.defaults, o );
		addBehaviourTypeChangedListener();
		addBehaviourDiscountChangedListener();
		useDefaults();
		createCarts();
//		addPayoutButtons();
		addSellButtons();
//		addBehaviourBtnCartPaid();
	}
	
	function useDefaults(){
		if ( app.defaults.selected_type > 0){
			$(app.defaults.selector_type).val( app.defaults.selected_type );
			typeChanged();
		}
	}
	
	
	
	////////////////////////////////////////
	//	carts
	////////////////////////////////////////
	
	/**
	 * payout out user or customer cart
	 * */
	/*
	function addPayoutButtons(){
//		addBehaviourPayoutUserCart();
//		addBehaviourPayoutCustomerCart();
	}
	*/
	/**
	 * display user and customer cart on page load
	 * */
	function createCarts(){
		if ( app.defaults.user_cart_on ){
			createUserCartTable();
		}
		createCustomerCartTable();
	}
	
	function createUserCartTable(){
		$(app.defaults.selector_user_cart).transferList({
			'transfers' : app.defaults.user_cart,
			'url_delete' : app.defaults.url_delete_transaction,
			'url_pay' : app.defaults.url_pay_transaction,
			'url_pay_all_visible' : app.defaults.url_pay_user_cart,
			'name_transfers' : 'UserCartPayoutForm[transfers][]',
			'id_form' : 'frm_user_cart',
		});
	}
	function createCustomerCartTable(){
		$(app.defaults.selector_customer_cart).transferList({
			'transfers' : app.defaults.customer_cart,
			'url_delete' : app.defaults.url_delete_transaction,
			'url_pay' : app.defaults.url_pay_transaction,
			'url_pay_all_visible' : app.defaults.url_pay_customer_card,
			'name_transfers' : 'CustomerCartPayoutForm[transfers][]',
			'id_form' : 'frm_customer_cart',
		});
	}
	
	function addSellButtons(){
		if ( app.defaults.user_cart_on ){
			addBehaviourBtnSellAndAppendToUserCart();
		}
		addBehaviourBtnAddToCustomerCart();
		addBehaviourBtnSell();
	}
	
	function addBehaviourBtnSellAndAppendToUserCart(){
		$('#btn_add_to_user_cart').on('click',submitAddTicketToUserCart);
	}

	function addBehaviourBtnAddToCustomerCart(){
		$('#btn_add_to_customer_cart').on('click',submitAddTicketToCustomerCart);
	}
	
	function submitSell(){
		$('#ticketcreate-cart').val('');
		$('#ticket_form').submit();
	}
	function submitAddTicketToUserCart(){
		$('#ticketcreate-cart').val('user');
		$('#ticket_form').submit();
	}
	
	function submitAddTicketToCustomerCart(){
		$('#ticketcreate-cart').val('customer');
		$('#ticket_form').submit();
	}
	
	function addBehaviourBtnSell(){
		$('#btn_sell').on('click',submitSell);
	}
	
	
	
	function addBehaviourTypeChangedListener(){
		$(app.defaults.selector_type).change(change);
	}
	function addBehaviourDiscountChangedListener(){
		$(app.defaults.selector_discount).change(typeChanged);
	}
	
	function change(event){
		if ( '#'+event.target.id == app.defaults.selector_type ){
			typeChanged();
		}
	}
	
	function typeChanged(){
		validateTypeChanged();
		refresh();
	}
	
	function validateTypeChanged(){
		validateType();
		validateStartDate();
		validateEndDate();
		validateMaxUsageCount();
		validateDiscount();
		validatePriceBrutto();
		validateAccount();
	}
	
	function validateType(){
		var type;
		type =  +$(app.defaults.selector_type).val();
		app.defaults.ticket_type = null;
		for ( var i = 0; i < app.defaults.types.length; i++ ){
			if ( app.defaults.types[i].id_ticket_type == type){
				app.defaults.ticket_type =  app.defaults.types[i];
				break;
			}
			
		}
	}
	
	function validateDiscount(){
		var discount;
		discount =  +$(app.defaults.selector_discount).val();
		app.defaults.discount = null;
		console.info( app.defaults.discounts );
		for ( var i = 0; i < app.defaults.discounts.length; i++ ){
			if ( app.defaults.discounts[i].id_discount == discount){
				app.defaults.discount =  app.defaults.discounts[i];
				break;
			}
			
		}
		console.info( 'discount found:');
		console.info( app.defaults.discount );
	}
	
	function validateStartDate(){
		 app.defaults.start_date =  moment( $(  app.defaults.selector_start  ).val(),  app.defaults.date_format_moment) ;
		 
		 if ( !app.defaults.start_date.isValid() ){
			 app.defaults.start_date = moment();
		 }
		 
	}
	
	function validateEndDate(){
		var units;
		units = app.defaults.ticket_type.time_unit_count;
		app.defaults.end_date = moment(app.defaults.start_date);
		switch(app.defaults.ticket_type.time_unit_type){
		case app.defaults.time_unit_day: 
			app.defaults.end_date.add( units, 'days');
			app.defaults.end_date.subtract( 1, 'days');
			break;
		case app.defaults.time_unit_month: 
			app.defaults.end_date.add( units, 'month');
			app.defaults.end_date.subtract( 1, 'days');
			break;
		case app.defaults.time_unit_month_reference: 
			if ( units > 1){
				app.defaults.end_date.add( units -1, 'month');
			}
			app.defaults.end_date.endOf('month');
			break;
		}
	}
	
	function validateMaxUsageCount(){
		app.defaults.max_usage_count = app.defaults.ticket_type.max_usage_count;
	}
	
	function validatePriceBrutto(){
		app.defaults.price = app.defaults.ticket_type.price_brutto;
		if ( app.defaults.discount != null ){
			var d = app.defaults.price * app.defaults.discount['value'] /100;
			d =  Math.floor( d );
			app.defaults.price = app.defaults.price - d;
		}
	}
	function validateAccount(){
		app.defaults.id_account = $('#ticketcreate-id_account').val();
	}
	
	function refresh(){
		console.info( app.defaults.start_date.toDate());
		$(app.defaults.selector_start ).val(   app.defaults.start_date.format(   app.defaults.date_format_moment ) );
		$(app.defaults.selector_end ).val(   app.defaults.end_date.format(   app.defaults.date_format_moment ) );
		$(app.defaults.selector_account ).val(app.defaults.id_account);
		$(app.defaults.selector_price ).val(app.defaults.price);
		$(app.defaults.selector_max_usage_count ).val(app.defaults.max_usage_count);
	}
	
}