function getClockTime(){
	var date = new Date(); 
	var h = String(date.getHours());
	var m = String(date.getMinutes());
	var s = String(date.getSeconds());

	h = (h.length == 1) ? ("0" + h) : h;
	m = (m.length == 1) ? ("0" + m) : m;
	s = (s.length == 1) ? ("0" + s) : s;
	return {hour:h, minute:m, second:s};
}

/* 
Clock - CLASS
fileName - the name of .js file with clock digits
htmlObj - html object to show clock in


We create instance of Clock CLASS, passing two params.
This instance creates 6 corresponding instances of ASCIImationClock CLASS
(Every ASCIImationClock instance are responce for animation in one time cell)

.js file must consist of 10 digit transitions
It must start from 0-1 transition, then 1-2, 2-3, 3-4, 4-5, 5-6, 6-7, 7-8, 8-9, 9-0.
All transitions must have similar frames quantity and last less then 1 second
*/
function Clock(fileName, htmlObj){
	this.htmlObj = htmlObj; // html container for total clock

	// Objects for all clock cells (hour, minute, second)
	// first digit cell of hour
	this.h1 = {
		parent : this,
		// it's value
		value : '',
		// previous value
		prevValue : '9',
		// corresponding ASCIImation instance
		ASCIImation : new Object(),
		// corresponding html object for output
		htmlObj : getElementsByClassName(this.htmlObj, 'td', 'h1')[0]
	}
	this.h2 = {
		parent : this,
		value : '',
		prevValue : '9',
		ASCIImation : new Object(),
		htmlObj : getElementsByClassName(this.htmlObj, 'td', 'h2')[0]
	}
	this.m1 = {
		parent : this,
		value : '',
		prevValue : '9',
		ASCIImation : new Object(),
		htmlObj : getElementsByClassName(this.htmlObj, 'td', 'm1')[0]
	}
	this.m2 = {
		parent : this,
		value : '',
		prevValue : '9',
		ASCIImation : new Object(),
		htmlObj : getElementsByClassName(this.htmlObj, 'td', 'm2')[0]
	}
	this.s1 = {
		parent : this,
		value : '',
		prevValue : '9',
		ASCIImation : new Object(),
		htmlObj : getElementsByClassName(this.htmlObj, 'td', 's1')[0]
	}
	this.s2 = {
		parent : this,
		value : '',
		prevValue : '9',
		ASCIImation : new Object(),
		htmlObj : getElementsByClassName(this.htmlObj, 'td', 's2')[0]
	}

	// current time object
	this.time = {};

	// asciimation frames keeper
	this.frames = [];

	ajaxLoad("/ajax/get_file.php?clip_id=" + fileName, this.onload, this);
}

Clock.prototype.onload = function(ajaxObject) {
	// delete global vars loaded before
	content = []; contentParams = undefined;
	
	eval (ajaxObject.responseText);

	this.initBaseParams();
	this.initClipData();
	this.initASCIImationInstances();
	this.initDelimitersView();
	this.flowTime();
}

Clock.prototype.initBaseParams = function(){
	this.fontFamily = (typeof FFamily != 'undefined') ? FFamily : "courier new";
	this.fontSize = ((typeof FSize != 'undefined') ? FSize : 12)/16 + "em";
	this.lineHeight = (typeof LH != 'undefined') ? LH : "120%";
	this.fontWeight = (typeof FWeight != 'undefined') ? FWeight : "normal";
	
	// this.loop = (typeof looped != 'undefined') ? looped : true; // in clock we use looped = false
	this.speed = (typeof sp != 'undefined') ? sp : 100;

	this.frameHeight = ((typeof FrHeight != 'undefined') ? FrHeight : 10) * parseInt(this.lineHeight)/100 + 0.3 + "em";
	this.frameWidth = ((typeof FrWidth != 'undefined') ? FrWidth : 20) * 3/5 + "em";

	this.fontColor = (typeof FColor != 'undefined') ? FColor : "#000000";
	this.backgroundColor = (typeof BGcolor != 'undefined') ? BGcolor : "#ffffff";
}

Clock.prototype.initClipData = function (){
	var frameContentBefore = "";
	var frameRepeatBefore = 1;
	var fontColorBefore = this.fontColor;
	var bgColorBefore = this.backgroundColor;

	for(var i=0; i<content.length; i++){
		
		this.frames[i] = {};
		this.frames[i].content = "";
		this.frames[i].frameData = [];
		
		if(typeof content[i] != "undefined"){ // если фрейм не дублирует предыдущий (когда вместо контента фрейма стоит служебный символ \t)
			if (navigator.userAgent.match(/MSIE/) != null && !navigator.userAgent.match(/Opera/))	content[i]=content[i].replace(/\n/g,'\n\r'); //подгонка под IE
			this.frames[i].content = content[i];
			frameContentBefore = content[i];
		} else {
			this.frames[i].content = frameContentBefore;
		}
		
		if(typeof contentParams != "undefined" && contentParams[i][0]){ // если определено количество повторений
			this.frames[i].frameData[0] = contentParams[i][0];
			frameRepeatBefore = contentParams[i][0]; 
		}	else	{
			this.frames[i].frameData[0] = frameRepeatBefore;
		}
		if(typeof contentParams != "undefined" && contentParams[i][1]){ // если определен цвет шрифта
			this.frames[i].frameData[1] = contentParams[i][1]; 
			fontColorBefore = contentParams[i][1];
		}	else	{
			this.frames[i].frameData[1] = fontColorBefore;
		}
		if(typeof contentParams != "undefined" && contentParams[i][2]){ // если определен цвет фона
			this.frames[i].frameData[2] = contentParams[i][2]; 
			bgColorBefore = contentParams[i][2]; 
		}	else	{
			this.frames[i].frameData[2] = bgColorBefore;
		}
	}
}

