﻿var lag_itemslisting_countdowns_root,
	lag_itemslisting_countdowns = null,
	lag_itemslisting_countdowns_timer = null,
	lag_itemslisting_countdowns_timing = 0,
	lag_itemslisting_countdowns_timertime = 10, // default time in seconds to refresh timers (will reduce to 1 sec once one item's timer is close to running out)
	lag_itemslisting_countdowns_refreshfreq = 10, // how many timer refreshes (timertime) need to happen before an ajax check is done to ensure the time is still accurate
	lag_itemslisting_countdowns_refreshcount = 0;

function initCountdowns(rooturl)
{
	if (rooturl) lag_itemslisting_countdowns_root = rooturl;

	lag_itemslisting_countdowns = $('.datetime.countdown');

	if (lag_itemslisting_countdowns.length > 0)
	{
		lag_itemslisting_countdowns.each(function(){ this.countdowntime = $(this).attr('data-countdowntime'); });
		
		lag_itemslisting_countdowns_timing = Math.floor(new Date().getTime() / 1000);

		updateCountdowns();
	}
}
function updateCountdowns()
{
	var secs = Math.floor(new Date().getTime() / 1000) - lag_itemslisting_countdowns_timing;
	lag_itemslisting_countdowns_timing += secs;

	var mintime = 0;
	lag_itemslisting_countdowns.each(function()
	{
		if (this.countdowntime > 1)
		{
			this.countdowntime -= secs;
			$(this).html(formatCountdown(this.countdowntime));
			if (mintime == 0 || (this.countdowntime > 0 && this.countdowntime < mintime)) mintime = this.countdowntime;
		}
		else if (this.countdowntime != null)
		{
			this.countdowntime = null;
			$(this).html('').removeAttr('countdown');

			var itemid = $(this).attr('id').match(/\d+/);
			$('#i' + itemid + ' .placebid a').html('Bidding Closed').removeAttr('href');
			$('#i' + itemid + ' .placebid').addClass('bidding_finished');
			$('#i' + itemid + ' .linkinfo.bidinfo').html('Bidding Has Concluded');
		}
	});

	if (mintime > 0)
	{
		if (mintime < 180) lag_itemslisting_countdowns_timertime = 1;

		if (lag_itemslisting_countdowns_refreshcount >= lag_itemslisting_countdowns_refreshfreq)
		{
			lag_itemslisting_countdowns_refreshcount = 0;
			refreshCountdown();
		}
		else
		{
			lag_itemslisting_countdowns_refreshcount++;
			lag_itemslisting_countdowns_timer = setTimeout('updateCountdowns()', lag_itemslisting_countdowns_timertime * 1000);
		}
	}
}

function formatCountdown(tsec)
{
	if (tsec <= 0)
		return 'Ended';

	var d = Math.floor(tsec / 86400);
	tsec -= d * 86400;
	var h = Math.floor(tsec / 3600);
	tsec -= h * 3600;
	var m = Math.floor(tsec / 60);
	tsec -= m * 60;
	var s = tsec;

	if (d >= 1)
		return 'Ending In: ' + d + 'd ' + h + 'h ' + m + 'm';
	else if (h >= 1)
		return 'Ending In: ' + h + 'h ' + m + 'm';
	else if (m >= 2)
		return 'Ending In: ' + m + 'm';
	else if (m > 0)
		return 'Ending In: ' + m + 'm ' + s + 's';
	else
		return 'Ending In: ' + s + 's';
}

function refreshCountdown()
{
	var arrayItemIDs = [];
	$('.datetime.countdown').each(function(index) { arrayItemIDs[index] = $(this).attr('id').match(/\d+/); });

	var ids = "";
	for (var i=0; i < arrayItemIDs.length; ++i)
	{
		// do an api request with ids each time we get
		// close to reaching a string length of 1000,
		// we do this to avoid making our url too long
		// which could cause issues with some browsers
		if (ids.length + arrayItemIDs[i].length > 1000)
		{
			apiItemRequest(ids);
			ids = "";
		}

		ids += arrayItemIDs[i] + ",";
	}
	
	if (ids.length > 0) { apiItemRequest(ids); }
}

function apiItemRequest(ids)
{
	// trim off commas and space characters at beginning or end of ids string
	ids = ids.replace(/(^[,\s]+)|([,\s]+$)/g, "");

	$.getJSON(
		lag_itemslisting_countdowns_root + "handlers/controls/ItemsListing.ashx?i=" + ids + "&callback=?",
		function(data)
		{
			var item;
			// loop through each item returned by the api call
			for (var i=0; i < data.item.length; ++i)
			{
				item = data.item[i]; // get the JSON structured data for the item
				item.currentbid.amount = item.currentbid.amount|0; // if amount isn' set then set it to 0

				// set amount value into appropriate element
				$("#itime" + item.id).attr("data-countdowntime", item.countdown);
				if (item.currentbid.amount > 0) $("#i" + item.id + " .linkinfo.bidinfo").html("Current Bid: " + item.currentbid.amount.toFixed(2));
			}
			
			initCountdowns();
		});
}
