var clock =
{
	images: new Array (12),
	ranges: null,
	timeoffset: 0,
	oneday: 1000*60*60*24,
	onehour: 1000*60*60,
	oneminute: 1000*60,
	onesecond: 1000,
	colormap: [ {color: 'G', size: 'm'}, {color: 'Y', size: 'm'}, {color: 'R', size: 'm'} ],
	rangeIndex: -1,
	zerohour: null,
	servertime: null,

	initialize: function (info)
	{
		if (info)
		{
			this.ranges = info.CountdownRanges;
			this.zerohour = new Date (info.ZeroHour).getTime ();
			this.servertime = new Date (info.ServerTime).getTime ();
			this.timeoffset = this.servertime - new Date ().getTime ();
			this.enabled = true;
		}
	},
	
	set_clock_msg: function (msg, index)
	{
		if (this.enabled)
		{
			this.ranges[index].Msg = msg;
		}
	},
	
	run: function ()
	{
		if (this.enabled)
		{
			this.tick ();
		}
	},
	
	calc_range_index: function ()
	{
		var clientTime = (new Date ()).getTime() + this.timeoffset;;
		
		for (var i = 0; i < this.ranges.length; i++)
		{
			if (clientTime >= this.ranges[i].StartTime && clientTime <= this.ranges[i].EndTime)
			{
				return i;
			}
		}
		
		return 0;
	},
	
	load_images: function (color, size)
	{
		this.images[0] = new Image (28, 50);
		this.images[0].src = "skins/Skin_1/images/clock/0_" + color + size + ".gif";

		this.images[1] = new Image (28, 50);
		this.images[1].src = "skins/Skin_1/images/clock/1_" + color + size + ".gif";

		this.images[2] = new Image (28, 50);
		this.images[2].src = "skins/Skin_1/images/clock/2_" + color + size + ".gif";

		this.images[3] = new Image (28, 50);
		this.images[3].src = "skins/Skin_1/images/clock/3_" + color + size + ".gif";

		this.images[4] = new Image (28, 50);
		this.images[4].src = "skins/Skin_1/images/clock/4_" + color + size + ".gif";

		this.images[5] = new Image (28, 50);
		this.images[5].src = "skins/Skin_1/images/clock/5_" + color + size + ".gif";

		this.images[6] = new Image (28, 50);
		this.images[6].src = "skins/Skin_1/images/clock/6_" + color + size + ".gif";

		this.images[7] = new Image (28, 50);
		this.images[7].src = "skins/Skin_1/images/clock/7_" + color + size + ".gif";

		this.images[8] = new Image (28, 50);
		this.images[8].src = "skins/Skin_1/images/clock/8_" + color + size + ".gif";

		this.images[9] = new Image (28, 50);
		this.images[9].src = "skins/Skin_1/images/clock/9_" + color + size + ".gif";

		this.images[10] = new Image (8, 50);
		this.images[10].src = "skins/Skin_1/images/clock/c_" + color + size + ".gif";
		
		this.images[11] = new Image (8, 50);
		this.images[11].src = "skins/Skin_1/images/clock/b_" + color + size + ".gif";
	},

	tick: function ()
	{
		with (document)
		{
			var index = this.calc_range_index ();
			if (this.rangeIndex != index)
			{
				if (this.rangeIndex != -1)
				{
					if (typeof (clock_upate) == 'function')
					{
						if (index == 0)
						{
							// Today shipping is now open
							setTimeout ("clock_upate (false);", 5000);
						}
						else if (index == 2)
						{
							// Today shipping is now closed
							setTimeout ("clock_upate (true);", 5000);
						}
					}
				}

				this.rangeIndex = index;
				colorIndex = this.ranges[index].Color;
				this.load_images (this.colormap[colorIndex].color, this.colormap[colorIndex].size);
				
				clockMsg = document.getElementById ('clockmsg');
				clockMsg.innerHTML = this.ranges[index].Msg;
			}
			
			var date = new Date ();
			var base = new Date (0);
			var hour = 0;
			var minute = 0;
			var second  = 0;

			var diff = this.zerohour - (date.getTime () + this.timeoffset);
			if (diff <= 0)
			{
				hour = 0;
				minute = 0;
				second = 0;
			}
			else
			{
				diff = Math.abs (diff);
				hour = Math.floor (diff / this.onehour);
				minute = Math.floor ((diff - (hour * this.onehour)) / this.oneminute);
				second = Math.floor ((diff - (hour * this.onehour) - (minute * this.oneminute)) / this.onesecond);
			}
			
			hour = hour < 10 ? "0" + hour : "" + hour;
			minute = minute < 10 ? "0" + minute : "" + minute;
			second = second < 10 ? "0" + second : "" + second;

			var hr1 = eval (hour.charAt (0));
			var hr2 = eval (hour.charAt (1));
			var min1 = eval (minute.charAt (0));
			var min2 = eval (minute.charAt (1));
			var sec1 = eval (second.charAt (0));
			var sec2 = eval (second.charAt (1));

			document.images['h1'].src = this.images[hr1].src;
			document.images['h2'].src = this.images[hr2].src;

			document.images['b1'].src = this.images[10].src;

			document.images['m1'].src = this.images[min1].src;
			document.images['m2'].src = this.images[min2].src;
			
			document.images['b2'].src = this.images[10].src;
			
			document.images['s1'].src = this.images[sec1].src;
			document.images['s2'].src = this.images[sec2].src;
		}

		setTimeout ("clock.tick ();", 1000);
	}
}