var fullscreen_click = false;

$(document).ready(function() {
	
	$('.gallery').each(function(n) {
		var $images = $(this).find('.images');
		var $gallery = $(this);
		var $thumbGrid = $(this).find('.thumbGrid');
		
		var _class = '.gallery_' + n;
		
		var id = '#' + $(this).attr('id');
		
		$(this).addClass( _class.substr(1) );
		
		controls_html = '<div class="controls clearfix">';
			controls_html += '<div class="grid_4 alpha icons">';
				
				controls_html += '<span id="singleImageViewControls">';
					controls_html += '<a href="#" class="grid"><span>Show Thumbnails</span></a>';
					controls_html += '<a href="#" class="fullscreen"><span>Fullscreen</span></a>';
					controls_html += '<a href="#" class="previous"><span>Previous</span></a>';
					controls_html += '<a href="#" class="next"><span>Next</span></a>';
				controls_html += '</span>';

				controls_html += '<span id="ThumbnailViewControls">';
					controls_html += '<a href="#" class="singlePhoto"><span>Show Single Photo</span></a>';
					controls_html += '<a href="#" class="thumb_previous"><span>Previous</span></a>';
					controls_html += '<a href="#" class="thumb_next"><span>Next</span></a>';
				controls_html += '</span>';


				controls_html += '<span class="instructions">Use Left / Right Arrow Keys to Navigate</span>';
			controls_html += '</div>';
				controls_html += '<span class="paginationInfo"></span>';
				controls_html += '<span class="caption">Caption</span>';
		controls_html += '</div>';
		
		if( $gallery.hasClass('controls') ) {
			$(controls_html).prependTo( $(this) );
			$('.container_12').addClass('gallery-with-controls');
			
			var $controls = $(this).find('.controls');
			
			$controls.find('.fullscreen').bind('click', function() {
				$images.find('a').attr('rel', 'gallery').fancybox({
					padding		: 0,
					hideOnOverlayClick: false,
					onCleanup : function() {
						$images.find('a').unbind('click.fb');
						$('#fancybox-left,#fancybox-right').unbind('click.cy');
					},
					overlayOpacity: 1,
					overlayColor: '#ffffff',
					cyclic: true
				});

				$images.find('a:visible').trigger('click.fb');

			});
			



/**** Thumbnail/photo toggle click part1: toggle to thumbnail view. *********/
			$('.controls .grid').click(function(){

				$('.images').fadeOut(0);
				$(this).fadeOut(0);
				$('.fullscreen').fadeOut(0);
				$('.previous').fadeOut(0);
				$('.next').fadeOut(0);
				$('.instructions').fadeOut(0);
				$('.caption').fadeOut(0);

				$('.paginationInfo').fadeIn('slow');
				$('.thumbGrid').fadeIn("slow");
				$('.thumb_previous').fadeIn('slow');
				$('.thumb_next').fadeIn('slow');
				$('.singlePhoto').fadeIn();

			});
			
/**** Thumbnail/photo toggle click part2: toggle to single photo view *******/

			$(".controls .singlePhoto").click(function(){
				$('.thumbGrid').fadeOut(0);
				$(this).fadeOut(0);
				$('.thumb_next').fadeOut(0);				
				$('.thumb_previous').fadeOut(0);
				$('.paginationInfo').fadeOut(0);

				$('.caption').fadeIn('slow');
				$('.images').fadeIn('slow');
				$('.grid').fadeIn('slow');
				$('.fullscreen').fadeIn('slow');
				$('.previous').fadeIn('slow');
				$('.next').fadeIn('slow');
				$('.instructions').fadeIn('slow');

				$('.caption').html($images.find('a:visible img').attr('alt'));
			});

/**** End Thumbnail/photo toggle click part2: toggle to single photo view ***/

			// Thumbnails set to 70% opacity and fadeIn to 100% on rollover
			$('.thumbGrid ul li a').fadeTo('slow',0.7,function(){
				$(this).hover(
					function(){
						$(this).fadeTo('fast',1);
					},
					function(){
						$(this).fadeTo('fast',.7);
					}
				);
			});

			$thumbGrid.find('ul li a').each(function(n) {
				$(this).click(function() {


					$images.cycle(n);


					return false;
				});
			});
			
			$thumbGrid.find('ul li:nth-child(6n)').addClass('end');
			
			if( $thumbGrid.find('ul').length <= 1 ) {
				$controls.find('.thumb_previous,.thumb_next').remove();
			}
		}


		/* 'Use Left / Right Arrow Keys to Navigate' message dissappears onclick */
		$( _class + ' .images a' ).click(function(){
			$('.gallery .controls .instructions').fadeOut();
		});
		
		$('.controls .next').click(function(){
			$('.gallery .controls .instructions').fadeOut();
		});

		$('.controls .previous').click(function(){
			$('.gallery .controls .instructions').fadeOut();
		});


		$images.cycle({
			next: ( $gallery.hasClass('controls') ? _class + ' .controls .next, ' + _class + ' .images a' : '' ),
			prev: ( $gallery.hasClass('controls') ? _class + ' .controls .previous' : '' ),
			after: function() {
				if ( $('.thumbGrid').is(':visible')) {
					$('.thumbGrid').fadeOut(0);
					$('.singlePhoto').fadeOut(0);
					$('.thumb_next').fadeOut(0);				
					$('.thumb_previous').fadeOut(0);
					$('.paginationInfo').fadeOut(0);

					$('.caption').fadeIn('slow');
					$('.images').fadeIn('slow');
					$('.grid').fadeIn('slow');
					$('.fullscreen').fadeIn('slow');
					$('.previous').fadeIn('slow');
					$('.next').fadeIn('slow');
					$('.instructions').fadeIn('slow');
				}


			},
			before: function() {
				var $slide = $(this);
				
				if( $(this).find('span').length > 0 ) {
					$slide.find('span').timbarber(function() {
						if( $slide.next().length > 0 ) $slide.next().find('span').timbarber();
						if( $slide.prev().length > 0 ) $slide.prev().find('span').timbarber();
					});
				} else {
					$slide.timbarber(function() {
						if( $slide.next().length > 0 ) $slide.next().timbarber();
						if( $slide.prev().length > 0 ) $slide.prev().timbarber();
					});
				}

				if ( $('.images').is(':visible')) {



					$('.caption').html( $(this).find('img').attr('alt') );


				}
			},
			timeout: 0
		});


		$thumbGrid.cycle({
			fx: 'scrollHorz',
			next: _class + ' .controls .thumb_next',
			prev: _class + ' .controls .thumb_previous',
			timeout: 0,
			easing: 'easeOutQuart',
			before: function() {
					$('.paginationInfo').html( 'Page ' + ( parseInt($thumbGrid.find('ul').index($(this))) + 1 ) + ' / ' + $thumbGrid.find('ul').length );	
			},
			nowrap: true
		});





		$thumbGrid.find('li a').hover(function() {
			var _caption = $(this).find('img').attr('alt');
			
			if( _caption != '' ) {
				$('.caption').html( _caption );
			}
		});

		
	});


	
}).bind('keydown', 'left', function() {
	$('.gallery .controls .previous:visible, .gallery .controls .thumb_previous:visible').click();
}).bind('keydown', 'right', function() {
	$('.gallery .controls .next:visible, .gallery .controls .thumb_next:visible').click();
});



/*
	rewrite the fade effect (why???)
*/
/*
$.fn.cycle.transitions._fade = function($cont, $slides, opts) {
	$slides.not(':eq('+opts.currSlide+')').css('opacity',0);
	opts.before.push(function(curr,next,opts) {
		$.fn.cycle.commonReset(curr,next,opts);
		opts.cssBefore.opacity = 0;
	});
	opts.animIn	   = { opacity: 1 };
	opts.animOut   = { opacity: 1 };
	opts.cssBefore = { top: 0, left: 0 };
}
*/