var curtab = 1;

function loadtab(tab) {
	if(tab != curtab) {
		$('tab_' + tab).className = "tab curtab";
		$('tab_' + curtab).className = "tab";
		$('box_' + tab).className = "tabbox curbox";
		$('box_' + curtab).className = "tabbox";
		curtab = tab;
	}
}

// Change these
var rotatetime = 3000; // Time for each image to be displayed in milliseconds
var fadetime = 1000; // Time for cross-fade in milliseconds

// Don't touch these!
var rotateimgs = new Array();
var pos = 0;
var go;
var inprogress = 0;

function getmodeimages(div) {
	var container = $(div);
	var useel = "A";
	var count = 0;
	for(var i = 0; i < container.childNodes.length; i++) {
		if(container.childNodes[i].nodeName == useel) {
			container.childNodes[i].id = "fadeimg_" + count;
			$('fadeimg_' + count).style.display = "none";
			rotateimgs[rotateimgs.length] = container.childNodes[i];
			if(rotateimgs.length == 1) container.childNodes[i].style.display = "block";
			count++;
		}
	}
	if(rotateimgs.length > 1) go = setInterval(rotateimages, rotatetime + fadetime);
	$('dot_0').className = "dot curdot";
}

function rotateimages() {
	if(!rotateimgs[pos + 1]) var newpos = 0;
	else var newpos = pos + 1;
	fadeimg(rotateimgs[newpos], rotateimgs[pos]);
	pos = newpos;
}

function fadeimg(el, oldel) {
	var myFade = new Fx.Tween($(el.id), {
		duration: fadetime,
		onStart: function() { inprogress = 1; },
		onComplete: function() { 
			oldel.style.display = "none";
			inprogress = 0;
		}
	});
	myFade.set('opacity', 0);
	el.style.display = "block";
	oldel.style.zIndex = 4;
	el.style.zIndex = 5;
	myFade.start('opacity', 1);
	var newimgid = el.id.split('_');
	for(var j = 0; j <= 3; j++) {
		if(newimgid[1] == j) $('dot_' + j).className = "dot curdot";
		else $('dot_' + j).className = "dot";
	} 
}

function fadeto(newpos) {
	if(newpos != pos && inprogress != 1) {
		fadeimg(rotateimgs[newpos], rotateimgs[pos]);
		pos = newpos;
		clearInterval(go);
	}
}

window.addEvent('domready', function() {
	if($('imgfade')) getmodeimages('imgfade');
});
