var scrolltimer;
var top_position = 0;  
var scrollrunning = 0;
var newslinkcharlimit = 50;
var newsscrollspeed = 5000;

var focusloop = 0;
var focallisthighlight = 1;
var focuspointerpos = -230;
var focustimer;
var focusrunning = 0;
var focusscrollspeed = 5000;

function scrollfocus(){
	focallisthighlight = parseInt(focallisthighlight) + 1;
	if (focallisthighlight > 5){
		focusloop = focusloop + 2;
		focallisthighlight = 1;
	}
	if (focallisthighlight == 1){
		$('#FocalPoint_ul li:nth-child(5)').css({'backgroundColor' : '#DBE3DA'});
	}
	if (focallisthighlight > 1){
		$('#FocalPoint_ul li:nth-child('+(parseInt(focallisthighlight-1))+')').css({'backgroundColor' : '#DBE3DA'});
	}
	$('#focuspointer').css({'visibility' : 'hidden'});
	$('#focalimage').animate({
	opacity: 0
	}, 500, function() {
		$('#focalimage').attr('src', 'http://casb.org/images/homeslide/homeslide-'+focallisthighlight+'.jpg');
		$('#FocalPoint_ul li:nth-child('+focallisthighlight+')').css({'backgroundColor' : '#c2d0c1'});
		focuspointerpos = parseInt(focuspointerpos)+55
		if (focallisthighlight == 1){
			focuspointerpos = -230;
		}
		$('#focuspointer').css({'top' : focuspointerpos+'px'});
		$('#focuspointer').css({'visibility' : 'visible'});	
		$('#focalimage').animate({
			opacity: 1
		}, 500, function() {
		});
		$('#focalimage').wrap('<a href="'+$('#FocalPoint_ul li:nth-child('+focallisthighlight+') a').attr('href')+'" id="focuslink" />');
	});
	if (focusloop == 1){
		stopfocus();
	}
	//clearTimeout(focustimer);
	//focusrunning = 0;
	//startfocus();
}

function startfocus() {
	if (focusrunning == 0) {
		focusrunning = 1;
		//focustimer=setTimeout("scrollfocus()",focusscrollspeed);
		focusloop = 0;
		focustimer=setInterval("scrollfocus()",focusscrollspeed);
	}
}

function stopfocus() {
	if (focusrunning == 1) {
		clearInterval(focustimer);
		focusrunning = 0;
	}
}

function updatefocus(thefocusitem) {
	$('#FocalPoint_ul li:nth-child('+focallisthighlight+')').css({'backgroundColor' : '#DBE3DA'});
	$('#FocalPoint_ul li:nth-child('+thefocusitem+')').css({'backgroundColor' : '#c2d0c1'});
	$('#focalimage').attr('src', 'http://casb.org/images/homeslide/homeslide-'+thefocusitem+'.jpg');
	$('#focuslink').attr('href', $('#FocalPoint_ul li:nth-child('+thefocusitem+') a').attr('href'));
	//$('#focalimage').wrap('<a href="'+$('#FocalPoint_ul li:nth-child('+thefocusitem+') a').attr('href')+'" id="focuslink" />');
	focuspointerpos = -260+(55*(thefocusitem-1))
	$('#focuspointer').css({'top' : focuspointerpos+'px'});
	focallisthighlight = thefocusitem
}

function scrollnews(){
	item_height = $('#newslinks_ul p').outerHeight();
	top_position = parseInt(top_position) - parseInt(item_height);
	if ($('#newslinks_ul p:nth-child(2) a').text().length > (newslinkcharlimit + 3)){
		$('#newslinks_ul p:nth-child(2) a').text($('#newslinks_ul p:nth-child(2) a').text().substring(0, newslinkcharlimit)+"...");
	}
	$('#newslinks_ul:not(:animated)').animate({'marginTop' : top_position},500,function(){
		$('#newslinks_ul p:last').after($('#newslinks_ul p:first')); 
		top_position = 0;
		$('#newslinks_ul').css({'marginTop' : top_position});
		//clearTimeout(scrolltimer);
		//scrollrunning = 0;
		//startnews();
	});
}

function startnews() {
	if (scrollrunning == 0) {
		scrollrunning = 1;
		//scrolltimer=setTimeout("scrollnews()",newsscrollspeed);
		scrolltimer=setInterval("scrollnews()",newsscrollspeed);
	}
}

function stopnews() {
	if (scrollrunning == 1) {
		clearInterval(scrolltimer);
		scrollrunning = 0;
	}
}

