var count_ids=[]
function setCountDown(id,tf,sh,cb) {
	var o=$(id)
	var d=new Date()
	var df=new Date(tf.substr(0,4),tf.substr(4,2)-1,tf.substr(6,2),tf.substr(8,2),tf.substr(10,2),tf.substr(12,2))
	if (o) {
		if (d>df) {
			if (cb!=undefined) {cb()}
		} else {
			count_ids.push(new CountDown(count_ids.length,o,df,sh,cb))
		}
	}
}
addLoader(startCountDown)
addUnLoader(stopCountDown)
function startCountDown() {
	for(var i=0;i<count_ids.length;i++) {
		count_ids[i].startCount()
	}
}
function stopCountDown() {
	for(var i=0;i<count_ids.length;i++) {
		count_ids[i].stopCount()
	}
}

function CountDown(idx,o,df,sh,cb) {
	this.index=idx
	this.realObj=o
	this.endDate=df
	this.countObj=null
	this.countD=null
	this.countH=null
	this.countM=null
	this.countS=null
	this.hideExtra=(sh!=undefined)?sh:false
	this.timer=0
	this.callBack=(cb!=undefined)?cb:null
	this.step=1000
	
	this.startCount=function startCount() {
		this.countObj=$b("div")
		this.countObj.className="count_down"
		this.realObj.parentNode.insertBefore(this.countObj,this.realObj)
		this.countD=$b("span")
		this.countD.className="giorni"
		this.countObj.appendChild(this.countD)
		var u=$b("span")
		this.countD.appendChild(u)
		var d=$b("span")
		d.className="unit"
		d.appendChild($ct("g"))
		this.countD.appendChild(d)
		
		this.countH=$b("span")
		this.countH.className="ore"
		this.countObj.appendChild(this.countH)
		u=$b("span")
		this.countH.appendChild(u)
		d=$b("span")
		d.className="unit"
		d.appendChild($ct("h"))
		this.countH.appendChild(d)
		
		this.countM=$b("span")
		this.countM.className="minuti"
		this.countObj.appendChild(this.countM)
		u=$b("span")
		this.countM.appendChild(u)
		d=$b("span")
		d.className="unit"
		d.appendChild($ct("m"))
		this.countM.appendChild(d)
		
		this.countS=$b("span")
		this.countS.className="secondi"
		this.countObj.appendChild(this.countS)
		u=$b("span")
		this.countS.appendChild(u)
		d=$b("span")
		d.className="unit"
		d.appendChild($ct("s"))
		this.countS.appendChild(d)
		this.elabora()
	}
	
	this.stopCount=function stopCountDown() {
		clearTimeout(this.timer)
	}
	
	this.elabora=function() {
		if (this.countObj) {
			var d=Math.round((this.endDate-(new Date()))/1000)
			var s=d%60
			var g=((d-s)/60)
			var m=g%60
			g=((g-m)/60)
			var h=g%24
			g=((g-h)/24)
			this.countD.firstChild.innerHTML=g
			this.countH.firstChild.innerHTML=(((h<10)?"0":"")+h)
			this.countM.firstChild.innerHTML=((m<10)?"0":"")+m
			this.countS.firstChild.innerHTML=((s<10)?"0":"")+s
			if (d<=0) {
				sc(this.countObj,"hide")
				if (this.callBack) {this.callBack()}
			} else {
				if (this.hideExtra) {
					sc(this.countD,(g==0)?"hide":"")
					sc(this.countS,(g==0)?"":"hide",(g==0)?"hide":"")
				}
				this.timer=setTimeout("count_ids["+this.index+"].elabora()",this.step)
			}
		}
	}
}
