function afterAnimation(carousel){
if(carousel.autoStopped){
	carousel.startAuto()
}
}	
	
function imageChange(current) {

	a = 0;
	$("#header-image img").each(function(i) {
	a++;
	if(current == a) {
	$("#squares div#"+current).attr('class','sel');
	} else {
	$("#squares div#"+a).attr('class','');
	}
	});
	return false;
}

function trigger(carousel, state) {
imageChange(carousel.first);
return false;
}

function mycarousel_initCallback(jcarousel) {
	jQuery('#squares div').bind('click', function() {
		jcarousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
		
		return false;
	});
};


$(document).ready(function() {
	p = 0;
	$("#header-image img").each(function(i) { 
	p++;
		var alt = $(this).attr("alt");
		var headertext = alt.split("|", 2);
		
		if(headertext[0] == null) {
			headertext[0] = "";
		}
		if(headertext[1] == null) {
			headertext[1] = "";
		}
		
		$("#frontpageScrollerList").append('<li><span class="headertext"><span class="blue">'+headertext[0]+'</span><br />'+headertext[1]+'</span><img src="'+$(this).attr("src")+'" src="'+$(this).attr("src")+'" alt="'+$(this).attr("alt")+'" width="'+$(this).css("width")+'" height="'+$(this).css("height")+'" /></li>');

		$("#squares").append('<div id="'+p+'">'+p+'</div><span class="space"></span>');

	});
	
	if(p == 1) {
	$("#squares").attr('style','display:none;');
	}
	
	$('#frontpageScrollerList').jcarousel({
		wrap: 'last',
		vertical: true,
		easing: 'swing',
		animation: 500,
		auto: 4,
		scroll: 1,
		visible: 1,
		initCallback: mycarousel_initCallback,
		itemLoadCallback: trigger,
		itemLastInCallback:{
		onAfterAnimation: afterAnimation
		}
		
	});
});
