/* jquery.scrolling-carousel.js - Michael Tomczak copyright 2010 */

(function($){$.fn.scrolling_carousel = function(o) {
	
	var defaults = { 
	    slidePadding:	0,
	    loop:			false,
	    autoplayDelay: 	6000,
	    homepage: 		false
	 }; 
	var options = $.extend({}, defaults, o);
	var autoplayEnabled = false;
	var timer;
	
	return this.each(function(){
	
		$(this).addClass("scrolling-carousel");
		
		$(this).hover(
			function() {
				pauseAutoPlay();
			},
			function() {
				if (autoplayEnabled) {
					enableAutoPlay();
				}
			}
		);	
	
		// make sure we have more than one item to scroll
		if( $(this).children().length > 1){
						
			//put all the children into a  set of containers and append it back to the parent object
			var viewer 		= $('<div class="viewing-window"></div>');
			var panes 		=  $('<div class="pane-container clearfix"></div>').append($(this).children());
			viewer.append(panes).appendTo($(this));
			
			//add the scroll buttons
			$(this).prepend('<div class="left-scroll-button"></div><div class="right-scroll-button"></div>');
			$(this).find(".left-scroll-button").css("display", "none");
			
			//add the regulating class to the first element
			$(this).find(".pane-container").children().first().addClass("at-left-margin");

			// set up for infinite looping if we have enough elements
			if (options.loop && panes.children().length >= 3 ){
				options.loop = true;		
				$(this).find(".left-scroll-button").css("display", "block");
			} else options.loop = false;
			
			
			function scrollRight(e){
				var pane_container	= $(e.data.carousel).find(".pane-container");
				var sliding_element = pane_container.children(".at-left-margin"); 
				
				//specific to home page :-( if there are not two slides on the right add one more then move
				if(sliding_element.next().next().width() == null && options.loop){
					pane_container.children('div:last').after( pane_container.children('div:first') );
					$(e.data.carousel).find(".pane-container").css('right', function(i,v){ 
						return (parseInt(v) - (sliding_element.width() + options.slidePadding)) + "px";
						}
					); 	
				}
				
				if(sliding_element.next().width() != null){
					sliding_element.removeClass("at-left-margin").next().addClass("at-left-margin");

					$(e.data.carousel).find(".pane-container").stop().animate({
						"right": "+=" + (sliding_element.width() + options.slidePadding) },
						1000,
						'easeInOutExpo', 
						function(){
							//active button once animation is complete
							$(e.target).one("click", {carousel: e.data.carousel}, scrollRight );
							$(e.data.carousel).trigger("scrollComplete");
						} 
					);
					
					if( !options.loop ){
						// hide button when at end
						var displayVal = ( sliding_element.next().index() == (panes.children().length - 1) ) ? "none" : "block";
						$(this).css("display", displayVal );
						$(e.data.carousel).find(".left-scroll-button").css("display", "block");		
					}
				
				}
				
				if(options.homepage) {
					if (e.type == "click") {
						trackHomepageScrollRight();
						disableAutoPlay();
					} else {
						enableAutoPlay();
					}
				}
			}
			
			function scrollLeft(e){
				var pane_container	= $(e.data.carousel).find(".pane-container");
				var sliding_element = pane_container.children(".at-left-margin"); 

				if( options.loop && sliding_element.prev().width() == null ){
					pane_container.children('div:first').before( pane_container.children('div:last') );
					pane_container.css('right', sliding_element.prev().width() + options.slidePadding);
				}
			
				if(sliding_element.prev().width() != null){
					$(e.data.carousel).find(".pane-container").stop().animate(
						{"right": "-=" + (sliding_element.prev().width() + options.slidePadding)  }, 
						1000, 
						'easeInOutExpo', 
						function(){
							//active button once animation is complete
							$(e.target).one("click", {carousel: e.data.carousel}, scrollLeft );
							$(e.data.carousel).trigger("scrollComplete");
						}
					);
					
					sliding_element.removeClass("at-left-margin").prev().addClass("at-left-margin");
				
					if( !options.loop ){
						// hide button when at end
						var displayVal = ( sliding_element.prev().index() == 0 ) ? "none" : "block";
						$(this).css("display", displayVal );
						$(e.data.carousel).find(".right-scroll-button").css("display", "block");
					}
				}
				
				if(options.homepage) {
					trackHomepageScrollLeft();
					disableAutoPlay();
				}
			};
			
			function doAutoPlay(e) {
				if (autoplayEnabled) {
					scrollRight(e);
				}
			}
			
			function pauseAutoPlay() {
				clearTimeout(timer);
			}
			
			function disableAutoPlay() {
				clearTimeout(timer);
				autoplayEnabled = false;
			}
			
			function enableAutoPlay() {
				timer = setTimeout(function() { doAutoPlay({data: {carousel: $("#homepage-carousel")}}); },options.autoplayDelay);
			}
			
			//attach the event listeners
			$(this).find(".right-scroll-button").one("click", {carousel: this}, scrollRight );
			$(this).find(".left-scroll-button").one("click", {carousel: this}, scrollLeft );
			
			if(options.homepage) {
				autoplayEnabled = true;
				enableAutoPlay();
			}
			
			$(this).find(".caption-toggle").click({carousel: this}, function(e){
	
				var caption = $(this).parents('.photo-gallery-caption');
	
				if( $(this).hasClass('active')){
					caption.animate({"bottom": caption.height() * -1 + "px"}, 500,'swing', function(){ caption.find('span').css('display','none'); });
					$(this).removeClass('active');
				}
				else{
					caption.animate({"bottom":"0"}, 500, 'swing', function(){ caption.find('span').css('display','inline'); } );	
					$(this).addClass('active');
				}
			});	
		}	
	  });
};})(jQuery)
