function passionblogger_subscribe(key, event) {
	
	event.preventDefault();
	
	var formContainer = jQuery('.passionblogger-token-' + key);
	
	if (formContainer.length) {
		var form = jQuery(formContainer).find('.subscribe-form');
		var user_mail = jQuery(formContainer).find('.email-field').val();
		var user_name = jQuery(formContainer).find('.name-field').length ? jQuery(formContainer).find('.name-field').val() : '';
		jQuery(formContainer).find('.submit').prop('disabled', true);
		jQuery(form).fadeOut();
		var submitapi_call = form.attr('action') + '&mailchimp_email='+user_mail+'&mailchimp_name='+user_name;
			
		jQuery.post(submitapi_call, { mailchimp_email1: user_mail, mailchimp_name1: user_name}, 
				function (data) {

					if (data) {
					
						console.log(data);
					
					
					}
					jQuery(formContainer).find('.subscribe-thankyou').fadeIn();
				},
		'json');
	}
	
} 


function passionblogger_print_element(elementID) {
	if (!jQuery(elementID).length) return;
	
	var data = jQuery(elementID).html();
	var custom_css_url = jQuery(elementID).attr('data-css') || '';
	var recipe_title = jQuery(elementID).attr('data-title') || '';
	if (recipe_title == '') recipe_title = 'PassionBlogger Print';
	
    var mywindow = window.open('', 'new div', 'height=650,width=900');
    mywindow.document.write('<html><head><title>'+recipe_title+'</title>');
    mywindow.document.write('</head><body >');
    mywindow.document.write(data);
    if (custom_css_url != '')
    	mywindow.document.write('<link rel="stylesheet" href="'+custom_css_url+'" type="text/css" />');
    mywindow.document.write('</body></html>');

    setTimeout(function() {
        mywindow.print();    	
    }, 1);
    ///mywindow.close();

    return true;
}


