	/*
	* Author: Alex Baskov, 2010
	* NOTE: make sure you include this script *after* jquery.js
	*/

	var cdToday = new Date(); // is used only as REAL CURRENT date
	var activeCountDown = false;
	var countDown = null;
	var globalIndex = 0;

	$(document).ready(function() {

		var startCountDown = showCounter || false;

		if (startCountDown)
		{
			// defining the startup parameters for countdown
			var cdTomorrow = new Date();
			cdTomorrow.setHours(0);
			cdTomorrow.setMinutes(0);
			cdTomorrow.setSeconds(0);
			cdTomorrow.setDate(cdTomorrow.getDate()+1);
			var intervalSeconds = Math.floor(getTimeInterval(cdTomorrow, cdToday) / 1000);
			var h1 = h2 = m1 = m2 = s1 = s2 = 0;

			if (intervalSeconds > 3600)
			{
				// more than 1 hour...
				var hours = Math.floor(intervalSeconds / 3600);
				h1 = Math.floor(hours / 10);
				h2 = hours - (h1 * 10);

				var mins = Math.floor((intervalSeconds - (hours * 3600)) / 60);
				m1 = Math.floor(mins / 10);
				m2 = mins - (m1 * 10);

				var secs = Math.floor(intervalSeconds - (hours * 3600) - (mins * 60));
				s1 = Math.floor(secs / 10);
				s2 = secs - (s1 * 10);
			}
			else if (intervalSeconds > 60)
			{
				// more than 1 minute...
				var mins = Math.floor(intervalSeconds / 60);
				m1 = Math.floor(mins / 10);
				m2 = mins - (m1 * 10);

				var secs = Math.floor(intervalSeconds - (mins * 60));
				s1 = Math.floor(secs / 10);
				s2 = secs - (s1 * 10);
			}
			else
			{
				// less than 1 minute
				var secs = intervalSeconds;
				s1 = Math.floor(secs / 10);
				s2 = secs - (s1 * 10);
			}

			$("#timer_h1").html(h1);
			$("#timer_h2").html(h2);
			$("#timer_m1").html(m1);
			$("#timer_m2").html(m2);
			$("#timer_s1").html(s1);
			$("#timer_s2").html(s2);

			initCountDown();
		}
	});



	////
	// starts the countdown process...
	//
	function initCountDown()
	{
		activeCountDown = true;
		doCountDown();

		return true;
	} // /setActiveDate



	////
	// does the countdown
	//
	function doCountDown()
	{
		var stillDo = activeCountDown || false;

		if ($("#timer_s2").html() >= 0 && $("#timer_s2").html() >= 0)
		{

			$("#timer_s2").html(parseInt($("#timer_s2").html()) - 1);
			if ($("#timer_s2").html() == -1)
			{
				$("#timer_s2").html("9");
				$("#timer_s1").html(parseInt($("#timer_s1").html()) - 1);
			}

			if ($("#timer_s1").html() == -1)
			{
				$("#timer_s1").html("5");
				$("#timer_m2").html(parseInt($("#timer_m2").html()) - 1);
			}

			if ($("#timer_m2").html() == -1)
			{
				$("#timer_m2").html("9");
				$("#timer_m1").html(parseInt($("#timer_m1").html()) - 1);
			}

			if ($("#timer_m1").html() == -1)
			{
				$("#timer_m1").html("5");
				$("#timer_h2").html(parseInt($("#timer_h2").html()) - 1);
			}

			if ($("#timer_h2").html() == -1)
			{
				$("#timer_h2").html("9");
				$("#timer_h1").html(parseInt($("#timer_h1").html()) - 1);
			}

			if ($("#timer_h1").html() == -1)
			{
				stillDo = false;
			}

		}
		else
		{
			stillDo = false;
		}

		if (stillDo)
		{
			countDown = setTimeout(doCountDown, 1000);
		}
		else
		{
			$("#main_mid_head").html('<div id="main_mid_head_wide">Цінова пропозиція була дійсна <b>вчора</b>. <a href="/">Переглянути сьогонішню пропозицію</a>.</div>');
			$("#main_bot").html('<div id="product_sold_outer"><div class="product_unavailable">Цей товар продавався вчора</div></div><div class="c"></div>');
			clearTimeout(countDown);
			countDown = null;
		}

		return true;
	} // /doCountDown



	function getTimeInterval(date1, date2)
	{
		return date1.getTime()-date2.getTime();
	}
