"use strict";
/******************************************************************************/
/******************************************************************************/

jQuery(document).ready(function($) 
{	
	/**************************************************************************/
	
	$('.vc_row.theme-background-image-1,.vc_row.theme-background-image-2,.vc_row.theme-background-image-3,.vc_row.theme-background-image-4').each(function()
	{
		var helper=new SwimAcademy_ThemeHelper();
		
		var id=parseInt(helper.getValueFromClass($(this),'theme-background-image-'),10);
		
		if($.inArray(id,[1,2,3,4])>-1)
		{
			$(this).prepend($('.theme-background-image-set>span[data-id="'+id+'"]'));
		}
	});
	
	/**************************************************************************/
	
	$('#comment-form').each(function()
	{
		var sum=0;
		
		$(this).find('.theme-form-column-1_2-left').each(function()
		{
			sum+=$(this).actual('height');
		});
		
		if(sum!==0)
			$(this).find('.theme-form-column-1_2-right').css('height',sum);
	});
	
	/**************************************************************************/
	
	$('.theme-page-header .theme-component-icon-list .theme-icon-meta-24-search').on('click',function(e)
	{
		e.preventDefault();
		
		$('#theme-full-screen-search-form').css('display','table').animate({opacity:1},200);
		$('#theme-full-screen-search-form input[type="text"]').focus();
	});
	
	$('#theme-full-screen-search-form #theme-full-screen-search-form-close-button').on('click',function(e)
	{
		e.preventDefault();
		
		$('#theme-full-screen-search-form').animate({opacity:0},200,function()
		{
			$(this).css('display','none');
		});
	});
	
	/**************************************************************************/
	
	$('.vc_row[data-vc-full-width="true"]').each(function()
	{
		var $this=$(this);
		
		var clock=setInterval(function()
		{
			if($this.attr('data-vc-full-width-init')=='true')
			{
				clearInterval(clock);
				$this.animate({opacity:1},200);
			}
		});
	});
	
	/**************************************************************************/
	
	if(parseInt(themeOption.config.woocommerce.enable,10)===1)
	{
		$('.quantity>input[type="button"]').on('click',function()
		{
			var field=$(this).siblings('input[type="text"]');
			var value=parseInt(field.val(),10);
			
			if($(this).hasClass('plus')) value+=1;
			else
			{
				value-=1;
				if(value<=0) value=1;
			}
				
			field.val(value);
		});
	}
	
	/**************************************************************************/
	
	var clickEventType=((document.ontouchstart!==null) ? 'click' : 'touchstart');
	
	/**************************************************************************/
	
	$().header(themeOption);
	
	/**************************************************************************/
	
	$('#comments').on(clickEventType,'.theme-comment-content-read-more-link',function(e)
	{
		e.preventDefault();
		var parent=$(this).parent('p');
		
		parent.children('.theme-comment-content-excerpt,.theme-comment-content-read-more-link').css('display','none');
		parent.children('.theme-comment-content-content,.theme-comment-content-read-less-link').css('display','inline');
	});
	
	$('#comments').on(clickEventType,'.theme-comment-content-read-less-link',function(e)
	{
		e.preventDefault();
		var parent=$(this).parent('p');
		
		parent.children('.theme-comment-content-excerpt,.theme-comment-content-read-more-link').css('display','inline');
		parent.children('.theme-comment-content-content,.theme-comment-content-read-less-link').css('display','none');
	});
	
	/**************************************************************************/
	
 	if($('#comment-form').length===1)
	{
		$('#reply-title').replaceWith('<h3 id="reply-title">'+$('#reply-title').html()+'</h3>');
		
		$().ThemeComment({'requestURL':themeOption.config.ajax_url,'page':$('#comments').data('cpage')});
		
		$('#comment-form').css('display','block');
	}
	
	/**************************************************************************/
	
	$('.widget_archive:has(select),.widget_categories:has(select),.woocommerce .woocommerce-ordering select').css('display','block');
	
	$('.widget_archive select,.widget_categories select,.woocommerce .woocommerce-ordering select').dropkick();
	
	$('.widget_archive .dk_options_inner,.widget_categories .dk_options_inner').each(function() 
	{
		$(this).find('a[data-dk-dropdown-value="-1"],a[data-dk-dropdown-value=""]').parent('li').remove();
	});
	
	/**************************************************************************/
	
	$('.widget_meta ul>li,.widget_pages ul>li').each(function() 
	{
		var link=$(this).children('a').remove();
		
		var span1='<span></span>';
		
		link.html(span1+'<span>'+link.text()+'</span>');
		$(this).html(link);
	});
	
	/**************************************************************************/
	
	$('.widget_archive ul>li').each(function() 
	{
		var link=$(this).children('a').remove();
		
		var span1='<span></span>';
		var span2='<span>'+$(this).text()+'</span>';
		
		link.html(span1+'<span>'+link.text()+'</span>'+span2);
		$(this).html(link);
	});
	 
	/**************************************************************************/
	
	$('.widget_categories:not(.dk_container) ul>li,.woocommerce .woocommerce-MyAccount-navigation ul li').each(function() 
	{
		var link=$(this).children('a').remove();
		
		var list='';
		if($(this).children('ul').length)
			list=$(this).children('ul').remove();
		
		var span1='<span></span>';
		var span2='<span>'+$(this).text()+'</span>';
		
		link.html(span1+'<span>'+link.text()+'</span>'+span2);
		$(this).html('').append(link).append(list);
	});
	
	/**************************************************************************/
	
	$('.widget_nav_menu ul>li').each(function() 
	{
		var link=$(this).children('a').remove();
		
		var list='';
		if($(this).children('ul').length)
			list=$(this).children('ul').remove();
		
		var span='<span></span>';
		
		link.html(span+'<span>'+link.text()+'</span>');
		$(this).html('').append(link).append(list);
	});
	
	/**************************************************************************/
	
	$('.widget_recent_entries>ul>li,ul.wp-block-latest-posts.wp-block-latest-posts__list>li').each(function() 
	{
		var link=$(this).children('a')[0];
		
		if($(this).children('span').length)
			var date=$(this).children('span')[0];
		else var date=$(this).children('time')[0];
		
		$(date).prepend($('<span class="theme-icon-meta-16-time"></span>'));
		
		$(this).html('').append(date).append(link);
	});	
	
	/**************************************************************************/
	
	$('.widget_recent_comments>ul>li').each(function() 
	{
		var link=$(this).children('a')[0];
		var author=$(this).children('span')[0];
			
		$(author).prepend($('<span class="theme-icon-meta-16-author"></span>'));
			
		$(this).html('').append(author).append(link);
	});
	
	/**************************************************************************/
	
	$('.widget_rss ul>li').each(function() 
	{
		$(this).children('.rss-date').prepend($('<span class="theme-icon-meta-16-time"></span>'));
	});	
	
	/**************************************************************************/
	
	$('.widget_search input[type="text"],.widget_product_search input[type="search"],.wp-block-search input[type="search"]').after('<span class="theme-icon-meta-24-search"></span>');
	$('.widget_product_search input[type="search"]').attr('placeholder','');
	
	/**************************************************************************/
	
	$('.widget_calendar,.wp-block-calendar').each(function()
	{
		$(this).find('#prev>a').html('<span class="theme-icon-meta-arrow-horizontal-3"></span>');
		$(this).find('#next>a').html('<span class="theme-icon-meta-arrow-horizontal-3"></span>');
		$(this).css('display','block');
	});	
	
	/**************************************************************************/
	
	$('.widget_rss>ul>li').each(function() 
	{
		var link=$(this).children('a')[0];
		var date=$(this).children('.rss-date')[0];
		var content=$(this).children('.rssSummary')[0];
				 
		$(this).html('').append(date).append(link).append(content);
	});
	
	/**************************************************************************/
	
	$('ol.wp-block-latest-comments>li').each(function() 
	{
		var link=$(this).find('.wp-block-latest-comments__comment-link')[0];
		var author=$(this).find('.wp-block-latest-comments__comment-author')[0];
				 
		$(this).html('').append(author).append(link);
	});
	
	/**************************************************************************/
	
	$('.widget_rating_filter ul>li').each(function() 
	{
		var link=$(this).children('a').remove();
		var starRating=link.children('.star-rating').remove();
		
		var list='';
		if($(this).children('ul').length)
			list=$(this).children('ul').remove();
		
		var count='<span>'+link.text()+'</span>';
		
		link.html('').append(starRating).append(count);
		
		$(this).html('').append(link).append(list);
	});
	
	/**************************************************************************/
	
	$('.woocommerce.widget_layered_nav ul>li').each(function() 
	{
		var link=$(this).children('a');
		
		var span1=$(this).children('a').text();
		var span2=$(this).children('span.count').text();
		
		$(this).html('');
		
		link.html('<span></span><span>'+span1+'</span><span>'+span2+'</span>');
		
		$(this).append(link);
	});
	
	/**************************************************************************/
	
	$('.woocommerce.widget_product_categories ul>li').each(function() 
	{
		var link=$(this).children('a').remove();
		
		var list='';
		if($(this).children('ul').length)
			list=$(this).children('ul').remove();
		
		var span1=link.text();
		var span2=$(this).children('span.count').text();
		
		$(this).html('');
		
		link.html('<span></span><span>'+span1+'</span><span>'+span2+'</span>');
		
		$(this).append(link).append(list);
	});
	
	/**************************************************************************/

	$('.widget_meta,.widget_pages,.widget_archive,.widget_categories,.widget_nav_menu,.widget_recent_entries,.widget_recent_comments,.widget_rss,.widget_calendar,.widget_products,.widget_recent_reviews,.widget_top_rated_products,.widget_recently_viewed_products,.widget_rating_filter,.widget_layered_nav,.widget_product_categories').css('display','block');
	
	/**************************************************************************/
	
	if(parseInt(themeOption.goToPageTop.enable,10)===1)
	{
		if($('.theme-page-footer').length)
		{
			new Waypoint(
			{
				offset															  :	'100%',
				element															 :	$('.theme-page-footer'),
				handler															 :	function(direction) 
				{
				if(direction==='down')
					$('#theme-go-to-top').css({opacity:1});
				else $('#theme-go-to-top').css({opacity:0});
				}
			});

			$(window).bind('hashchange',function(e) 
			{
				e.preventDefault();

				var hash=window.location.hash.substring(1);
				if($.trim(hash)===$.trim(themeOption.goToPageTop.hash))
				{
					var options={};

					if(parseInt(themeOption.goToPageTop.animation_enable,10)===1)
						options={duration:parseInt(themeOption.goToPageTop.animation_duration,10),easing:themeOption.goToPageTop.animation_easing};

					options.onAfter=function() { window.location.hash='#'; };

					$.scrollTo(0,options);
				}
			});
		}
	};
	
	/**************************************************************************/

	$('a.theme-image-fancybox,.woocommerce-product-gallery__image>a').fancyBoxLaunch();
	
	/**************************************************************************/
	
	$('.wpcf7 .wpcf7-submit').addClass('theme-component-button theme-component-button-style-1');
	
	$('.wpcf7 .theme-form-field').on('click',function()
	{
		$(this).find(':input').focus();
	});
	
	$('.wpcf7').on('wpcf7submit',function()
	{
		var $this=$(this);
	
		var formResponseInterval=setInterval(function() 
		{
			$this.find('*').qtip('destroy');
	 
			$this.find('.theme-form-field').each(function() 
			{
				var tip=$(this).find('.wpcf7-not-valid-tip');

				if(!tip.length) return(true);

				$(this).find('label:first').qtip(
				{
					show:	
					{ 
						target:$(this) 
					},
					style:	
					{ 
						classes:'theme-qtip theme-qtip-error'
					},
					content: 	
					{ 
						text:tip.text()
					},
					position: 	
					{ 
						my:'left center',
						at:'right center' 
					}
				}).qtip('show');	

				tip.remove();
			});
			
			var response=$this.find('.wpcf7-response-output');
			if(parseInt(response.length,10)===1)
			{
				var responseText=response.text();
				if($.trim(responseText).length===0) return;
				
				$this.find('input[type="submit"]').qtip(
				{
					show:	
					{ 
						target:$(this) 
					},
					style:	
					{ 
						classes:(response.hasClass('wpcf7-validation-errors') ? 'theme-qtip theme-qtip-error' : 'theme-qtip theme-qtip-success')
					},
					content: 	
					{ 
						text:responseText
					},
					position: 	
					{ 
						my:'right center',
						at:'left center' 
					}
				}).qtip('show');   
			}
			
			clearInterval(formResponseInterval);
			
		},100);
	});
	
	$('.wpcf7').css('display','block');
	
	/**************************************************************************/
	
	$('table.woocommerce-orders-table,table.cart').addClass('theme-table-responsive-on');
	
	$('.theme-table-responsive-on').responsiveTable();
	
	$(document.body).on('updated_cart_totals',function()
	{
		$('.cart').responsiveTable();
	});
	
	/**************************************************************************/
});

/******************************************************************************/
/******************************************************************************/