"use strict";
$ = jQuery;
$(window).load(function(){
	//getSizeOfNav();
    testmonialsWidgetEffect();
    twitterWidgetEffect();
    $(".acc-container").mCustomScrollbar();
    $("#introduction").mCustomScrollbar();

    if(!(navigator.appVersion.indexOf("MSIE 9.")!=-1)){
        $("#flickr").mCustomScrollbar();
        $("#insta").mCustomScrollbar();
    }



    $("#simple-about").mCustomScrollbar();
    //$(".tab-content").mCustomScrollbar();
    
    $('.blogPosts').blogPosts({
		'navigation':'top',
		'itemsPerPage':1
	});

	$('.latestWorks').latestWorks({
		'navigation':'top',
		'itemsPerPage':4
	});

	$('.loading-container').css('display', 'none');
	$('.content-wrapper').css('visibility', 'visible');
	$('.content-wrapper').addClass('animated fadeInUp');

	var $containerPort = $('#portfolio-items');
	// filter items when filter link is clicked
	$('#filters a').click(function(){		
	  var selector = $(this).attr('data-filter');
	  $containerPort.isotope({ filter: selector });
	  return false;
	});

	$('#portfolio-items').isotope({
		itemSelector : '.portfolio-item',
		layoutMode: 'fitRows'
	});

	$container = $('#items');
	
	$container.isotope({
		itemSelector : '.item',
		//  layoutMode: 'fitRows',
		animationEngine : 'css',
		//isAnimated : true,
		//isResizable : false,
		masonry: {
			columnWidth: 1
		}
	});	
	//$testmonials_container = $('#item-testmonials');


	
	$('#portfolio2-items').isotope({
		itemSelector : '.portfolio-item',
		  layoutMode: 'fitRows'
	});

	var $container = $('#portfolio2-items');
	// filter items when filter link is clicked
	$('#filters a').click(function(){
	  var selector = $(this).attr('data-filter');
	  $container.isotope({ filter: selector });
	  return false;
	});

	if($('#commentform div:first-child').attr('class') != 'span4'){
		$('#commentform div:last-child').removeClass('span8');
		//$('#commentform div:last-child').addClass('span12');
	}

	//socialShares();
});

$( document ).ready(function($){
    $(".desc-container").mCustomScrollbar();
    $("#testmonials-feed li").css('display', "none");
    $("#testmonials-feed li:first-child").css('display', "list-item");
	
	$(".icon-search").click(function(){
	    $("#search-bar").slideToggle('slow');
	    $('.search-bar-container').css('z-index', 99999);
  	});

  	$(".icon-remove").click(function(){
	    $("#search-bar").slideToggle('slow');
  	});

    var isMenuShown = false;	
	$('.show-nav').click(function(e){
		e.preventDefault();
		if(isMenuShown){
			isMenuShown = false;
			$('body').animate({
				right:  200
				}, 
				400, 
				function(){
					$('.main-menu').css('display', 'none');
					//$('.main-menu').css('width', 0);
				}
			);
		}
		else {
			//$('.main-menu').css('width', 200);
			$('.main-menu').css('display', 'block');
			isMenuShown = true;
			$('body').animate({
					right:  200
				}, 400);
		
		}	
	});

	/*services, container height*/
	var height = 0;
	var maxHeight = 0;
	var obj = $('.services-elem:last-child .span3 #col-container');
	$.each(obj, function(){
		height = $(this).height();
		if(height > maxHeight){
			maxHeight = height;
		}
	});
	obj.css('height', maxHeight);

	var height1 = 0;
	var maxHeight1 = 0;
	var obj1 = $('.services-elem .span6 #col-container');
	$.each(obj1, function(){
		height1 = $(this).height();
		if(height1 > maxHeight1){
			maxHeight1 = height1;
		}
	});
	obj1.css('height', maxHeight1);


	$("input[name='ns_widget_mailchimp_first_name']").attr('placeholder', 'Full Name');
	$('[id*="ns_widget_mailchimp_form-"] label:last-of-type').after('<i class="icon-user inline-icon"></i>');

	$("input[name='ns_widget_mailchimp_email']").attr('placeholder', 'E-mail address');
	$("input[name='ns_widget_mailchimp_email']").after('<i class="icon-envelope inline-icon"></i>');
});

function getSizeOfNav () 
{
	var width_of_nav = $('#cssmenu ul').width();
	$('#cssmenu ul:first-child').css('width', width_of_nav);
	$('#cssmenu ul').css('float', 'none');
}
function testmonialsWidgetEffect()
{
    setInterval(function(){
        $( "#testmonials-feed li" ).each(function( index ) {
          //console.log( index + ": " + $(this).text() );
          $(this).css('display', "none");
        });
        // Do something every 10 seconds
        var item_count = $( "#testmonials-feed li" ).size();
        var random_li = Math.floor((Math.random()*item_count)+1);
        $("#testmonials-feed li:nth-child("+random_li+")").fadeIn("slow");
    }, 10000);
}
function twitterWidgetEffect()
{
    setInterval(function(){
        $( "#twitter-feed li" ).each(function( index ) {
          //console.log( index + ": " + $(this).text() );
          $(this).css('display', "none");
        });
        // Do something every 5 seconds
        var item_count = $( "#twitter-feed li" ).size();
        var random_li = Math.floor((Math.random()*item_count)+1);
        $("#twitter-feed li:nth-child("+random_li+")").css('display', "block");
        $("#twitter-feed li:nth-child("+random_li+")").fadeIn("slow");
    }, 5000);
}

function test(){
	$('footer-text');
}

$("#load-more").click(function(event){
	event.preventDefault();
	$(this).html('<i class="icon-spinner icon-spin icon-3x"></i>');
    loadMore();
});

