Epson.Carousel =
{
	teasers:[],

	setup: function()
	{
		var swf = jQuery( '#FlashCarousel' )[0].className.match( /fc-swf-([^\s]+)/ );
		var config = jQuery( '#FlashCarousel' )[0].className.match( /fc-config-([^\s]+)/ );
		var data = jQuery( '#FlashCarousel' )[0].className.match( /fc-data-([^\s]+)/ );
		var panel = jQuery( '#FlashCarousel' )[0].className.match( /fc-panel-([^\s]+)/ );
		if ( panel ) panel = panel[1];
		if ( !swf || !config || !data ) return;

		var flashvars = { carouselDataXmlPath:config[1], panelDataXmlPath:data[1] };

		// if the page has a pre-defined item, set it
		var anchor = window.location && window.location.toString().match( /#([a-zA-Z]+)$/ );
		if ( anchor ) anchor = anchor[1];
		flashvars.panelName = panel || anchor;

		// write in flash module
		swfobject.embedSWF( swf[1], 'FlashCarousel', 940, 400, '9.0.28', false, flashvars, { wmode:'transparent' } );
		
		// load in correct teaser
		if ( anchor ) Epson.Carousel.loadTeasers( anchor[1], 0 );		
	},

	loadTeasers: function( item, direction )
	{
		var teasers = Epson.Carousel.teasers[ item ];

		if ( teasers )
		{
			// set up scrolling dimensions
			jQuery( '#Teasers' ).css( { overflow:'hidden', position:'relative' } ).find( 'ul' ).css( { width:'1880px' } );

			var x = direction ? 0 : 2;
			while ( true )
			{
				var teaser;

				if ( teasers[x].type == 0 )
				{
					teaser = jQuery( '<li class="type0"><h4><a href="'+teasers[x].link+'">'+teasers[x].title+'</a></h4><p>'+teasers[x].description+'</p><a href="'+teasers[x].link+'"><img src="'+teasers[x].image+'" /></a></li>' );
				}
				else if ( teasers[x].type == 1 )
				{
					teaser = jQuery( '<li class="type1"><h4><a href="'+teasers[x].link+'">'+teasers[x].title+'</a></h4><p>'+teasers[x].description+'</p><a href="'+teasers[x].link+'"><img src="'+teasers[x].image+'" /></a></li>' );
				}
				else if ( teasers[x].type == 2 )
				{
					teaser = jQuery( '<li class="type2"><a href="'+teasers[x].link+'"><img src="'+teasers[x].image+'" /></a></li>' );
				}

				if ( direction )
				{
					jQuery( '#Teasers ul' ).append( teaser );
					if ( ++x > 2 ) break;
				}
				else
				{
					jQuery( '#Teasers ul' ).prepend( teaser );
					jQuery( '#Teasers .content' ).css( { marginLeft:parseInt( jQuery( '#Teasers .content' ).css( 'marginLeft' ) ) - 314 + 'px' } );
					if ( --x < 0 ) break;
				}
			}

			// animate new teasers in
			if ( direction )
			{
				jQuery( '#Teasers .content' ).animate( { marginLeft:-940 }, 750, 'swing', function(){ for ( var x = 0; x < 3; x++ ) { jQuery( '#Teasers ul li:first' ).remove(); jQuery( '#Teasers .content' ).css( { marginLeft:parseInt( jQuery( '#Teasers .content' ).css( 'marginLeft' ) ) + 314 + 'px' } ) }; jQuery( '#Teasers .content' ).css( { marginLeft:'' } ); jQuery( '#Teasers' ).css( { overflow:'' } ).find( 'ul' ).css( { width:'' } ) } );
			}
			else
			{
				jQuery( '#Teasers .content' ).animate( { marginLeft:0 }, 750, 'swing', function(){ for ( var x = 0; x < 3; x++ ) { jQuery( '#Teasers ul li:last' ).remove(); }; jQuery( '#Teasers .content' ).css( { marginLeft:'' } ); jQuery( '#Teasers' ).css( { overflow:'' } ).find( 'ul' ).css( { width:'' } ) } );
			}

			// update position in window location
//			window.location = window.location.toString().match( /^([^#]+)/ )[1] + '#' + item;
		}
	}
}
