$(document).ready(function() {
	/* ============================ do in progress */

	var inProgress = $('body').is('.in_progress');
	if (inProgress) {
		window.UpdatePhotos = {
			init : function() {
				this.checkCounters();
				setTimeout(this.checkUnprocessed, 20000);
				return this;
			},
			userOpenedProgressWindow : false,
			timerTemplate : '<li class="last"><strong id="timer" title="Processing can take up to 30 minutes � please be patient." href="#"></strong></li>',
			checkCounters : function() {
				ModalUtils.getAjaxJson('/get-counters/', function(data) {
					var stats = data.userStatistics;
					if (stats.numberOfUserOwnPhotos == 0 && stats.numberOfUnprocessedPhotos == 0) {
						var inProgress = $('body').is('.in_progress');
						if ($('#modal-data #modal-please-wait').length == 0 && !UpdatePhotos.userOpenedProgressWindow) {
							ModalUtils.showModalPleaseWait();
							UpdatePhotos.userOpenedProgressWindow = true;
						}
						setTimeout(UpdatePhotos.checkCounters, 10000);
					} else {
						if ($('#modal-data #modal-please-wait').length > 0) {
							ModalUtils.deleteModal();
							$('#overview_navigation li:last').after(UpdatePhotos.timerTemplate);
							UpdatePhotos.updateCounters(stats);
							UpdatePhotos.checkUnprocessed(true);
							setTimeout(UpdatePhotos.checkCounters, 20000);
						} else {
							if (stats.numberOfUnprocessedPhotos == 0) {
								$('#timer').remove();
								if ($.find('#photos #message').length == 0) {
									window.location.href = window.location.href.replace('inProgress=true', '');
								}
							} else {
								/*
								 * var processed = stats.numberOfProcessedPhotos /
								 * stats.numberOfUserOwnPhotos;
								 */
								if (stats.numberOfProcessedPhotos == stats.numberOfUserOwnPhotos && $.find('#photos #message').length == 0 && stats.numberOfAlphaUnnamedPeople > 0) {
									window.location.reload();

								} else {
									UpdatePhotos.updateCounters(stats);
									setTimeout(UpdatePhotos.checkCounters, 20000);
								}
							}
						}
					}
				});
			},
			updateCounters : function(stats) {
				$('#timer').text('Processed ' + stats.numberOfProcessedPhotos + ' of ' + stats.numberOfUserOwnPhotos + ' photos');
			},

			checkUnprocessed : function(forceUpdate) {
				var unprocessed = $(".photo_container.processing");
				if (unprocessed.length > 0 || forceUpdate) {
					UpdatePhotos.updateUnprocessedImages();
					setTimeout(UpdatePhotos.checkUnprocessed, 20000);
				}
			},

			updateUnprocessedImages : function() {
				$.ajax( {
					url : document.location.href,
					cache : false,
					dataType : 'html',
					success : function(data, status) {
						var currentUnprocessed = $(document).find(".photo_container.processing").length;
						var newUnprocessed = $(data).find(".photo_container.processing").length;
						if (currentUnprocessed != newUnprocessed) {
							if (currentUnprocessed == 0) {
								$(document).find("#content").replaceWith($(data).find("#content"));
								Sets.attachSourcesHandlers('#content .photo_wrapper .scroller');
								Sets.preloadImages();
							} else {
								var setsInProcessing = $(document).find(".photo_container.processing");
								setsInProcessing.each(function() {
									var set = this;
									var id = $(set).attr('id');
									if (!$(data).find("#" + id).hasClass('processing')) {
										$(document).find("#" + id).replaceWith($(data).find("#" + id));
										eval($(data).find('#content script').text());
										Sets.attachSourcesHandlers("#" + id + ' .photo_wrapper .scroller');
									}
								})
								Sets.preloadImages();
							}
							var pagination = $(document).find(".pagination");
							if (pagination.length == 1) {
								pagination.replaceWith($(data).find(".pagination"));
							} else {
								$(document).find("#content").after($(data).find(".pagination"));
								GeneralUtils.setThumbnailsPositions();
							}
						}
					}
				});
			}
		}.init();
	}
});