/* these two globals are very important and let us know what is the current and next study outside of the main function rotateTab */

var nextStudy = 'mobilewireless';
var currentStudy = 'introduction';
var t; /* current timeout */
var defaultTime = 8000;
function rotateTab(study, mouseover) {
//alert('rotatetab called with study = ' + study + " and mouseover = " + mouseover + " current = " + currentStudy + " netxt =  " + nextStudy);
	clearTimeout(t);
	var next;
	var nexti;
	var previ;
	var i;
	var current;
	var index;
	var rotation = new Array('introduction','mobilewireless', 'autoid','embeddedhardware', 'embeddedsoftware', 'industrialauto','emergingtech');
	
	if (study == currentStudy) {
	// another hack to prevent images from reloading if they are the same. say you move your mouse back and forth quickly between tabs but end up on the same tab as the active panel.
	// the next setTimeout will be called when the mouse leaves the button.
	return;
	}
	
	/* find where this next panel is in the rotation */
	for (i = 0; i < rotation.length; i++) {
		if (rotation[i] == study) {
			
			if (i == rotation.length -1 ) { /* your at the last item */
				nexti = 0;
			} else {
				nexti = i + 1;
			}
			
			if (i == 0) {
				previ = rotation.length -1;
			} else {
				previ = i - 1;
			}
			/* set the global next study */
			nextStudy = rotation[nexti];
			next = $("#" + rotation[i]);
			
			break;
		}
	}

	
	// animate the opacity from 0 to 1
	$("#" + currentStudy).css('z-index',-6);
	next.show();
	next.css({opacity: 0.0})
	.css('z-index',-5)
	.animate({opacity: 1.0}, 500, function() {
	/* hide all other panels */
			for (j = 0; j < rotation.length; j++) {
				if (rotation[j] != study) {
					$("#" + rotation[j]).hide();
					$("#" + rotation[j]).css('z-index',-6);
				}
			}
			/* final global switch here */
			currentStudy = study;
	});
	if (mouseover) {
	/* set it for much longer if timeout is set */
		t = setTimeout("rotateTab('" + nextStudy + "',false)", defaultTime*4);
	} else {
		if (nexti == 1) {
			t = setTimeout("rotateTab('" + nextStudy + "',false)", defaultTime*1.2);
		} else {
			t = setTimeout("rotateTab('" + nextStudy + "',false)", defaultTime);
		}
	}
	
	

}

/* Initialize everything */
$(document).ready(function() {
//preload images

var image1 = new Image();
image1.src = '/images/homer_autoid.jpg';
var image2 = new Image();
image2.src = '/images/homer_embeddedhardware.jpg';
var image3 = new Image();
image3.src = '/images/homer_embeddedsoftware.jpg';
var image4 = new Image();
image4.src = '/images/homer_emergingtech.jpg';
var image5 = new Image();
image5.src = '/images/homer_industrialauto.jpg';
var image6 = new Image();
image6.src = '/images/homer_mobilewireless.jpg';


$(".case_study_container").mouseover(function(){
clearTimeout(t);
t = setTimeout("rotateTab('" + nextStudy + "',false)", defaultTime * 2 );
});




//$("#prod_buttons ul li a").bind("mouseenter", function(e){
$("#prod_buttons ul li a").hoverIntent(function(e){	
	 clearTimeout(t);
	 newStudy = $(this).attr("id").substr(4);
	  // links are called "tab-" + panelname (which matches id in the rotation)
	
	 /* find where this next is in the rotation */
	 //if (nextStudy != newStudy) {
		t = setTimeout("rotateTab('" + newStudy + "',true)", 100 );
	 // }
},function(){
	 clearTimeout(t);
	 t =  setTimeout("rotateTab('" + nextStudy + "',false)", defaultTime ); 
 } );



 $("#prod_buttons ul li a").mouseout(function(){
	 clearTimeout(t);
	 t =  setTimeout("rotateTab('" + nextStudy + "',false)", defaultTime ); 
 });



t =  setTimeout("rotateTab('" + nextStudy + "',false)", defaultTime * 1.2);
	
});


