// Rotating Tablist Item Script

$(document).ready(function(){

	

	// prepend a raquo to any sector link
	$("div#jobsearch li").prepend("&raquo;&nbsp;");
	
	// display the first item
	var sts = $("div#messages").find("div.message_text");
	sts.eq(0).fadeIn(100);
	var sts = $("div#messages").find("div.message_illustration");
	sts.eq(0).fadeIn(100);
	
	// activate the first link
	var tablist = $("div#pick_message a");
	tablist.eq(0).addClass("active");
	
	// start the timer
	changeStInt = setInterval ("nextSt()", 15000 );
	
	
	// apply links to the text and illustration panels
	var stt = $("div#messages").find("div.message_text");
	var sti = $("div#messages").find("div.message_illustration");
	// var tablist = $("div#pick_message a");
	stt.click(function(){
		var thislinknum = $(this).prevAll().filter("div.message_text").length;
		window.location=tablist.eq(thislinknum).attr("href");
	});
	sti.click(function(){
		var thislinknum = $(this).prevAll().filter("div.message_illustration").length;
		window.location=tablist.eq(thislinknum).attr("href");
	});
	
	
	// on the home page, apply a click event to the entire "latest job" paragraph
	$("div#latest_job").click(function(){
		window.location=$(this).find("a").attr('href');
	});
	// on the home page, apply a click event to the entire "profile" paragraph
	$("div#profile").click(function(){
		window.location=$(this).find("a").attr('href');
	});


	$("div#pick_message a").mouseover(function () { 

		if(!$(this).hasClass("active")) {
			var thislinknum = $(this).parent().prevAll().length;
			changeSt(thislinknum);
	
			
	
			// reset the interval
	
			clearInterval(changeStInt);
	
			changeStInt = setInterval ("nextSt()", 15000 );
	
			return false;
		}

	});

	$("div#pick_message a").mouseout(function () { 
		
		if($(this).hasClass("active")) {
			var thislinknum = $(this).parent().prevAll().length;
			
			$(this).parent().siblings().find("a").removeClass("active");
			
			
			var stt = $("div#messages").find("div.message_text");
			var sti = $("div#messages").find("div.message_illustration");
			sttnum = stt.length;
				
		
			for(i=0;i<sttnum;i++){
				if(i!=thislinknum) {
					stt.eq(i).hide();
					sti.eq(i).hide();
				}
				else {
					stt.eq(i).show();
					sti.eq(i).show();
				}
			}
			
		}
		
		/*
		if(!$(this).hasClass("active")) {
			var thislinknum = $(this).parent().prevAll().length;
			changeSt(thislinknum);
	
			
	
			// reset the interval
	
			clearInterval(changeStInt);
	
			changeStInt = setInterval ("nextSt()", 15000 );
	
			return false;
		}
		*/
	});
	

  });

function nextSt() {

	var stt = $("div#messages").find("div.message_text");
	var sti = $("div#messages").find("div.message_illustration");
	var tablist = $("div#pick_message a");

	sttnum = stt.length;

	
	for(i=0;i<sttnum;i++){

		if(stt.eq(i).is(":visible")) {

			currentNum = i;

		}

	}

	nextOne = currentNum +1;

	if(nextOne >= sttnum) {

		nextOne = 0;	

	}

	

	changeSt(nextOne);

	

}

function changeSt(nextOne) {

	var stt = $("div#messages").find("div.message_text");
	var sti = $("div#messages").find("div.message_illustration");
	var tablist = $("div#pick_message a");

	// stt.stop();
	// sti.stop();
	
	sttnum = stt.length;

	

	

	for(i=0;i<sttnum;i++){

		if(stt.eq(i).is(":visible")) {

			currentNum = i;

		}

	}

	

	stt.eq(currentNum).fadeOut(100, function () {

      stt.hide();
	  stt.eq(nextOne).fadeIn(200);


	});
	sti.eq(currentNum).fadeOut(100, function () {
	
		sti.hide();
		// tablist.removeClass("active");
		sti.eq(nextOne).fadeIn(200);

	});
	
	// tablist.eq(currentNum).removeClass("active");
	tablist.removeClass("active");
	tablist.eq(nextOne).addClass("active");

}