Clock.prototype.initASCIImationInstances = function(){
	this.h1.ASCIImation = new ASCIImationClock(this.h1);
	this.h2.ASCIImation = new ASCIImationClock(this.h2);
	this.m1.ASCIImation = new ASCIImationClock(this.m1);
	this.m2.ASCIImation = new ASCIImationClock(this.m2);
	this.s1.ASCIImation = new ASCIImationClock(this.s1);
	this.s2.ASCIImation = new ASCIImationClock(this.s2);
}

Clock.prototype.initDelimitersView = function(){
	var min = getElementsByClassName(this.htmlObj, 'td', 'min')[0].firstChild;
	var sec = getElementsByClassName(this.htmlObj, 'td', 'sec')[0].firstChild;
	min.style.fontFamily = sec.style.fontFamily = this.fontFamily;
	min.style.fontSize = sec.style.fontSize = this.fontSize;
	min.style.lineHeight = sec.style.lineHeight = this.lineHeight;
	min.style.fontWeight = sec.style.fontWeight = this.fontWeight;
	min.style.width = "1.7em";
	sec.style.width = "2.2em";
}

Clock.prototype.flowTime = function (){
	this.time = getClockTime();
	this.drawTime();
	var _this = this;
	setTimeout(function(){_this.flowTime()}, 100);
}

Clock.prototype.drawTime = function (){
	var h1 = this.time.hour.substr(0,1);
	var h2 = this.time.hour.substr(1,1);
	var m1 = this.time.minute.substr(0,1);
	var m2 = this.time.minute.substr(1,1);
	var s1 = this.time.second.substr(0,1);
	var s2 = this.time.second.substr(1,1);
	
	if (this.h1.value != h1) {
		this.h1.prevValue = this.h1.value;
		this.h1.value = h1;
		this.h1.ASCIImation.start();
	}
	if (this.h2.value != h2) {
		this.h2.prevValue = this.h2.value;
		this.h2.value = h2;
		this.h2.ASCIImation.start();
	}
	if (this.m1.value != m1) {
		this.m1.prevValue = this.m1.value;
		this.m1.value = m1;
		this.m1.ASCIImation.start();
	}
	if (this.m2.value != m2) {
		this.m2.prevValue = this.m2.value;
		this.m2.value = m2;
		this.m2.ASCIImation.start();
	}
	if (this.s1.value != s1) {
		this.s1.prevValue = this.s1.value;
		this.s1.value = s1;
		this.s1.ASCIImation.start();
	}
	if (this.s2.value != s2) {
		this.s2.prevValue = this.s2.value;
		this.s2.value = s2;
		this.s2.ASCIImation.start();
	}
}


/* 
ASCIImationClock - CLASS
*/
function ASCIImationClock(parentObj){
	this.htmlObj = parentObj.htmlObj;
	this.parentObj = parentObj;
	this.timer = null;
	this.speed = parentObj.parent.speed;
	this.initView();
}

ASCIImationClock.prototype.initView = function (){
	this.htmlObj.firstChild.style.fontFamily = this.parentObj.parent.fontFamily;
	this.htmlObj.firstChild.style.fontSize = this.parentObj.parent.fontSize;
	this.htmlObj.firstChild.style.lineHeight = this.parentObj.parent.lineHeight;
	this.htmlObj.firstChild.style.fontWeight = this.parentObj.parent.fontWeight;
	this.htmlObj.firstChild.style.height = this.parentObj.parent.frameHeight;
	this.htmlObj.firstChild.style.width = this.parentObj.parent.frameWidth;	
}

ASCIImationClock.prototype.start = function(){
	this.stop();
	this.startIndex = Number(this.parentObj.prevValue) * (this.parentObj.parent.frames.length/10);
	this.endIndex = this.startIndex + (this.parentObj.parent.frames.length/10) - 1;

	this.currentFrameIndex = this.startIndex;
	this.drawFrame();
	this.animate();
}

ASCIImationClock.prototype.animate = function(){
	var _this = this;

	this.currentFrameIndex ++;
	if (this.currentFrameIndex > this.endIndex){
		this.stop();
		this.currentFrameIndex = Number(this.parentObj.value) * (this.parentObj.parent.frames.length/10);
		this.drawFrame();
		return;
	} else {
		this.drawFrame();
		this.timer = setTimeout(function(){_this.animate()}, this.speed);		
	}
}

ASCIImationClock.prototype.drawFrame = function() {
	try{
		this.htmlObj.firstChild.firstChild.data = this.parentObj.parent.frames[this.currentFrameIndex].content;
		this.htmlObj.firstChild.style.color = this.parentObj.parent.frames[this.currentFrameIndex].frameData[1];
		this.htmlObj.firstChild.style.backgroundColor = this.parentObj.parent.frames[this.currentFrameIndex].frameData[2];
	} catch(e){
		
	}
}

ASCIImationClock.prototype.stop = function(){
	clearInterval(this.timer);
}
