$(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 classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" width="802" height="500" id="readerApplication"><param name="movie" value="http://c177366.r66.cf3.rackcdn.com/CdnReader.swf?issueID='+issueID+'&mode=embed" /><param name="quality" value="high" /><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><embed height="500" width="802" allowfullscreen="true" allowscriptaccess="always" wmode="transparent" type="application/x-shockwave-flash" src="http://c177366.r66.cf3.rackcdn.com/CdnReader.swf" quality="high" name="Reader"  pluginspace="http://www.macromedia.com/go/getflashplayer" flashvars="issueID='+issueID+'&mode=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();
    });
};

