﻿function countDown(endDate,outputId) {
	this.containerId = outputId ;
	this.endDate = endDate;
	var dateCounter = new Date(this.endDate - (new Date()));
	this.currentValue = Math.floor(dateCounter.valueOf()/1000);
	this.renderControl = document.getElementById(outputId);
	this.timerControl =  null;
	this.CounterMap = null;
	this.useImage = false ;
	this.couterWidth = null;
	this.couterHeight = null;

	this.defaultBackgroundImage = null;
	
	

	
	// graphical element
	this.activeBackgroundImage = null;
	this.activeBackgroundImageMap = null;
	this.overBackgroundImage = null;
	this.overBackgroundImageMap = null;
	this.numberImage = null ;
	this.numberHeight = null;
	this.numberWidth = null;
	this.numberLeft =null;
	this.numberTop = null;
	
	// text Element
	this.textFont = null;
	this.textColor = null;
	this.textSize = null;
	this.textWeight = null ;
	this.textMask = null;
	this.textLeft =null;
	this.textTop = null;
	this.textOverMessage = null;
	
	
	
	
	
    countDown.prototype.Go = function() {
	
		var counterElements = "";
	
    	if(this.activeBackgroundImage!=null){
			counterElements = '<img src="'+ this.activeBackgroundImage +'"  width='+ this.counterWidth + '" height="' + this.counterHeight + '" style="position: absolute;" useMap="#' + this.activeBackgroundImageMap + '" border="0"/>' ;
		}
		
		if (this.useImage)	{
			var placeMap = this.getChildById(this.activeBackgroundImageMap);
			if (placeMap != null) {
				for(var i=0;i<placeMap.childNodes.length;i++){
					var mapCo = placeMap.childNodes[i];
					if (mapCo.alt!=null && mapCo.alt.match("^#(.)*#$")) { 
						var mapRay = mapCo.coords.split(",");
						var mapType= mapCo.alt.replace("#","");
						for(var j=1;j<mapType.length;j++){
							var positionLeft = parseInt(mapRay[0])+ ((j-1)*this.numberWidth);
							var positionTop = mapRay[1];
							counterElements = counterElements + '<div id="' +this.containerId + '_'+ mapType.charAt(0) + j + '" style="height:'+this.numberHeight+'px;width:'+this.numberWidth+'px;background-repeat:no-repeat; background-position:0px 0px;background-image:url('+ this.numberImage + ');position:absolute;left:' + positionLeft +'px;top:'+ positionTop +'px;">&nbsp;</div>';
						}
	
					}
				}		
			}	
		} else {
			counterElements = counterElements + '<div id="'+ this.containerId + '_Time" style="font-family:' + this.textFont + ';font-size:' + this.textSize + 'px;font-weight:'+ this.textWeight+';color:' + this.textColor + ';position:absolute;left:' + this.textLeft + 'px;top:' + this.textTop + 'px;">&nbsp;</div>';
		}	
	
		this.renderControl.innerHTML= counterElements;
		this.TimeCheck();
	}


	countDown.prototype.TimeCheck = function (){
			clearTimeout(this.timerControl);	
			var dateCounter = new Date(this.endDate - (new Date()));
			this.currentValue = Math.floor(dateCounter.valueOf()/1000);

			
			if (this.currentValue>0) {
				var showDay = this.Days() ;
				var showHour = this.Hours();
				var showMinute = this.Minutes();
				var showSecond = this.Seconds();
				if (this.useImage) {
					if (this.getChildById(this.containerId+ '_D3')!=null) {
					    this.setNumber(this.containerId+ '_D1',(Math.floor((showDay/100))*this.numberWidth));
						this.setNumber(this.containerId+ '_D2',(Math.floor(((showDay %100)/10))*this.numberWidth));
					    this.setNumber(this.containerId+ '_D3', ((showDay % 10)*this.numberWidth));
					} else {
						this.setNumber(this.containerId+ '_D1',(Math.floor(((showDay %100)/10))*this.numberWidth));
					    this.setNumber(this.containerId+ '_D2', ((showDay % 10)*this.numberWidth));
					}
					this.setNumber(this.containerId+ '_H1', (Math.floor((showHour/10))*this.numberWidth));
				    this.setNumber(this.containerId+ '_H2', ((showHour % 10)*this.numberWidth));
					this.setNumber(this.containerId+ '_M1', (Math.floor((showMinute/10))*this.numberWidth));
				    this.setNumber(this.containerId+ '_M2', ((showMinute % 10)*this.numberWidth));
				    this.setNumber(this.containerId+ '_S1',(Math.floor((showSecond/10))*this.numberWidth));
				    this.setNumber(this.containerId+ '_S2',((showSecond % 10)*this.numberWidth));
				} else {
					var tempMask = this.textMask ;
					while(match = /(#(.*?)#)/m.exec(tempMask)) {
						var replaceValue ;
						switch(match[1][1])
							{
							case "D":
								replaceValue = showDay ;
								break;
							case "H":
								replaceValue = (showHour<10 ? '0' : '') + showHour ;
								break;	
							case "M":
								replaceValue = (showMinute<10 ? '0' : '') + showMinute ;
		
								break;	
							case "S":
								replaceValue = (showSecond<10 ? '0' : '') + showSecond ;
								break;	
							default:
								replaceValue ="";
								break;
							}		
							tempMask = tempMask.replace(match[0],replaceValue);
					}
					 this.getChildById(this.containerId+ '_Time').innerHTML = tempMask;
				}	
				var _self = this;
				this.timerControl = setTimeout(function(){_self.TimeCheck();},1000);
			} else {
				var counterElements =  '<div id="'+ this.containerId + '_TimeEnd" style="font-family:' + this.textFont + ';font-size:' + this.textSize + 'px;font-weight:'+ this.textWeight+';color:' + this.textColor + ';position:absolute;left:' + this.textLeft + 'px;top:' + this.textTop + 'px;">' + this.textOverMessage +'</div>';
				this.renderControl.innerHTML= counterElements;
			}	
	}
	
	countDown.prototype.setNumber = function(numberId,position){
	      var num = this.getChildById(numberId);
		  if (num !=null) {
		  	num.style.backgroundPosition = "-" + position+ "px 0px";
		  } else {
		  }	
	}
	
	countDown.prototype.getChildById = function(childId) {
			var selectedElement = null ;
			var children = this.renderControl.childNodes ;
			for(i=0;i<children.length;i++) {
			    if (childId==children[i].id) {
					selectedElement = children[i];
					break;
				}   
		}
		return selectedElement ;
	}
	
	countDown.prototype.Days = function() { 
			return Math.floor(this.currentValue/86400) ;
	}
	
	countDown.prototype.Hours = function() {
			return (Math.floor(this.currentValue/3600)%24) ;
	}
	
	countDown.prototype.Minutes = function() {
			return (Math.floor(this.currentValue/60)%60) ;
	}
	
	countDown.prototype.Seconds = function() {
			return (this.currentValue%60) ;
	}
	
	
}