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

	
	/* ============== Preloader ============== */
	
	var preloader = function() {
		$("#preloader").fadeOut(600);
	};

	/* ============== MENU ============== */

	$('.navbar .navbar-nav').onePageNav({
		currentClass: 'active',
		filter: ':not(.external)'
	});

	var navigation_offset = '', $navigation = '';
	function navigation_scroll(){
		var scrollY = $(window).scrollTop();
			if(scrollY > navigation_offset){
				$navigation.addClass('fixed-nav');
			}else if(scrollY < navigation_offset){
				$navigation.removeClass('fixed-nav');
			}
	}

	var navigation = function(){

		try {
			
			$navigation = $('.fixed-enabled');
			navigation_offset = $navigation.offset().top  + 10;
				
			$(window).resize(function(){
				$navigation.removeClass('fixed-nav');
				navigation_offset = $navigation.offset().top  + 10;
				navigation_scroll();
			});
			
			$(window).scroll(function(){
				navigation_scroll();
			});
				
			

		} catch(e) {

		}

	};



	/* ============== SEARCH ============== */

	$('#search-trigger , .search-container .close').on('click', function(event) {
		$('.search-container').toggle();
		$('#s').focus();
		event.preventDefault();
	});


	/* ============== WOW JS ============== */

	new WOW().init();

	
	/* ============== placeholder ============== */

	$('input, textarea').placeholder();

	$('[data-toggle="tooltip"]').tooltip();


	/* ============== PARALLAX  ============== */

	var parallax = function() {

		$('.parallax').each(function(index, el) {
			$(this).parallax("50%", 0.2);
		});

	};


	/* ============== LIGHTBOX  ============== */

	$('.lightbox').magnificPopup({
		type:'image',
		gallery: {
			enabled: true
		}
	});



	/* ============== COUNTUP ============== */

	$('.countup').appear(function() {

		var count_element =  $(this);

		count_element.countTo({
			from: 0,
			to: parseInt( count_element.text() , 10 ) ,
			speed: 3000
		});

	});



	/* ============== SKILL ============== */

	$('.chart').appear(function() {

		var barColor = $(this).css('color'),
			trackColor =   $(this).css('borderTopColor'),
			size  = parseInt( $(this).css('width') , 10 );

		$(this).easyPieChart({
			barColor: barColor ,
			trackColor: trackColor, 
			scaleColor: false,
			lineCap: 'square',
			lineWidth: 20,
			size: size,
			animate: 1500,
			onStep: function(from, to, percent) {
				$(this.el).find('span').html(Math.round(percent) + '<small>%</small>');
			}
		});

	});


	/* ============== accordion ============== */

	var $accordion = $(".accordion-item");

	if ($accordion.length) {

		$accordion.each(function () {
			if (!$(this).hasClass("active")) {
				$(this).find(".accordion-content").hide();
			}
		});

		$(".accordion-title").on('click', function(event) {

			var $this = $(this),
				$item = $this.parents(".accordion-item"),
				$accordion = $item.parents(".accordion");

			if ($accordion.length) {

				if ($item.hasClass("active")) {

				}else{
					$accordion.find(".accordion-item").removeClass("active");
					$accordion.find(".accordion-content").slideUp();
					$item.addClass("active");
					$item.find(".accordion-content").slideToggle();
				}
				
			}else{
				$item.toggleClass("active");
				$item.find(".accordion-content").slideToggle();
			}

			event.preventDefault();
		});

	}


	/* ============== fitVids ============== */

	$('.fit').fitVids();


	/* ============== CLIENTS ============== */

	$('.clients-carousel').owlCarousel({
		items: 5,
		autoPlay: true
	});

	$('.portfolio-carousel').owlCarousel({
		items: 1,
		autoPlay: true
	});	
	/* ============== TWITTER ============== */

	$('.twitter-carousel').each(function(index, el) {

		var $this = $(this),
		screen = $this.data('screen'),
		number = $this.data('number');

		$this.owlCarousel({
			pagination: true,
			singleItem: true,
			autoHeight: true,
			jsonPath : ajaxurl+'?action=_sh_ajax_callback&subaction=tweets&number='+number+'&screen_name='+screen,
			jsonSuccess : function(data) {
				
				var content = "";
				for(var i in data){
					content += "<p>";
					content +=   data[i].tweet;
					// content +=   "<div>"+ data[i]["date"] +"</div>";
					content += "</p>";
				}
				$this.html(content);

			}
		});

	});


	/* ============== VIDEO BACKGROUND ============== */

	var videoBg = function() {
		$('.videoBg').each(function(index, el) {

			var $this = $(this),
				$video_controls = $this.find(".video-controls"),
				$video_controls_play = $video_controls.find(".play"),
				$video_controls_pause = $video_controls.find(".pause"),
				video = $this.data("vide").getVideoObject(); // Get video object
			
			$video_controls.find(".play , .pause").hide();

			if (video.paused) {
				$video_controls_play.show();
			}else{
				$video_controls_pause.show();
			}

			$video_controls_play.on('click', function() {
				$video_controls.find(".play , .pause").hide();
				$video_controls_pause.show();
				video.play();
			});

			$video_controls_pause.on('click', function() {
				$video_controls.find(".play , .pause").hide();
				$video_controls_play.show();
				video.pause();
			});


		});
	};

	/* ============== BLOG ============== */

	var $blog_container = $('.blog-masonry');

	$blog_container.isotope({
		itemSelector: '.blog-post-masonry'
	});

	$blog_container.imagesLoaded( function() {
		$blog_container.isotope('layout');
	});

	$('.slide').owlCarousel({
		navigation : true,
		pagination: true,
		navigationText: ['<i class="fa fa-angle-left"></i>','<i class="fa fa-angle-right"></i>'],
		autoHeight: true,
		singleItem: true
	});


	/* ============== PORTFOLIO ============== */

	var $container = $('.portfolio-container'),
		$containerProxy = $container.clone().empty().css({ visibility: 'hidden' }),
		colWidth;

	$container.after( $containerProxy );

	$(window).resize( function() {

		colWidth = Math.floor( $containerProxy.width() / 4 );
		$container.css({
			width: colWidth * 4
		})
		.isotope({
			resizable: false,
			itemSelector: '.portfolio-item',
			masonry: {
				columnWidth: colWidth
			}
		});
		
	}).resize();


	$container.imagesLoaded( function() {
		$container.isotope('layout');
	});


	// filters
	$('.portfolio-filters li:eq(0) a').addClass('active');

	$('.portfolio-filters a').on('click',function(){

		$('.portfolio-filters a').removeClass('active');
		$(this).addClass('active');

		var selector = $(this).attr('data-filter');

		$container.isotope({ filter: selector });

		return false;
	});



	/* ============== CONTACT FORM ============== */

	$('#contactform').submit(function(event) {

		var $span   = $(this).find('span'),
			action   = $(this).attr('action'),
			data  = $(this).serialize();

		$span.removeClass("text-danger , text-success").hide().text('Sending...').fadeIn();

		$.ajax({
			url: action,
			type: 'POST',
			data: data,
			dataType: 'json'
		}).done(function(response) {

			if (response.errors) {
				$span.text(response.errors[0]).addClass('text-danger');
			}else{
				$span.text(response.success).addClass('text-success');
			}

		});

		return false;
	});






	$(window).on('load', function() {

		preloader();
		navigation();
		parallax();
		videoBg();

	});

});



