UserPriceAlert = {
	price_limit : 0.5,
	init : function(limit) {
		if (limit) {
			this.price_limit = limit;
		}
		$(window).load(function(){
			$('#palert_dialog_value').numericInput({signed: false});
		});
	},
	showDialog : function(id, price, show_delete) {
		var current_alert = $('#palert_current_' + id).val(),
			buttons = {};

		buttons['Anuluj'] = function() {
			$(this).dialog('destroy');
		};

		if (current_alert > 0) {
			if (show_delete == true) {
				buttons['Usuń'] = function() {
					$(this).dialog('destroy');
					UserPriceAlert.remove(id);
				};
			}
		}

		buttons['Zapisz'] = function() {
			var el_value = $('#palert_dialog_value'),
				current = parseFloat($('#palert_price').val()),
				price = parseFloat(el_value.val().replace(',', '.'));

			if (UserPriceAlert.isValidPrice(price, current)) {
				$(this).dialog('destroy');
				UserPriceAlert.add(id, price);
			}
			else {
				el_value.focus();
				el_value.select();
			}
		};

		$('#palert_dialog').dialog( {
			modal : true,
			width : 300,
			close: function(event, ui) {$('#palert_dialog').dialog('destroy');},
			buttons : buttons
		});

		var el_value = $('#palert_dialog_value');

		price = price.toFixed(2);
		current_alert = parseFloat(current_alert).toFixed(2);
		this.setErrorMessage('');
		$('#palert_price').val(price);

		if (current_alert == 0) {
			el_value.val(price.toString().replace('.', ','));
		}
		else {
			el_value.val(current_alert.toString().replace('.', ','));
		}
		el_value.select();
		el_value.keypress(function(){
			UserPriceAlert.hideErrorMessage();
		});
	},
	isValidPrice : function(price, current) {
		var limit = current * this.price_limit;

		if (isNaN(price)) {
			this.setErrorMessage('Wprowadzona wartość jest nieprawidłowa');
			return false;
		}
		else if (price < limit || price <= 0) {
			this.setErrorMessage('Wprowadzona wartość nie może być mniejsza niż ' + limit.toFixed(2) + ' zł');
			return false;
		}
		else if (current <= price) {
			this.setErrorMessage('Wprowadzona wartość musi być mniejsza niż ' + current.toFixed(2) + ' zł');
			return false;
		}
		return true;
	},
	add : function(id, price) {
		var edit = false;

		if ($('#palert_edit_' + id).attr('display') != 'none') {
			edit = true;
		}
		
		$('#palert_add_' + id).hide();
		$('#palert_edit_' + id).hide();
		$('#palert_process_' + id).show();

		$.ajax({
			type	: 'POST',
			url		: '/konto/alert_cenowy/ajax.php',
			data	: ({id : id, price : price}),
			success : function(data) {
				if (data == 'ok') {
					$('#palert_process_' + id).hide();
					$('#palert_edit_' + id).show();
					$('#palert_current_' + id).val($('#palert_dialog_value').val().replace(',', '.'));
				}
				else if (edit) {
					UserPriceAlert.errorEdit(id, data);
				}
				else {
					UserPriceAlert.errorAdd(id, data);
				}
			},
			error : function(data) {
				if (edit) {
					UserPriceAlert.errorEdit(id);
				}
				else {
					UserPriceAlert.errorAdd(id);
				}
			}
		});
	},
	remove : function(id) {
		$('#palert_add_' + id).hide();
		$('#palert_edit_' + id).hide();
		$('#palert_delete_' + id).attr('disabled', true);
		$('#palert_process_' + id).show();

		$.ajax({
			type	: 'POST',
			url		: '/konto/alert_cenowy/ajax.php',
			data	: ({id : id, remove : 1}),
			success : function(data) {
				if (data == 'ok') {
					$('#palert_process_' + id).hide();
					$('#palert_add_' + id).show();
					$('#palert_current_' + id).val('0');
				}
				else {
					UserPriceAlert.errorRemove(id, data);
				}
			},
			error : function(data) {
				UserPriceAlert.errorRemove(id);
			}
		});
	},
	setErrorMessage : function(msg) {
		var el_error = $('#palert_error');
		el_error.text(msg);
		el_error.show();
	},
	hideErrorMessage : function() {
		$('#palert_error').fadeOut('slow', function(){
			UserPriceAlert.setErrorMessage('');
		});
	},
	errorAdd : function(id, data) {
		$('#palert_process_' + id).hide();
		$('#palert_add_' + id).show();
		if (data == 'not logged') {
			alert('Musisz być zalogowany, aby dodawać alerty cenowe.');
		}
		else {
			alert('Przepraszamy, ale wystąpił błąd przy dodawaniu alertu cenowego.');
		}
	},
	errorEdit : function(id, data) {
		$('#palert_process_' + id).hide();
		$('#palert_edit_' + id).show();
		if (data == 'not logged') {
			alert('Musisz być zalogowany, aby modyfikować alerty cenowe.');
		}
		else {
			alert('Przepraszamy, ale wystąpił błąd przy modyfikacji alertu cenowego.');
		}
	},
	errorRemove : function(id, data) {
		$('#palert_process_' + id).hide();
		$('#palert_edit_' + id).show();
		if (data == 'not logged') {
			alert('Musisz być zalogowany, aby usuwać alerty cenowe.');
		}
		else {
			alert('Przepraszamy, ale wystąpił błąd przy usuwaniu alertu cenowego.');
		}
	}
}

UserPriceAlert.init();