$(document).ready(function() {
	if ($('#newslinks_ul p:nth-child(1) a').text().length > (newslinkcharlimit + 3)){
		$('#newslinks_ul p:nth-child(1) a').text($('#newslinks_ul p:nth-child(1) a').text().substring(0, newslinkcharlimit)+"...");
	}
	startnews();
	$('#newslinks').mouseenter(function(){
		stopnews();
	});
	$('#newslinks').mouseleave(function(){
		if (scrollrunning == 0) {
			startnews();
		}
	});
	
	var focus_height1 = parseInt($('#cdesecondtierblock1 p:nth-child(3)').height());
	var focus_height2 = parseInt($('#cdesecondtierblock2 p:nth-child(3)').height());
	if (focus_height1 > focus_height2){
		$('#cdesecondtierblock2 p:nth-child(3)').height(focus_height1);
	} else {
		$('#cdesecondtierblock1 p:nth-child(3)').height(focus_height2);
	}
	
	var tier3_height
	var tier3_height1 = parseInt($('#cdethirdtierblock1 p:first').height());
	var tier3_height2 = parseInt($('#cdethirdtierblock2 p:first').height());
	var tier3_height3 = parseInt($('#cdethirdtierblock3 p:first').height());
	
	if (tier3_height1 > tier3_height2){
		tier3_height = tier3_height1;
	} else {
		tier3_height = tier3_height2;
	}
	if (tier3_height3 > tier3_height){
		tier3_height = tier3_height3;
	}
	$('#cdethirdtierblock1 p:first').height(tier3_height);
	$('#cdethirdtierblock2 p:first').height(tier3_height);
	$('#cdethirdtierblock3 p:first').height(tier3_height);

	$('#InformedSignUp').mouseenter(function(){
		$('#InformedLabel img:first').attr("src","/images/homeslideBUTTON_ICON_SignUp-Label.gif");
		$('#InformedSignUp img:first').attr("src","/images/homeslideBUTTON_ICON_SignUp_ON.gif");
	});	
	$('#InformedSignUp').mouseleave(function(){
		$('#InformedLabel img:first').attr("src","/images/homeslideBUTTON_ICON_StayInformed_STATIC-Label.gif");
		$('#InformedSignUp img:first').attr("src","/images/homeslideBUTTON_ICON_SignUp_OFF.gif");
	});	

	$('#InformedView').mouseenter(function(){
		$('#InformedLabel img:first').attr("src","/images/homeslideBUTTON_ICON_ViewReleases-Label.gif");
		$('#InformedView img:first').attr("src","/images/homeslideBUTTON_ICON_View_ON.gif");
	});	
	$('#InformedView').mouseleave(function(){
		$('#InformedLabel img:first').attr("src","/images/homeslideBUTTON_ICON_StayInformed_STATIC-Label.gif");
		$('#InformedView img:first').attr("src","/images/homeslideBUTTON_ICON_View_OFF.gif");
	});	

	$('#InformedTwitter').mouseenter(function(){
		$('#InformedLabel img:first').attr("src","/images/homeslideBUTTON_ICON_Twitter-Label.gif");
		$('#InformedTwitter img:first').attr("src","/images/homeslideBUTTON_ICON_Twitter_ON.gif");
	});	
	$('#InformedTwitter').mouseleave(function(){
		$('#InformedLabel img:first').attr("src","/images/homeslideBUTTON_ICON_StayInformed_STATIC-Label.gif");
		$('#InformedTwitter img:first').attr("src","/images/homeslideBUTTON_ICON_Twitter_OFF.gif");
	});	
	startfocus();
	$('#FocalPoint').mouseenter(function(){
		stopfocus();
	});
	$('#FocalPoint').mouseleave(function(){
		if (focusrunning == 0) {
			startfocus();
		}
	});
	$('#FocalPoint_ul li:nth-child('+focallisthighlight+')').css({'backgroundColor' : 'transparent'});
	$('#FocalPoint_ul li:nth-child(1)').click(function(){
  		window.location = $('#FocalPoint_ul li:nth-child(1) a').attr('href');
	});
	$('#FocalPoint_ul li:nth-child(1)').mouseover(function(){
		updatefocus(1);
	});
	$('#FocalPoint_ul li:nth-child(2)').click(function(){
  		window.location = $('#FocalPoint_ul li:nth-child(2) a').attr('href');
	});
	$('#FocalPoint_ul li:nth-child(2)').mouseover(function(){
		updatefocus(2);
	});
	$('#FocalPoint_ul li:nth-child(3)').click(function(){
  		window.location = $('#FocalPoint_ul li:nth-child(3) a').attr('href');
	});
	$('#FocalPoint_ul li:nth-child(3)').mouseover(function(){
		updatefocus(3);
	});
	$('#FocalPoint_ul li:nth-child(4)').click(function(){
  		window.location = $('#FocalPoint_ul li:nth-child(4) a').attr('href');
	});
	$('#FocalPoint_ul li:nth-child(4)').mouseover(function(){
		updatefocus(4);
	});
	$('#FocalPoint_ul li:nth-child(5)').click(function(){
  		window.location = $('#FocalPoint_ul li:nth-child(5) a').attr('href');
	});
	$('#FocalPoint_ul li:nth-child(5)').mouseover(function(){
		updatefocus(5);
	});
	$('#focalimage').wrap('<a href="'+$('#FocalPoint_ul li:nth-child(1) a').attr('href')+'" id="focuslink" />');
});
