// GALLERYSHOW SETTINGS (times are in milliseconds)
var galsho_slide_pauseTime = 8000;
var galsho_slide_fadeTime  = 1000;
var galsho_slide_slideVer  = false;
var galsho_slide_slideHor  = false;
var galsho_slide_carousel  = true;

var galsho_autoplay        = true;
var galsho_search          = false;
var galsho_playpause       = false;
var galsho_thumbnav        = false;
var galsho_thumb_paginate           = false;
var galsho_thumb_paginate_perPage   = 2;
var galsho_thumb_paginate_pageIcons = true;
var galsho_thumb_paginate_search    = true;

var slide_switch;
var active_position;
var globalDeactivate = false;
var thumbnavDeactivate = false;

// Only run if jQuery exists
if ( typeof jQuery == 'function' ) {
jQuery(document).ready(function($){

/* = GALLERYSHOW SETTINGS
------------------------------------------------------------- */
	
/* Make galleryshow active if js is enabled */
	jQuery('.galleryshow').addClass('galleryshow-active');
	
	jQuery('.galleryshow-active').each(function(i){
		// If nothing is active, make the first item active
		if (! jQuery(this).children('li.active').length ) {
			jQuery(this).children('li:first').addClass('active');
		}
		// fade in active item
		jQuery(this).children('li').css({opacity: 0.0}).filter('li.active').animate({opacity: 1.0}, galsho_slide_fadeTime);
		active_position = jQuery('.active', this).position();
		//console.log(active_position);
	});
	
/* Start the show */
	if ( galsho_autoplay ) slide_switch = setInterval("slide_switcher()", galsho_slide_pauseTime);
	// Adding search smallslideshow
	jQuery('.smallslideshow').addClass('galleryshow-addsearch');
	jQuery('.slideshow').addClass('galleryshow-addsearch');
	
/* Setup slide thumb nav */
	if ( galsho_thumbnav ) {
		jQuery('.galleryshow-thumbnav')
			.show()
			.each(function(i){
				if (! jQuery(this).children('li.active').length ) jQuery(this).children('li:first').addClass('active');
				jQuery(this).show();
			});
		if ( galsho_thumb_paginate ) {
			jQuery('.galleryshow-thumbnav').paginateThumbs({
				'perPage':   galsho_thumb_paginate_perPage, 
				'pageIcons': galsho_thumb_paginate_pageIcons, 
				'search':    galsho_thumb_paginate_search
			});
		}
		jQuery('.galleryshow-thumbnav a').click(function(e){
			e.preventDefault();
			if (! jQuery(this).parent('li').hasClass('active') && ! globalDeactivate ) {
				slide_switcher(jQuery('.galleryshow-active '+ jQuery(this).attr('href')), jQuery(this).parent());
				jQuery(this).parent().siblings('active').removeClass('active');
				jQuery(this).parent().addClass('active');
			}
			jQuery(this).blur();
		});
	}
	
/* Adds previous and next slide search */
	if ( galsho_search ) jQuery('.galleryshow-active').addClass('galleryshow-addsearch');
	jQuery('.galleryshow-addsearch').after('<ul class="galleryshow-search structural clearfix"><li class="prev"><a href="#" title="View Previous">Previous</a></li><li class="next"><a href="#" title="View Next">Next</a></li></ul>');
	jQuery('.galleryshow-search .prev a').click(function(e){
		e.preventDefault();
		if (! globalDeactivate ) slide_switcher('previous');
		jQuery(this).blur();
	});
	jQuery('.galleryshow-search .next a').click(function(e){
		e.preventDefault();
		if (! globalDeactivate ) slide_switcher();
		jQuery(this).blur();
	});

/* Setup galleryshow playpause */
	if ( galsho_playpause ) jQuery('.galleryshow-active').addClass('galleryshow-addplaypause');
	jQuery('.galleryshow-addplaypause').after('<ul class="galleryshow-playpause structural clearfix"><li class="play"><a href="#" title="Play Slideshow">Play</a></li><li class="pause"><a href="#" title="Pause Slideshow">Pause</a></li></ul>');
	jQuery('.galleryshow-playpause .pause a').click(function(e){
		e.preventDefault();
		jQuery(this).parent().addClass('active').siblings('.play').removeClass('active');
		clearInterval(slide_switch);
	});
	jQuery('.galleryshow-playpause .play a').click(function(e){
		e.preventDefault();
		jQuery(this).parent().addClass('active').siblings('.pause').removeClass('active');
		slide_switch = setInterval("slide_switcher()", galsho_slide_pauseTime);
	}).addClass('active');

/* = END GALLERYSHOW SETTINGS */

});

// Setup thumbnail pagination
(function($){
	jQuery.fn.paginateThumbs = function(opts){
		if (! opts ) opts = {};
		
		// Total per page
		var resultsPerPage = opts.perPage || 6;
		
		// Add page icons, default is on
		var addPageIcons = ( typeof(opts.pageIcons) != "undefined" && opts.pageIcons !== null ) ? opts.pageIcons : true;
		
		// Add previous / next search, default is on
		var addSearch = ( typeof(opts.search) != "undefined" && opts.search !== null ) ? opts.search : true;
		
		var child_prefix = 'gtc-page';
		
		var $ul = jQuery(this);
		var $li = jQuery('li', this);
		
		// Total number of pages:
		var pagesNumber = Math.ceil($li.length/resultsPerPage);
		
		var li_margin = parseInt(jQuery($li[0]).css('margin-right')) + parseInt(jQuery($li[0]).css('margin-left'));
		var li_width = jQuery($li[0]).width();
		var li_height = jQuery($li[0]).height();
		
		// Creating the controls div:
		var $controls = jQuery('<div class="galleryshow-thumbnav-controls clearfix">');
		
		for ( var i=0; i<pagesNumber; i++ ) {
			// Slice a portion of the li elements, and wrap it in a swPage div:
			$li.slice(i*resultsPerPage, (i+1)*resultsPerPage).wrapAll('<div class="'+ child_prefix +'" rel="'+ (i+1) +'" />');
			
			// Add a link to the controls div:
			if ( addPageIcons ) $controls.append('<a href="#'+ (i+1) +'" class="'+ child_prefix +'-icon">'+ (i+1) +'</a>');
		}
		
		var widthCount = ( $li.length < resultsPerPage ) ? $li.length : resultsPerPage;
		$ul.css({
			'width': (widthCount*li_width + widthCount*li_margin) +'px', // calculates the width of the li's and adds right margin
			'height': li_height
		});
		$controls.css('width', i*17);
		
		// If the pages are less than two, do nothing:
		if ( pagesNumber < 2 ) return this;
		
		$ul.after($controls);
		
		var $page = $ul.find('.'+ child_prefix);
		$page.wrapAll('<div class="'+ child_prefix +'-slider clearfix" />');
		var $slider = $ul.find('.'+ child_prefix +'-slider');
		//jQuery('h1#site-title').text($slider.css('left'));
		
		// add prev / next controls
		if ( addSearch ) {
			$controls
				.prepend('<a href="#prev" class="'+ child_prefix +'-prev">Previous</a>')
				.append('<a href="#next" class="'+ child_prefix +'-next">Next</a>');
			jQuery('.'+ child_prefix +'-prev').click(function(e){
				e.preventDefault();
				if (! thumbnavDeactivate ) {
					jQuery('.'+ child_prefix +'-next').removeClass('inactive');
					var $active = jQuery('.'+ child_prefix).filter('.active');
					if ( addPageIcons ) var $activeicon = $controls.find('a.active');
					
					if ( $active.prev('.'+ child_prefix).length ) {
						thumbnavDeactivate = true;
						var $next = $active.prev('.'+ child_prefix);
						jQuery(this).removeClass('inactive');
						
						$slider.stop().animate({'left': -(parseInt($next.attr('rel')) - 1) * $ul.width()}, 'slow', function(){ thumbnavDeactivate = false; });
						
						$next.addClass('active').siblings().removeClass('active');
						if ( addPageIcons && $activeicon.prev('.'+ child_prefix +'-icon').length ) {
							var $nexticon   = $activeicon.prev('.'+ child_prefix +'-icon');
							$nexticon.addClass('active').siblings().removeClass('active');
						}
						if (! $next.prev('.'+ child_prefix).length ) jQuery(this).addClass('inactive');
					}
				}
				jQuery(this).blur();
			});
			jQuery('.'+ child_prefix +'-next').click(function(e){
				e.preventDefault();
				if (! thumbnavDeactivate ) {
					jQuery('.'+ child_prefix +'-prev').removeClass('inactive');
					var $active = jQuery('.'+ child_prefix).filter('.active');
					if ( addPageIcons ) var $activeicon = $controls.find('a.active');
					
					if ( $active.next('.'+ child_prefix).length ) {
						thumbnavDeactivate = true;
						var $next = $active.next('.'+ child_prefix);
						jQuery(this).removeClass('inactive');
						
						$slider.stop().animate({'left': -(parseInt($next.attr('rel')) - 1) * $ul.width()}, 'slow', function(){ thumbnavDeactivate = false; });
						
						$next.addClass('active').siblings().removeClass('active');
						if ( addPageIcons && $activeicon.next('.'+ child_prefix +'-icon').length ) {
							var $nexticon   = $activeicon.next('.'+ child_prefix +'-icon');
							$nexticon.addClass('active').siblings().removeClass('active');
						}
						if (! $next.next('.'+ child_prefix).length ) jQuery(this).addClass('inactive');
					}
				}
				jQuery(this).blur();
			});
		}
		
		if ( addPageIcons ) {
			var $links = $controls.find('a.'+ child_prefix +'-icon');
			$links.click(function(e){
				e.preventDefault();
				if (! thumbnavDeactivate ) {
					thumbnavDeactivate = true;
					if ( addSearch ) jQuery('.'+ child_prefix +'-prev, .'+ child_prefix +'-next').removeClass('inactive');
					var $active = jQuery('.'+ child_prefix).filter('.active');
					jQuery('.'+ child_prefix +'[rel='+ jQuery(this).text() +']').addClass('active').siblings().removeClass('active');
					// If one of the control links is clicked, slide the swSlider div
					// (which contains all the pages) and mark it as active:
					jQuery(this).addClass('active').siblings().removeClass('active');
					$slider.stop().animate({'left': -(parseInt(jQuery(this).text()) - 1) * $ul.width()}, 'slow', function(){ thumbnavDeactivate = false; });
					
					if ( addSearch ) {
						if (! jQuery(this).prev('.'+ child_prefix +'-icon').length ) {
							jQuery('.'+ child_prefix +'-prev').addClass('inactive');
						}
						if (! jQuery(this).next('.'+ child_prefix +'-icon').length ) {
							jQuery('.'+ child_prefix +'-next').addClass('inactive');
						}
					}
				}
				jQuery(this).blur();
			});
			
			// Mark the first link as active the first time the code runs
			//$links.eq(0).addClass('active');
			$links.filter(':first').addClass('active');
		}
		
		jQuery('.'+ child_prefix +':first').addClass('active');
		
		return this;
	};
})(jQuery);

} // End jQuery test

