/* global vars */
var curBox = 1;
var curSwitch = 1;
var rotateTimer;

/* functions */
function startTimer(delay) {
	delay = delay ? delay : 10000;
	rotateTimer = setTimeout('rotator()',delay);
}

function rotator() {
	var box_number = (curBox < 3) ? curBox + 1: 1;
	switch_box(box_number, null, 'auto');
	startTimer();
}

function switch_box(box_number, box_class, rotate_type) {
	if (rotate_type != 'auto') {
		clearTimeout(rotateTimer);
	}
	if (curBox != box_number) {
		_update_switches(box_number);
		
		if(box_number == 1) {
			_show_box_1(box_class);
		}
		
		_hide_box();
		jQuery('#box' + box_number).fadeIn(800);
		curBox = box_number;
	}
	if (rotate_type != 'auto') {
		startTimer(10000);
	}
}

/* helper functions */
function _show_box_1(box_class) {
	jQuery('#box1').removeClass();
	if (box_class) {
		jQuery('#box1').addClass(box_class);
	}
	else {
		var n = Math.floor(Math.random() * 3);
		var t = ['a','b','c'];
		jQuery('#box1').addClass('box box1' + t[n]);
	}
}

function _hide_box() {
	jQuery('#box' + curBox).hide();
}

function _hide_boxes() {
	jQuery('#box1').hide();
	jQuery('#box2').hide();
	jQuery('#box3').hide();
}

function _update_switches(box_number) {
	if (curSwitch != box_number) {
		var pos = ['left top','-22px top','right top'];
		switch(box_number) {
			case 1: pos[0] = 'left bottom';  break;
			case 2: pos[1] = '-22px bottom'; break;
			case 3: pos[2] = 'right bottom'; break;
		}
		jQuery('#switch1').css('background-position',pos[0]);
		jQuery('#switch2').css('background-position',pos[1]);	
		jQuery('#switch3').css('background-position',pos[2]);	
	}
	curSwitch = box_number;
}
