$(function() {
	// 連携キット、local時はexit
	if ($.LiVE.bdkit && location.protocol=="file:") return;
	
	var $counter = $.LiVE.Elements.WIDGETS.counter.find(".flipclock");
	$counter.each(function() {
	
		var id = $(this).parent().attr("id"),
			data = $.LiVE.widget[id].data,
			targetDate = new Date(data.set[0],data.set[1]-1,data.set[2],data.set[3],data.set[4]),
			_today = new Date(),
			_S = 1000,
			_M = _S * 60,
			_H = _M * 60,
			_D = _H * 24,
			_counter = $("#counter-" + id);
		
		// calc startTime.
		var diff = targetDate.getTime() - _today.getTime();
		var startTime = "";
		if (diff<0) {
			startTime = "00:00:00:00";
		} else {
			startTime = addZero(Math.floor(diff / _D)) + ":";
			diff = diff % _D;
			startTime += addZero(Math.floor(diff / _H)) + ":";
			diff = diff % _H;
			startTime += addZero(Math.floor(diff / _M)) + ":";
			diff = diff % _M;
			startTime += addZero(Math.floor(diff / _S));
		}
		
		_counter.countdown_full({
			image: "core/widget/counter/flipclock/digits.png",
			startTime: startTime,
			timerEnd: function () {
				if (data.action==1 && $.LiVE.mode.name == "default") {
					if (data.window) {
						var tada = window.open(data.url, "LiVEwindow");
						tada.focus();
					}
					else location.href = data.url;
				}
			}
		});
		
	});
	
	function addZero(src) {
		if (typeof(src) != 'string') src = ""+src;
		for (var i=0; i<2-src.length; i++) src = "0" + src;
		return src;
	}
});
