// Carousel initialization callback function, 
// adds bindings and commands once the carousel is initialized
// -----------------------------------------------------------
function mycarousel_initCallback(carousel) {
	jQuery('.arrow-right').bind('click', function() {
		carousel.next();
		carousel.startAuto(0);
		return false;
	});
	jQuery('.arrow-left').bind('click', function() {
		carousel.prev();
		carousel.startAuto(0);
		return false;
	});
	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto(5); // This is the delay between scrolls after moving the mouse away.
	});	
	jQuery('#carousel_div').hover(function() {
		jQuery('.arrow-left').fadeTo('fast', 0.8);
		jQuery('.arrow-right').fadeTo('fast', 0.8);
	}, function() {
		jQuery('.arrow-left').fadeOut('fast');
		jQuery('.arrow-right').fadeOut('fast');
	});
};
// -----------------------------------------------------------

// jQuery document ready function, executes when the page is loaded and ready to receive user input
// -------------------------------------------------------------------------------------------------
jQuery(document).ready(function() {
	// Initialize the carousel using jCarousel
	// ---------------------------------------
    jQuery('#mycarousel').jcarousel({
    	wrap: 'circular',
		scroll: 1,
		auto: 5, // This is the delay between scrolls on startup. 
		initCallback: mycarousel_initCallback,
		buttonNextHTML: null,
        buttonPrevHTML: null
    });
	// --------------------------------------
	
	// Move the item text overlay to the right side of the carousel item
	// -----------------------------------------------------------------
	var text_width;
	var item_width = 568;
	var padding = 10; 
	if (navigator.userAgent.indexOf("Firefox") !=-1 || navigator.userAgent.indexOf("MSIE") !=-1) {
		padding = 10.2; //This value is 10.2 instead of 10 to fix line break issues in FF/IE9 on 64-bit Windows
	}
	jQuery('.item_text').each(function () {
		text_width = jQuery(this).width();
		jQuery(this).css('left', (item_width-text_width-padding*2) + 'px');
	});
	// -----------------------------------------------------------------
	
	
	// Firefox and SeaMonkey specific arrow position adjustment (for some reason they measure position differently)
	// ----------------------------------------------------------------------------------------------------------
	function positionFix() {
		var position = jQuery('#carousel_div').position();
		jQuery('.arrow-left').css('top', position.top + 50 + 'px');
		jQuery('.arrow-right').css('top', position.top + 50 + 'px');
		jQuery('.arrow-left').css('left', position.left + 50 + 'px');
		jQuery('.arrow-right').css('left', position.left + 518 + 'px');
		
		position = jQuery('#left_hand_text_div').position();
		jQuery('.left_hand_text_box').css('top', position.top + 10 + 'px');
		jQuery('.left_hand_text_box').css('left', position.left + 10 + 'px');
	}
	
	if (navigator.userAgent.indexOf("Firefox") !=-1 || navigator.userAgent.indexOf("SeaMonkey") !=-1) {
		positionFix();
	}
	
	var resizeTimer;
	jQuery(window).resize(function() {
		if (navigator.userAgent.indexOf("Firefox") !=-1 || navigator.userAgent.indexOf("SeaMonkey") !=-1) {
			clearTimeout(resizeTimer);
			resizeTimer = setTimeout(positionFix, 100);
		}
	});
	// ----------------------------------------------------------------------------------------------------------
});
// -------------------------------------------------------------------------------------------------
