jQuery.fn.countdown = function (date, settings) {
	var countdown_settings = {
		lang: {
			years:   ['год', 'года', 'лет'],
			months:  ['месяц', 'месяца', 'месяцев'],
			days:    ['день', 'дня', 'дней'],
			hours:   ['час', 'часа', 'часов'],
			minutes: ['минута', 'минуты', 'минут'],
			seconds: ['секунда', 'секунды', 'секунд'],
			plurar:  function(number) {
				cases = [2, 0, 1, 1, 1, 2];
				return ((number%100>4 && number%100<20)? 2 : cases[(number%10<5)?number%10:5]);
			}
		},
		prefix: "Осталось: ",
		finish: "Всё"
	};
	countdown_settings = jQuery.extend(countdown_settings, settings);

	var timeDifference = function(begin, end) {
	    if (end < begin) {
		    return false;
	    }
	    var
	    days   = end.getDate()  - begin.getDate(),
	    months = end.getMonth() - begin.getMonth(),
	    years  = end.getYear()  - begin.getYear(),
	    hms    = (end / 1000 - begin / 1000) % 86400,
	    seconds = Math.floor(hms % 60),
	    minutes = Math.floor(hms/60) % 60,
	    hours   = Math.floor(hms/3600) % 60,
	    date = new Date();
	    if (days < 0) {
	    	date.setFullYear(begin.getYear(), begin.getMonth(), 32);
	    	days += 31 - date.getDate();
	    	months--;
	    }
	    if (months < 0) {
	    	months += 12;
	    	years--;
	    }
	    years1 = years;
	    months1 = months;
	    days1 = days;
	    hours1 = hours;
	    minutes1 = minutes;
	    seconds1 = seconds;
	    if( years > 0 )
	    {

	    	years = '<div class="timer_numbers"  id="timer_days" >'+years+'</div>'	    }
	    if( months >= 10 )
	    {
	    	months = '<div class="timer_numbers"  id="timer_days" >'+months+'</div>'
	    }
	    if( months < 10 &&  months > 0 )
	    {
	    	months = '<div class="timer_numbers"  id="timer_days" >0'+months+'</div>'
	    }
	    if( days >= 10 )
	    {
	    	days = '<div class="timer_numbers"  id="timer_days" >'+days+'</div>'
	    }
	    if( days < 10 && days > 0 )
	    {
	    	days = '<div class="timer_numbers"  id="timer_days" >0'+days+'</div>'
	    }
	    if( hours >= 10 )
	    {
	    	hours = '<div class="timer_numbers"  id="timer_days" >'+hours+'</div>'
	    }
	    if( hours < 10 && hours >= 0 )
	    {
	    	hours = '<div class="timer_numbers"  id="timer_days" >0'+hours+'</div>'
	    }
	    if( minutes >= 10 )
	    {
	    	minutes = '<div class="timer_numbers"  id="timer_days" >'+minutes+'</div>'
	    }
	    if( minutes < 10 &&  minutes >= 0 )
	    {
	    	minutes = '<div class="timer_numbers"  id="timer_days" >0'+minutes+'</div>'
	    }
	    if( seconds >= 10 )
	    {
	    	seconds = '<div class="timer_numbers"  id="timer_seconds" >'+seconds+'</div>'
	    }
	    if( seconds < 10 &&  seconds > 0 )
	    {
	    	seconds = '<div class="timer_numbers"  id="timer_seconds" >0'+seconds+'</div>'
	    }
	    if( seconds == 0 )
	    {
	    	seconds = '<div class="timer_numbers"  id="timer_seconds" >00</div>'
	    }

	    var diff = {years: years, months: months, days: days, hours: hours, minutes: minutes, seconds: seconds,years1: years1,months1: months1,days1: days1,hours1: hours1,minutes1: minutes1,seconds1: seconds1};
	    var result = new Array();
	    for (i in diff) {
	    	if(!diff[i]) continue;
		    result.push(diff[i] + ' <div class="timer_lab">' + countdown_settings.lang[i][countdown_settings.lang.plurar(diff[i+'1'])]+'</div>');
		    if( i == 'seconds' )
		    	break;
	    }

	    return result.join(' ');
	};
	var elem = $(this);
	var timeUpdate = function () {
	    var s = timeDifference(new Date(), date);
	    if (s.length) {
	    	elem.html('<div class="timer_lab">'+countdown_settings.prefix+'</div><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>'+ s);
	    } else {
	        clearInterval(timer);
	        elem.html(countdown_settings.finish);
	    }
	};
	timeUpdate();
	var timer = setInterval(timeUpdate, 1000);
};

