/**
 * We use the initCallback callback
 * to assign functionality to the controls
 */
function mycarousel_initCallback(carousel) {

    // Pause autoscrolling if the user moves with the cursor over the clip.
    /*carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });*/
    
    jQuery('.jcarousel-control a').bind('click', function() {											  
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        //stop autoscroll if someone interacts with the carousel
        carousel.startAuto(0);
        return false;
    });
    
};

//function to select the external control to focus when autoscroll is applied so that the proper style is applied
function mycarousel_itemFirstInCallback(carousel, item, idx, state) { 
    //selected_div_id = "jcarousel-control-" + idx;
    //document.getElementById(selected_div_id).focus();
    //selected_pane_id = "mycarousel-pane-" + idx;
    if (idx == 1) {
        document.getElementById('jcarousel-control-1').style.color = '#fff';
        document.getElementById('jcarousel-control-1').style.backgroundColor = '#CC6600';
        document.getElementById('jcarousel-control-2').style.color = '#666';
        document.getElementById('jcarousel-control-2').style.backgroundColor = '#ccc';
        document.getElementById('jcarousel-control-3').style.color = '#666';
        document.getElementById('jcarousel-control-3').style.backgroundColor = '#ccc';
    } 
    else if (idx == 2) {
        document.getElementById('jcarousel-control-1').style.color = '#666';
        document.getElementById('jcarousel-control-1').style.backgroundColor = '#ccc';
        document.getElementById('jcarousel-control-2').style.color = '#fff';
        document.getElementById('jcarousel-control-2').style.backgroundColor = '#CC6600';
        document.getElementById('jcarousel-control-3').style.color = '#666';
        document.getElementById('jcarousel-control-3').style.backgroundColor = '#ccc';
    } 
    else { 
        document.getElementById('jcarousel-control-1').style.color = '#666';
        document.getElementById('jcarousel-control-1').style.backgroundColor = '#ccc';
        document.getElementById('jcarousel-control-2').style.color = '#666';
        document.getElementById('jcarousel-control-2').style.backgroundColor = '#ccc';
        document.getElementById('jcarousel-control-3').style.color = '#fff';
        document.getElementById('jcarousel-control-3').style.backgroundColor = '#CC6600';
    } 		
    
}; 


// Ride the carousel...
jQuery(document).ready(function() {
    $("#jcarousel-pane-2").css("display","block");
    $("#jcarousel-pane-3").css("display","block");							
    jQuery("#mycarousel").jcarousel({
        auto: 5,
        wrap: 'last',
        scroll: 1,
        initCallback: mycarousel_initCallback,
        itemFirstInCallback:mycarousel_itemFirstInCallback, 
        //itemLoadCallback:mycarousel_itemLoadCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
});