var AnalysisId = -1;

function StartAnalysisProgress(id) {
	$('#spider-progress').find(">p").each(function(x) {
		$(this).find(">span").each(function(i) {
			$(this).hide();
		});
	});

	AnalysisId = id;
	AnalysisProgress();
}

function AnalysisProgress() {
	$.getJSON(baseurl + 'ajax/progress/' + AnalysisId + '/', function (jsonObject) {
		if (jsonObject.finished == false) {
			setTimeout("AnalysisProgress();", 2000);
			if (jsonObject.progress == false) {
				$('#progressbar').hide();
				
				$('#spider-progress').find(">p").each(function(x) {
					$(this).find(">span").each(function(i) {
						$(this).hide();
						if (x == 1) {					
							if (jsonObject.queue == null && i == 0) {
								$(this).show();
							} else if (jsonObject.queue != null && jsonObject.queue == 0 && i == 2) {
								$('#queue').show();
								$(this).show();
							} else if (jsonObject.queue != null && jsonObject.queue > 0 && i == 1) {
								$('#queue').html(jsonObject.queue);
								$(this).show();
							}
						} else if(i == 0) {
							$(this).show();
						}
					});
				});

			} else if (jsonObject.progress == true) {			
				$('#progressbar').show();
				$('#bar').show();
			
				if (jsonObject.spider != null) {
					$('#spider-progress').find(">p").each(function(x) {
						$(this).find(">span").each(function(i) {
							$(this).hide();
							if (x == 1) {					
								if (jsonObject.spider.percentagedone == null && jsonObject.queue == null && i == 0) {
									$(this).show();
								} else if (jsonObject.spider.percentagedone != null && i == 3) {
									$(this).show();
								} else if (jsonObject.queue != null && jsonObject.queue == 0 && i == 2) {
									$(this).show();
								} else if (jsonObject.queue != null && jsonObject.queue > 0 && i == 1) {
									$('#queue').html(jsonObject.queue);
									$(this).show();
								}
							} else if(i == 1) {
								$(this).show();
							}
						});
					});
				
					$('#spider').html(jsonObject.spider.name);
					$('#cpu').html(jsonObject.spider.cpuload + "%");
				
					if (jsonObject.spider.url != null) {
						//Make sure URL alway's fits in div
						if (jsonObject.spider.url.length > 0) {
							if (jsonObject.spider.url.length > 74){
								$('#current-url').html('<a href="' + jsonObject.spider.url + '" target="_blank">' + jsonObject.spider.url.substring(0, 74) + ' ...</a>');
							} else {
								$('#current-url').html('<a href="' + jsonObject.spider.url + '" target="_blank">' + jsonObject.spider.url + '</a>');
							}
						}
					}
			
					if (jsonObject.spider.percentagedone != null) {
						$('#external-links').html(jsonObject.spider.externallinks);
						$('#time-total').html(jsonObject.spider.remainingtime);
						$('#time-page').html(jsonObject.spider.pageloadtime);
						$('#time-elapsed').html(jsonObject.spider.elapsedtime);
						$('#progress').html(jsonObject.spider.percentagedone + '%');
						$("#bar").width(jsonObject.spider.percentagedone+'%');
						$('#pages-analyzed').html(jsonObject.spider.pagesdone);
						$('#pages-sitemap').html(jsonObject.spider.pagesfound);
						$('#pages-found').html(jsonObject.spider.pagesfound);
						$('#seo').html(jsonObject.spider.seoscore + "%");
					}
				} else {
					
				}
			}
		} else {
			setTimeout("redirectToResults()", 5000);
		}
	});
}

function redirectToResults(){
	window.location = window.location;
}
