// Spring media jQuery plugin
(function($){  
$.fn.springGallery = function(options) {  
	var defaults = {};
	var options = $.extend(defaults, options);
	
	return this.each(function(index,el) {
		var $gallery = $(this).next();
		
		// Create new ID for this gallery
		var galleryId = "spring_gallery_" + index;
		$gallery.attr('id',galleryId);
		$gallery.find('.spring_player').each( function() {
			
		});
		
		
		
		// Initialize overlay
		$(this).find('.media_link').attr('rel','#'+galleryId).overlay({
			fixed: false,
			mask: {
				color: '#000',
				opacity: 0.3,
				closeSpeed: '1000',
				loadSpeed: '1000'
			},
			onClose: function() {
				$gallery.find('.slides').cycle('destroy');
			}
		});
		
		$(this).find('.slideshow_link').attr('rel','#'+galleryId).overlay({
			fixed: false,
			mask: {
				color: '#000',
				opacity: 0.3,
				closeSpeed: '1000',
				loadSpeed: '1000'
			},
			onClose: function() {
				$gallery.find('.slides').cycle('destroy');
			}
		});
		
		$('.media_link').bind("click", function(event) {
			// Initialize cycle on click
			$gallery.find('.slides').cycle({
				fx: 'fade',
				speed: '200',
				top: 'center',
				sync: 0, // Don't run in/out effects simultaneously
				timeout: 0, // Don't cycle automatically
				close: '#'+galleryId+' .close',
				prev: '#'+galleryId+' .prev',
			    next: '#'+galleryId+' .next',
			    before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
					$(nextSlideElement).css('background-color','transparent');
				},
				after: function(currSlideElement, nextSlideElement, options, forwardFlag) {
					$gallery.find('.count').html((options.currSlide + 1) + " of " + options.slideCount);
					$(nextSlideElement).css('background-color','#000000');
				},
				startingSlide: $(this).index('.media_link') // So cycle starts at correct position
			});
		});
		
		$('.slideshow_link').bind("click", function(event) {
			// Initialize cycle on click
			$gallery.find('.slides').cycle({
				fx: 'fade',
				speed: '200',
				top: 'center',
				sync: 0, // Don't run in/out effects simultaneously
				timeout: 0, // Don't cycle automatically
				close: '#'+galleryId+' .close',
				prev: '#'+galleryId+' .prev',
			    next: '#'+galleryId+' .next',
			    before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
					$(nextSlideElement).css('background-color','transparent');
				},
				after: function(currSlideElement, nextSlideElement, options, forwardFlag) {
					$gallery.find('.count').html((options.currSlide + 1) + " of " + options.slideCount);
					$(nextSlideElement).css('background-color','#000000');
				},
				startingSlide: $(this).index('.slideshow_link') // So cycle starts at correct position
			});
		});
		
		jQuery("a.player").flowplayer("typo3conf/ext/spring_media/Resources/Public/Scripts/flowplayer-3.2.7.swf",{ 
			clip: {
			autoPlay: false,
			autoBuffering: true,
			//scaling:'fit',
			onStart: function(clip) {
				jQuery(this.getParent()).css({width:clip.metaData.width, height:clip.metaData.height });
			}
		},
			canvas: {backgroundColor: "transparent"},	
				plugins: {
				   controls: {				   
				   	  url: 'typo3conf/ext/spring_media/Resources/Public/Scripts/flowplayer.controls-3.2.5.swf',
				      volumeColor: '#ffffff',
				      tooltipColor: '#000000',
				      borderRadius: '0px',
				      tooltipTextColor: '#ffffff',
				      backgroundColor: 'rgb(0, 0, 0, 0.5)',
				      timeBgColor: 'rgb(0, 0, 0, 0)',
				      volumeSliderGradient: 'none',
				      progressColor: '#ffffff',
				      bufferColor: '#333333',
				      timeSeparator: ' ',
				      sliderColor: '#000000',
				      sliderBorder: '1px solid rgba(128, 128, 128, 0.7)',
				      progressGradient: 'none',
				      timeColor: '#ffffff',
				      backgroundGradient: 'none',
				      volumeSliderColor: '#ffffff',
				      durationColor: '#ffffff',
				      bufferGradient: 'none',
				      volumeBorder: '1px solid rgba(128, 128, 128, 0.7)',
				      buttonColor: '#ffffff',
				      buttonOverColor: '#689fe2',
				      sliderGradient: 'none',
				      timeBorder: '0px solid rgba(0, 0, 0, 0.3)',
				      autoHide: false,
				      height: 40,
				      opacity: 1.0,			  
				      sliderBorder: 'none',
				      scrubberHeightRatio: 0.34,
				      scrubberBarHeightRatio: 0.3,
				      volumeSliderHeightRatio: 0.3,
				      timeBgHeightRatio: 0.5, 
				      timeSeparator: '/',
				      timeFontSize: 12,
				      fullscreen: false,
				      volume: false
				   }
				}
			});	
		
    }); // end plugin constructor
	
}; 
})(jQuery);  




//Initialize all galleries
jQuery(function() { 
	
	jQuery('.media_list').springGallery();
	jQuery('.media_scrollable').springGallery();
	jQuery('.media_scrollable').scrollable({
		next: '.scroll_next',
		prev: '.scroll_prev'
	});
	
	var constants = {
		'moveNext' : 3,
		'movePrev' : -3
		}
	
	jQuery("a.scroll_next").unbind();
	jQuery("a.scroll_next").click(function(){
			var items_remaining = jQuery(".media_scrollable").data("scrollable").getSize() - (jQuery(".media_scrollable").data("scrollable").getIndex() + 3);
			jQuery(".media_scrollable").data("scrollable").move(itemsToMove(items_remaining));
		});
	
	jQuery("a.scroll_prev").unbind();
	jQuery("a.scroll_prev").click(function(){
			var items_remaining_back = 0 - (jQuery(".media_scrollable").data("scrollable").getIndex());
			jQuery(".media_scrollable").data("scrollable").move(itemsToMoveBack(items_remaining_back));
		});
	
	function itemsToMove(items_remaining){
		if (items_remaining < constants.moveNext)
			return items_remaining;
		else
			return constants.moveNext; 
	}
	
	function itemsToMoveBack(items_remaining_back){
		if (items_remaining_back > constants.movePrev)
			return items_remaining_back;
		else
			return constants.movePrev; 
	}
	
	jQuery('.media_scrollable').find('.spring_gallery').insertAfter(jQuery('.media_scrollable'));
	jQuery('.album_pod .pods').cycle();
});
