/*
 * Title: Boutique carousel jQuery plugin
 * Author: Berend de Jong, Frique
 * Author URI: http://www.frique.me/
 * Version: 1.33 (20110910.2)
 */

(function($){
	$.fn.caroussel = function(options){

		// OPTION DEFAULTS
		var o = $.extend({
			container_width:		800,			// Total carousel width (value in pixels or "100%")
			front_img_width:		280,			// Width of the frontal image (value in pixels)
			front_img_height:		400,			// Height of the frontal image (value in pixels)
			starter:				1,				// Which frame to start with (number)
			speed:					600,			// Overall animation speed (time in ms)
			behind_opac:			0.4,			// Opacity of the furter back items (between or equal to 0-1)
			back_opac:				0.15,			// Opacity of the furthest back items (between or equal to 0-1)
			behind_size:			0.7,			// Size of the further back images (between or equal to 0-1)
			back_size:				0.4,			// Size of the furthest back images (between or equal to 0-1)
			behind_distance:		'auto',			// Option to manually set the horizontal position of the behind frames (number 2 and 4) ('auto' or pixel value)
			autoplay:				false,			// Autoplay on/off (true/false)
			autointerval:			3000,			// Autoplay interval (time in ms)
			freescroll:				true,			// Whether you can still navigate while animating (true/false)
			hovergrowth:			0.08,			// How much the front item will enlarge on mouse-over (between or equal to 0-1)
			easing:					'easeInOutQuad',// Standard easing type if the easing plugin is provided (easing type title)
			move_more_easein:		'easeInQuad',	// Easing type for the first part of moving twice (easing type title)
			move_more_easebetween:	'linear',		// Easing type for moving more than twice but not first and last (easing type title)
			move_more_easeout:		'easeOutQuad',	// Easing type for the second part of moving twice (easing type title)
			move_more_directly:		false,			// Will not bring the in-between frames forward when moving more than once (true/false)
			never_move_twice:		false,			// Prevents moving 2 steps when frame 1 or 5 is clicked (true/false)
			text_front_only:		false,			// Show the title/description only in the front frame (true/false)
			text_opacity:			0.7,			// Opacity of the title/description layer (value between or equal to 0-1)
			keyboard:				true,			// Enable/disable keyboard functionality (true/false)
			move_on_hover:			false			// Navigating with mouse-over instead of clicking (true/false)

			// Extra options:
			// hoverspeed = Speed in which the frontal frame will zoom in on mouse-over (time in ms) (default: speed/4)
		}, options);

		$(this).each(function(){

			var $container = $(this),
				$parent = $container.parent(),
				$lis = $('li', $container);

			// Set constants
			var autotimer, easingplugin, hoverspeed, $newitem1, $newitem2, $newitem3, $newitem4, $newitem5, zpeed, next, iegrow, container_height,
				containerid = $container.attr('id'),
				busy = false,
				current = o.starter,
				items = $lis.length,
				ie = false,
				ie6 = false,
				container_100 = false;
			if($.browser.msie){
				ie = true;
				if($.browser.version < 7){
					ie6 = true;
				}
			}
			if(o.hoverspeed){
				hoverspeed = o.hoverspeed;
			}else{
				hoverspeed = (o.speed/4);
			}
			if(o.starter > items){
				o.starter = items;
			}

			// Easing type for easing plugin:
			if( $.easing.def ){
				easingplugin = true;
				$.easing.def = o.easing;
			}else{
				easingplugin = false;
			}

			// For each list item...
			var x=1;
			var $li = [];
			$lis.each(function(){
				var $this = $(this);
				// Number all items
				$this.addClass('li'+x);
				// Set headers from alt tags
				var header = $this.find('img').attr('alt'),
					$span = $this.find('span');
				if( !$span.length ){
					if( $this.find('a').length ){ $this.children('a').append('<span/>'); }
					else{ $this.append('<span/>'); }
					$span = $this.find('span');
					if(!header){ $span.hide(); }
				}else{
					$span = $this.find('span');
				}
				var $h6 = $('<h6>'+header+'</h6>').prependTo($span);
				if(!header){ $h6.hide(); }
				// Cache the element:
				$li[x] = $this;
				x++;
			});

			// Set order id's
			if( items==1 ){
				$li[1].clone().addClass('frame1').prependTo($container);
				$li[1].clone().addClass('frame2').prependTo($container);
			}
			else if( o.starter == 2 ){
				$li[1].clone().addClass('frame2').prependTo($container);
				$li[items].clone().addClass('frame1').prependTo($container);
			}
			else if( o.starter == 1 ){
				$li[items-1].clone().addClass('frame1').prependTo($container);
				$li[items].clone().addClass('frame2').prependTo($container);
			}
			else{
				$li[o.starter-1].clone().addClass('frame2').prependTo($container);
				$li[o.starter-2].clone().addClass('frame1').prependTo($container);
			}
			$li[o.starter].clone().addClass('frame3').prependTo($container);
			if( items==1 ){
				$li[1].clone().addClass('frame4').prependTo($container);
				$li[1].clone().addClass('frame5').prependTo($container);
			}
			else if( o.starter == (items-1) ){
				$li[items].clone().addClass('frame4').prependTo($container);
				$li[1].clone().addClass('frame5').prependTo($container);
			}
			else if( o.starter == items ){
				$li[1].clone().addClass('frame4').prependTo($container);
				$li[2].clone().addClass('frame5').prependTo($container);
			}
			else{
				$li[o.starter+1].clone().addClass('frame4').prependTo($container);
				$li[o.starter+2].clone().addClass('frame5').prependTo($container);
			}

			// Cache the first 5
			var $item1 = $container.find('.frame1'),
				$item2 = $container.find('.frame2'),
				$item3 = $container.find('.frame3'),
				$item4 = $container.find('.frame4'),
				$item5 = $container.find('.frame5');

			// Set CSS classes
			$item1.add($item5).show().animate({ opacity:0 }, 0).addClass('back');
			$item2.add($item4).show().animate({ opacity:0 }, 0).addClass('behind');
			$item3.show().animate({ opacity:0 }, 0).addClass('front');

			// Set variables based on CSS classes
			var $back = $('.back', $container),
				$behind = $('.behind', $container),
				$front = $('.front', $container);

			if(o.container_width=='100%'){
				container_100 = true;
				o.container_width = parseInt($parent.width(), 10);
			}

			var li_border			= parseInt( $lis.css('borderLeftWidth'), 10 ),
				li_padding			= parseInt( $lis.css('padding-left'), 10 ),
				front_header		= $('h6', $front).css('font-size'),
				front_span			= $('span', $front).css('font-size'),
				front_top			= $front.css('margin-top'),
				front_margin		= parseInt( $('img', $front).css('margin-left'), 10 ),
				front_width			= Math.round( o.front_img_width + (front_margin*2) + (li_padding*2) + (li_border*2) ),
				front_height		= Math.round( o.front_img_height + (front_margin*2) + (li_padding*2) + (li_border*2) ),
				behind_img_width	= Math.round(o.front_img_width * o.behind_size ),
				behind_img_height	= Math.round(o.front_img_height * o.behind_size ),
				behind_header		= $('h6', $behind).css('font-size'),
				behind_span			= $('span', $behind).css('font-size'),
				behind_top			= $behind.css('margin-top'),
				behind_margin		= parseInt( $('img', $behind).css('margin-left'), 10 ),
				behind_width		= Math.round( behind_img_width + (behind_margin*2) + (li_padding*2) + (li_border*2) ),
				behind_height		= Math.round( behind_img_height + (behind_margin*2) + (li_padding*2) + (li_border*2) ),
				back_img_width		= Math.round(o.front_img_width * o.back_size),
				back_img_height		= Math.round(o.front_img_height * o.back_size),
				back_header			= $('h6', $back).css('font-size'),
				back_span			= $('span', $back).css('font-size'),
				back_top			= $back.css('margin-top'),
				back_margin			= parseInt( $('img', $back).css('margin-left'), 10 ),
				back_width			= Math.round( back_img_width + (back_margin*2) + (li_padding*2) + (li_border*2) ),
				back_height			= Math.round( back_img_height + (back_margin*2) + (li_padding*2) + (li_border*2) ),
				item3_pos			= Math.round( (o.container_width/2)-(front_width/2) ),
				item5_pos			= (o.container_width - back_width),
				item2_pos;

			// Optional custom behind frame distance
			if(o.behind_distance != 'auto'){
				item2_pos = parseInt(o.behind_distance, 10);
			}else{
				item2_pos = Math.round( (o.container_width/4)-(behind_width/2) );
			}
			var item4_pos = (o.container_width - item2_pos - behind_width);

			// Remove CSS classes
			$back.removeClass('back');
			$behind.removeClass('behind');
			$front.removeClass('front');

			// Deal with the text container <span> padding for future animation
			var front_span_paddingTop = $('span', $lis).css('padding-top'),
				front_span_paddingRight = $('span', $lis).css('padding-right'),
				front_span_paddingBottom = $('span', $lis).css('padding-bottom'),
				front_span_paddingLeft = $('span', $lis).css('padding-left'),
				front_span_animate = {
					opacity: o.text_opacity,
					fontSize: front_span,
					paddingTop: front_span_paddingTop,
					paddingRight: front_span_paddingRight,
					paddingBottom: front_span_paddingBottom,
					paddingLeft: front_span_paddingLeft
					},
				behind_span_animate = {
					opacity: o.text_opacity,
					fontSize: behind_span,
					paddingTop: Math.round( parseInt(front_span_paddingTop, 10)*0.8 ),
					paddingRight: Math.round( parseInt(front_span_paddingRight, 10)*0.8 ),
					paddingBottom: Math.round( parseInt(front_span_paddingBottom, 10)*0.8 ),
					paddingLeft: Math.round( parseInt(front_span_paddingLeft, 10)*0.8 )
					},
				back_span_animate = {
					opacity: o.text_opacity,
					fontSize: back_span,
					paddingTop: Math.round( parseInt(front_span_paddingTop, 10)*0.6 ),
					paddingRight: Math.round( parseInt(front_span_paddingRight, 10)*0.6 ),
					paddingBottom: Math.round( parseInt(front_span_paddingBottom, 10)*0.6 ),
					paddingLeft: Math.round( parseInt(front_span_paddingLeft, 10)*0.6 )
					};
			if(o.text_front_only){
				behind_span_animate.opacity = 0;
				back_span_animate.opacity = 0;
			}
			if(ie6){
				var front_span_margin = (parseInt($('span:visible', $front).css('margin-left'), 10) + parseInt($('span:visible', $front).css('margin-right'), 10)),
					behind_span_margin = (parseInt($('span:visible', $behind).css('margin-left'), 10) + parseInt($('span:visible', $behind).css('margin-right'), 10)),
					back_span_margin = (parseInt($('span:visible', $back).css('margin-left'), 10) + parseInt($('span:visible', $back).css('margin-right'), 10)),
					ie6_front_span_animate = $.extend({ width:front_width-parseInt(front_span_paddingRight, 10)-parseInt(front_span_paddingLeft, 10)-front_span_margin-(li_border*2) }, front_span_animate),
					ie6_behind_span_animate = $.extend({ width:behind_width-behind_span_animate.paddingRight-behind_span_animate.paddingLeft-behind_span_margin-(li_border*2) }, behind_span_animate),
					ie6_back_span_animate = $.extend({ width:back_width-back_span_animate.paddingRight-back_span_animate.paddingRight-back_span_margin-(li_border*2) }, back_span_animate);
			}

			// Get total container height
			var front_space = (front_height + parseInt(front_top, 10)),
				behind_space = (behind_height + parseInt(behind_top, 10)),
				back_space = (back_height + parseInt(back_top, 10));
			if( front_space > behind_space && front_space > back_space ){ container_height = front_space; }
			else if( behind_space > front_space && behind_space > back_space ){ container_height = behind_space; }
			else{ container_height = back_space; }

			// Starting positions
			$container
				.height(container_height)
				.width(o.container_width);
			$item1.css({ left:0, top:back_top }).animate({ opacity:o.back_opac }, 0)
				.find('img').animate({ width:back_img_width, height:back_img_height, margin:back_margin, opacity:1 }, 0)
				.siblings('span:visible').css(back_span_animate)
				.children('h6:visible').css({'font-size':back_header});
			$item2.css({ left:item2_pos, top:behind_top, 'z-index':2 }).animate({ opacity:o.behind_opac }, 0)
				.find('img').animate({width:behind_img_width, height:behind_img_height, margin:behind_margin, opacity:1}, 0)
				.siblings('span:visible').css(behind_span_animate)
				.children('h6:visible').css({'font-size':behind_header});
			$item3.css({ left:item3_pos, top:front_top, 'z-index':3 }).animate({ opacity:1 }, 0)
				.find('a *').css({ cursor:'pointer' }).end()
				.find('img').animate({width:o.front_img_width, height:o.front_img_height, margin:front_margin, opacity:1}, 0)
				.siblings('span:visible').css(front_span_animate)
				.children('h6:visible').css({'font-size':front_header});
			$item4.css({ left:item4_pos, top:behind_top, 'z-index':2 }).animate({ opacity:o.behind_opac }, 0)
				.find('img').animate({width:behind_img_width, height:behind_img_height, margin:behind_margin, opacity:1}, 0)
				.siblings('span:visible').css(behind_span_animate)
				.children('h6:visible').css({'font-size':behind_header});
			$item5.css({ left:item5_pos, top:back_top }).animate({ opacity:o.back_opac }, 0)
				.find('img').animate({width:back_img_width, height:back_img_height, margin:back_margin, opacity:1}, 0)
				.siblings('span:visible').css(back_span_animate)
				.children('h6:visible').css({'font-size':back_header});
			if(ie6){
				$('span:visible', $back).css(ie6_back_span_animate);
				$('span:visible', $behind).css(ie6_behind_span_animate);
				$('span:visible', $front).css(ie6_front_span_animate);
			}

// FUNCTIONS

			// Set container width
			function setContainerWidth(){
				// Container
				var width = parseInt($parent.width(), 10);
				$container.width( width );
				// Position frames
				item2_pos = Math.round( (width/4)-(behind_width/2) );
				item3_pos = Math.round( (width/2)-(front_width/2) );
				item4_pos = (width - item2_pos - behind_width);
				item5_pos = (width - back_width);
				$item2.css({ left:item2_pos });
				$item3.css({ left:item3_pos });
				$item4.css({ left:item4_pos });
				$item5.css({ left:item5_pos });
			};
			if(container_100){
				setContainerWidth();
			}

			// Autoplay functions
			function stopInterval(){
				if( autotimer ){
					clearInterval(autotimer);
					autotimer = false;
				}
			};
			function startInterval(){
				if( autotimer ){
					stopInterval();
				}
				autotimer = setInterval(function(){
					$item4.click();
				}, o.autointerval);
			};

			// Move right
			function moveRight(times, state){
				busy = true;
				state = state || 'normal';

				// Set easing type and easing speed
				var eazing = 'swing';
				zpeed = o.speed;
				if(times > 1 && state=='first'){
					if(easingplugin){ eazing = o.move_more_easein; }
					zpeed = Math.round(o.speed * 0.7);
				}else if(times > 1){
					if(easingplugin){ eazing = o.move_more_easebetween; }
					zpeed = Math.round(o.speed * 0.7);
				}else if(state=='last'){
					if(easingplugin){ eazing = o.move_more_easeout; }
				}else{
					if(easingplugin){ eazing = o.easing; }
				}

				// Pause autoplay
				if(o.autoplay){
					stopInterval();
				}

				// Set next item number
				if( current == (items-2) ){
					next = 1;
				}else if( current == (items-1) ){
					next = 2;
					if(next > items){next = 1;}
				}else if( current == items ){
					next = 3;
					if(next > items){next = 1;}
				}
				else{
					next = (current+3);
				}

				// Move
				$item1.removeClass('frame1').addClass('remove').css('z-index', -1);

				$newitem1 = $item2;
				$newitem1.removeClass('frame2').addClass('frame1').stop().animate({ opacity:o.back_opac, left:0, top:back_top }, zpeed, eazing)
					.find('img').stop().animate({ width:back_img_width, height:back_img_height, marginTop:back_margin, marginRight:back_margin, marginBottom:back_margin, marginLeft:back_margin, opacity:1 }, zpeed, eazing)
					.end().find('h6:visible').stop().animate({ 'font-size':back_header }, zpeed, eazing);
				if(ie6){ $newitem1.find('span:visible').stop().animate(ie6_back_span_animate, zpeed, eazing); }
				else{ $newitem1.find('span').stop().animate(back_span_animate, zpeed, eazing); }
				setTimeout( function(){ $newitem1.css('z-index', 1); }, (zpeed/4));

				$newitem2 = $item3;
				$newitem2.removeClass('frame3').addClass('frame2').stop().animate({ opacity:o.behind_opac, left:item2_pos, top:behind_top }, zpeed, eazing)
					.find('img').stop().animate({ width:behind_img_width, height:behind_img_height, marginTop:behind_margin, marginRight:behind_margin, marginBottom:behind_margin, marginLeft:behind_margin, opacity:1 }, zpeed, eazing)
					.end().find('h6:visible').stop().animate({ 'font-size':behind_header }, zpeed, eazing);
				if(ie6){ $newitem2.find('span:visible').stop().animate(ie6_behind_span_animate, zpeed, eazing); }
				else{ $newitem2.find('span').stop().animate(behind_span_animate, zpeed, eazing); }
				setTimeout( function(){ $newitem2.css('z-index', 2); }, (zpeed/4));

				$newitem3 = $item4;
				$newitem3.removeClass('frame4').addClass('frame3').stop().animate({ opacity:1, left:item3_pos, top:front_top }, zpeed, eazing)
					.find('img').stop().animate({ width:o.front_img_width, height:o.front_img_height, marginTop:front_margin, marginRight:front_margin, marginBottom:front_margin, marginLeft:front_margin, opacity:1 }, zpeed, eazing)
					.end().find('h6:visible').stop().animate({ 'font-size':front_header }, zpeed, eazing);
				if(ie6){ $newitem3.find('span:visible').stop().animate(ie6_front_span_animate, zpeed, eazing); }
				else{ $newitem3.find('span').stop().animate(front_span_animate, zpeed, eazing); }
				setTimeout( function(){ $newitem3.css('z-index', 3); }, (zpeed/4));

				$newitem4 = $item5;
				$newitem4.removeClass('frame5').addClass('frame4').stop().animate({ opacity:o.behind_opac, left:item4_pos, top:behind_top }, zpeed, eazing)
					.find('img').stop().animate({width:behind_img_width, height:behind_img_height, marginTop:behind_margin, marginRight:behind_margin, marginBottom:behind_margin, marginLeft:behind_margin, opacity:1 }, zpeed, eazing)
					.end().find('h6:visible').stop().animate({ 'font-size':behind_header }, zpeed, eazing);
				if(ie6){ $newitem4.find('span:visible').stop().animate(ie6_behind_span_animate, zpeed, eazing); }
				else{ $newitem4.find('span').stop().animate(behind_span_animate, zpeed, eazing); }
				setTimeout( function(){ $newitem4.css('z-index', 2); }, (zpeed/4));

				$li[next].clone()
					.addClass('frame5')
					.prependTo($container)
					.show()
					.animate({ opacity:0, left:item5_pos, top:back_top }, 0)
					.animate({ opacity:o.back_opac }, zpeed, function(){
						// When done animating:
						// Remove pointer cursor from previous item
						$newitem2.find('a *').css({ cursor:'default' });
						// Continue autoplay
						if(o.autoplay){
							startInterval();
						}
						// Move 2nd time if requested
						if(times==2){
							moveRight(1, 'last');
						}
						// Move more
						else if(times > 1){
							moveRight(times-1);
						}
						// Add pointer cursor if front frame has a link
						else{
							$newitem3.find('a *').css({ cursor:'pointer' });
						}
						if(!$newitem3.is(':animated')){
							// Reenable click events
							busy = false;
							// Make sure old items are removed
							$container.find('.remove').stop().fadeOut(zpeed, function(){ $(this).remove(); });
							// Callback: item with this anchor moved forward
							if(typeof move_callback == 'function'){
								var href = $newitem3.find('a').attr('href');
								if(!href){
									href = 'no_anchor';
								}
								move_callback(href);
							}
						}
					})
					.find('img').animate({ width:back_img_width, height:back_img_height, margin:back_margin, opacity:1 }, 0)
					.end().find('h6:visible').css({ 'font-size':back_header });
				// Recache
				$item1 = $newitem1,
				$item2 = $newitem2,
				$item3 = $newitem3,
				$item4 = $newitem4,
				$item5 = $container.find('.frame5');
				// New frame description
				if(ie6){ $item5.find('span:visible').animate(ie6_back_span_animate, 0); }
				else{ $item5.find('span').animate(back_span_animate, 0); }

				// Remove the out of range item
				$container.find('.remove').fadeOut(zpeed, function(){ $(this).remove(); });

				// Set new current
				if(current==items){
					current = 1;
				}else{
					current = (current+1);
				}
			};

			// Move left
			function moveLeft(times, state){
				busy = true;
				state = state || 'normal';

				// Set easing type and easing speed
				var eazing = 'swing';
				var zpeed = o.speed;
				if(times > 1 && state=='first'){
					if(easingplugin){ eazing = o.move_more_easein; }
					zpeed = Math.round(o.speed * 0.7);
				}else if(times > 1){
					if(easingplugin){ eazing = o.move_more_easebetween; }
					zpeed = Math.round(o.speed * 0.7);
				}else if(state=='last'){
					if(easingplugin){ eazing = o.move_more_easeout; }
				}else{
					if(easingplugin){ eazing = o.easing; }
				}

				// Pause autoplay
				if(o.autoplay){
					stopInterval();
				}

				// Set next item number
				if( current == 3 ){
					next = items;
				}else if( current == 2 ){
					next = (items-1);
					if(next < 1){next = items;}
				}else if( current == 1 ){
					next = (items-2);
					if(next < 1){next = items;}
				}
				else{
					next = (current-3);
				}

				// Move
				$item5.removeClass('frame5').addClass('remove').css('z-index', -1);

				$newitem5 = $item4;
				$newitem5.removeClass('frame4').addClass('frame5').stop().animate({ opacity:o.back_opac, left:item5_pos, top:back_top }, zpeed, eazing)
					.find('img').stop().animate({ width:back_img_width, height:back_img_height, marginTop:back_margin, marginRight:back_margin, marginBottom:back_margin, marginLeft:back_margin, opacity:1 }, zpeed, eazing)
					.end().find('h6:visible').stop().animate({ 'font-size':back_header }, zpeed, eazing);
				if(ie6){ $newitem5.find('span:visible').stop().animate(ie6_back_span_animate, zpeed, eazing); }
				else{ $newitem5.find('span').stop().animate(back_span_animate, zpeed, eazing); }
				setTimeout( function(){ $newitem5.css('z-index', 1); }, (zpeed/4));

				$newitem4 = $item3;
				$newitem4.removeClass('frame3').addClass('frame4').stop().animate({ opacity:o.behind_opac, left:item4_pos, top:behind_top }, zpeed, eazing)
					.find('img').stop().animate({ width:behind_img_width, height:behind_img_height, marginTop:behind_margin, marginRight:behind_margin, marginBottom:behind_margin, marginLeft:behind_margin, opacity:1 }, zpeed, eazing)
					.end().find('h6:visible').stop().animate({ 'font-size':behind_header }, zpeed, eazing);
				if(ie6){ $newitem4.find('span:visible').stop().animate(ie6_behind_span_animate, zpeed, eazing); }
				else{ $newitem4.find('span').stop().animate(behind_span_animate, zpeed, eazing); }
				setTimeout( function(){ $newitem4.css('z-index', 2); }, (zpeed/4));

				$newitem3 = $item2;
				$newitem3.removeClass('frame2').addClass('frame3').stop().animate({ opacity:1, left:item3_pos, top:front_top }, zpeed, eazing)
					.find('img').stop().animate({ width:o.front_img_width, height:o.front_img_height, marginTop:front_margin, marginRight:front_margin, marginBottom:front_margin, marginLeft:front_margin, opacity:1 }, zpeed, eazing)
					.end().find('h6:visible').stop().animate({ 'font-size':front_header }, zpeed, eazing);
				if(ie6){ $newitem3.find('span:visible').stop().animate(ie6_front_span_animate, zpeed, eazing); }
				else{ $newitem3.find('span').stop().animate(front_span_animate, zpeed, eazing); }
				setTimeout( function(){ $newitem3.css('z-index', 3); }, (zpeed/4));

				$newitem2 = $item1;
				$newitem2.removeClass('frame1').addClass('frame2').stop().animate({ opacity:o.behind_opac, left:item2_pos, top:behind_top }, zpeed, eazing)
					.find('img').stop().animate({width:behind_img_width, height:behind_img_height, marginTop:behind_margin, marginRight:behind_margin, marginBottom:behind_margin, marginLeft:behind_margin, opacity:1 }, zpeed, eazing)
					.end().find('h6:visible').stop().animate({ 'font-size':behind_header }, zpeed, eazing);
				if(ie6){ $newitem2.find('span:visible').stop().animate(ie6_behind_span_animate, zpeed, eazing); }
				else{ $newitem2.find('span').stop().animate(behind_span_animate, zpeed, eazing); }
				setTimeout( function(){ $newitem2.css('z-index', 2); }, (zpeed/4));

				$li[next].clone()
					.addClass('frame1')
					.prependTo($container)
					.show()
					.animate({ opacity:0, left:0, top:back_top }, 0)
					.animate({ opacity:o.back_opac }, zpeed, function(){
						// When done animating:
						// Remove pointer cursor from previous item
						$newitem4.find('a *').css({ cursor:'default' });
						// Continue autoplay
						if(o.autoplay){
							startInterval();
						}
						// Move 2nd time if requested
						if(times==2){
							moveLeft(1, 'last');
						}
						// Move more
						else if(times > 1){
							moveLeft(times-1);
						}
						// Add pointer cursor if front frame has a link
						else{
							$newitem3.find('a *').css({ cursor:'pointer' });
						}
						if(!$newitem3.is(':animated')){
							// Reenable click events
							busy = false;
							// Make sure old items are removed
							$container.find('.remove').stop().fadeOut(zpeed, function(){ $(this).remove(); });
							// Callback: item with this anchor moved forward
							if(typeof move_callback == 'function'){
								var href = $newitem3.find('a').attr('href');
								if(!href){
									href = 'no_anchor';
								}
								move_callback(href);
							}
						}
					})
					.find('img').animate({ width:back_img_width, height:back_img_height, margin:back_margin, opacity:1 }, 0)
					.end().find('h6:visible').css({ 'font-size':back_header });
				// Recache
				$item1 = $container.find('.frame1'),
				$item2 = $newitem2,
				$item3 = $newitem3,
				$item4 = $newitem4,
				$item5 = $newitem5;
				// New frame description
				if(ie6){ $item1.find('span:visible').animate(ie6_back_span_animate, 0); }
				else{ $item1.find('span').animate(back_span_animate, 0); }

				// Remove the out of range item
				$container.find('.remove').fadeOut(zpeed, function(){ $(this).remove(); });

				// Set new current
				if(current==1){
					current = items;
				}else{
					current = (current-1);
				}
			};

// PUBLIC FUNCTIONS
			// Previous frame
			window[containerid+'_ext_prev'] = function(){
				moveLeft(1);
			};

			// Next frame
			window[containerid+'_ext_next'] = function(){
				moveRight(1);
			};

// ACTIONS


			// Frame 1 click (move 2 steps left)
			$item1.live('click', function(e){
				if(o.freescroll || !busy){
					if(o.never_move_twice){
						moveLeft(1);
					}else if(o.move_more_directly){
						moveLeft(1);
						moveLeft(1);
					}else{
						moveLeft(2, 'first');
					}
					e.preventDefault();
				}
			});
			// Frame 2 click (move 1 step left)
			$item2.live('click', function(e){
				if(o.freescroll || !busy){
					moveLeft(1);
					e.preventDefault();
				}
			});
			// Frame 4 click (move 1 step right)
			$item4.live('click', function(e){
				if(o.freescroll || !busy){
					moveRight(1);
					e.preventDefault();
				}
			});
			// Frame 5 click (move 2 steps right)
			$item5.live('click', function(e){
				if(o.freescroll || !busy){
					if(o.never_move_twice){
						moveRight(1);
					}else if(o.move_more_directly){
						moveRight(1);
						moveRight(1);
					}else{
						moveRight(2, 'first');
					}
					e.preventDefault();
				}
			});
			if(o.move_on_hover){
				// Frame 1 hover (move 2 steps left)
				$item1.live('mousemove', function(){
					$item1.click();
				});
				// Frame 2 hover (move 1 step left)
				$item2.live('mousemove', function(){
					$item2.click();
				});
				// Frame 4 hover (move 1 step right)
				$item4.live('mousemove', function(){
					$item4.click();
				});
				// Frame 5 hover (move 2 steps right)
				$item5.live('mousemove', function(){
					$item5.click();
				});
			}

			$item3.live('hover', function(e){
				// Hover frame 3 = zoom in
				if((e.type == 'mouseover' || e.type == 'mouseenter') && !busy){
					if(o.autoplay){
						stopInterval();
					}
					$item3.addClass('zoomed')
						.stop(true,true).animate({ left:'-='+(o.front_img_width*(o.hovergrowth/2)), top:'-='+(o.front_img_height*o.hovergrowth) }, hoverspeed)
						.find('img').stop().animate({ width:(o.front_img_width*(1+o.hovergrowth)), height:(o.front_img_height*(1+o.hovergrowth)) }, hoverspeed);
					$item2.stop(true,true).animate({ left:'-='+(behind_img_width*o.hovergrowth) }, hoverspeed);
					$item4.stop(true,true).animate({ left:'+='+(behind_img_width*o.hovergrowth) }, hoverspeed);
					// Set span width for IE6
					if(ie6){
						iegrow = Math.round(o.hovergrowth*o.front_img_width);
						$item3.find('span:visible').animate({ width:'+='+iegrow }, hoverspeed);
					}
				}
				// Mouse-out center frame: zoom out
				else if(!busy){
					if(o.autoplay){
						startInterval();
					}
					$item3.stop().animate({ left:item3_pos, top:front_top }, hoverspeed)
						.find('img').stop().animate({ width:o.front_img_width, height:o.front_img_height }, hoverspeed, function(){
							$item3.removeClass('zoomed');
						});
					$item2.stop().animate({ left:item2_pos }, hoverspeed);
					$item4.stop().animate({ left:item4_pos }, hoverspeed);
					// Set span width for IE6
					if(ie6){
						iegrow = Math.round(o.hovergrowth*o.front_img_width);
						$container.find('.zoomed span:visible').animate({ width:'-='+iegrow }, hoverspeed);
					}
				}
			});

			// Also zoom in if center frame is slided in under your cursor (Excluding IE)
			if(!ie){
				$container.find('.frame3:not(.zoomed)').live('mousemove', function(){
					$item3.mouseover();
				});
			}

			// Callback: when a front-frame link is clicked
			$item3.find('a').live('click', function(){
				if(typeof link_callback == 'function'){
					link_callback( $(this).attr('href') );
				}
			});

			// Keystrokes
			if(o.keyboard){
				$(document).keydown(function(e){
					// Enter key / Space key / Right arrow key = move right
					if( e.keyCode==13 || e.keyCode==32 || e.keyCode==39 ){
						moveRight(1);
					}
					// Left arrow key = move left
					if( e.keyCode==37 ){
						moveLeft(1);
					}
				});
			}

			// Initiate first autoplay
			if(o.autoplay){
				startInterval();
			}

			// Window resize (100% container width)
			if(container_100){
				$(window).resize(function(){
					setContainerWidth();
				});
			}

		// End plugin wrap
		});
	}
})(jQuery);
