var is_loding = false;

function statusRefreshEffect(element)
{
	var background = element.css('background-color');
	
	element.css({'transition': 'background-color 0.5s', 'background-color': 'rgba(243, 255, 164, 1)'});
	setTimeout(function()
	{
		element.css({'background-color': background});
	}, 800);
}

function showError()
{
	jQuery('.error-msg-refresh-bar').remove();
	jQuery('.refresh div:eq(0)').after('<div class="red error-msg-refresh-bar" style="vertical-align: bottom; font-weight: bold;">' + _t('Fehler!') + '</div>');
	jQuery('.refresh-bar').stop(false, true).css('width', 0);
	jQuery('a[href=#refresh] img').removeClass('rotate-icon');
	
	setTimeout('statusRefresh()', 3000);
}

function statusRefresh()
{
	jQuery('.error-msg-refresh-bar').remove();
	jQuery('.refresh-bar').animate({width: '100%'}, 30000, 'linear', function(e)
	{
		var this_ =			jQuery(this);
		var connection =	jQuery('.table tr:eq(1)');
		var uptime =		jQuery('.table tr:eq(2)');
		var publicIP =		jQuery('.table tr:eq(3)');
		var totalReceived =	jQuery('.table tr:eq(4)');
		var totalSent =		jQuery('.table tr:eq(5)');
		var bandwidthDown =	jQuery('.table tr:eq(6)');
		var bandwidthUp =	jQuery('.table tr:eq(7)');
		
		jQuery('a[href=#refresh] img').addClass('rotate-icon');
		
		this_.animate({width: '87.5%'}, 300, 'linear');
		jQuery.post('api/v1/plugins.php', { id: 'fritzbox', action: 'v1/status', data: 'connection' }, function(data)
		{
			if (connection.find('td:eq(1)').html() != data.data.connection)
			{
				statusRefreshEffect(connection);
				connection.find('td:eq(1)').html(data.data.connection);
			}
			
			this_.animate({width: '75%'}, 300, 'linear');
			jQuery.post('api/v1/plugins.php', { id: 'fritzbox', action: 'v1/status', data: 'uptime' }, function(data)
			{
				if (uptime.find('td:eq(1)').html() != data.data.uptime)
				{
					statusRefreshEffect(uptime);
					uptime.find('td:eq(1)').html(data.data.uptime);
				}
			
				this_.animate({width: '62.5%'}, 300, 'linear');
				jQuery.post('api/v1/plugins.php', { id: 'fritzbox', action: 'v1/status', data: 'publicIP' }, function(data)
				{
					if (publicIP.find('td:eq(1) a').text() != data.data.publicIP)
					{
						statusRefreshEffect(publicIP);
						publicIP.find('td:eq(1) a').text(data.data.publicIP).attr('href', 'http://'+data.data.publicIP);
					}
					
					this_.animate({width: '50%'}, 300, 'linear');
					jQuery.post('api/v1/plugins.php', { id: 'fritzbox', action: 'v1/status', data: 'totalReceived' }, function(data)
					{
						if (totalReceived.find('td:eq(1)').html() != data.data.totalReceived)
						{
							statusRefreshEffect(totalReceived);
							totalReceived.find('td:eq(1)').html(data.data.totalReceived);
						}
						
						this_.animate({width: '37.5%'}, 300, 'linear');
						jQuery.post('api/v1/plugins.php', { id: 'fritzbox', action: 'v1/status', data: 'totalSent' }, function(data)
						{
							if (totalSent.find('td:eq(1)').html() != data.data.totalSent)
							{
								statusRefreshEffect(totalSent);
								totalSent.find('td:eq(1)').html(data.data.totalSent);
							}
							
							this_.animate({width: '25%'}, 300, 'linear');
							jQuery.post('api/v1/plugins.php', { id: 'fritzbox', action: 'v1/status', data: 'bandwidthDown' }, function(data)
							{
								if (bandwidthDown.find('td:eq(1) .custom-progressbar').attr('data-text') != data.data.current + ' / ' + data.data.max)
								{
									statusRefreshEffect(bandwidthDown);
									bandwidthDown.find('td:eq(1) .custom-progressbar').attr('data-text', data.data.current + ' / ' + data.data.max).find('div').css('width', data.data.percentage);
								}
								
								this_.animate({width: '12.5%'}, 300, 'linear');
								jQuery.post('api/v1/plugins.php', { id: 'fritzbox', action: 'v1/status', data: 'bandwidthUp' }, function(data)
								{
									if (bandwidthUp.find('td:eq(1) .custom-progressbar').attr('data-text') != data.data.current + ' / ' + data.data.max)
									{
										statusRefreshEffect(bandwidthUp);
										bandwidthUp.find('td:eq(1) .custom-progressbar').attr('data-text', data.data.current + ' / ' + data.data.max).find('div').css('width', data.data.percentage);
									}
									
									this_.animate({width: '0%'}, 300, 'linear', function(e)
									{
										is_loding = false;
										jQuery('a[href=#refresh] img').removeClass('rotate-icon');
									});
									
									statusRefresh();
								}).fail(function(e) { showError(); });
							}).fail(function(e) { showError(); });
						}).fail(function(e) { showError(); });
					}).fail(function(e) { showError(); });
				}).fail(function(e) { showError(); });
			}).fail(function(e) { showError(); });
		}).fail(function(e) { showError(); });
	});
}

jQuery(document).on('click', 'a[href=#refresh]', function(e)
{
	if (is_loding == false)
		jQuery('.refresh-bar').stop(false, true);
	
	return false;
});

jQuery(document).ready(function(e)
{
	statusRefresh();
});