(function($) {
	$.fn.zoomFilmstrip = function( userOptions ) {
		var defaultOptions = {
			speed: 500,
			filmstripW: 1400,
			filmstripH: 900,
			filmstripP: 0.8,
			imageW:1400,
			imageH:900,
			imageM:50,
			backButton:"gallery_back",
			nextButton:"gallery_next",
			thumbnailHolder:"gallery_nav",
			imageHolder:"gallery_images",
			activeClass:"gallery_active",
			zoom:false
		}
		var uO = $.extend( defaultOptions, userOptions );

		var self = this;
	
		return this.each(function() {
			var $this = $( this );
			var $galleryImages = $this.find( ".gallery_images" );
			var $galleryNav = $this.find( ".gallery_nav" );

			var wrapperW = $this.width();
			var wrapperH = $this.height();
			var totalImages = $galleryNav.find( ".gallery_viewport li" ).length;
			var activeIndex = $galleryNav.find( "." + uO.activeClass ).index();
			var imageIndex = 0;
			var isNext = true;

			if( uO.filmstripW == 0 ) uO.filmstripW = Math.round( uO.imageW * uO.filmstripP );
			if( uO.filmstripH == 0 ) uO.filmstripH = Math.round( uO.imageH * uO.filmstripP );

			/****************************************
			 *  Functions
			 */

			function getCurrentDuration() {
				var currentDistance = activeIndex - imageIndex;
				var currentDuration = ( currentDistance * currentDistance ) + ( 300 * Math.abs( currentDistance ) ) + 200;
				return currentDuration;
			}
			function translateThumbnails() {
				
				if( document.location.hash == "#dark-ride-card" ) {
					fireTag(2258.2,{'<city>':imageIndex});
				}
				else {
					fireTag(2207.1,{'<image_num>':imageIndex});
				}

				$galleryNav.find( ".gallery_viewport li:eq(" + imageIndex + ")" ).addClass( uO.activeClass );

				var thumbnailW = $galleryNav.find( ".gallery_viewport img" ).width();
				var thumbnailM = parseInt( $galleryNav.find( ".gallery_viewport li" ).css( "margin-right" ).replace( /px$/, "" ) );
				var imageOffset;
				
				if( imageIndex > 2 && imageIndex < ( totalImages - 3 ) ) {
					imageOffset = ( thumbnailW + thumbnailM ) * ( imageIndex - 2 );
				}
				else {
					if( imageIndex < 3 ) {
						imageOffset = 0;
					}
					else {
						imageOffset = ( thumbnailW + thumbnailM ) * ( totalImages - 5 );
					}
				}
				var leftOffset = 0 - imageOffset;
				$galleryNav.find( ".gallery_viewport ul" ).animate({left:leftOffset + "px"}, getCurrentDuration() );
			}
			function startAnimation() {
				$('.scrollable .close:visible').click(); // remove all overlays before proceeding
				if( uO.zoom ) scaleToFilmstrip();
				else translateFilmstrip();
			}
			function scaleFromFilmstrip() {
				var imageOffset = ( uO.filmstripW + uO.imageM ) * imageIndex;
				var leftOffset = Math.round( ( wrapperW - ( uO.imageW + uO.imageM ) ) / 2 ) - imageOffset;
				var topOffset = 0;

				$galleryImages.fadeTo( uO.speed, 1.0 );
				$galleryImages.find( "ul" ).animate({left:leftOffset}, uO.speed );
				$galleryImages.find( "img:eq(" + imageIndex + ")" ).animate({"margin-top":topOffset + "px", width:uO.imageW, height:uO.imageH}, uO.speed, function() {
					translateThumbnails();
				});
			}
			function translateFilmstrip() {
				if( uO.zoom ) {
					var imageOffset = ( uO.filmstripW + uO.imageM ) * imageIndex;
					var leftOffset = Math.round( ( wrapperW - ( uO.filmstripW + uO.imageM ) ) / 2 ) - imageOffset;
					$galleryImages.fadeTo( uO.speed, 0.25 );
					translateThumbnails();
					$galleryImages.find( "ul" ).animate({left:leftOffset + "px"}, getCurrentDuration(), function() {
						scaleFromFilmstrip();
					});
				}
				else {
					var imageOffset = ( uO.imageW + uO.imageM ) * imageIndex;
					var leftOffset = ( Math.round( ( wrapperW - ( uO.filmstripW + uO.imageM ) ) / 2 ) + imageOffset ) * -1;
					$galleryImages.fadeTo( uO.speed, 0.25, function() {
						translateThumbnails();
						$galleryImages.find( "ul" ).animate({left:leftOffset + "px"}, getCurrentDuration(), function() {
							//scaleFromFilmstrip();
							$galleryImages.fadeTo( uO.speed, 1.0 );
						});
					});
				}
			}
			function scaleToFilmstrip() {
				var imageOffset = ( uO.filmstripW + uO.imageM ) * activeIndex;
				var leftOffset = Math.round( ( wrapperW - ( uO.filmstripW + uO.imageM ) ) / 2 ) - imageOffset;
				var topOffset = Math.round( ( wrapperH - uO.filmstripH ) / 2 );

				$galleryImages.fadeTo( uO.speed, 0.25 );
				$galleryImages.find( "ul" ).animate({left:leftOffset}, uO.speed );
				$galleryImages.find( "img:eq(" + activeIndex + ")" ).animate({"margin-top":topOffset + "px", width:uO.filmstripW, height:uO.filmstripH}, uO.speed, function() {
					translateFilmstrip();
				});
			}
			function imageAdvance() {
				if( isNext ) {
					$this.find( ".gallery_next" ).trigger( "click" );
				}
				else {
					$this.find( ".gallery_back" ).trigger( "click" );
				}
			}
			function initialize() {
				$this.find(".gallery_viewport").find("li").eq(0).addClass(uO.activeClass);
				$galleryImages.css({left:"0px"});
				$galleryImages.find( "li img" ).css({"margin-top":"0px"});
				if( uO.zoom ) {
					var topOffset = Math.round( ( wrapperH - uO.filmstripH ) / 2 );
					$galleryImages.find( "li:gt(0) img" ).css({"margin-top":topOffset + "px"}).attr({width:uO.filmstripW,height:uO.filmstripH});
				}

				$this.find( ".gallery_back" ).bind( "click", function() {
					activeIndex = $galleryNav.find( "." + uO.activeClass ).index();
					isNext = true;
					if( activeIndex > 0 ) {
						$galleryNav.find( "li" ).removeClass( uO.activeClass );
						imageIndex = activeIndex - 1;
						if( activeIndex == 1 ) isNext = true;
						else isNext = false;
						startAnimation();						
					}
				});
				$this.find( ".gallery_viewport li" ).bind( "click", function() {
					activeIndex = $galleryNav.find( "." + uO.activeClass ).index();
					$galleryNav.find( ".gallery_viewport li" ).removeClass( uO.activeClass );
					imageIndex = $( this ).index();
					startAnimation();						
				});
				$this.find( ".gallery_next" ).bind( "click", function() {
					activeIndex = $galleryNav.find( "." + uO.activeClass ).index();
					isNext = false;
					if( activeIndex < ( totalImages - 1 ) ) {
						$galleryNav.find( "li" ).removeClass( uO.activeClass );
						imageIndex = activeIndex + 1;
						if( activeIndex == ( totalImages - 2 ) ) isNext = false;
						else isNext = true;
						startAnimation();						
					}
				});
				/*
				$galleryImages.find( "img" ).bind( "click", function() {
					imageAdvance();
				});
				*/
//				$this.find( ".gallery_top li" ).bind( "click", function(){
//					window.location='#' + $( this ).attr('rel');
//				});

			}
			initialize();

		}); // end return this
		
		return this;
	}; // end plugin
	
})(jQuery);
// comment for ant
jQuery(document).ready(function($) {
		
		$(".scrollable").each( function() {
			$( this ).data( "lS", $( this ).scrollable({
				loop:false,
				size:1,
				clickable:false,
				disabledClass:"disabled",
				api:true
			}));
			$( this ).find( "a" ).bind( "click", function() {
				$( this ).attr( "target", "_blank" );
				return window.confirm( "You are now leaving the Darker Side of Green website.\n\nThe views and opinions on this website reflect the author's point of view and not those of Lexus. The material shown on this website is for entertainment purposes only. We regret if we unintentionally offend any organization, group, or individual.. This Site is operated by Lexus, a division of Toyota Motor Sales, USA, Inc.\n\nContinue to external website >>" );
			});
		})
		$('.logo').overlay({
			left:0,
			top:79,
			onClose:function(o) { 
			$('.partner_logo_wrap .logo').fadeTo("normal", 1.0);}
		});
		$('.logo').bind( "click", function(){
			var i = $( this ).index();
			var r = $(this).attr('rel');
			$(r).data('lS').seekTo(i);

			var toSend = {'<city>':$( this ).parents('.gallery_images li').index(),'<partner>':i};
			fireTag(2258.3,toSend);
//			eventTracker({ category:'Overlay', action:'View', label:'Sponsor', value:i });
		});
		$('.next, .prev').click(function(){
			var toSend = {'<city>':$( this ).parents('.gallery_images li').index(),'<partner>':$( this ).parents('.long_content').find( '.scrollable' ).data('lS').getIndex()};
			fireTag(2258.3,toSend);
//			eventTracker({ category:'Overlay', action:'View', label:'Sponsor', value:i });
		 });

// fade out logo links when overlay opens
	     $('.partner_logo_wrap .logo').click(function() {
	            $('.partner_logo_wrap .logo').fadeTo("normal", 0.3);
	      });
// right nav links trigger overlay to close 
		$('.nav_right_item_h').click(function(){
			if( document.location.hash == "#dark-ride-card" ) {
				$('.scrollable').close();
			}
		})
//		if ($('.nav_right_item_h') && !$('#nav_right_item_6_h.nav_right_item_h')) {
});


