//
//  Intelligent Fading LCD Countdown Timer
//  Copyright 2011 GoLocal.biz LLC. All Rights Reserved.
//

var update_ms = 66;

var timer_box = null;
var end_date = null;
var adjust_ms = null;
var timer_format = null;

var last_html = "";

var num_timers = 0;

function lcd_digits(number, opacity)
{
	number = number.toString();
	var str = "";
	for (var c = 0; c < number.length; c++)
	{
		var digit = number.substr(c, 1);
		if (digit == ":") digit = "sep";
		str += "<img src=\"/deal/images/lcd/lcd" + digit + ".png\" width=\"9\" height=\"17\"";
		str += " style=\"margin: 0px 1px; opacity: " + opacity + "; filter:alpha(opacity=" + Math.floor(opacity * 100) + ");\" align=\"absbottom\"/>";
	}
	return str;
}

function lcd_pad(number, length)
{
	var str = number.toString();
	while (str.length < length) str = '0' + str;
	return str;
}

function LCD_Countdown_Init(_box_id, _timer_format, _start_epoch, _end_epoch)
{
	//  Initialize Counter
	
	timer_box = document.getElementById(_box_id);
	timer_format = _timer_format;
	var start_date = new Date(_start_epoch * 1000);
	end_date = new Date(_end_epoch * 1000);
	adjust_ms = start_date.getTime() - new Date().getTime();

	if (num_timers == 0)
	{
		setTimeout("LCD_Countdown_Update()", update_ms);
		num_timers++;
	}
}

function LCD_Countdown_Update()
{
	//  Get Time Remaining
	
	var current_date = new Date();
	var delta_ms = end_date.getTime() - (current_date.getTime() + adjust_ms);
	delete current_date;
	
	if (delta_ms <= 0)
	{
		timer_box.innerHTML = "This deal has ended.";
		return;
	}

	//  Separate Fields
	
	var calc_ms = delta_ms;

	var days = Math.floor(calc_ms / 86400000); //days
	calc_ms = calc_ms % 86400000;
	
	var hours = Math.floor(calc_ms / 3600000); //hours
	calc_ms = calc_ms % 3600000;
	
	var minutes = Math.floor(calc_ms / 60000); //minutes
	calc_ms = calc_ms % 60000;

	var seconds = Math.floor(calc_ms / 1000); //minutes
	calc_ms = calc_ms % 1000;
	
	var milliseconds = calc_ms;
	
	//  Adjust Opacity
	
	var opacity = 1;
	if (milliseconds < 200) opacity = 0.1 * Math.floor(milliseconds / 20);
	//timer_box.style.opacity = opacity;
	//timer_box.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + Math.floor(opacity * 100) + ")";

	var minutes_opacity = (seconds == 0);
	var hours_opacity = (minutes_opacity && minutes == 0);
	var days_opacity = (hours_opacity && hours == 0);

	//  Create New HTML
	
	var new_html = "";

	if (timer_format == "standard")
	{
		new_html = "Deal ends in&nbsp; ";
		if (days > 0)
			new_html += lcd_digits(days, days_opacity ? opacity : 1) + " &nbsp;day" + (days == 1 ? "s" : "") + "&nbsp; ";
		new_html += lcd_digits(lcd_pad(hours, 2), hours_opacity ? opacity : 1) + " &nbsp;hours&nbsp; ";
		new_html += lcd_digits(lcd_pad(minutes, 2), minutes_opacity ? opacity : 1) + " &nbsp;minutes&nbsp; ";
		new_html += lcd_digits(lcd_pad(seconds, 2), opacity) + " &nbsp;seconds";
		//new_html += " " + lcd_digits(lcd_pad(milliseconds, 3), opacity) + " milliseconds";
	}
	else if (timer_format == "compact")
	{
		new_html = "";
		if (days > 0)
			new_html += lcd_digits(days, days_opacity ? opacity : 1) + " &nbsp;day" + (days > 1 ? "s" : "") + "&nbsp; ";
		new_html += lcd_digits(lcd_pad(hours, 2), hours_opacity ? opacity : 1) + " &nbsp;hr&nbsp; ";
		new_html += lcd_digits(lcd_pad(minutes, 2), minutes_opacity ? opacity : 1) + " &nbsp;min&nbsp; ";
		new_html += lcd_digits(lcd_pad(seconds, 2), opacity) + " &nbsp;sec";
		//new_html += " " + lcd_digits(lcd_pad(milliseconds, 3), opacity) + " &nbsp;ms&nbsp;";
	}
	else if (timer_format == "clock")
	{
		new_html = "";
		if (days > 0)
			new_html += lcd_digits(days, days_opacity ? opacity : 1) + lcd_digits(":", opacity);
		new_html += lcd_digits(lcd_pad(hours, 2), hours_opacity ? opacity : 1);
		new_html += lcd_digits(":", opacity);
		new_html += lcd_digits(lcd_pad(minutes, 2), minutes_opacity ? opacity : 1);
		new_html += lcd_digits(":", opacity);
		new_html += lcd_digits(lcd_pad(seconds, 2), opacity);
		//new_html += lcd_digits(":", opacity);
		//new_html += " " + lcd_digits(lcd_pad(milliseconds, 3), opacity) + " &nbsp;ms&nbsp;";
	}
	
	//  Modify HTML only if changed
	
	if (new_html != last_html) timer_box.innerHTML = last_html = new_html;
	
	num_timers--;

	if (num_timers == 0)
	{
		setTimeout("LCD_Countdown_Update()", update_ms);
		num_timers++;
	}
}

