Fibonacci.INSTANCE = null;
Fibonacci.getInstance = function() {
    if (!Fibonacci.INSTANCE) {
        Fibonacci.INSTANCE = new Fibonacci();
    }
    
    return Fibonacci.INSTANCE;
}
function Fibonacci(first, second) {
    this.first = (first)? first : 1;
    this.second = (second)? second : 1;
    
    this.storage = new Object();
}
Fibonacci.prototype.getAtPlace = function(place) {
    if (this.storage[place]) {
        return this.storage[place];
    } else {
        if (place > 1) {
            var result = this.getAtPlace(place - 1) + this.getAtPlace(place - 2);
            this.storage[place] = result;
            return result;
        } else if (place == 1) {
            return this.second;
        } else if (place == 0) {
            return this.first;
        }
    }
}
Fibonacci.prototype.findNearestIndex = function(value) {
    var offset = 0;
    while (this.getAtPlace(++offset) <= value);

    return offset;
}
Fibonacci.GET_NEXT = function(value) {
	var fibonacciObject = Fibonacci.getInstance();
	return fibonacciObject.getAtPlace(fibonacciObject.findNearestIndex(value));
	//return value + 25;
}
Fibonacci.GET_PREV = function(value) {
	var fibonacciObject = Fibonacci.getInstance();
	return fibonacciObject.getAtPlace(fibonacciObject.findNearestIndex(value) - 2);
	//return (value - 25 < 0)? 0 : value - 25;
}

AutoLooper.AUTO_CHANGE_INTERVAL = 10 * 700;
AutoLooper.MAIN_CONTENT_SCROLL_INTERVAL = 65;
AutoLooper.MIN_ELEMENT = 0;
AutoLooper.MAX_ELEMENT = itemsCount - 1;
AutoLooper.DIGIT_ID_PREFIX = "digitLink";
AutoLooper.MAIN_CONTENT = "mainContent";
function AutoLooper() {
	this.init();
}
AutoLooper.prototype.interval = null;
AutoLooper.prototype.currentElement = null;
AutoLooper.prototype.mainContentInterval = null;
AutoLooper.prototype.mainImageChangerInterval = null;

AutoLooper.prototype.init = function() {
	var obj = this;
	for (var i = AutoLooper.MIN_ELEMENT; i <= AutoLooper.MAX_ELEMENT; i++) {
		document.getElementById(AutoLooper.DIGIT_ID_PREFIX + i).onclick = function() {
			obj.stop();
			obj.getCustom(this.id.replace(AutoLooper.DIGIT_ID_PREFIX, ""));
		}
	}
}
AutoLooper.prototype.start = function(initialIndex) {
	this.currentElement = (initialIndex)? initialIndex : AutoLooper.MIN_ELEMENT;
	this.resume();
}
AutoLooper.prototype.resume = function() {
	this.stop();
	var obj = this;
	this.interval = setInterval(function() {
		obj._handleLoop();
	}, AutoLooper.AUTO_CHANGE_INTERVAL);
	this._handleLoop();
}
AutoLooper.prototype.stop = function() {
	if (this.interval) {
		clearInterval(this.interval);
		this.interval = null;
	}
}
AutoLooper.prototype._handleLoop = function() {
	this.getRight();
}
AutoLooper.prototype.getLeft = function() {
	this.currentElement--;
	if (this.currentElement < AutoLooper.MIN_ELEMENT) {
		this.currentElement = AutoLooper.MAX_ELEMENT;
	}
	this.populate();
}
AutoLooper.prototype.getRight = function() {
	this.currentElement++;
	if (this.currentElement > AutoLooper.MAX_ELEMENT) {
		this.currentElement = AutoLooper.MIN_ELEMENT;
	}
	this.populate();
}
AutoLooper.prototype.getCustom = function(index) {
	this.currentElement = index;
	this.populate();
}
AutoLooper.prototype.populate = function() {
	for (var i = AutoLooper.MIN_ELEMENT; i <= AutoLooper.MAX_ELEMENT; i++) {
		if (i == this.currentElement) {
			document.getElementById(AutoLooper.DIGIT_ID_PREFIX + i).className = "linkCircles1 linkCircles1Force";
		} else {
			document.getElementById(AutoLooper.DIGIT_ID_PREFIX + i).className = "linkCircles1";
		}
	}
	this._mainContentScroll();
}
AutoLooper.prototype._mainContentScroll = function() {
	var startScroll = document.getElementById(AutoLooper.MAIN_CONTENT).scrollLeft;
	var maxScroll = this.currentElement * 1000;
	var current = Math.abs(maxScroll - startScroll);
	if (this.mainContentInterval) {
		clearInterval(this.mainContentInterval);
		this.mainContentInterval = null;
	}
	var obj = this;
	this.mainContentInterval = setInterval(function() {
		if (startScroll < maxScroll) {
			current = Fibonacci.GET_PREV(current);
			if (current > 1) {
				document.getElementById(AutoLooper.MAIN_CONTENT).scrollLeft = maxScroll - current;
			} else {
				document.getElementById(AutoLooper.MAIN_CONTENT).scrollLeft = maxScroll;
				clearInterval(obj.mainContentInterval);
				obj.mainContentInterval = null;
			}
		} else {
			current = Fibonacci.GET_PREV(current);
			if (current > 1) {
				document.getElementById(AutoLooper.MAIN_CONTENT).scrollLeft = maxScroll + current;
			} else {
				document.getElementById(AutoLooper.MAIN_CONTENT).scrollLeft = maxScroll;
				clearInterval(obj.mainContentInterval);
				obj.mainContentInterval = null;
			}
		}
	}, AutoLooper.MAIN_CONTENT_SCROLL_INTERVAL);
}
