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

(function($){
	jQuery.fn.boutique = function(options){

		// OPTION DEFAULTS
		var opt = $.extend({
			starter:			1,				// Which frame to start with (number)
			speed:				600,			// Overall animation speed (time in ms)
			behind_opac:		1,			// Opacity of the furter back items (between or equal to 0-1)
			back_opac:			1,			// 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.6,			// 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_twice_easein:	'easeInCirc',	// Easing type for the first part of moving twice (easing type title)
			move_twice_easeout:	'easeOutCirc',	// Easing type for the second part of moving twice (easing type title)
			text_front_only:	false,			// Show the title/description only in the front frame (true/false)
			keyboard:			true,			// Enable/disable keyboard functionality (true/false)
			move_on_hover:		false			// Navigating with mouse-over instead of clicking (true/false)

			// Extra options:
			// container_width	= Total carousel width (value in pixels or "100%") (default: read from CSS)
			// front_img_width	= Width of the frontal image (value in pixels) (default: read from CSS)
			// front_img_height	= Height of the frontal image (value in pixels) (default: read from CSS)
			// hoverspeed		= Speed in which the frontal frame will zoom in on mouse-over (time in ms) (default: speed/4)
			// text_opacity		= Opacity of the title/description layer (default: read from CSS)
		},options);

		$(this).each(function(){

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

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

			// Easing type for easing plugin:
			if( $.easing.def ){
				easingplugin = true;
				$.easing.def = opt.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;
				if( !$this.find('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( opt.starter == 2 ){
				$li[1].clone().addClass('frame2').prependTo($container);
				$li[items].clone().addClass('frame1').prependTo($container);
			}
			else if( opt.starter == 1 ){
				$li[items-1].clone().addClass('frame1').prependTo($container);
				$li[items].clone().addClass('frame2').prependTo($container);
			}
			else{
				$li[opt.starter-1].clone().addClass('frame2').prependTo($container);
				$li[opt.starter-2].clone().addClass('frame1').prependTo($container);
			}
			$li[opt.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( opt.starter == (items-1) ){
				$li[items].clone().addClass('frame4').prependTo($container);
				$li[1].clone().addClass('frame5').prependTo($container);
			}
			else if( opt.starter == items ){
				$li[1].clone().addClass('frame4').prependTo($container);
				$li[2].clone().addClass('frame5').prependTo($container);
			}
			else{
				$li[opt.starter+1].clone().addClass('frame4').prependTo($container);
				$li[opt.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(opt.container_width){ container_width = opt.container_width; }
			else{ container_width = $container.css('width'); }
			if(container_width=='100%'){ container_100 = true; container_width = parseInt($(window).width(), 10); }
			else{ container_width = parseInt( container_width, 10 ); }
			if(opt.front_img_width){ front_img_width = opt.front_img_width; }
			else{ front_img_width = parseInt( $('img',$front).css('width'), 10 ); }
			if(opt.front_img_height){ front_img_height = opt.front_img_height; }
			else{ front_img_height = parseInt( $('img',$front).css('height') ); }
			if(opt.text_opacity){ text_opacity = opt.text_opacity; }
			else{ text_opacity = parseFloat( $('span',$container).css('opacity') ); }

			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( front_img_width + (front_margin*2) + (li_padding*2) + (li_border*2) ),
				front_height		= Math.round( front_img_height + (front_margin*2) + (li_padding*2) + (li_border*2) ),
				behind_img_width	= Math.round(front_img_width * opt.behind_size ),
				behind_img_height	= Math.round(front_img_height * opt.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(front_img_width * opt.back_size),
				back_img_height		= Math.round(front_img_height * opt.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( (container_width/2)-(front_width/2) ),
				item5_pos			= (container_width - back_width),
				item2_pos;

			// Optional custom behind frame distance
			if(opt.behind_distance != 'auto'){
				item2_pos = parseInt(opt.behind_distance, 10);
			}else{
				item2_pos = Math.round( (container_width/4)-(behind_width/2) );
			}
			var item4_pos = (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'),
				behind_span_paddingTop = Math.round( parseInt(front_span_paddingTop, 10)*0.8 )+'px',
				behind_span_paddingRight = Math.round( parseInt(front_span_paddingRight, 10)*0.8 )+'px',
				behind_span_paddingBottom = Math.round( parseInt(front_span_paddingBottom, 10)*0.8 )+'px',
				behind_span_paddingLeft = Math.round( parseInt(front_span_paddingLeft, 10)*0.8 )+'px',
				back_span_paddingTop = Math.round( parseInt(front_span_paddingTop, 10)*0.6 )+'px',
				back_span_paddingRight = Math.round( parseInt(front_span_paddingRight, 10)*0.6 )+'px',
				back_span_paddingBottom = Math.round( parseInt(front_span_paddingBottom, 10)*0.6 )+'px',
				back_span_paddingLeft = Math.round( parseInt(front_span_paddingLeft, 10)*0.6 )+'px',
				front_span_animate = { 'font-size':front_span, 'padding-top':front_span_paddingTop, 'padding-right':front_span_paddingRight, 'padding-bottom':front_span_paddingBottom, 'padding-left':front_span_paddingLeft },
				behind_span_animate = { 'font-size':behind_span, 'padding-top':behind_span_paddingTop, 'padding-right':behind_span_paddingRight, 'padding-bottom':behind_span_paddingBottom, 'padding-left':behind_span_paddingLeft },
				back_span_animate = { 'font-size':back_span, 'padding-top':back_span_paddingTop, 'padding-right':back_span_paddingRight, 'padding-bottom':back_span_paddingBottom, 'padding-left':back_span_paddingLeft };
			if(opt.text_front_only){
				front_span_animate = $.extend({ opacity:text_opacity }, front_span_animate);
				behind_span_animate = $.extend({ opacity:0 }, behind_span_animate);
				back_span_animate = $.extend({ opacity:0 }, back_span_animate);
			}
			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-parseInt(behind_span_paddingRight, 10)-parseInt(behind_span_paddingLeft, 10)-behind_span_margin-(li_border*2) }, behind_span_animate),
					ie6_back_span_animate = $.extend({ width:back_width-parseInt(back_span_paddingRight, 10)-parseInt(back_span_paddingLeft, 10)-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);
			$item1.css({ left:0, top:back_top }).animate({ opacity:opt.back_opac }, 0)
				.find('img').animate({ width:back_img_width+'px', height:back_img_height+'px', margin:back_margin+'px', opacity:1 }, 0)
				.siblings('span:visible').css(back_span_animate)
				.children('h6:visible').css({'font-size':back_header});
			$item2.css({ left:item2_pos+'px', top:behind_top, 'z-index':2 }).animate({ opacity:opt.behind_opac }, 0)
				.find('img').animate({width:behind_img_width+'px', height:behind_img_height+'px', margin:behind_margin+'px', opacity:1}, 0)
				.siblings('span:visible').css(behind_span_animate)
				.children('h6:visible').css({'font-size':behind_header});
			$item3.css({ left:item3_pos+'px', top:front_top, 'z-index':3 }).animate({ opacity:1 }, 0)
				.find('a *').css({ cursor:'pointer' }).end()
				.find('img').animate({width:front_img_width+'px', height:front_img_height+'px', margin:front_margin+'px', opacity:1}, 0)
				.siblings('span:visible').css(front_span_animate)
				.children('h6:visible').css({'font-size':front_header});
			$item4.css({ left:item4_pos+'px', top:behind_top, 'z-index':2 }).animate({ opacity:opt.behind_opac }, 0)
				.find('img').animate({width:behind_img_width+'px', height:behind_img_height+'px', margin:behind_margin+'px', opacity:1}, 0)
				.siblings('span:visible').css(behind_span_animate)
				.children('h6:visible').css({'font-size':behind_header});
			$item5.css({ left:item5_pos+'px', top:back_top }).animate({ opacity:opt.back_opac }, 0)
				.find('img').animate({width:back_img_width+'px', height:back_img_height+'px', margin:back_margin+'px', 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($(window).width(), 10);
				$container.width( width );
				// 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();
				}, opt.autointerval);
			}

			// Move right
			function moveRight(times){
				busy=true;

				// Set easing type and easing speed
				eazing = '';
				zpeed = opt.speed;
				if(easingplugin){
					if(times=='twice'){
						eazing = opt.move_twice_easein;
						zpeed = Math.round(opt.speed*0.5);
					}else if(times=='twice_end'){
						eazing = opt.move_twice_easeout;
					}else{
						eazing = opt.easing;
					}
				}

				// Pause autoplay
				if(opt.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:opt.back_opac, left:0, top:back_top }, zpeed, eazing)
					.find('img').stop().animate({ width:back_img_width+'px', height:back_img_height+'px', marginTop:back_margin+'px', marginRight:back_margin+'px', marginBottom:back_margin+'px', marginLeft:back_margin+'px', 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:opt.behind_opac, left:item2_pos+'px', top:behind_top }, zpeed, eazing)
					.find('img').stop().animate({ width:behind_img_width+'px', height:behind_img_height+'px', marginTop:behind_margin+'px', marginRight:behind_margin+'px', marginBottom:behind_margin+'px', marginLeft:behind_margin+'px', 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+'px', top:front_top }, zpeed, eazing)
					.find('img').stop().animate({ width:front_img_width+'px', height:front_img_height+'px', marginTop:front_margin+'px', marginRight:front_margin+'px', marginBottom:front_margin+'px', marginLeft:front_margin+'px', 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:opt.behind_opac, left:item4_pos+'px', top:behind_top }, zpeed, eazing)
					.find('img').stop().animate({width:behind_img_width+'px', height:behind_img_height+'px', marginTop:behind_margin+'px', marginRight:behind_margin+'px', marginBottom:behind_margin+'px', marginLeft:behind_margin+'px', 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+'px', top:back_top }, 0)
					.animate({ opacity:opt.back_opac }, zpeed, function(){
						// When done animating:
						// Remove pointer cursor from previous item
						$newitem2.find('a *').css({ cursor:'default' });
						// Continue autoplay
						if(opt.autoplay){
							startInterval();
						}
						// Move 2nd time if requested
						if(times=='twice'){
							moveRight('twice_end');
						}
						// 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!=undefined && href!=''){
									move_callback(href);
								}
							}
						}
					})
					.find('img').animate({ width:back_img_width+'px', height:back_img_height+'px', margin:back_margin+'px', 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){
				busy = true;

				// Set easing type and easing speed
				eazing = '';
				zpeed = opt.speed;
				if(easingplugin){
					if(times=='twice'){
						eazing = opt.move_twice_easein;
						zpeed = Math.round(opt.speed*0.5);
					}else if(times=='twice_end'){
						eazing = opt.move_twice_easeout;
					}else{
						eazing = opt.easing;
					}
				}

				// Pause autoplay
				if(opt.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:opt.back_opac, left:item5_pos+'px', top:back_top }, zpeed, eazing)
					.find('img').stop().animate({ width:back_img_width+'px', height:back_img_height+'px', marginTop:back_margin+'px', marginRight:back_margin+'px', marginBottom:back_margin+'px', marginLeft:back_margin+'px', 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:opt.behind_opac, left:item4_pos+'px', top:behind_top }, zpeed, eazing)
					.find('img').stop().animate({ width:behind_img_width+'px', height:behind_img_height+'px', marginTop:behind_margin+'px', marginRight:behind_margin+'px', marginBottom:behind_margin+'px', marginLeft:behind_margin+'px', 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+'px', top:front_top }, zpeed, eazing)
					.find('img').stop().animate({ width:front_img_width+'px', height:front_img_height+'px', marginTop:front_margin+'px', marginRight:front_margin+'px', marginBottom:front_margin+'px', marginLeft:front_margin+'px', 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:opt.behind_opac, left:item2_pos+'px', top:behind_top }, zpeed, eazing)
					.find('img').stop().animate({width:behind_img_width+'px', height:behind_img_height+'px', marginTop:behind_margin+'px', marginRight:behind_margin+'px', marginBottom:behind_margin+'px', marginLeft:behind_margin+'px', 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:opt.back_opac }, zpeed, function(){
						// When done animating:
						// Remove pointer cursor from previous item
						$newitem4.find('a *').css({ cursor:'default' });
						// Continue autoplay
						if(opt.autoplay){
							startInterval();
						}
						// Move 2nd time if requested
						if(times=='twice'){
							moveLeft('twice_end');
						}
						// 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!=undefined && href!=''){
									move_callback(href);
								}
							}
						}
					})
					.find('img').animate({ width:back_img_width+'px', height:back_img_height+'px', margin:back_margin+'px', 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(){
				$item2.click();
			};

			// Next frame
			window[containerid+'_ext_next'] = function(){
				$item4.click();
			};

// ACTIONS

			
			// Frame 1 click (move 2 steps left)
			$item1.live('click', function(e){
				if(opt.freescroll || !busy){
					moveLeft('twice');
					e.preventDefault();
				}
			});
			// Frame 2 click (move 1 step left)
			$item2.live('click', function(e){
				if(opt.freescroll || !busy){
					moveLeft();
					e.preventDefault();
				}
			});
			// Frame 4 click (move 1 step right)
			$item4.live('click', function(e){
				if(opt.freescroll || !busy){
					moveRight();
					e.preventDefault();
				}
			});
			// Frame 5 click (move 2 steps right)
			$item5.live('click', function(e){
				if(opt.freescroll || !busy){
					moveRight('twice');
					e.preventDefault();
				}
			});
			if(opt.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(opt.autoplay){
						stopInterval();
					}
					$item3.addClass('zoomed')
						.stop(true,true).animate({ left:'-='+(front_img_width*(opt.hovergrowth/2))+'px', top:'-='+(front_img_height*opt.hovergrowth)+'px' }, hoverspeed)
						.find('img').stop().animate({ width:(front_img_width*(1+opt.hovergrowth))+'px', height:(front_img_height*(1+opt.hovergrowth))+'px' }, hoverspeed);
					$item2.stop(true,true).animate({ left:'-='+(behind_img_width*opt.hovergrowth)+'px' }, hoverspeed);
					$item4.stop(true,true).animate({ left:'+='+(behind_img_width*opt.hovergrowth)+'px' }, hoverspeed);
					// Set span width for IE6
					if(ie6){
						iegrow = Math.round(opt.hovergrowth*front_img_width);
						$item3.find('span:visible').animate({ width:'+='+iegrow }, hoverspeed);
					}
				}
				// Mouse-out center frame: zoom out
				else if(!busy){
					if(opt.autoplay){
						startInterval();
					}
					$item3.stop().animate({ left:item3_pos+'px', top:front_top }, hoverspeed)
						.find('img').stop().animate({ width:front_img_width+'px', height:front_img_height+'px' }, 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(opt.hovergrowth*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(opt.keyboard){
				$(document).keydown(function(e){
					// Enter key / Space key / Right arrow key = move right
					if( e.keyCode==13 || e.keyCode==32 || e.keyCode==39 ){
						$item4.click();
					}
					// Left arrow key = move left
					if( e.keyCode==37 ){
						$item2.click();
					}
				});
			}

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

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

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