function commercewatchlist_class() {
	
	this.ajaxRemoveArticleFromList = function(watchlist, article) {
		$('.loader_icon').dialog('open');
		$.getJSON(
			window.location.href,
			{
			'tx_commercewatchlists_pi1[action]': 'ajax,removeArticle,showSingle',
			'tx_commercewatchlists_pi1[watchlist]' : watchlist,
			'tx_commercewatchlists_pi1[article]' : article
			},
			function(data) {
			 	if(data['HTML']){
			 		$('#watchlist_single').replaceWith(data['HTML']);
					$('#watchlist_single').prepend('<div class="json_success">'+data['SUCCESS']+'</div>');
				}
				if(data['ERROR']) {
					$('.json_error').remove();
					$('.json_success').remove();
					$('#watchlist_single').prepend('<div class="json_error">'+data['ERROR']+'</div>');
				}
				$('.loader_icon').dialog('close');
			}
		);
	}
	
	this.ajaxAddArticleByProductId = function(url, watchlist, product) {
		$('.loader_icon').dialog('open');
		$.getJSON(
			url,
			{
				'tx_commercewatchlists_pi1[action]': 'ajax,addArticle,showLists',
				'tx_commercewatchlists_pi1[watchlist]' : watchlist,
				'tx_commercewatchlists_pi1[product]' : product
			},
			function(data) {
			 	if(data['HTML']){
					$('#watchlist_list').replaceWith(data['HTML']);
					$('#watchlist_list').prepend('<div class="json_success">'+data['SUCCESS']+'</div>');
				}
				if(data['ERROR']) {
					$('.json_error').remove();
					$('.json_success').remove();
					$('#watchlist_list').prepend('<div class="json_error">'+data['ERROR']+'</div>');
				}
				$('.loader_icon').dialog('close');
			}
		);
	}
	
	this.ajaxAddArticleById = function(url, watchlist, article, resultId) {
		$('.loader_icon').dialog('open');
		$.getJSON(
			url,
			{
				'tx_commercewatchlists_pi1[action]': 'ajax,addArticle,showLists',
				'tx_commercewatchlists_pi1[watchlist]' : watchlist,
				'tx_commercewatchlists_pi1[article]' : article
			},
			function(data) {
			 	if(data['HTML']){
			 		$('.json_error').remove();
					$('.json_success').remove();
			 		$(resultId).prepend('<div class="json_success">'+data['SUCCESS']+'</div>');
				}
				if(data['ERROR']) {
					$('.json_error').remove();
					$('.json_success').remove();
					$(resultId).prepend('<div class="json_error">'+data['ERROR']+'</div>');
				}
				$('.loader_icon').dialog('close');
			}
		);
	}
	
	this.ajaxSubmit = function(form, callback) {
		formData = $.makeArray(form);
		while(res = formData.pop()) {
			if(!(res.type=="button")) {
				formData.push(res);
				break;
			}
		}
		$.getJSON(
			form.action,
			formData,
			callback
		);
	}
	
	this.ajaxAddWatchlistLock=false;
	this.ajaxAddWatchlist = function (form) {
		if(!this.ajaxAddWatchlistLock) {
			this.ajaxAddWatchlistLock=true;
			$('#new_watchlist_dialog').dialog('disable');
			$('.loader_icon').dialog('open');
			this.ajaxSubmit(form, function (data) {
				$('#new_watchlist_dialog_error').remove();
				if(data['HTML']) {
					$('#watchlist_list').replaceWith(data['HTML']);
					$('#watchlist_list').prepend('<div class="json_success">'+data['SUCCESS']+'</div>');
					$('#new_watchlist_dialog').dialog('close');
					form.reset();
				} else {
					$('.json_error').remove();
					$('.json_success').remove();
					$('#new_watchlist_dialog').prepend('<div class="json_error">'+data['ERROR']+'</div>');
				}
				commercewatchlist.ajaxAddWatchlistLock=false;
				$('#new_watchlist_dialog').dialog('enable');
				$('.loader_icon').dialog('close');
			});
		}
	}
	
	this.ajaxEditWatchlist = function (form) {
		$('.loader_icon').dialog('open');
		this.ajaxSubmit(form, function (data) {
			if(data['HTML']) {
				$('#watchlist_single').replaceWith(data['HTML']);
				$('#watchlist_single').prepend('<div class="json_success">'+data['SUCCESS']+'</div>');
			} else {
				$('.json_error').remove();
				$('.json_success').remove();
				$('#watchlist_single').prepend('<div class="json_error">'+data['ERROR']+'</div>');
			}
			$('.loader_icon').dialog('close');
		});
	}
	
	this.ajaxRemoveWatchlist = function(url, id) {
		$('.loader_icon').dialog('open');
		$.getJSON(
				url,
				{
					'tx_commercewatchlists_pi1[action]': 'ajax,removeWatchlist,showWatchlists',
					'tx_commercewatchlists_pi1[watchlist]' : id
				},
				function(data) {
					if(data['HTML']){
						$('#watchlist_list').replaceWith(data['HTML']);
						$('#watchlist_list').prepend('<div class="json_success">'+data['SUCCESS']+'</div>');
					} else {
						$('.json_error').remove();
						$('.json_success').remove();
						$('#watchlist_list').prepend('<div class="json_error">'+data['ERROR']+'</div>');
					}
					$('.loader_icon').dialog('close');
				}
		);
	}
}

$(document).ready(function() {
	$('#new_watchlist_dialog').dialog({autoOpen:false, modal:true});
	$('.loader_icon').dialog({autoOpen:false, modal:true, resizable:false, title:'Bitte warten'});
	
	if($('#watchlist_list').length!=0) {
//		$('.loader_icon').dialog('open');
		$.getJSON(
			window.location.href,
			{'tx_commercewatchlists_pi1[action]': 'ajax,showLists'},
			function(data) {
				if(data['HTML']){
					$('#watchlist_list').replaceWith(data['HTML']);
					$('#watchlist_list').prepend('<div class="json_success">'+data['SUCCESS']+'</div>');
				} else {
					$('.json_error').remove();
					$('.json_success').remove();
					$('#watchlist_list').prepend('<div class="json_error">'+data['ERROR']+'</div>');
				}
				$('.loader_icon').dialog('close');
			}
		);
	}
	if($('#watchlist_single').length!=0) {
//		$('.loader_icon').dialog('open');
		$.getJSON(
				window.location.href,
				{'tx_commercewatchlists_pi1[action]': 'ajax,showSingle'},
				function(data) {
					if(data['HTML']){
						$('#watchlist_single').replaceWith(data['HTML']);
						$('#watchlist_single').prepend('<div class="json_success">'+data['SUCCESS']+'</div>');
					} else {
						$('.json_error').remove();
						$('.json_success').remove();
						$('#watchlist_single').prepend('<div class="json_error">'+data['ERROR']+'</div>');
					}
					$('.loader_icon').dialog('close');
				}
			);
	}
});

var commercewatchlist = new commercewatchlist_class();

