// JavaScript Document
/***********************************************

* JavaScript Image Clock- by JavaScript Kit (www.javascriptkit.com)
* This notice must stay intact for usage
* Visit JavaScript Kit at http://www.javascriptkit.com/ for this script and 100s more

***********************************************/

var imageclock=new Object()

	imagePath = 'images/clock/'
	
	/*var dayCount  = 0;
	var dhourCount = 0;
	var dminCount  = 0;
	var dsecCount  = 0;
	
	var dcounterDown = 1;*/
	
	//Enter path to clock digit images here, in order of 0-9, then "am/pm", then colon image:
	imageclock.digits=["counterT_0.gif", "counterT_1.gif", "counterT_2.gif", "counterT_3.gif", "counterT_4.gif", "counterT_5.gif", "counterT_6.gif", "counterT_7.gif", "counterT_8.gif", "counterT_9.gif", "counterT_LH.gif", "counterT_Day.gif", "counterT_Hour.gif", "counterT_Min.gif", "counterT_Sec.gif"]
	imageclock.instances=0
	var preloadimages=[]
	for (var i=0; i<imageclock.digits.length; i++){ //preload images
		preloadimages[i]=new Image()
		preloadimages[i].src=imagePath+imageclock.digits[i]
	}

	imageclock.imageHTML=function(timestring){ //return timestring (ie: 79:5:56:38) into string of images instead
		var sections=timestring.split(":")
		for (var i=0; i<sections.length; i++){
			if (sections[i].length==1) {
				if (i != 0) {
					sections[i]='<img src="'+imagePath+imageclock.digits[0]+'" />'+'<img src="'+imagePath+imageclock.digits[parseInt(sections[i])]+'" />'
				} else {
					sections[i]='<img src="'+imagePath+imageclock.digits[0]+'" />'+'<img src="'+imagePath+imageclock.digits[0]+'" />'+'<img src="'+imagePath+imageclock.digits[parseInt(sections[i])]+'" />'
				}
			} else {
				if (i == 0) {
					if (sections[i].length==2) {
						sections[i]='<img src="'+imagePath+imageclock.digits[0]+'" />'+'<img src="'+imagePath+imageclock.digits[parseInt(sections[i].charAt(0))]+'" />'+'<img src="'+imagePath+imageclock.digits[parseInt(sections[i].charAt(1))]+'" />'
					} else {
						sections[i]='<img src="'+imagePath+imageclock.digits[parseInt(sections[i].charAt(0))]+'" />'+'<img src="'+imagePath+imageclock.digits[parseInt(sections[i].charAt(1))]+'" />'+'<img src="'+imagePath+imageclock.digits[parseInt(sections[i].charAt(2))]+'" />'
					}
				} else {
					sections[i]='<img src="'+imagePath+imageclock.digits[parseInt(sections[i].charAt(0))]+'" />'+'<img src="'+imagePath+imageclock.digits[parseInt(sections[i].charAt(1))]+'" />'
				}
			}
		}
		return '<img src="'+imagePath+imageclock.digits[10]+'" />'+sections[0]+'<img src="'+imagePath+imageclock.digits[11]+'" />'+sections[1]+'<img src="'+imagePath+imageclock.digits[12]+'" />'+sections[2]+'<img src="'+imagePath+imageclock.digits[13]+'" />'+sections[3]+'<img src="'+imagePath+imageclock.digits[14]+'" />'
	}

	imageclock.display=function(dayRem, hourRem, minRem, secRem, start, countDown){
		this.dayCount = dayRem;
		this.hourCount = hourRem;
		this.minCount = minRem;
		this.secCount = secRem;
		this.counterDown = countDown;
		
		var clockinstance=this
		this.spanid="clockspan"+(imageclock.instances++)
		if (countDown) {
			document.write ('<img src="images/clock/counterT_Down.gif">');
		} else {
			document.write ('<img src="images/clock/counterT_Up.gif">');
		}
		document.write('<span id="'+this.spanid+'"></span>')
		this.update()
		if (start) setInterval(function(){clockinstance.update()}, 1000)
	}

	imageclock.display.prototype.update=function(){
		var dateobj=new Date()
		var currenttime=this.dayCount.toString()+":"+this.hourCount.toString()+":"+this.minCount.toString()+":"+this.secCount.toString() //create time string
		var currenttimeHTML=imageclock.imageHTML(currenttime)
		document.getElementById(this.spanid).innerHTML=currenttimeHTML
		
		if (this.counterDown) {
			this.secCount--;
			if (this.secCount < 0) {this.minCount--; this.secCount=59}
			if (this.minCount < 0) {this.hourCount--; this.minCount=59}
			if (this.hourCount < 0) {this.dayCount--; this.hourCount=23}
		} else {
			this.secCount++;
			if (this.secCount > 59) {this.minCount++; this.secCount=0}
			if (this.minCount > 59) {this.hourCount++; this.minCount=0}
			if (this.hourCount > 23) {this.dayCount++; this.hourCount=0}
		}
	}
