Object.prototype.method = function (method) {
	var context = this;
	return function () {
		method.apply(context, arguments);
		if (this.blur && this.tagName=="A") this.blur();
		return false;
	}
}

function isChild (ancestor, candidate) {
	while (candidate && candidate != ancestor.parentNode) {
		if (candidate == ancestor) return true;
		try {
			candidate = candidate.parentNode;
		} catch (c) {return false}
	}
	return false;
}


Rotator = function (scrollableElement, itemContainer, itemTagName, buttonContainer, visibleItems, scrollDistance, initialDelay) {
	this.itemSize = scrollDistance;
	this.visibleItems = visibleItems;
	this.orientation = 0;

	this.scrollable = scrollableElement;
	this.items = itemContainer.getElementsByTagName(itemTagName);
	this.totalItems = this.items.length;
	//if (this.totalItems <= this.visibleItems) return;
	//alert(this.items[0].className);
	// If statement to reset the amount of projects if it is less than 6
	
	if (this.items.length < 6 ) {
			
		this.visibleItems = this.items.length;
	}
	
	var rotator = this;//Needed for closures below
	
	var leftbutton = document.createElement("a");
	leftbutton.className = "leftbutton";
	leftbutton.href = "#";
	leftbutton.onclick = function () {this.blur();rotator.manual();rotator.back();return false}
	buttonContainer.appendChild(leftbutton);

	var rightbutton = document.createElement("a");
	rightbutton.className = "rightbutton";
	rightbutton.href = "#";
	rightbutton.onclick = function () {this.blur();rotator.manual();rotator.forward();return false}
	buttonContainer.appendChild(rightbutton);

	
	// If statement to remove scrolling buttons if there are too few items
	if (this.totalItems < 6) {
		buttonContainer.style.display = "none";
	}
	if (document.getElementById("filters")){
		if (this.totalItems < 6) {
			document.getElementById("filters").parentNode.parentNode.style.display = "none";
		}
	}
		
	var bottomClones = new Array(this.visibleItems);
	var topClones = new Array(this.visibleItems);
	
	/* Duplicate head and tail of the item list */
	for (var i = 0; i < this.visibleItems; i++) {
		bottomClones[i] = this.items[this.totalItems - 1 - i].cloneNode(true);
		
		// If statement to stop cloning if there are less than six items
		if (this.items.length >= 6 ) {
			topClones[i] = this.items[i].cloneNode(true);
		}
		else{
			topClones[i] = this.items[i];
		}
	}
	/* Grow the list */
	for (var i = 0; i < this.visibleItems; i++) {
		itemContainer.insertBefore(bottomClones[i], this.items[0]);//items is dynamic, so items[0] always refers to first item
		itemContainer.appendChild(topClones[i]);
	}

	this.index = this.visibleItems;
	if (this.orientation) this.scrollable.scrollTop = this.index*this.itemSize; else this.scrollable.scrollLeft = this.index*this.itemSize;

	this.scrollForward = new Animator(0, this.itemSize, this.method(this.animateForward), this.method(this.checkIndex));
	this.scrollForward.setStep(3);
	this.scrollForward.setType(this.scrollForward.EASEINOUT);
	this.scrollForward.setRate(20);
	this.scrollBack = new Animator(0, this.itemSize, this.method(this.animateBack), this.method(this.checkIndex));
	this.scrollBack.setStep(3);
	this.scrollBack.setType(this.scrollForward.EASEINOUT);
	this.scrollBack.setRate(20);
	

		
	itemContainer.controller = this;
	itemContainer.onmouseover = function (){this.controller.pause()};
	itemContainer.onmouseout = function (){if (!isChild(itemContainer,this) || this == itemContainer) this.controller.resume()};
	
	//alert(this.items[i].className);
	
	var projectarray = itemContainer.getElementsByTagName("li");
	
	for (var i=0; i < projectarray.length; i++) {
		//alert(testingit);
		//alert(projectarray[i].className);
		projectarray[i].onmouseover = function(){
										
										//alert(projectarray);
										for (var i=0; i < projectarray.length; i++) {
											projectarray[i].className = projectarray[i].className = "";
										}
										this.className = this.className.replace(new RegExp(""), "active");
									};
	}
	
	var projectlength = this.items.length / 2;
	
	// If statement stops auto scroll of carousel if there are less than 6 items
	
	if (projectlength >= 6 ) {
		this.auto = setTimeout(this.method(this.automatic), "2800");
	}
}

Rotator.prototype.setHorizontalMode = function () {this.orientation = 0}
Rotator.prototype.setVerticalMode = function () {this.orientation = 1}

Rotator.prototype.automatic = function () {
	if (this.isPaused) {
		//this.restart = setTimeout(this.method(this.automatic), 5000);
		this.restart = setTimeout(this.method(this.automatic), 3500);
		return;
	}
	this.forward();
	if (this.restart) clearInterval(this.restart);
	this.restart = null;
	//this.auto = setInterval(this.method(this.forward), 14000);
	this.auto = setInterval(this.method(this.forward), 3500);
}
Rotator.prototype.manual = function () {
	if (!this.auto || this.isPaused) return;
	if (this.animating) {
		this.scrollForward.stop();
		this.checkIndex();
		this.animating = false;
	}
	if (this.auto) clearInterval(this.auto);
	this.auto = null;
	if (this.restart) clearInterval(this.restart);
	//this.restart = setTimeout(this.method(this.automatic), 5000);
	this.restart = setTimeout(this.method(this.automatic), 3500);
}
Rotator.prototype.forward = function () {
	if (this.animating) return;
	this.animating = this.scrollForward;
	this.index++;
	this.scrollForward.start();
}
Rotator.prototype.animateForward = function (value) {
	if (this.orientation) this.scrollable.scrollTop = (this.index-1)*this.itemSize + value;
	else this.scrollable.scrollLeft = (this.index-1)*this.itemSize + value;
}
Rotator.prototype.back = function () {
	if (this.animating) return;
	this.animating = this.scrollBack;
	this.index--;
	this.scrollBack.start();
}
Rotator.prototype.animateBack = function (value) {
	if (this.orientation) this.scrollable.scrollTop = (this.index+1)*this.itemSize - value;
	else this.scrollable.scrollLeft = (this.index+1)*this.itemSize - value;
}
Rotator.prototype.checkIndex = function () {
	if (this.index == 0) this.index = this.totalItems;
	if (this.index == this.totalItems + this.visibleItems) this.index = this.visibleItems;
	if (this.orientation) this.scrollable.scrollTop = this.index*this.itemSize; else this.scrollable.scrollLeft = this.index*this.itemSize;
	this.animating = false;
}
Rotator.prototype.pause = function () {
	if (this.animating) {
		this.animating.pause();
	} else {
		this.manual();
	}
	this.isPaused = true;
}
Rotator.prototype.resume = function () {
	if (this.animating) {
		this.animating.resume();
	}
	this.isPaused = false;
}