Rotator = function (rotatorID,params) {
    this.itemWidth = 143;
    this.totalWidth = 143*6;
    this.visibleItems = 6;

    this.start_int = 1500;
    this.auto_int = 4000;
    this.restart_int = 7000;

    function getChildElements(node) {
	var elements = new Array();
	for (var i = 0; i < node.childNodes.length; i++ ) {
	    var child = node.childNodes[i];
	    // nodeType 1 er ELEMENT_NODE
	    if ( child.nodeType == 1 ) {
		elements.push(child);
	    }
    }
	return elements;
    }

    if (params) {
	for(p in params) {
	    this[p] = params[p];
	}
    }

    this.rotator = document.getElementById(rotatorID);
    if(this.rotator == undefined) return;

    this.area = getElementsByClass(this.rotator,'scrollarea','div')[0];
    //this.area.style.width = this.totalWidth+'px';
    //this.items = this.area.getElementsByTagName("img");
    this.items = getChildElements(this.area);
    this.total = this.items.length;
    if (this.total < this.visibleItems) return;
    var rotatorup = getElementsByClass(this.rotator,'rotatorup','div')[0];
    rotatorup.rotator = this;
    rotatorup.onclick = function () {this.rotator.manual();this.rotator.down()}
    rotatorup.onmouseover = function () {this.className = "rotatorup over";}
    rotatorup.onmouseout = function () {this.className = "rotatorup";}
    
    
    var rotatordown = getElementsByClass(this.rotator,'rotatordown','div')[0];
    rotatordown.rotator = this;
    rotatordown.onclick = function () {this.rotator.manual();this.rotator.up()}
    rotatordown.onmouseover = function () {this.className = "rotatordown over";}
    rotatordown.onmouseout = function () {this.className = "rotatordown";}

    var bottomClones = new Array(this.visibleItems);
    var topClones = new Array(this.visibleItems);
    /* Duplicate head and tail of the newsitem list */
    for (var i = 0; i < this.visibleItems; i++) {
	bottomClones[i] = this.items[this.total - 1 - i].cloneNode(true);
	topClones[i] = this.items[i].cloneNode(true);
    }
    /* Grow the list */
    for (var i = 0; i < this.visibleItems; i++) {
	//items is dynamic, so items[0] always refers to first item
	this.area.insertBefore(bottomClones[i], this.items[0]); 

	this.area.appendChild(topClones[i]);
    }
    this.index = 0;
    this.area.scrollLeft = this.index*this.itemWidth+this.totalWidth;
    this.scrollUp = new Animator(0, this.itemWidth, this.createContextFunction(this, "animateUp"));
    this.scrollDown = new Animator(0, this.itemWidth, this.createContextFunction(this, "animateDown"));

    this.auto = setTimeout(this.createContextFunction(this, "automatic"), this.start_int);
    //this.rotator.style.display = 'block';

}
Rotator.prototype.automatic = function () {
    this.up();
    if (this.restart) clearInterval(this.restart);
    this.restart = null;
    this.auto = setInterval(this.createContextFunction(this, "up"), this.auto_int);
}
Rotator.prototype.manual = function () {
    if (this.auto) clearInterval(this.auto);
    this.auto = null;
    if (this.restart) clearInterval(this.restart);
    this.restart = setTimeout(this.createContextFunction(this, "automatic"), this.restart_int);
}
Rotator.prototype.up = function () {
    this.index++;
    if (this.index == this.total) this.index = 0;
    this.scrollUp.start();
}
Rotator.prototype.animateUp = function (value) {
    this.area.scrollLeft = (this.index-1)*this.itemWidth + value + this.totalWidth;
}
Rotator.prototype.down = function () {
    this.index--;
    if (this.index == -this.visibleItems) this.index = this.total - this.visibleItems;
    this.scrollDown.start();
}
Rotator.prototype.animateDown = function (value) {
    this.area.scrollLeft = (this.index+1)*this.itemWidth - value + this.totalWidth;
}

Rotator.prototype.createContextFunction = function(context, method) {
    var context = this;
    return (function(){
	context[method](arguments[0],arguments[1],arguments[2]);
	return false;
    });
}

function getElementsByClass(node,searchClass,tag) {
    var classElements = new Array();
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    //var pattern = new RegExp("\b"+searchClass+"\b");
    var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
    for (i = 0, j = 0; i < elsLen; i++) {
	if ( pattern.test(els[i].className) ) {
	    classElements[j] = els[i];
	    j++;
	}
    }
    return classElements;
}

