Epson.ProGraphics =
{
	image:null,
	temp_image:null,
	description:null,
	products:[],
	transition:0,
	current:-1,
	current_item:null,
	
	list_element:null,

	setup: function()
	{
		Epson.ProGraphics.description = jQuery( '#ProShowCase .description' );

		// get data
		var x  = 0;
		jQuery( '#ProShowCaseList li' ).each( function()
		{
			var image = this.className.match( /psc-image-([^\s]+)/ );
			var heading = this.className.match( /psc-heading-([^\s]+)/ );
			var caption = this.className.match( /psc-text-([^\s]+)/ );
			var link = this.className.match( /psc-link-([^\s]+)/ );
			if (this.className.match( /psc-alt-([^\s]+)/ )) var alt = this.className.match( /psc-alt-([^\s]+)/ );
			else alt = '';

			if ( image && heading && caption )
			{
				// save data
				var product = { image:image[1], heading:unescape( heading[1] ), caption:unescape( caption[1] ), alt:unescape( alt[1] ) };
				if ( link ) product.link = link[1];

				Epson.ProGraphics.products.push( product );

				// set up events
				var index = x++;
				jQuery( this ).bind( 'mouseover', function(e) { Epson.ProGraphics.swap( this, index, e) } );
			}
		} );

		// preload images
		Epson.ProGraphics.preload();

		// create images
		Epson.ProGraphics.image = jQuery( '#ProShowCase img.mainImage' );
		Epson.ProGraphics.image.bind( 'load', Epson.ProGraphics.notifyLoaded );

		Epson.ProGraphics.temp_image = jQuery( '<img class="tempImage" src="/assets/images/blank.gif" />' );
		Epson.ProGraphics.temp_image.bind( 'load', Epson.ProGraphics.notifyTempLoaded );
		jQuery( '#ProShowCase' ).append( Epson.ProGraphics.temp_image );
	},

	swap: function( element, index, e )
	{
		if ( index != Epson.ProGraphics.current )
		{
			Epson.ProGraphics.list_element = element;

			if ( Epson.ProGraphics.transition == 1 )
			{
				if ( !jQuery.browser.msie )
				{
					if ( parseInt( Epson.ProGraphics.temp_image.css( 'opacity' ) * 10 ) > 10 )
					{
						var clone = Epson.ProGraphics.temp_image.stop().clone();
						jQuery( '#ProShowCase' ).append( clone.animate( { opacity:0, left:-50 }, 200, 'swing', function() { jQuery( this ).remove() } ) );
			
						Epson.ProGraphics.temp_image.stop().css({opacity:0});
					}
				}
				else Epson.ProGraphics.temp_image.css( { visibility:'hidden' } );
			}


			var target = e.target.nodeName == 'LI' ? e.target : jQuery( e.target ).parents( 'li' )[0];
			var related_target = e.relatedTarget.nodeName == 'LI' ? e.relatedTarget : jQuery( e.relatedTarget ).parents( 'li' )[0];
			if ( target == related_target ) return;
	
			// unhighlight old list item
			if ( Epson.ProGraphics.current_item && !jQuery.browser.msie ) Epson.ProGraphics.current_item.animate( { opacity:0.5 }, 300, 'swing' );
	
			Epson.ProGraphics.current = index;
	
			// clone old image for crossfade
			Epson.ProGraphics.temp_image[0].src = Epson.ProGraphics.image[0].src;
		}
	},

	notifyLoaded: function(e)
	{
		if ( Epson.ProGraphics.current != -1 )
		{
			Epson.ProGraphics.transition = 1;
	
			// fade out old image
			if ( !jQuery.browser.msie )
			{
				Epson.ProGraphics.temp_image.animate( { opacity:0, left:-50 }, 200, 'swing', function()
				{
					Epson.ProGraphics.transition = 2;
	
					// fade in new image
					Epson.ProGraphics.image.animate( { opacity:1, left:0 }, 500, 'swing', Epson.ProGraphics.cleanup );
				} );
			}
			else
			{
				Epson.ProGraphics.temp_image.css( { visibility:'hidden', top:-10000 } );
				Epson.ProGraphics.image.css( { visibility:'visible' } );
				Epson.ProGraphics.cleanup();
			}
	
			// show image
			Epson.ProGraphics.image.attr( 'alt', Epson.ProGraphics.products[ Epson.ProGraphics.current ].alt )
	
			// change text
			Epson.ProGraphics.description.find( 'h1' ).html( Epson.ProGraphics.products[ Epson.ProGraphics.current ].heading );
			Epson.ProGraphics.description.find( 'p' ).html( Epson.ProGraphics.products[ Epson.ProGraphics.current ].caption );
	
			// bind image to link
			if ( Epson.ProGraphics.products[ Epson.ProGraphics.current ].link ) Epson.ProGraphics.image.css( { cursor:'pointer' } ).bind( 'click', function() { window.location = Epson.ProGraphics.products[ Epson.ProGraphics.current ].link } );
			else { Epson.ProGraphics.image.css( { cursor:'default' } ).unbind( 'click' ) }
	
			if ( Epson.ProGraphics.products[ Epson.ProGraphics.current ].link && Epson.ProGraphics.description.find( 'a' ).length )
			{
				Epson.ProGraphics.description.find( 'a' ).css( { display:'inline-block' } ).attr( { href:Epson.ProGraphics.products[ Epson.ProGraphics.current ].link } );
			}
			else Epson.ProGraphics.description.find( 'a' ).css( { display:'none' } );
		}
	},

	notifyTempLoaded: function()
	{
		if ( Epson.ProGraphics.transition == 1 )
		{
			if ( !jQuery.browser.msie )
			{
				Epson.ProGraphics.temp_image.stop().css( { left:Epson.ProGraphics.image.css( 'left' ), opacity:Epson.ProGraphics.image.css( 'opacity' ), zIndex:10  } );
			}
			else Epson.ProGraphics.temp_image.css( { visibility:'hidden' } );
		}
		else if ( Epson.ProGraphics.transition == 2 )
		{
			Epson.ProGraphics.image.stop();

			Epson.ProGraphics.temp_image.stop().css( { left:Epson.ProGraphics.image.css( 'left' ), opacity:Epson.ProGraphics.image.css( 'opacity' ), zIndex:10  } );
		}
		else Epson.ProGraphics.temp_image.css( { opacity:1, left:0, top:0, zIndex:10 } );		


		// change image
		if ( !jQuery.browser.msie ) Epson.ProGraphics.image.css( { opacity:0, left:'50px', zIndex:11 } );
		else Epson.ProGraphics.image.css( { visibility:'hidden' } );
		//alert(Epson.ProGraphics.current);
		if ( Epson.ProGraphics.products[ Epson.ProGraphics.current ].image.match( /([^\/]+)$/ )[1] != Epson.ProGraphics.image[0].src.match( /([^\/]+)$/ )[1] )
		{
			Epson.ProGraphics.image[0].src = Epson.ProGraphics.products[ Epson.ProGraphics.current ].image;
		}
		else Epson.ProGraphics.notifyLoaded();

		// fade in current item
		Epson.ProGraphics.current_item = jQuery( Epson.ProGraphics.list_element );
		if ( !jQuery.browser.msie ) Epson.ProGraphics.current_item.animate( { opacity:1 }, 300, 'swing' );
	},

	cleanup: function()
	{
		Epson.ProGraphics.transition = false;
	},

	preload: function()
	{
		jQuery.each( Epson.ProGraphics.products, function()
		{
			var image = new Image();
			image.src = this.image;
		} );
	}
}