jQuery( document ).ready( function( $ ) {

	'use strict';

	/*--------------------------------------------------------------
	Screen size class
	--------------------------------------------------------------*/

	function gpScreenSizeClass() {
	
		if ( $( window ).width() <= 767 && $( 'body' ).hasClass( 'gp-responsive' ) ) {
		
			$( 'body' ).addClass( 'gp-mobile' ).removeClass( 'gp-desktop' ).removeClass( 'gp-tablet-portrait' ).removeClass( 'gp-tablet-landscape' );
			
		} else if ( $( window ).width() <= 1023 && $( 'body' ).hasClass( 'gp-responsive' ) ) {
			
			$( 'body' ).addClass( 'gp-tablet-portrait' ).removeClass( 'gp-desktop' ).removeClass( 'gp-tablet-landscape' ).removeClass( 'gp-mobile' );
			
		} else if ( $( window ).width() <= 1082 && $( 'body' ).hasClass( 'gp-responsive' ) ) {
			
			$( 'body' ).addClass( 'gp-tablet-landscape' ).removeClass( 'gp-desktop' ).removeClass( 'gp-tablet-portrait' ).removeClass( 'gp-mobile' );	
		
		} else {
			
			$( 'body' ).addClass( 'gp-desktop' ).removeClass( 'gp-tablet-landscape' ).removeClass( 'gp-tablet-portrait' ).removeClass( 'gp-mobile' );
		
		}
		
	}
	
	gpScreenSizeClass();
	$( window ).resize( gpScreenSizeClass );
	

	/*--------------------------------------------------------------
	Retina images
	--------------------------------------------------------------*/

	if ( $( 'body' ).hasClass( 'gp-retina' ) ) {
	
		window.devicePixelRatio >= 2 && $( '.post-thumbnail img' ).each( function() {
			$( this ).attr( { src: $( this ).attr( 'data-rel' ) } );
		});
	
		window.devicePixelRatio >= 2 && $( '.image-loop img' ).each(function() {
			$( this ).attr( { 'data-original': $( this ).attr( 'data-rel' ) } );
		});

		window.devicePixelRatio >= 2 && $( '.gp-slider .slide-image' ).each(function(){
			var bgImage = $(this).attr( 'data-rel' );
			$( this ).css( 'background-image', 'url(' + bgImage + ')' );
		});
	
	}
	

	/*--------------------------------------------------------------
	Parallax Effect
	--------------------------------------------------------------*/

	if( $( 'div' ).hasClass( 'gp-parallax' ) || $( 'header' ).hasClass( 'gp-parallax' ) ) {
		$( window ).stellar( {
			responsive: true,
			horizontalScrolling: false
		});
	}
		

	/*--------------------------------------------------------------
	Hide content depending on width
	--------------------------------------------------------------*/
		
	function gpHideContent() {	
	
		$( '.inner-loop' ).each( function() {
			if ( $( this ).width() < 300 ) {
				$( this ).find( '.featured-content.image-align-left' ).hide();
			} else {
				$( this ).find( '.featured-content.image-align-left' ).show();
			}
		});	

		$( '.blog-wrapper .post-item .post-thumbnail, .related-wrapper .post-item .post-thumbnail' ).each( function() {
			if ( $( this ).width() < 175 ) {
				$( this ).find( '.rating-wrapper, .thumbnail-header' ).hide();
			} else {
				$( this ).find( '.rating-wrapper, .thumbnail-header' ).show();
			}
		});	
				
	}
		
	gpHideContent();
	$( window ).resize( gpHideContent );
	
			
	/*--------------------------------------------------------------
	Masonry blog
	--------------------------------------------------------------*/

	if ( $( '.blog-wrapper' ).hasClass( 'blog-masonry' ) ) {
	
		var container = $( '.blog-masonry .inner-loop' );
		var element = container;

		if ( container.find( 'img' ).length == 0 ) {
			element = $( '<img />' );
		}	

		imagesLoaded( element, function( instance ) {

			container.isotope({
				itemSelector: '.post-item',
				masonry: {
					columnWidth: container.find( '.post-item' )[0],
					gutter: 20
				}
			});

			container.animate( { 'opacity': 1 }, 1300 );
			$( 'ul.page-numbers' ).animate( { 'opacity': 1 }, 1300 );

		});
				
	}

	
	/*--------------------------------------------------------------
	Lazyload Images
	--------------------------------------------------------------*/

	if ( $( 'div' ).hasClass( 'images-wrapper' ) ) {

		$( '.image-loop img' ).lazyload({
			 effect: 'fadeIn'
		});
	
		$( '.images-wrapper').animate( { 'opacity': 1 }, 1300 );

	}
	

	/*--------------------------------------------------------------
	Tab/Content Menu Width
	--------------------------------------------------------------*/

	function gpMenuWidth() {
	
		var menuWidth = $( '#main-header .gp-container' ).width();

		function gpScrollLogoWidth( width ) {
			if ( $( 'body' ).hasClass( 'gp-header-centered' ) ) {
				var menuLeftMargin = 0;
			} else {
				var menuLeftMargin = width;
			}
			$( '.nav ul > li.megamenu, .nav ul > li.content-menu, .nav ul > li.tab-content-menu' ).each( function() {
				var navItem = $( this );
				if ( $( navItem ).find( 'ul' ).length > 0 ) {	
					navItem.find( '.sub-menu.menu-depth-1' ).css({ 'width': menuWidth, 'left': -menuLeftMargin });
				}	
			});						
		}		
						
		// Before Scrolling
		if ( $( window ).scrollTop() > 50 && $( 'body' ).hasClass( 'gp-desktop' ) && $( 'body' ).hasClass( 'gp-fixed-header' ) && $( 'body' ).hasClass( 'gp-header-resize' ) ) {
			gpScrollLogoWidth( parseInt( gp_script.logoWidth ) / gp_script.headerSizeReduction );
		} else {
			gpScrollLogoWidth( parseInt( gp_script.logoWidth ) );
		}
				
		// Upon Scrolling		
		$( window ).scroll( function() {
			if ( $( window ).scrollTop() > 50 && $( 'body' ).hasClass( 'gp-desktop' ) && $( 'body' ).hasClass( 'gp-fixed-header' ) && $( 'body' ).hasClass( 'gp-header-resize' ) ) {
				gpScrollLogoWidth( parseInt( gp_script.logoWidth ) / gp_script.headerSizeReduction );		
			} else {
				gpScrollLogoWidth( parseInt( gp_script.logoWidth ) );
			}	
		});	
	
	}
	
	$( window ).load(function() { gpMenuWidth(); });
	$( window ).resize( gpMenuWidth );
	
		
	/*--------------------------------------------------------------
	Switch navigation position if near edge
	--------------------------------------------------------------*/

	function gpSwitchNavPosition() {
		$( '#main-nav .menu > li.standard-menu' ).each( function() {
			$( this ).on( 'mouseenter mouseleave', function(e) {
				if ( $( this ).find( 'ul' ).length > 0 ) {
					var menuElement = $( 'ul:first', this );
					var pageWrapper = $( '#page-wrapper' );
					var pageWrapperOffset = pageWrapper.offset();
					var menuOffset = menuElement.offset();
					var menuLeftOffset = menuOffset.left - pageWrapperOffset.left;
					var menuWidth = menuElement.width() + 260;		
					var pageWrapperWidth = pageWrapper.width();
					var isEntirelyVisible = ( menuLeftOffset + menuWidth <= pageWrapperWidth );	
					if ( ! isEntirelyVisible ) {
						$( this ).addClass( 'nav-edge' );
					} else {
						$( this ).removeClass( 'nav-edge' );
					}
				}   
			});
		});	
	}

	gpSwitchNavPosition();
	$( window ).resize( gpSwitchNavPosition );
	
	
	/*--------------------------------------------------------------
	FontAwesome menu icons
	--------------------------------------------------------------*/
		
	$( '.menu li.fa' ).each( function() {	
		var all = $( this ).attr( 'class' ).split(' ');
		for ( var i = 0; i < all.length; ++i ) {
			var cls = all[i];
			if ( cls.indexOf( 'fa' ) == 0 ) {
				$( this ).find( '> a' ).addClass( cls );
				$( this ).removeClass( cls );
			}
		}
	});
		

	/*--------------------------------------------------------------
	Dropdown menu icons
	--------------------------------------------------------------*/
		
	$( '#main-nav .menu > li' ).each( function() {
		if ( $( this ).find( 'ul' ).length > 0 ) {	
			$( '<i class="dropdown-icon primary-dropdown-icon fa fa-caret-down" />' ).appendTo( $( this ).children( ':first' ) );		
		}		
	});
	
	$( '#main-nav .menu > li.standard-menu ul > li, #right-top-nav .menu > li ul > li' ).each( function() {
		if ( $( this ).find( 'ul' ).length > 0 ) {	
			$( '<i class="dropdown-icon secondary-dropdown-icon fa" />' ).appendTo( $( this ).children( ':first' ) );
		}					
	});
		
		
	/*--------------------------------------------------------------
	Mega menus text/image support
	--------------------------------------------------------------*/
		
	if ( $( '.megamenu' ).length > 0 ) {
		
		$( '.nav-text > a, .menu-text > a' ).contents().unwrap();
			
		$( '.nav .megamenu .sub-menu .sub-menu li.nav-image, .nav .megamenu .sub-menu .sub-menu li.menu-image' ).each( function() {
			if ( $( this ).find( 'a' ).length > 0 ) {	
				var src = $( this ).find( 'a' ).attr( 'href' );
				var alt = $( this ).find( 'a' ).attr( 'title' );
				$( '<img class="menu-image" alt="">' ).insertAfter( $( this ).children( ':first' ) );
				$( this ).find( '.menu-image' ).attr( { 'src': src, 'alt': alt } );
				$( this ).find( 'a' ).remove();				
			}			
		});
	
		$( '#mobile-nav .menu-image' ).hide();
	
	}
							
	/*--------------------------------------------------------------
	Slide up/down header mobile navigation
	--------------------------------------------------------------*/

	function gpHeaderMobileNav() {

		$( '#header-mobile-nav-button' ).toggle( function() {
			$( '#mobile-nav' ).stop().slideDown().removeClass( 'auto-height' );
			$( '#header-mobile-nav-button' ).addClass( 'gp-active' );
		}, function() {
			$( '#mobile-nav' ).stop().slideUp().removeClass( 'auto-height' );
			$( '#header-mobile-nav-button' ).removeClass( 'gp-active' );
		});
		
	}
	
	gpHeaderMobileNav();
	$( window ).resize( gpHeaderMobileNav );


	/*--------------------------------------------------------------
	Slide up/down header mobile dropdown menus
	--------------------------------------------------------------*/

	$( '#mobile-nav .menu li' ).each( function() {
		if ( $( this ).find( 'ul' ).length > 0 ) {
			$( '<i class="mobile-dropdown-icon" />' ).insertAfter( $( this ).children( ':first' ) );		
		}		
	});
	
	function gpHeaderMobileTopNav() {

		$( '#mobile-nav ul > li' ).each( function() {
			
			var navItem = $( this );
			
			if ( $( navItem ).find( 'ul' ).length > 0 ) {	
		
				$( navItem ).children( '.mobile-dropdown-icon' ).toggle( function() {
					$( navItem ).addClass( 'gp-active' );
					$( navItem ).children( '.sub-menu' ).stop().slideDown()
					$( '#mobile-nav' ).addClass( 'auto-height' );
				}, function() {
					$( navItem ).removeClass( 'gp-active' );
					$( navItem ).children( '.sub-menu' ).stop().slideUp();
				});
		
			}
					
		});
	
	}
	
	gpHeaderMobileTopNav();
	$( window ).resize( gpHeaderMobileTopNav );

	
	/*--------------------------------------------------------------
	Slide up/down hub tabs mobile navigation
	--------------------------------------------------------------*/

	function gpTabsMobileNav() {

		$( '#hub-tabs #hub-tabs-mobile-nav-button' ).toggle( function() {
			$( '#hub-tabs ul' ).stop().slideDown();
			$( '#hub-tabs-mobile-nav-button' ).addClass( 'gp-active' );
		}, function() {
			$( '#hub-tabs ul' ).stop().slideUp();
			$( '#hub-tabs-mobile-nav-button' ).removeClass( 'gp-active' );
		});
		
	}
	
	gpTabsMobileNav();
	$( window ).resize( gpTabsMobileNav );
			
			
	/*--------------------------------------------------------------
	Smooth scroll
	--------------------------------------------------------------*/

	if ( $( 'body' ).hasClass( 'gp-smooth-scrolling' ) && $( window ).width() > 767 && $( 'body' ).outerHeight( true ) > $( window ).height() ) {
		$( 'html' ).niceScroll({
			cursorcolor: '#424242',
			scrollspeed: 100,
			mousescrollstep: 40,
			cursorwidth: 10,
			cursorborder: '0',
			zindex: 10000,
			cursoropacitymin: 0.3,
			cursoropacitymax: 0.6
		});
	}
	
	
	/*--------------------------------------------------------------
	Remove gallery shortcode when gallery slider active 
	--------------------------------------------------------------*/

	$( '.gallery-slider-active .gallery' ).remove();
	
	
	/*--------------------------------------------------------------
	Back to top button
	--------------------------------------------------------------*/

	if ( $( 'body' ).hasClass( 'gp-back-to-top' ) ) {
		$().UItoTop({ 
			text: '<i class="fa fa-chevron-up"></i>',
			scrollSpeed: 600
		});
	}
		

	/*--------------------------------------------------------------
	prettyPhoto lightbox
	--------------------------------------------------------------*/

	if ( gp_script.lightbox != 'disabled' ) {
		$( 'a[data-rel^="prettyPhoto"]' ).prettyPhoto({
			hook: 'data-rel',
			theme: 'pp_default',
			deeplinking: false,
			social_tools: '',
			default_width: '768'
		});
	}	
	

	/*--------------------------------------------------------------
	Remove WooCommerce Comment Meta Dash 
	--------------------------------------------------------------*/

	$( '.comment-text .meta' ).html( function( index, text ) {
		return text.replace( '–', '' );
	});


	/*--------------------------------------------------------------
	WooCommerce image overlay 
	--------------------------------------------------------------*/

	$( '.product-image-container .image-overlay' ).css( 'opacity', 0 ).hover( function() {
		$( this ).fadeTo( 'fast', 1 );
	}, function() {
		$( this ).fadeTo( 'fast', 0 );
	});	


	/*--------------------------------------------------------------
	Set slider height to auto 
	--------------------------------------------------------------*/

	$( window ).load( function() {
		if ( $( window ).width() > 1082 ) {
			$( '#homepage-slider' ).css( 'height', 'auto' );	
		}
	});


	/*--------------------------------------------------------------
	Featured wrapper caption
	--------------------------------------------------------------*/

	$( '.featured-post' ).css( 'opacity', 1 );	
	 	
	function gpCaptionPadding() {
		var padding = ( $( '#featured-wrapper .gp-container' ).width() - $( '#main-header .gp-container' ).width() ) / 2;
		$( '.large-post .featured-caption' ).css( 'padding-left', padding );
		$( '.small-posts .featured-caption' ).css( 'padding-right', padding );	 	
	}

	gpCaptionPadding();
	$( window ).resize( gpCaptionPadding );
	

	/*--------------------------------------------------------------
	Follow Items
	--------------------------------------------------------------*/

	$( '.wpfp-span .unfollow-item' ).show();
	
	$( '.wpfp-link' ).live( 'click', function() {
		var dhis = $( this );
		wpfp_do_js( dhis, 1 );
		if ( dhis.hasClass( 'unfollow-item' ) ) {
			dhis.parent( '.post-item' ).fadeOut();
		}
		return false;
	});

	function wpfp_do_js( dhis, doAjax ) {
		dhis.addClass( 'follow-loading' );
		var url = document.location.href.split( '#' )[0];
		var params = dhis.attr('href').replace( '?', '' ) + '&ajax=1';
		if ( doAjax ) {
			jQuery.get( url, params, function( data ) {
					dhis.parent().html( data );
					if ( typeof wpfp_after_ajax == 'function' ) {
						wpfp_after_ajax( dhis );
					}
					dhis.removeClass( 'follow-loading' );
				}
			);
		}
	}
	
	/*--------------------------------------------------------------
	Title header video
	--------------------------------------------------------------*/
	
	if ( $( '.entry-header' ).hasClass( 'video-header' ) ) {
		headerVideo.init({
			mainContainer: $( '.large-header.entry-header' ),
			videoContainer: $( '.header-video' ),
			header: $( '.header-video-media' ),
			videoTrigger: $( '.play-video-button' ),
			autoPlayVideo: false
		});
	}		
		
	/*--------------------------------------------------------------
	Resize header
	--------------------------------------------------------------*/

	function gpResizeHeader() {

		// Before Scrolling
		
		if ( ( $( window ).width() <= 982 && $( 'body' ).hasClass( 'gp-responsive' ) ) || ( $( window ).scrollTop() > 0 && $( 'body' ).hasClass( 'gp-desktop' ) && $( 'body' ).hasClass( 'gp-fixed-header' ) && $( 'body' ).hasClass( 'gp-header-resize' ) ) ) {

			$( '.gp-header-standard #main-header' ).data( 'size', 'small' ).addClass( 'header-small' ).removeClass( 'header-large' ).stop().css({
				height: parseInt( gp_script.smallHeaderHeight )
			});
					
			$( '.gp-header-centered #main-header' ).data( 'size', 'small' ).addClass( 'header-small' ).removeClass( 'header-large' ).stop().css({
				height: parseInt( gp_script.smallHeaderHeight ) + parseInt( gp_script.logoMarginTop ) / gp_script.headerSizeReduction + parseInt( gp_script.logoMarginBottom ) / gp_script.headerSizeReduction
			});
														
			$( '#main-header #logo' ).stop().css({
				marginTop: parseInt( gp_script.logoMarginTop ) / gp_script.headerSizeReduction,
				marginBottom: parseInt( gp_script.logoMarginBottom ) / gp_script.headerSizeReduction
			}); 
		
			$( '#main-header #logo img' ).stop().css({
				height: parseInt( gp_script.logoHeight ) / gp_script.headerSizeReduction
			}); 
			
					
			$( '#fixed-header-padding' ).stop().css({
				paddingTop: parseInt( gp_script.smallHeaderHeight )
			});
						
		} else {

			$( '.gp-header-standard #main-header' ).data( 'size', 'large' ).addClass( 'header-large' ).removeClass( 'header-small' ).stop().css({
				height: gp_script.headerHeight
			});
					
			$( '.gp-header-centered #main-header' ).data( 'size', 'large' ).addClass( 'header-large' ).removeClass( 'header-small' ).stop().css({
				height: parseInt( gp_script.headerHeaderHeight ) + parseInt( gp_script.logoMarginTop ) + parseInt( gp_script.logoMarginBottom )
			});
														 
			$( '#main-header #logo' ).stop().css({
				marginTop: parseInt( gp_script.logoMarginTop ),
				marginBottom: parseInt( gp_script.logoMarginBottom )
			}); 
		
			$( '#main-header #logo img' ).stop().css({
				height: parseInt( gp_script.logoHeight )
			});
			
		
			$( '#fixed-header-padding' ).stop().css({
				paddingTop: parseInt( gp_script.headerHeight )
			});	
									
		}
					
		// Upon Scrolling

		$( window ).scroll( function() {
		
			if ( $( 'body' ).hasClass( 'gp-desktop' ) && $( 'body' ).hasClass( 'gp-fixed-header' ) && $( 'body' ).hasClass( 'gp-header-resize' ) ) {

				if ( $( document ).scrollTop() > 50 ) {

					if ( $( '#main-header' ).data( 'size' ) == 'large' )  {
	
						$( '.gp-header-standard #main-header' ).data( 'size', 'small' ).addClass( 'header-small' ).removeClass( 'header-large' ).stop().animate({
							height: parseInt( gp_script.smallHeaderHeight )
						}, 300 );
						
						$( '.gp-header-centered #main-header' ).data( 'size', 'small' ).addClass( 'header-small' ).removeClass( 'header-large' ).stop().animate({
							height: parseInt( gp_script.smallHeaderHeight ) + ( parseInt( gp_script.logoMarginTop ) / gp_script.headerSizeReduction ) + ( parseInt( gp_script.logoMarginBottom ) / gp_script.headerSizeReduction )
						}, 300 );
															
						$( '#main-header #logo' ).stop().animate({
							marginTop: parseInt( gp_script.logoMarginTop ) / gp_script.headerSizeReduction,
							marginBottom: parseInt( gp_script.logoMarginBottom ) / gp_script.headerSizeReduction
						}, 300 ); 
			
						$( '#main-header #logo img' ).stop().animate({
							height: parseInt( gp_script.logoHeight ) / gp_script.headerSizeReduction
						}, 300 ); 
				
						
						$( '#fixed-header-padding' ).stop().animate({
							paddingTop: parseInt( gp_script.smallHeaderHeight )
						}, 400 );
						
					}

				} else {

					if ( $( '#main-header' ).data( 'size' ) == 'small' ) {
	
						$( '.gp-header-standard #main-header' ).data( 'size', 'large' ).addClass( 'header-large' ).removeClass( 'header-small' ).stop().animate({
							height: gp_script.headerHeight
						}, 300 );
						
						$( '.gp-header-centered #main-header' ).data( 'size', 'large' ).addClass( 'header-large' ).removeClass( 'header-small' ).stop().animate({
							height: parseInt( gp_script.headerHeight ) + parseInt( gp_script.logoMarginTop ) + parseInt( gp_script.logoMarginBottom )
						}, 300 );
															 
						$( '#main-header #logo' ).stop().animate({
							marginTop: parseInt( gp_script.logoMarginTop ),
							marginBottom: parseInt( gp_script.logoMarginBottom )
						}, 300 ); 
			
						$( '#main-header #logo img' ).stop().animate({
							height: parseInt( gp_script.logoHeight )
						}, 300 );
				
			
						$( '#fixed-header-padding' ).stop().animate({
							paddingTop: parseInt( gp_script.headerHeight )
						}, 400 );
							 
					}
				
				}
			
			}

		});				

	}

	gpResizeHeader();
	$( window ).resize( gpResizeHeader );

});