$(document).ready(function() {
    
    /* GENERAL SETTINGS */
    var docHeight = 0.5 * $(document).height();
    $('a.btn, .social-media ul li a, .twitter-follow, #how-we-work ul.triple-animation li a').blend(200);


    $('.photo-gallery a img').hover(function() {
        $(this).fadeTo(200, '0.6');
    }, function() {
        $(this).stop().fadeTo(200, '1');
    });
    
    
    
    
    $('.portfolio li a:first-child').hover(function() {
        $(this).children('img').fadeTo(200, '0.6');
    }, function() {
        $(this).children('img').stop().fadeTo(200, '1');
    });
    
    
    
      
    $(".social-media ul li").mouseenter(function () {
      $(this).children('a').stop(true, false).animate({ 'padding-bottom' : "15px" }, 150);
    }).mouseleave(function () {
      $(this).children('a').stop(true, false).animate({ 'padding-bottom' : "1px" }, 150);
    });
    
    
    
    $(".tweet-tweet header a").mouseenter(function () {
      $(this).stop(true, false).animate({ 'top' : "10px" }, 230);
    }).mouseleave(function () {
      $(this).stop(true, false).animate({ 'top' : "20px" }, 230);
    });

    $('.photo-gallery a').append('<span></span>');
    
    
    
    
                                                                        
   /* <-- ABOUTS US - EFFECTS */                         
   if( jQuery('#presentation-section > .site-about').get(0) ) {
        if (!($.browser.msie && $.browser.version < 9)) {
            $(".site-about > .columns article").mouseenter(function () {                  
        
              $(this).children('header').stop(true, false).animate({'background-position-x': 'center', 'background-position-y': '15px'}, 150);
              
            }).mouseleave(function () {
              $(this).children('header').stop(true, false).animate({'background-position-x': 'center', 'background-position-y': '25px'}, 150);
            });
            
            $('.columns article header').css({'background-position-y':'-300px'});
        }
        
        $('.columns article').css({'opacity':'0'});
        
        marginTopColumns = 0.5 * $('.columns').offset().top;
        
	/* <-- SPECIAL FOR ANCHOR POINT */
		$(window).mouseenter(function () {        
            if($(window).scrollTop() > marginTopColumns && $('.columns article').css('opacity') == 0) {
                $('.columns article:eq(0)').animate({'opacity': '1'}, 250, function() { $(this).next("article").animate({'opacity': '1'}, 250, arguments.callee) });
                if (!($.browser.msie && $.browser.version < 9)) {
                  $('.columns article:eq(0)').children('header').animate({'background-position-y': '25px'}, 250, function() { $(this).parent().next("article").children('header').animate({'background-position-y': '25px'}, 250, arguments.callee); });
                }
            }
        });
	/* // --> */

        $(window).scroll(function () {        
            if($(window).scrollTop() > marginTopColumns && $('.columns article').css('opacity') == 0) {
                $('.columns article:eq(0)').animate({'opacity': '1'}, 250, function() { $(this).next("article").animate({'opacity': '1'}, 250, arguments.callee) });
                if (!($.browser.msie && $.browser.version < 9)) {
                  $('.columns article:eq(0)').children('header').animate({'background-position-y': '25px'}, 250, function() { $(this).parent().next("article").children('header').animate({'background-position-y': '25px'}, 250, arguments.callee); });
                }
            }
        });
    }
   /* // --> */    
    


   /* <-- WHAT WE DO - EFFECTS */
   else if( jQuery('#presentation-section > .site-case').get(0) ) {
       $('.photo-gallery li').append('<span></span>');
   }
    
    
    
   /* <-- WHAT WE DO - EFFECTS */
   else if( jQuery('#presentation-section > .site-what-we-do').get(0) ) {
        $('.columns article').css({'opacity':'0'});
        
        if (!($.browser.msie && $.browser.version < 9)) {
          $('.columns article header').css({'background-position-y':'-300px'});
        }
        
        if($('.columns article').css('opacity') == 0) { 
            $('.columns article:eq(0)').animate({'opacity': '1'}, 250, function() { $(this).next("article").animate({'opacity': '1'}, 250, arguments.callee) });
            if (!($.browser.msie && $.browser.version < 9)) {
              $('.columns article:eq(0)').children('header').animate({'background-position-y': '40px'}, 250, function() { $(this).parent().next("article").children('header').animate({'background-position-y': '40px'}, 250, arguments.callee); });
            }
        }
   }
   /* // --> */  
   
   
   
   
   /* <-- WHAT WE DO - SINGLE PAGE - EFFECTS */
   else if( jQuery('#presentation-section > .site-what-we-do-single').get(0) ) {
   
        $('.site-what-we-do-single article section').css({'opacity':'0'});
        $('.site-what-we-do-single article section figure img').css({'display':'none'});

        if($('.site-what-we-do-single article section').css('opacity') == 0) {
        setTimeout(function() { 
            $('.site-what-we-do-single article section:eq(0)').animate({'opacity': '1'}, 250, function() { $(this).next("section").animate({'opacity': '1'}, 250, arguments.callee) });
            
            setTimeout(function() {
                $('.site-what-we-do-single article section figure img:first-child').fadeIn(750).addClass('active');
            }, 800);
        }, 350);
        }
        
        
        $('.site-what-we-do-single article section').each(function() {
            var figure = $(this).children('figure');
            var countImgs = figure.children('img').length;
            var lastImg = figure.find('img:last-child').index();
            
            if(countImgs > 0) {
                setInterval(function(){
                    var activeNow = figure.find('img.active');
                    var next = activeNow.next();
                    var nextIndex = next.index();
            
                    if(nextIndex == -1) {
                        activeNow.fadeOut(500).removeClass('active');
                        setTimeout(function() {
                            figure.children('img:first-child').fadeIn(700).addClass('active');
                        }, 500);
                    }
                    else {
                        activeNow.fadeOut(500).removeClass('active');
                        setTimeout(function() {
                            next.fadeIn(700).addClass('active');
                        }, 500);
                    }
                }, 4000)        
            }
        });
  }
   /* // --> */ 
   
   
   
  else if( jQuery('#presentation-section > .site-our-work').get(0) ) { 
	     var $portfolioClone = $(".portfolio").clone();  
  }
	
	
	// Attempt to call Quicksand on every click event handler
	$(".filter a").click(function(e){
		
		$(".filter li").removeClass("current");	
		
		// Get the class attribute value of the clicked link
		var $filterClass = $(this).parent().attr("class");

		if ( $filterClass == "all" ) {
			var $filteredPortfolio = $portfolioClone.find("li");
		} else {
			var $filteredPortfolio = $portfolioClone.find("li[data-type~=" + $filterClass + "]");
		}
		
		// Call quicksand
		$(".portfolio").quicksand( $filteredPortfolio, { 
			duration: 600, 
			easing: 'easeInOutQuad' 
		}, function() {
    $('.portfolio li a:first-child').hover(function() {
        $(this).children('img').fadeTo(200, '0.6');
    }, function() {
        $(this).children('img').stop().fadeTo(200, '1');
    });
    }
    );

		$(this).parent().addClass("current");

		// Prevent the browser jump to the link anchor
		e.preventDefault();
	})   



   // OPEN SITE IN NEW TAB WITH REL EXTERNAL ATTRIBUTE                        
   $('a[rel="external"]').click(function () {
      $(this).attr({'target':'_blank'});
   });    
   
   
   // SCROLL TO TOP
   $('a.scroll-top').click(function () {  
       $('body,html').animate({scrollTop: 0}, 400);
       return false;
   }); 
});

function send_message() {
if(!(jQuery('#response').get(0))) { 
    $('#contact-form-data').prepend('<div id="response"></div>');
}

$('#response').html('<p class="wait"><img src="gfx/contact_mail.gif" alt="" /> Wait for a while. Sending...</p>');

data = $('#contact-form-data').serialize();

$.ajax({
    type: 'POST',
    url: "ajax/send.php",
    data: data,
    dataType: 'json',
    success: function (j) {
    
        if(j.res == 'sent') {
            $('#response').html('<p class="correct">Your message has been sent correctly. Thank you!</p>');
            $('#contact-form-data')[0].reset();
        }
        else {
            $('#response').html('<p class="wrong">Please, fill in all required fields.</p>');
        }
        
    }
});

}
