$('div.tim-chat a').click( function() {
	pageTracker._trackEvent('Post-Carousel Mini Ad', 'click', 'Click to Census Map');
});

$('div#carousel h3 a').click( function() {
	thisLink = $(this).attr('rel').replace(/^\w+/, '');
	thisLink = 'Story headline link ' + thisLink;
	pageTracker._trackEvent('Carousel', 'Click', thisLink);
});

$('div#carousel a.img').click( function() {
	thisLink = $(this).attr('rel').replace(/^\w+/, '');
	thisLink = 'Story lead photo link ' + thisLink;
	pageTracker._trackEvent('Carousel', 'Click', thisLink)
});

$('div#carousel div.tools a').click( function() {
	thisLink = $(this).attr('rel').replace(/^\w+/, '');
	thisLink = 'Story comment link ' + thisLink;
	pageTracker._trackEvent('Carousel', 'Click', thisLink)
});

jQuery.logThis = function( text ){
    if( (window['console'] !== undefined) ){
        console.log( text );
    }
}

function topcar_callbacks(carousel) {
	$('#carousel .carousel-controller a.left').click( function() {
		carousel.prev();
		$.logThis('moving to previous spot');
		return false;
	});
	$('#carousel .carousel-controller a.right').click( function() {
		carousel.next();
		$.logThis('moving to next spot');
		return false;
	});
	$('#carousel .carousel-controller a.btn').click( function() {
		thisStory = $(this).attr('rel');
		$.logThis('moving to ' + thisStory + ' which is jcar ' + $.jcarousel.intval(thisStory));
		carousel.scroll($.jcarousel.intval(thisStory));
		return false;
	});
}

function newHighLight (object, element, index, state) {
	while(index > totalListItems) {
		index = index - totalListItems;
	}
	imgIndex = index - 1;
	$('#carousel .carousel-controller a.btn img').removeClass('active');
	$('#carousel .carousel-controller a.btn img').eq(imgIndex).addClass('active');
}

$(document).ready( function() {
	totalListItems = $('#carousel ul li').length;
	$('#carousel ul').jcarousel({
		scroll: 1,
		visible: 1,
		auto: 9,
		wrap: 'circular',
		initCallback: topcar_callbacks,
		buttonNextHTML: null,
		buttonPrevHTML: null,
		itemVisibleInCallback: newHighLight
	});
	$('#special_coverage_promo_container ul').jcarousel({
		scroll: 1,
		auto: 7,
		wrap: 'circular'
	});
});