(function($) {
	"use strict";
	 
    $(document).ready(function($) {

    	if (!$('body').hasClass('no-backtotop')) {
	    	var element = '<div class="passionblogger-totop" id="pbToTop"><span></span></div>';
			$('body').append(element);
	
			var toTop = $('#pbToTop');
			$(window).on('scroll', function() {
				if ( $(window).scrollTop() > 250 ) {
					toTop.show();
				} else {
					toTop.hide();
				}
			});
	
			toTop.off('click').on('click',function() {
				$('html, body').animate({
					scrollTop: 0
				}, 1000);
			});
    	}
    	
    	// masonry layout
    	if ($('.blog-masonry').length) {

    		var debounce = function( func, wait ) {
    			var timeout, args, context, timestamp;
    			return function() {
    				context = this;
    				args = [].slice.call( arguments, 0 );
    				timestamp = new Date();
    				var later = function() {
    					var last = ( new Date() ) - timestamp;
    					if ( last < wait ) {
    						timeout = setTimeout( later, wait - last );
    					} else {
    						timeout = null;
    						func.apply( context, args );
    					}
    				};
    				if ( ! timeout ) {
    					timeout = setTimeout( later, wait );
    				}
    			};
    		};
    		
    		function masonryBlogLayout() {
    			var masonry_blog = $('.blog-masonry');
    			masonry_blog.masonry({
        			itemSelector : ".blog-masonry .hentry",
        			percentPosition: true
        		});
    		}
    		
    	    $( window ).load( debounce( masonryBlogLayout, 500 ) );

    	}
    	
    	var maxViewportWidth = $( window ).width();
    	if (maxViewportWidth > 840) {
	    	$('#site-navigation ul li.megamenu').mouseenter(function() {
	    		var parent = $(this),
	    			subMenu = $(this).find('.sub-menu').first(),
	    			headerElement = $('.site-header-inner'),
	    			megaClasses = $(this).attr('class');
	    		
	    		var isRightMenu = $('#site-navigation').hasClass('right'),
	    			isCenterMenu = $('#masthead').hasClass('center-navigation');
	    		
	    		var hasWidth = false,
	    			customWidth = '',
	    			megaClassList = megaClasses.split(' ');
	    		
	    		try {
		    		for (var i = 0;i < megaClassList.length;i++) {
		    			if (megaClassList[i].indexOf('width-') > -1) {
		    				hasWidth = true;
		    				customWidth = megaClassList[i].split('-')[1];
		    			}
		    		}
	    		}
	    		catch (e) {
	    			console.log('[MegaMenu Error] Width not set properly');
	    		}
	    		
	    		var appearAtLeft = subMenu.offset().left,
	    			headerWidth = headerElement.width();
	    			
	    		if (parseInt(customWidth) > 0 && hasWidth) {
	    			headerWidth = customWidth;
	    		}
	    		
	    		var offsetOfHeader = (maxViewportWidth - headerWidth) / 2;
	    		appearAtLeft -= offsetOfHeader;
	    		
	    		//if ((isCenterMenu || isRightMenu) && hasWidth) {
	    		if (hasWidth) {
	    			if ((subMenu.offset().left + customWidth) < maxViewportWidth)
	    				appearAtLeft = '';
	    		}
	    		
	    		//console.log('appearAtLeft = ' +appearAtLeft + ', headerWidth = ' + headerWidth);
	    		//console.log('window = '+ $( window ).width());
	    		
	    		subMenu.css('width', headerWidth+'px');
	    		subMenu.css('left', '-'+appearAtLeft+'px');
	    	});
    	}
    	
        if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {

            $("body").on("touchstart", ".pb_accordion_heading", function(e) {
                pb_accordion_handle($(this));
            });

            $("body").on("touchstart", ".pb_toggle_heading", function(e) {
                pb_toggle_handle($(this));
            });


            $("body").on("touchstart", ".pb_tabs_nav .pb_tab_nav_item", function(e) {
                pb_tab_handle($(this));
            });

        } else {

            $("body").on("click", ".pb_accordion_heading", function(e) {
                pb_accordion_handle($(this));
            });


            $("body").on("click", ".pb_toggle_heading", function(e) {
                pb_toggle_handle($(this));
            });


            $("body").on("click", ".pb_tabs_nav .pb_tab_nav_item", function(e) {
                pb_tab_handle($(this));
            });
        }

        /* Initialize tabs */
        $('.pb_tabs').each(function() {

            var tabs_nav = $(this).find('.pb_tabs_nav');

            $(this).find('.pb_tab_item').each(function() {
                tabs_nav.append('<div class="pb_tab_nav_item">' + $(this).find('.nav').html() + '</div>');
                $(this).find('.nav').remove();

            });

            $(this).find('.pb_tabs_nav').find('.pb_tab_nav_item:first').addClass('active');
            $(this).find('.pb_tab_item').hide();
            $(this).find('.pb_tab_item:first').fadeIn();
            $(this).show();

        });


        $('.entry-modern-header').each(function() {

            $(this).click(function(e) {
    			e.preventDefault();
    			
    			var url = $(this).attr('data-url') || '';
    			if (url != '') 
    				location.href = url;
            });

        });
        
        if ($('.slider-type-slider').length) {
            var swiper = new Swiper('.swiper-container', {
                pagination: '.swiper-pagination',
                paginationClickable: true,
                nextButton: '.swiper-button-next',
                prevButton: '.swiper-button-prev',
                spaceBetween: 10,
                slidesPerView: 4,
                autoplay: 4000,
                loop: true,
                effect: 'fade'
            });
        }

        if ($('.slider-type-slider2').length) {
        	
        	var agentSlidesPerView = 4;
        	if ($('.swiper-container').outerWidth() < 1024)
        		agentSlidesPerView = 2;
        	if ($('.swiper-container').outerWidth() < 650)
        		agentSlidesPerView = 1;
    
            var swiper = new Swiper('.swiper-container', {
                pagination: '.swiper-pagination',
                paginationClickable: true,
                nextButton: '.swiper-button-next',
                prevButton: '.swiper-button-prev',
                spaceBetween: 10,
                slidesPerView: agentSlidesPerView,
                autoplay: 4000,
                loop: true,
                grabCursor: true
            });
        }
    
    });

    function pb_accordion_handle($obj) {
        var toggle = $obj.parent('.pb_accordion_item');
        if (!toggle.hasClass('pb_accordion_active')) {
            toggle.parent('div').find('.pb_accordion_item').find('.pb_accordion_content:visible').slideUp("fast");
            toggle.parent('div').find('.pb_accordion_active').removeClass('pb_accordion_active');
            toggle.find('.pb_accordion_content').slideToggle("fast", function() {
                toggle.addClass('pb_accordion_active');
                if ((toggle.offset().top + 100) < $(window).scrollTop()) {
                    $('html, body').stop().animate({
                        scrollTop: (toggle.offset().top - 100)
                    }, '300');
                }
            });
        } else {
            toggle.parent('div').find('.pb_accordion_item').find('.pb_accordion_content:visible').slideUp("fast");
            toggle.parent('div').find('.pb_accordion_active').removeClass('pb_accordion_active');
        }
    }

    function pb_toggle_handle($obj) {
        var toggle = $obj.parent('.pb_toggle');
        toggle.find('.pb_toggle_content').slideToggle("fast", function() {
            toggle.toggleClass('pb_toggle_active');
        });
    }

    function pb_tab_handle($obj) {
        if ($obj.hasClass('active') == false) {

            var tab_to_show = $obj.parent('.pb_tabs_nav').find('.pb_tab_nav_item').index($obj);

            $obj.parent('.pb_tabs_nav').parent('.pb_tabs').find('.pb_tab_item').hide();
            $obj.parent('.pb_tabs_nav').parent('.pb_tabs').find('.pb_tab_item').eq(tab_to_show).fadeIn();

            $obj.parent('.pb_tabs_nav').find('.pb_tab_nav_item').removeClass('active');
            $obj.addClass('active');

        }
    }
    
    

})(jQuery);

