carousel = {
	shiftTime: 5000,
	fadeTime: 500,
	cases: {
		0: {
			caseItem: "",
			caseSelector: "",
			caseClicker: ""
		},
		1: {
			caseItem: "",
			caseSelector: "",
			caseClicker: ""
		},
		2: {
			caseItem: "",
			caseSelector: "",
			caseClicker: ""
		},
		3: {
			caseItem: "",
			caseSelector: "",
			caseClicker: ""
		}
	},
	currentCase: 0,
	oldCase: 0,
	click: function(e) {
		var target;
		if (e.target) {
			target = e.target;
		} else {
			target = window.event.srcElement;
		}
		
		clearTimeout(carousel.timeOut);
		carousel.oldCase = carousel.currentCase;

		var newX = target.id.split("_");
		x = newX[1];

		carousel.currentCase = x;
		//alert(this.currentCase);
		carousel.affect(x);
		//alert("HAHAHA2");
		this.timeOut = setTimeout(function() {carousel.next();},carousel.shiftTime);
	},
	hideElems: function() {
	
	},
	affect: function(x) {
		//alert(this.currentCase);
		
		this.cases[this.currentCase]["caseSelector"].className = "caseSelector active";
		this.cases[this.oldCase]["caseSelector"].className = "caseSelector";
		
		this.cases[this.oldCase]["caseItem"].style.zIndex = 1;
		this.cases[this.currentCase]["caseItem"].style.zIndex = 100;
		
		new Fx.Styles(this.cases[this.oldCase]["caseItem"], {duration: carousel.fadeTime, wait:false}).start({'opacity': 0});
		new Fx.Styles(this.cases[this.currentCase]["caseItem"], {duration: carousel.fadeTime, wait:false}).start({'opacity': 1});
		

	},
	next: function() {
		this.oldCase = this.currentCase;
		if (this.currentCase < 3) {
			this.currentCase++;
		} else {
			this.currentCase = 0;
		}
		
		this.affect(this.currentCase);
		this.timeOut = setTimeout(function() {carousel.next();},carousel.shiftTime);
	},
	timeOut: "",
	init: function() {
		var selectors = $('caseSelectorContainer').getElementsByTagName('div');
		var items = $('caseCarouselContainer').getElementsByTagName('div');
		var x = 0;
		var y = 0;
		var currentCaseCounter = 0;
		
		while (x < selectors.length) {
			if (selectors[x].className == "caseSelector") {
				//alert(currentCaseCounter);
				this.cases[currentCaseCounter]["caseSelector"] = selectors[x];
				currentCaseCounter++;
			}
			else if (selectors[x].className == "caseToggler") {
				if (selectors[x].addEvent) {
					selectors[x].addEvent('click',function (x) {carousel.click(x)});
				} else {
					//ar blah = currentCaseCounter;
					selectors[x].attachEvent('onclick',carousel.click);
				}
			}
			x++;	
		}
		
		x = 0;
		
		while (x < items.length) {
			if (items[x].className == "frontpageCase") {
				this.cases[y]["caseItem"] = items[x];
				new Fx.Styles(items[x], {duration:0, wait:false}).start({'opacity': 0});
				items[x].style.zIndex = 1;
				y++;
			}
			x++;
		}
		
		this.cases[0]["caseSelector"].className = "caseSelector active";
		new Fx.Styles(this.cases[0]["caseItem"], {duration: carousel.fadeTime, wait:false}).start({'opacity': 1});
		this.cases[0]["caseItem"].style.zIndex = 100;
		this.timeOut = setTimeout(function() {carousel.next();},carousel.shiftTime);
	}
}
