var txProductCataloguePi1 = {
	activeListRequest: false,
	activeFormRequest: false,
	activeUrlRequest: false,
	actualFormData: '',
	form: 'ajax-form',
	list: 'ajax-list',
		
	initialize: function() {
		$('#'+this.form+ ' input.ajax-checkbox').bind('click', function(event){
			txProductCataloguePi1.setActualFormData();
			
			txProductCataloguePi1.reloadForm();
			txProductCataloguePi1.reloadList();
		});
		$('#ajax-submit').hide();
	},
	
	setActualFormData: function() {
		this.actualFormData = $('#'+this.form).serialize();
	},
	
	reloadList: function() {
		//stop preceding requests from being executed
		if(this.activeListRequest != false) {
			this.activeListRequest.abort();
		} else {
			$('#'+txProductCataloguePi1.list).replaceWith(
				$('<div></div>').attr('id', txProductCataloguePi1.list).attr('class', 'ajax-loading-list').html('')
			);
		}
	
		this.activeListRequest = $.ajax({
			type: 'POST',
			url: txProductCatalogueAjaxUrl,
			data: this.actualFormData+'&tx_productcatalogue_pi1[render]=list',
			dataType: 'html',
			success: function(response){
				txProductCataloguePi1.activeListRequest = false;
				$('#'+txProductCataloguePi1.list).replaceWith(response);
				
				$.address.autoUpdate(false);
				$.address.value('selection');
				$.address.queryString(txProductCataloguePi1.actualFormData);
				$.address.update();
			}
		});
	},
	
	reloadForm: function() {
		if(this.activeFormRequest == false) {
			this.activeFormRequest = true;
			
			$('#'+txProductCataloguePi1.form).replaceWith(
				$('<div></div>').attr('id', txProductCataloguePi1.form).attr('class', 'ajax-loading').html('')
			);
			
			this.activeFormRequest = $.ajax({
				type: 'POST',
				url: txProductCatalogueAjaxUrl,
				data: this.actualFormData+'&tx_productcatalogue_pi1[render]=form',
				dataType: 'html',
				success: function(response){
					txProductCataloguePi1.activeFormRequest = false;
					$('#'+txProductCataloguePi1.form).replaceWith(response);
					txProductCataloguePi1.initialize();
				}
			});
		}
	},
	
	activateCheckbox: function(checkboxId) {
		var checkbox = $('#'+this.form+ ' #'+checkboxId);
		if($(checkbox).is(':checked') == false) {
			
			$(checkbox).attr('checked', true);
			
			txProductCataloguePi1.setActualFormData();
			
			txProductCataloguePi1.reloadForm();
			txProductCataloguePi1.reloadList();
		}
		
		return false;
	}
};

$(document).ready(function() {
	txProductCataloguePi1.initialize();
	
	$.address.externalChange(function(event) {
		//check if query parameters are submited in url, if yes update!
		txProductCataloguePi1.actualFormData = $.address.queryString();
		//if parameters in url reload form and list
		txProductCataloguePi1.reloadForm();
		txProductCataloguePi1.reloadList();
	});
});
