(function(window) {

window.auctionTimeCheck = {

	url: '',

	data: {},

	dataLength: 0,

	add: function(itemId, price, ending) {
		var order = this.dataLength + 1,
			auctionData = {};

		auctionData['auction(' + order + ')(itemId)'] = itemId;
		auctionData['auction(' + order + ')(price)'] = price;
		auctionData['auction(' + order + ')(ending)'] = ending;

		$.extend(this.data, auctionData);
		this.dataLength = order;
	},

	checkState: function() {
		$.ajax({
			url: window.auctionTimeCheck.url,
			dataType: 'json',
			data: window.auctionTimeCheck.data,
			cache: false,
			success: function(state) {
				if (state) {
					$.lightbox.setClass('');
					$.lightbox.container.attr('style', 'width:300px; text-align:center;');
					$.lightbox.show();
					$.lightbox.container.append('<div class="pt1 pb1"><h2>Ve sledované aukci došlo ke změně</h2><div><strong>Aktualizuji&hellip;</strong></div></div>');
					window.location.href = window.auctionTimeCheck.reloadUrl;
				}
				else {
					window.auctionTimeCheck.setStateCheck();
				}
			}
		});
	},

	setStateCheck: function() {
		if (this.dataLength) {
			setTimeout(this.checkState, 20000);
		}
	},

	init: function(url, reloadUrl) {
		this.url = url;

		this.reloadUrl = reloadUrl;

		this.setStateCheck();
	}

};

})(window);
