var Flipclock = function(flipHolderId, theNumber, frameShift, animationFrames, initialPos, increment, pace, digitsOld, digitsNew, subStart, subEnd, x, y) {
	this.inputData = {
		flipHolderId: flipHolderId?flipHolderId:'fliptimer',
// Starting number. If no number in URL (?n=number), then get a random one
//var theNumber = (window.location.search == "") ? Number(getRandom()) : Number(window.location.search.substring(3));
// Or you could get it via AJAX:
// var theNumber = Number($.ajax({ type: "GET", url: "number.php", async: false }).responseText);
// Where number.php can be a DB query, or any kind of script that outputs a number to use
		theNumber: theNumber ? Number(theNumber) : Number(Math.floor(1000000 - ((Math.round((+new Date()) / 1000) - Math.round((+new Date(2011,12,1,10,0,0,0)) / 1000))/42))),
// Frame shift
		frameShift: frameShift?frameShift:64,
// Amination frames
		animationFrames: animationFrames ? animationFrames : 6,
// Array to hold each digit's starting background-position Y value
		initialPos: initialPos?initialPos:[0, -384, -768, -1152, -1536, -1920, -2304, -2688, -3072, -3456],
// Increment
		increment: increment ? increment : 1,
		pace: pace ? pace : 3000,
// Initializing variables
		digitsOld: digitsOld?digitsOld:'',
		digitsNew: digitsNew?digitsNew:'',
		subStart: subStart?subStart:'',
		subEnd: subEnd?subEnd:'',
		x: x?x:'',
		y: y?y:''
	};
	this.frameShiftPx = this.inputData.frameShift * this.inputData.animationFrames * -1;
	this.inputData.initialPos = [0, this.frameShiftPx*1, this.frameShiftPx*2, this.frameShiftPx*3, this.frameShiftPx*4, this.frameShiftPx*5, this.frameShiftPx*6, this.frameShiftPx*7, this.frameShiftPx*8, this.frameShiftPx*9]
	this.flipinitialDigitCheck();
	//setInterval('xxx = '+flipHolderId+'.flipdoCount();', this.inputData.pace);
}

// Sets the correct digits on load
Flipclock.prototype.flipinitialDigitCheck = function(){
	// Creates the right number of digits
	var count = this.inputData.theNumber.toString().length;
	var bit = 1;
	for (var i = 0; i < count; i++){
		if (bit == 1) $("ul#"+this.inputData.flipHolderId).prepend('<li class="seperator">коп. </li>');
		$("ul#"+this.inputData.flipHolderId).prepend('<li id="d'+this.inputData.flipHolderId + i + '"></li>');
		if (bit == 2) $("ul#"+this.inputData.flipHolderId).prepend('<li class="seperator">руб. </li>');
		else if (bit != (count) && bit % 5 == 0) $("ul").prepend('<li class="seperator"></li>');
		bit++;
	}
	// Sets them to the right number
	var digits = this.flipsplitToArray(this.inputData.theNumber.toString());
	for (var i = 0, c = digits.length; i < c; i++){
		$("#d"+this.inputData.flipHolderId + i).css({'background-position': '0 ' + this.inputData.initialPos[digits[i]] + 'px'});
	}
}
// Function that controls counting
Flipclock.prototype.flipdoCount = function (){
	this.inputData.x = this.inputData.theNumber.toString();
	this.inputData.theNumber -= this.inputData.increment;
	this.inputData.y = this.inputData.theNumber.toString();
	this.flipdigitCheck();
}
// Splits each value into an array of digits
Flipclock.prototype.flipsplitToArray = function (input){
	var digits = new Array();
	for (var i = 0, c = input.length; i < c; i++){
		this.inputData.subStart = input.length - (i + 1),
		this.inputData.subEnd = input.length - i;
		digits[i] = input.substring(this.inputData.subStart, this.inputData.subEnd);
	}
	return digits;
}

// This checks the old count value vs. new value, to determine how many digits
// have changed and need to be animated.
Flipclock.prototype.flipdigitCheck = function (){
	if (this.inputData.y.length > this.inputData.x.length) this.flipaddDigit(this.inputData.y.length);
	if (this.inputData.y.length < this.inputData.x.length) this.flipdelDigit(0);
	this.inputData.digitsOld = this.flipsplitToArray(this.inputData.x),
	this.inputData.digitsNew = this.flipsplitToArray(this.inputData.y);
	for (var i = 0, c = this.inputData.digitsNew.length; i < c; i++){
		if (this.inputData.digitsNew[i] != this.inputData.digitsOld[i]){
			this.flipanimateDigit(i, this.inputData.digitsOld[i], this.inputData.digitsNew[i]);
		}
	}
}

// Animation function
Flipclock.prototype.flipanimateDigit = function (n, oldDigit, newDigit){
	// I want three different animations speeds based on the digit,
	// because the pace and increment is so high. If it was counting
	// slower, just one speed would do.
	// 1: Changes so fast is just like a blur
	// 2: You can see complete animation, barely
	// 3: Nice and slow
	var speed;
	switch (n){
		case 0:
			speed = this.inputData.pace/8;
			break;
		case 1:
			speed = this.inputData.pace/4;
			break;
		default:
			speed = this.inputData.pace/2;
			break;
	}
	// Cap on slowest animation can go
	speed = (speed > 100) ? 100 : speed;
	// Get the initial Y value of background position to begin animation
	var pos = this.inputData.initialPos[oldDigit];
	// Each animation is 5 frames long, and 103px down the background image.
	// We delay each frame according to the speed we determined above.
	//alert(this.inputData.flipHolderId); return false;
	var flipHolderId = this.inputData.flipHolderId;
	var initialPos = this.inputData.initialPos;
	for (var k = 0; k < this.inputData.animationFrames; k++){
		pos = pos + this.inputData.frameShift;
		if (k == (this.inputData.animationFrames - 1)){
			$("#d"+flipHolderId + n).delay(speed).animate({backgroundPosition: '0 ' + pos + ''}, 0, function(){
				// At end of animation, shift position to new digit.
				$("#d"+flipHolderId + n).css({'background-position': '0 ' + initialPos[newDigit] + 'px'});
			});
		}
		else{
			$("#d"+flipHolderId + n).delay(speed).animate({backgroundPosition: '0 ' + pos + ''}, 0);
		}
	}
}


// Adds new digit
Flipclock.prototype.flipaddDigit = function (len){
	var li = Number(len) - 1;
	if (li % 3 == 0) $("ul#"+this.inputData.flipHolderId).prepend('<li class="seperator"></li>');
	$("ul#"+this.inputData.flipHolderId).prepend('<li id="d'+this.inputData.flipHolderId + li + '"></li>');
	$("#d"+this.inputData.flipHolderId + li).css({'background-position': '0 ' + this.inputData.initialPos[1] + 'px'});
}
// Adds new digit
Flipclock.prototype.flipdelDigit = function (len){
	var li = Number(len) - 1;
	$("ul#"+this.inputData.flipHolderId+" li:eq(0)").remove();
	if ($("ul#"+this.inputData.flipHolderId+" li:eq(0)").hasClass('seperator')) $("ul#"+this.inputData.flipHolderId+" li:eq(0)").remove();
}


// Generates a good random number
Flipclock.prototype.getRandom = function (){
	var numD = Math.floor(Math.random()*9) + 3;
	var num = '';
	for (var i = 0; i < numD; i++){
		num += Math.floor(Math.random()*9).toString();
	}
	return num
}