var wow_themes = '';

(function($){
	wow_themes = {			
			count: 0,
			wishlist: function(options, selector)
			{
				options.action = '_sh_ajax_callback';
				
				if( $(selector).data('_sh_add_to_wishlist') === true ){
					wow_themes.msg( 'You have already done this job', 'error' );
					return;
				}
				
				$(selector).data('_sh_add_to_wishlist', true );
				wow_themes.loading(true);
				
				$.ajax({
					url: ajaxurl,
					type: 'POST',
					data:options,
					dataType:"json",
					success: function(res){
						try{
							var newjason = res;
							if( newjason.code === 'fail'){
								$(selector).data('_sh_add_to_wishlist', false );
								wow_themes.loading(false);
								wow_themes.msg( newjason.msg, 'error' );
							}else if( newjason.code === 'exists'){
								$(selector).data('_sh_add_to_wishlist', true );
								wow_themes.loading(false);
								wow_themes.msg( newjason.msg, 'error' );
							}else if( newjason.code === 'success' ){
								wow_themes.loading(false);
								$(selector).data('_sh_add_to_wishlist', true );
								$(selector).children('span').text(newjason.value);
								wow_themes.msg( newjason.msg, 'success' );
							}else if( newjason.code === 'del' ){
								wow_themes.loading(false);
								$(selector).data('_sh_add_to_wishlist', true );
								$(selector).parents('tbody').remove();
								wow_themes.msg( newjason.msg, 'success' );
							}
							
							
						}
						catch(e){
							wow_themes.loading(false);
							$(selector).data('_sh_add_to_wishlist', false );
							wow_themes.msg( 'There was an error while adding product to whishlist '+e.message, 'error' );
							
						}
					}
				});
			},
			loading: function( show ){
				if( $('.ajax-loading' ).length === 0 ) {
					$('body').append('<div class="ajax-loading" style="display:none;"></div>');
				}
				
				if( show === true ){
					$('.ajax-loading').show('slow');
				}
				if( show === false ){
					$('.ajax-loading').hide('slow');
				}
			},
			
			msg: function( msg, type ){
				if( $('#pop' ).length === 0 ) {
					$('body').append('<div style="display: none;" id="pop"><div class="pop"><div class="alert"><p></p></div></div></div>');
				}
				if( type === 'error' ) {
					type = 'danger';
				}
				var alert_type = 'alert-' + type;
				if( type == '' ) {
					$('#pop').html(msg);
					$('#pop').slideDown('slow').delay(5000).fadeOut('slow');
				} else {

					$('#pop > .pop p').html( msg );
					$('#pop > .pop > .alert').addClass(alert_type);
					
					$('#pop').slideDown('slow').delay(5000).fadeOut('slow', function(){
						$('#pop .pop .alert').removeClass(alert_type);
					});
				}
				
			},
			
		};
		
		$('.add_to_likelist').click(function(e) {

			e.preventDefault();
					
			var opt = {subaction:'like_it', data_id:$(this).attr('data-id')};
			wow_themes.wishlist( opt, this );
		
			
		});
})(jQuery);