
$j = jQuery.noConflict();
$scrollbarWidth = 100;
$widthOfImageSet = 0;

// functionality to allow for scrollbar on the sponsors area on homepage

function imageGalleryScroll(directionAmount) {
	// this is a helper function which runs when we hover over left/right buttons
	var $existingMarginLeft = $j('.sponsors .scrollarea').css('margin-left').replace(/px/gi, '');
	var $newMarginLeft = (parseInt($existingMarginLeft) + parseInt(directionAmount));
	if ($newMarginLeft > 0) { $newMarginLeft = 0; }
	if ($newMarginLeft < (0 - $widthOfImageSet + $scrollbarWidth)) { $newMarginLeft = (0 - $widthOfImageSet + $scrollbarWidth); }
	$j('.sponsors .scrollarea').css('margin-left', $newMarginLeft + 'px');
}

$j(function() {
	// this is the page onload() function.... setup the scrollbars / variables
	$scrollbarWidth = $j('.sponsors .scrollbar').width();
	$j('.sponsors .scrollbar img').each(function() {
		$widthOfImageSet += $j(this).width() + 1;
	});

	if ($j('.sponsors .image').length < 1) {
		$j('.sponsors .info').hide();
	}

	if ($j('.sponsors .image').length > 2) {
		$j('.sponsors .scrollbar').css('margin-left', '13px');
		var timer1 = setTimeout('initialScrollAnimate1()', 2000);
	}
});

function initialScrollAnimate1() {
	// automatically animate one scroll through
	$j('.sponsors .scrollarea').animate({'marginLeft': '-'+($widthOfImageSet - $scrollbarWidth)+'px'}, 4000, 'linear', function() { var timer2 = setTimeout('initialScrollAnimate2()', 1000); });
}
function initialScrollAnimate2() {
	// quickly return to initial position
	$j('.sponsors .scrollarea').animate({'marginLeft': '0px'}, 500, 'linear', function() { initialScrollSetup(); });
}
function initialScrollSetup() {
	// set up page for manual scrolling as necessary
	var scrollInterval;
	$j('.sponsors .scrollbar').before('<div class="scrollbutton" style="width:13px"><a href="#" class="imageGalleryScrollButton" da="+7"><img src="' + $templateBaseUrl + '/images/scrollbutton-left.gif" alt="Scroll left" /><br /></a></div>');
	$j('.sponsors .scrollbar').after('<div class="scrollbutton" style="width:13px"><a href="#" class="imageGalleryScrollButton" da="-7"><img src="' + $templateBaseUrl + '/images/scrollbutton-right.gif" alt="Scroll right" /><br /></a></div>');
	$j('.sponsors .scrollbar').css('margin-left', '0px');
	$j('.imageGalleryScrollButton').hover(
		function() {
			var directionAmount = $j(this).attr('da');
			scrollInterval = setInterval('imageGalleryScroll('+directionAmount+')', 50);
		},
		function() {
			clearInterval(scrollInterval);
		}
	);
}

