/*-----------------------------------------------------------------------------------

 	Custom JS - All front-end jQuery
 
-----------------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------------*/
/* JavaScript Fallback Class
/*-----------------------------------------------------------------------------------*/

jQuery('body').addClass('jQuery-enabled');

/*-----------------------------------------------------------------------------------*/
/* Document Ready
/*-----------------------------------------------------------------------------------*/

jQuery(document).ready(function() {
	
/*-----------------------------------------------------------------------------------*/
/*	Image Preload
/*-----------------------------------------------------------------------------------*/

	var speed = 100; //speed in ms
	
	jQuery(window).load(function() {
	
		jQuery("#gallery-wrap img").hide();
		
		timer = setInterval(function(){
		
			$notLoaded = jQuery("#gallery-wrap img").not(".loaded");
			$notLoaded.eq(Math.floor(Math.random()*$notLoaded.length)).fadeIn().addClass("loaded");
			
			if ($notLoaded.length == 0) {
				clearInterval(timer);
			}
		}, speed);

	}); 

/*-----------------------------------------------------------------------------------*/
/*	Image Hover
/*-----------------------------------------------------------------------------------*/

	jQuery('#gallery-wrap li img, .post-thumb a img, .flickr_badge_image a img').hover( function() { 
		jQuery(this).stop().animate({opacity : 0.5 }, 200);
	}, function () {
		jQuery(this).stop().animate({opacity : 1 }, 200);
	});

/*-----------------------------------------------------------------------------------*/
/*	Gallery Image Sorting
/*-----------------------------------------------------------------------------------*/
	
	function sortPhotos(cat) {
		
		if(jQuery('#gallery-wrap li').hasClass(cat)) {
			
			jQuery('#gallery-wrap li')
				.not('.'+cat)
				.removeClass('visible')
				.find('a')
				.fadeOut(500);
			
			jQuery('.'+cat)
				.addClass('visible')
				.find('a')
				.fadeIn(800);
		}
	
	}
		
	jQuery('#gallery-terms li a').click( function() { 
	
		var cat = jQuery(this).attr('data-value')

		sortPhotos(cat);

		jQuery('#gallery-terms li a').removeClass( "current" );
		
		jQuery(this).addClass( "current" );
		
		return false;

	});
	
	jQuery('#gallery-terms li a.all').click( function(e) { 	
		
		jQuery('#gallery-wrap li')
			.addClass('visible')
			.find('a')
			.fadeIn(500);
			
		return false;
		
	});
	
	
/*-----------------------------------------------------------------------------------*/
/*	Gallery Overlay
/*-----------------------------------------------------------------------------------*/
	
	if(jQuery('#gallery-overlay').hasClass('true')) {
		
		jQuery('#gallery-wrap li a').click( function(e) {
			
			//the post id
			var postId = jQuery(this)
							.parent()
							.attr('id')
							.split('photo-')[1];
			
			//get the photo
			getPhoto(postId, this);
			
			e.preventDefault();
			
		});
		
	}
	
	
	//Initialize the gallery
	function galleryInit() {
		
		jQuery('#next-post, #prev-post').click( function() {
			
			var postId = jQuery(this).attr('data-id');
		
			getPhoto(postId, jQuery('li#photo-'+postId+' a'));
			
			return false;
			
		});
		
		
		jQuery('.gallery-close a').click( function() { 
			
			jQuery('body').css({overflow: 'auto'});
			
			jQuery('#gallery-overlay, #body-overlay').fadeOut(500);
			
			return false;
			
		});
	}

/*-----------------------------------------------------------------------------------*/
/*	Gallery Overlay Ajax
/*-----------------------------------------------------------------------------------*/

	jQuery('#loader').click( function () {
		
		jQuery('#gallery-overlay').fadeOut(1, function () {
			jQuery('#loader').fadeOut(200);
		});
		
	});
	
	function getPhoto(postId, hasSelector) {
		
		//first visible value
		var next = jQuery('#gallery-wrap li.visible')
						.first()
						.attr('id').split('photo-')[1];
					
		//last visible value
		var prev = jQuery('#gallery-wrap li.visible')
						.last()
						.attr('id').split('photo-')[1];
		
		//has next var
		var hasNext = jQuery(hasSelector).parent().next();
		
		//has prev var
		var hasPrev = jQuery(hasSelector).parent().prev();
		

		//if there is a next object
		if(hasNext.length != 0) {
			
			while(hasNext.hasClass('visible') == false && hasNext.length != 0) {
				hasNext = hasNext.next();
			}
			
			if(hasNext.length != 0) {
				var next = hasNext
						.attr('id').split('photo-')[1];
			}
		}
		
		//if there is a previous object
		if(hasPrev.length != 0) {
			
			while(hasPrev.hasClass('visible') == false && hasPrev.length != 0) {
				hasPrev = hasPrev.prev();
			}
			
			if(hasPrev.length != 0) {
				var prev = hasPrev
						.attr('id').split('photo-')[1];
			}
		}
		
		jQuery('#loader').fadeIn(200, function() {
			
			jQuery.ajax({
				url: jQuery('#theme-url').attr('data-id'),
				data: {id : postId, next : next, prev : prev },
				success: function(data) {
	
					var gallery = jQuery('#gallery-overlay');
					
					gallery.html(data);
					
					jQuery('#next-post').attr('data-id', next);
					
					jQuery('body').css({overflow: 'hidden'});
					
					jQuery('#gallery-overlay, #body-overlay').fadeIn(500, function () {
						jQuery('#loader').fadeOut(500);
					});

					//re-initialize the gallery
					galleryInit();
				}
			});
			
		});
	}
	
/*-----------------------------------------------------------------------------------*/
/*	Gallery Scroller
/*-----------------------------------------------------------------------------------*/
	
	var width = 0;
	
	jQuery('#gallery-wrap li').each( function() { 
		 width += jQuery(this).width();
	});
	
	function handleSliderChange(e, ui) {
		var maxScroll = jQuery("#gallery-wrap").attr("scrollWidth") - jQuery("#gallery-wrap").width();
		jQuery("#gallery-wrap").stop().animate({
				scrollLeft: ui.value * (maxScroll / 100),
				duration: 1000,
				specialEasing: { 
					marginLeft: "easeInOutCirc" 
				}
			});
	}
	
	function handleSliderSlide(e, ui) {
		var maxScroll = jQuery("#gallery-wrap").attr("scrollWidth") - jQuery("#gallery-wrap").width();
		jQuery("#gallery-wrap").stop().animate({
				scrollLeft: ui.value * (maxScroll / 100),
				duration: 1000,
				specialEasing: { 
					marginLeft: "easeInOutCirc" 
				}
			});
	}
		
	function hideSlider() {
		
		if(width < jQuery(window).width() || jQuery("#gallery-wrap ul").width() < jQuery(window).width()) {
			jQuery('#content-slider').fadeOut(200);
		} else {
			jQuery('#content-slider').fadeIn(200);
			return true;
		}
	}
	
	jQuery("#content-slider").slider({
		animate: true,
		change: handleSliderChange,
		slide: handleSliderSlide,
		max: 100
  	});
	
	jQuery(window).resize(function() {
		hideSlider(); 
		//console.log(width);
	});
	
	hideSlider();
});