$(document).ready(function(){
	
	// load the data for the carousel
	$.get('/script/carousel.php',function(data){
		if(data) {
			$('#carousel').html(data);
			
			$('#carousel').jcarousel({
				scroll: 1,
				animation: 600,
				start: 1,
				offset: 1,
		        wrap: 'last',
				auto: 2,
		        initCallback: mycarousel_initCallback
		    });
			
			// bind the click event to opening a new digital edition
			$('#carousel img').bind('click',function(e){
				var issueID = $(this).attr('id');
				var pageServer = $(this).attr('src').substr(0,16);
				$('#reader').html('<object width="802" height="500" id="embed1"><param name="movie" value="'+pageServer+'/Reader.swf?issueID='+issueID+'&mode=embed"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" wmode="transparent" src="'+pageServer+'/Reader.swf" pluginspage="http://www.adobe.com/go/getflashplayer" width="802" height="500" flashvars="issueID='+issueID+'&mode=embed"></embed></object>');
			});
			
			// grab the first one in the list and click it
			$('.jcarousel-item:first img').trigger('click');
		}
	});
});

function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};