var count = 1;
var loadblogpost = true;
function loadMore(){	
	if(loadblogpost){
	var numpost = parseInt(num_of_posts);
	
	var data = {
        "action" : "load_posts",               
		"count_posts" : count  ,
		"num_of_posts": numpost
    };
    jQuery.post(ajax_url,data,function(data){
		var d = jQuery.parseJSON(data);
		if(d.length!=0){
			var item = '';
			$.each(d,function(i,val){
				item += val;
			})
			
			var $container_load_posts = $('#items');
			var $newItem = $(item);

			$container_load_posts.isotope( 'reloadItems' );
			$container_load_posts.isotope('reLayout');

			//setTimeout(function(){
				imagesLoaded($newItem, function() {
					$container_load_posts.isotope( 'insert', $newItem);
				});
			//},500);
			if(maxNumPages == count){
				$("#load-more").css("display", "none");
		}
		}
		else{
			loadblogpost = false;			
		}
    }).done(function(){
		if(loadblogpost){
			$("#load-more").html('LOAD <br> MORE </br>...');
		}
		else{
			$("#load-more").css("display", "none");
		}
    });
	count = count +1;	
	console.log('numpost: '+numpost+' count: '+count);
	}
	else{
		$("#load-more").css("display", "none");
	}
}

$("#load-more-testmonials").click(function(){
	//event.preventDefault();
	$("#load-more-testmonials").html('<i class="icon-spinner icon-spin icon-3x"></i>')
	loadMore_testmonials($(this));    
});

var paged_ = 1;
var loadtestimonialspost = true;
function loadMore_testmonials(test)
{	if(loadtestimonialspost){
	var numpost = parseInt(num_of_posts);			
	var data = {
        "action" : "load_testmonials",               
		"paged_" : paged_  ,
		"num_of_posts": numpost				
    };
    jQuery.post(ajax_url,data,function(data){
		var d = jQuery.parseJSON(data);
		if(d.length!=0){
		var item = '';
		$.each(d,function(i,val){
			item += val;
		})
		
		var $testmonials_container = $('#item-testmonials');
		var $newItem = $(item);
		console.log($newItem);
		$testmonials_container.append($newItem);
			if(maxNumPages == paged_){
			   $("#load-more-testmonials").css("display", "none");
			}
		}
		else{
			loadtestimonialspost = false;			
		}
    })
    .done(function() {
		if(loadtestimonialspost){
			$("#load-more-testmonials").html('LOAD <br> MORE </br>...');
		}
		else{
			$("#load-more-testmonials").css("display", "none");
		}
	 })
	paged_ = paged_ +1;
	}
	else{
		$("#load-more-testmonials").css("display", "none");
	}	
}
function loadMorePortfolio()
{
    var $newItems = $('<div class="portfolio-item apps"><div class="image-container"><a href="images/fullscreen/2.jpg"><img src="images/under-238x177.png" alt="T 2" /><div class="image-hover"></div></a></div><p class="post-title">Awsome forest reel shoot</p><p class="post-categoty capitalize">apps</p></div>');
    $('#portfolio-items').isotope( 'insert', $newItems );

}
function loadMorePortfolio2()
{
    var $newItems = $('<div class="portfolio-item apps"><div class="image-container"><a href="images/fullscreen/2.jpg"><img src="images/under-238x177.png" alt="T 2" /><div class="image-hover"></div></a></div><p class="post-title">Awsome forest reel shoot</p><p class="post-categoty capitalize">apps</p></div>');
    $('#portfolio2-items').isotope( 'insert', $newItems );

}
function socialShares(){
	var shares = $('.share .social-shares');
	var counter = 0;
	var content = "";
	//alert(shares.html());
	$.each(shares, function(key, value){
		//alert($(this).html());
		if(counter >= 4){
			content += $(this).html();
			$(this).hide();
		}
		counter++;
	});
	//content = content.html();
	console.log(content);
	$("#social-popover").popover({ content: content, html: true });
}

$("#goto-anim a i").hover(function(){
	$(this).addClass("icon-spin");
})

$("#goto-anim a i").mouseout(function(){
	$(this).removeClass("icon-spin");
})

if($('.slides-container2').length >= 1){
	$('#footer').css('margin-top', 0);
	var element_for_slider = $(".slides-container2");

var height_slider = $(window).height() - $('#header').outerHeight() - $('#footer').outerHeight();
console.log(height_slider);
element_for_slider.css('height', height_slider);

$(function() {
      $('#slides').superslides({inherit_height_from:element_for_slider});
    });

var $slides = $('#slides');
var hammertime = Hammer(document.getElementById("slides"),{swipe_velocity: 0.1});

 hammertime.on("swipeleft", function(e) {
    $slides.data('superslides').animate('next');
  });

  hammertime.on("swiperight", function(e) {
    $slides.data('superslides').animate('prev');
  });
/*window.addEventListener("orientationchange", function() {
	// Announce the new orientation number
	//alert(window.orientation);
	location.reload();
}, false);*/

var before_id;
//var animation = 'fadeInUpBig';
/*var effects={
	0:'fadeInUpBig', 
	1: 'fadeInDownBig',
	2:'fadeInUp', 
	3: 'fadeInDown'
};*/
$(document).on('animated.slides', function() {
	
	if(before_id != null){
	
		//alert(before_id)
		$('#' + before_id).removeClass('animated '+ effects[current_slide]);
		//$('#' + before_id).css('opacity', 0);
	
	}

	var current_slide = $('#slides').superslides('current');
	before_id = current_slide;

	$('#' + current_slide).addClass('animated '+ effects[current_slide]);
	
	
});
}