/** 
 * THIS RUNS THE GALLERYSHOW.
 * Speed is controlled through JS, but placement and dimensions are controlled through CSS.
 */
function slide_switcher( $nextSlide, $nextNavItem ) {
	globalDeactivate = true;
	jQuery('.galleryshow-active').each(function(i){
		clearInterval(slide_switch);
		
		if (! jQuery(this).data('gallery-id') ) jQuery(this).data('gallery-id', Math.floor(Math.random()*1001));
		
		if ( jQuery(this).children().size() > 1 ) {
			var $active = jQuery(this).children('li.active');
			var $next;
			
			//if ( $nextSlide )
			if ( $nextSlide == 'previous' ) {
				$next = ( $active.prev().length ) ? $active.prev() : jQuery(this).children('li:last');
			} else if ( $nextSlide && $nextSlide != 'next' ) {
				$next = $nextSlide;
			} else {
				$next = ( $active.next().length ) ? $active.next() : jQuery(this).children('li:first');
			}
			
			var $activeNav = jQuery('.galleryshow-thumbnav li.active');
			var $nextNav = jQuery('.galleryshow-thumbnav li.'+ $next.attr('id'));
			
			$active.addClass('last-active');
			
			// Find out which direction images are moving, forward or back
			var active_index = parseInt($active.attr('id').replace('item',''));
			var next_index = parseInt($next.attr('id').replace('item',''));
			//var backward = ( active_index > next_index ) ? true : false;
			//var backward = ( (active_index > next_index && !(active_index == jQuery(this).children().length-1 && next_index == 0)) || (active_index == 0 && next_index == jQuery(this).children().length-1) ) ? true : false;
			var backward = ( 	(
									active_index > next_index && 
									false == (active_index == jQuery(this).children().length-1 && next_index == 0) 
								) || (active_index == 0 && next_index == jQuery(this).children().length-1)
							) ? true : false;
			// || (active_index == 0 && next_index == jQuery(this).children().length-1)
			//active_position = $active.position();
			var width = ( galsho_slide_slideHor ) ? (backward ? $active.width() : -$active.width()) : active_position.left;
			var height = ( galsho_slide_slideVer ) ? (backward ? $active.height() : -$active.height()) : active_position.top;
			
			$active
				.animate({opacity: 0.0, top: height, left: width}, galsho_slide_fadeTime, function(){
					//jQuery('.gallery-caption li.'+this.className.split(' ')[0]).hide();
					//jQuery(this).css({top: 0, left: 0});
					jQuery(this).removeClass('active last-active');
					$nextNav.addClass('active');
					$activeNav.hide().removeClass('active');
				});
			$next
				.css({opacity: 0.0, top: height, left: -width})
				.show()
				.addClass('active')
				.animate({opacity: 1.0, top: active_position.top, left: active_position.left}, galsho_slide_fadeTime, function(){
					jQuery('.gallery-caption li.'+this.className.split(' ')[0]).show();
					globalDeactivate = false;
					//jQuery('.gallery-caption li').text(this.className.split(' ')[0]);
				});
				
			$activeNav.removeClass('active');
			$nextNav.addClass('active').siblings().removeClass('active');
			
					if ( (jQuery('.galleryshow-playpause .play').length && jQuery('.galleryshow-playpause .play').is('.active')) || (!jQuery('.galleryshow-playpause .play').length && galsho_autoplay) ) {
						slide_switch = setInterval("slide_switcher()", galsho_slide_pauseTime);
					}
		}
	});
};
/**/
