$(document).ready(function (){

	start_timer();

});
var cnt_days;
var cnt_hours;
var cnt_minutes;
var cnt_seconds;
var create_obj = false;
function setBanner()
{
	$(".7_yearth").each(function(){$(this).html(inc_container);});
	
	$.include({ "/site/jslibs/timer/css/main.css":function(data){}} );
	$.include({ "/site/jslibs/timer/inc/little.html":function(data){$(".7_yearth2").each(function(){$(this).html(data);});}} );
	$.include({ "/site/jslibs/timer/inc/big.html":function(data){$(".timer_inc").each(function(){$(this).html(data);});Run();}} );
}

function Run()
{
	$('#countdown_dashboard').countDown({
					targetOffset: {
						'day': 		cnt_days,
						'month': 	0,
						'year': 	0,
						'hour': 	cnt_hours,
						'min': 		cnt_minutes,
						'sec': 		cnt_seconds
					}
		});
}

function start_timer()
{
	
	$.ajax({
    	url: '/view/bonus_timer.php?' + (start_date!==null?"start="+start_date:"") + (finish_date!==null?"&finish="+finish_date:"") + (restart_period!==null?"&r="+restart_period:""),
        type : "GET",
        success: function (data, textStatus) {
        	
        	var data = processResponse(data);
       		time_to = new Date(data.time_to + " GMT" + GMT).getTime();
       		
			diff_time = (Math.round(data.time * 1000)) - ((new Date()).getTime());
			
			if (data.time_from)
			{
				time_from = new Date(data.time_from + " GMT" + GMT).getTime();
				if ((data.time * 1000) < time_from) return;
			}
			time_current = (new Date()).getTime() + diff_time;
			if (time_to > time_current)
			{
					if (window.location.pathname == '/view/contextual_links_packages' || window.location.pathname == '/view/contextual_links_packages2')
					{
						setBanner();
						setTimeCount();				
					} else {
					    $(".7_yearth").remove();
                        $(".7_yearth2").remove();
					}
			} else
            {
                $(".7_yearth").remove();
                $(".7_yearth2").remove();
            }
		}
 	});

}

function setTimeCount() {

	time_current = (new Date()).getTime() + diff_time;
	time_shift = Math.round((time_to-time_current)/1000);
	
	if (time_shift<0) {
		time_shift = 0;
	}

	if (time_to <= time_current)
	{
		$(":div [class=7_yearth]").html("");
		$(":div [class=7_yearth2]").html("");
		if (restart_period !== null)
		{
			start_timer();
		}
		return;
	}
	cnt_seconds = time_shift; 
	cnt_days = Math.floor(cnt_seconds/(24*60*60));  
	cnt_seconds -= cnt_days*60*60*24;
	cnt_hours = Math.floor(cnt_seconds /(60*60));
	cnt_seconds -= cnt_hours*60*60;	
	cnt_minutes = Math.floor(cnt_seconds/60); 
	cnt_seconds -= cnt_minutes*60;  
	
	// output templates
	var str_days = getTime(day_format, cnt_days, 'day');
	var str_hours = getTime(hour_format, cnt_hours, 'hour'); 
	var str_minutes = getTime(minute_format, cnt_minutes, 'minute'); 
	var str_seconds = getTime(second_format, cnt_seconds, 'second'); 
	
	var str_days2 = getTime(day_format2, cnt_days, 'day');
	var str_hours2 = getTime(hour_format2, cnt_hours, 'hour'); 
	var str_minutes2 = getTime(minute_format2, cnt_minutes, 'minute'); 
	var str_seconds2 = getTime(second_format2, cnt_seconds, 'second'); 	
	
//	str_days = '';
	if (hide_null_days && cnt_days == 0) { str_days = font_style + ' </font>';} // show days
	if (hide_null_hours && cnt_days == 0 && cnt_hours == 0) {str_hours = '';} // show hours
	if (hide_null_minutes && cnt_days == 0 && cnt_hours == 0 && cnt_minutes == 0) {str_minutes = '';} // show minutes
	if (hide_null_seconds && cnt_days == 0 && cnt_hours == 0 && cnt_minutes == 0 && cnt_seconds == 0) {str_seconds = '';} // show seconds
	
	if (hide_null_days && cnt_days == 0) { str_days2 = font_style2 + 'Offer Ends In </font>';} // show days
	if (hide_null_hours && cnt_days == 0 && cnt_hours == 0) {str_hours2 = '';} // show hours
	if (hide_null_minutes && cnt_days == 0 && cnt_hours == 0 && cnt_minutes == 0) {str_minutes2 = '';} // show minutes
	if (hide_null_seconds && cnt_days == 0 && cnt_hours == 0 && cnt_minutes == 0 && cnt_seconds == 0) {str_seconds2 = '';} // show seconds	
	create_obj = true;
	//$(":div [class=" + block_id + "]").html(HTMLbeforetimer + str_days + str_hours + str_minutes + str_seconds);
	//$(":span [class=7_yearth2]").html(str_days2 + str_hours2 + str_minutes2 + str_seconds2);

	time_shift--;
	if (time_shift >= 0) {
		setTimeout('setTimeCount();', 1000);
	}
	
}

function processResponse(result) {
    return eval('(' + result + ')');
}
function getTime(str_format, cnt, type) {
	
		var lastNum = cnt % 10;
		var last2Num = Math.floor((cnt % 100)/10);
		
		translationItem = type;
		if (lastNum != 1 || last2Num != 0) {
			translationItem += 's';
		}
	
	if ( cnt < 10 ) { strcnt = "0" + cnt; } else { strcnt = cnt; }   
	str_format = str_format.replace('%d', cnt);
	str_format = str_format.replace('%0d', strcnt);
	str_format = str_format.replace('%s', translationItem);
	return str_format;
}