/** Theme components **/
( function( $ ) {

	var debounce = function( func, wait ) {
		var timeout, args, context, timestamp;
		return function() {
			context = this;
			args = [].slice.call( arguments, 0 );
			timestamp = new Date();
			var later = function() {
				var last = ( new Date() ) - timestamp;
				if ( last < wait ) {
					timeout = setTimeout( later, wait - last );
				} else {
					timeout = null;
					func.apply( context, args );
				}
			};
			if ( ! timeout ) {
				timeout = setTimeout( later, wait );
			}
		};
	};

	function menuDropdownToggle() {
		$( '.main-navigation .page_item_has_children > a, .main-navigation .menu-item-has-children > a, .widget_nav_menu .page_item_has_children > a, .widget_nav_menu .menu-item-has-children > a' ).each( function() {
			if ( ! $( this ).find( '.dropdown-toggle' ).length ) {
				$( this ).append( '<button class="dropdown-toggle" aria-expanded="false"/>' );
			}
		} );

		if ( $( window ).width() > 839 ) {
			$( '.main-navigation .dropdown-toggle' ).remove();
		}
	}

	$( window ).load( menuDropdownToggle ).resize( debounce( menuDropdownToggle, 500 ) );

	$( window ).load( function() {
		$( '.dropdown-toggle' ).click( function( event ) {
			event.preventDefault();
			$( this ).toggleClass( 'toggled' );
			$( this ).parent().next( '.children, .sub-menu' ).toggleClass( 'toggled' );
			$( this ).attr( 'aria-expanded', $( this ).attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
		} );
	} );
	
	// featured content apply loader
	function featuredContentPosition() {
		var featuredContent, header, primary;
		featuredContent = $( '#featured-content' );
		header = $( '#masthead' );
		primary = $( '#primary' );

		if ( ! featuredContent.length ) {
			return;
		}

		/* Use Featured Image as a Background Image */
		featuredContent.find( '.hentry' ).each( function() {
			if ( ! $( this ).hasClass( 'background-done' ) && $( this ).hasClass( 'has-post-thumbnail' ) && ( $( this ).hasClass( 'format-image' ) || $( this ).hasClass( 'format-gallery' ) || $( this ).hasClass( 'format-standard' ) || $( this ).hasClass( 'format-video' ) ) ) {
				var entryImage = $( this ).find( '.post-thumbnail' ),
				    thumbnail = $( this ).find( 'img' );
				entryImage.css( 'background-image', 'url(' + thumbnail.attr( 'src' ) + ')' );
				$( this ).addClass( 'background-done' );
			}
		} );

		if ( ! header.length || ! primary.length ) {
			return;
		}

		
		if ( $( window ).width() > 959 ) {
			//featuredContent.insertAfter( header );
			featuredContent.insertBefore( primary );
		} else {
			featuredContent.insertBefore( primary );
		}
		featuredContent.fadeIn(300);
	}

	$( window ).load( featuredContentPosition ).resize( debounce( featuredContentPosition, 500 ) );

	// header
	function headerStyle() {
		var featuredContent, featuredContentHeight;

		featuredContent = $( '#featured-content' );
		featuredContentHeight = 0;
		if ( featuredContent.length ) {
			featuredContentHeight = featuredContent.outerHeight();
		}

		if ( $( '.site-branding' ).outerWidth() === 0 ) {
			$( 'body' ).addClass( 'no-branding' );
		} else {
			$( 'body' ).removeClass( 'no-branding' );
		}

		if ( $( '.site-branding' ).outerWidth() + $( '.main-navigation' ).outerWidth() + 15 >= $( '.site-header-inner' ).outerWidth() && $( window ).width() > 839 ) {
			$( 'body' ).addClass( 'long-menu' );
			$( '.main-navigation' ).css( 'margin-top', '' );
		} else if ( $( window ).width() < 840 ) {
			$( 'body' ).removeClass( 'long-menu' );
			$( '.main-navigation' ).css( 'margin-top', '' );
		} else {
			$( 'body' ).removeClass( 'long-menu' );
			//$( '.main-navigation' ).css( 'margin-top', ( $( '.site-header' ).height() - $( '.main-navigation' ).outerHeight() ) / 2 );
		}

		$( window ).scroll( function() {
			if ( $( 'body:not(.unfixed-header)' ).scrollTop() > featuredContentHeight && $( window ).width() > 959 ) {
				$( 'body' ).addClass( 'fixed' )
				           .css( 'padding-top', $( '#masthead' ).outerHeight() );
			} else {
				$( 'body' ).removeClass( 'fixed' )
				           .css( 'padding-top', '' );
			}
		} );
	}

	$( window ).load( headerStyle ).resize( debounce( headerStyle, 500 ) );	

} )( jQuery );

( function() {

	var container, button, menu;

	container = document.getElementById( 'site-navigation' );
	if ( ! container ) {
		return;
	}

	button = container.getElementsByTagName( 'button' )[0];
	if ( 'undefined' === typeof button ) {
		return;
	}

	menu = container.getElementsByTagName( 'ul' )[0];
	if ( 'undefined' === typeof menu ) {
		button.style.display = 'none';
		return;
	}
	menu.setAttribute( 'aria-expanded', 'false' );

	if ( -1 === menu.className.indexOf( 'nav-menu' ) ) {
		menu.className += ' nav-menu';
	}

	button.onclick = function() {
		if ( -1 !== container.className.indexOf( 'toggled' ) ) {
			container.className = container.className.replace( ' toggled', '' );
			button.setAttribute( 'aria-expanded', 'false' );
			menu.setAttribute( 'aria-expanded', 'false' );
		} else {
			container.className += ' toggled';
			button.setAttribute( 'aria-expanded', 'true' );
			menu.setAttribute( 'aria-expanded', 'true' );
		}
	};

} )();

//** Search in header **/
( function( $ ) {

	$( window ).load( function() {

		function searchAddClass() {
			$( this ).closest( '.search-form' ).addClass( 'hover' );
		}
		function searchRemoveClass() {
			$( this ).closest( '.search-form' ).removeClass( 'hover' );
		}
		var searchSubmit = $( '.search-submit' );
		searchSubmit.hover( searchAddClass, searchRemoveClass );
		searchSubmit.focusin( searchAddClass );
		searchSubmit.focusout( searchRemoveClass );

	} );

} )( jQuery );

( function() {

	var container, button, form, siteHeaderInner, siteNavigation, div;

	container = document.getElementById( 'search-header' );
	if ( ! container ) {
		return;
	}

	button = container.getElementsByTagName( 'button' )[0];
	if ( 'undefined' === typeof button ) {
		return;
	}

	form = container.getElementsByTagName( 'form' )[0];
	if ( 'undefined' === typeof form ) {
		button.style.display = 'none';
		return;
	}
	form.setAttribute( 'aria-expanded', 'false' );

	siteHeaderInner = document.getElementsByClassName( 'site-header-inner' )[0];

	button.onclick = function() {
		if ( -1 !== container.className.indexOf( 'toggled' ) ) {
			document.body.className = document.body.className.replace( ' search-toggled', '' );
			container.className = container.className.replace( ' toggled', '' );
			button.setAttribute( 'aria-expanded', 'false' );
			form.setAttribute( 'aria-expanded', 'false' );
		} else {
			document.body.className += ' search-toggled';
			container.className += ' toggled';
			button.setAttribute( 'aria-expanded', 'true' );
			form.setAttribute( 'aria-expanded', 'true' );
		}
	};

	siteNavigation = document.getElementById( 'site-navigation' );
	if ( ! siteNavigation ) {
		return;
	}

	div = siteNavigation.getElementsByTagName( 'div' )[0];

	function searchPosition() {
		if ( window.innerWidth > 599 ) {
			siteHeaderInner.appendChild( container );
			document.body.className = document.body.className.replace( ' search-toggled', '' );
			container.className = container.className.replace( ' toggled', '' );
			button.setAttribute( 'aria-expanded', 'false' );
			form.setAttribute( 'aria-expanded', 'false' );
		} else {
			div.insertBefore( container, div.firstChild );
			document.body.className += ' search-toggled';
			container.className += ' toggled';
			button.setAttribute( 'aria-expanded', 'true' );
			form.setAttribute( 'aria-expanded', 'true' );
		}
	}

	//window.onload = searchPosition;
	//window.onresize = searchPosition;

} )();
