// JavaScript Document
/*
 * jQuery
 *
*/

$(function(){

	// FireFox PC
	if (BrowserDetect.browser == 'Firefox' && BrowserDetect.OS == 'Windows' || BrowserDetect.browser == 'Chrome' && BrowserDetect.OS == 'Windows'){
		$('#bonjour p').css({fontWeight: 'bold', fontStyle: 'normal'});
		$('#bonjour p .webdesigner, #bonjour p .pixel-perfect, #bonjour p .studiogazoline').css({fontFamily: 'Cuprum'});
		$('.block .small-quote p.testify').css({fontWeight: 'bold', fontStyle: 'normal', fontSize: 14});
		$('.block .small-quote .author').css({fontWeight: 'normal', fontStyle: 'normal', fontSize: 12});
		$('.block p.text strong').css({fontWeight: 'bold', fontStyle: 'normal'});
		$('.block h2').css({fontWeight: 'bold'});
	};

	// Preloader
	//$('#preloader .preloader-content').html('<h1>Chargement en cours</h1><h2>Merci de patienter ...</h2>');
	$(window).load(function() {
		 $('#preloader .preloader-content').fadeOut(600,function(){
		 	$('#preloader').fadeOut(200, function(){
		 		$(this).remove();
		 		triggerTwitter();
		 	});
	 	});
	});

	$('body, #container').scrollTo( 0, 0 );

	// VARIABLES
	var $siteURL = $('.logo-top a').attr('href');
	var $easing = 'easeOutQuad';
	var $easing2 = 'easeInOutQuad';
	var $durationNav = 800;
	var $speedOpenClose = 500;
	var $containerBigHeight = 750;
	var $containerSmallHeight = 555;
	var $containerBigVideo = 600;
	var $containerSmallVideo = 455;
	var $bigRes = 1260;
	var $smallRes = 1024;
	var $widthDesc = 422;
	var $widthSmallImgs = 720;
	var $widthBigImgs = 990;
	// Big NAV PREV NEXT BUTTONS
	var $defautAlphaBigNav = .2;
	var $hoverAlphaBigNav = 1;
	var $bgNav = .8;
	
	// tipTip jQuery
	/*
	$('ul.logos-cms .wordpress').tipTip({defaultPosition: 'top', edgeOffset: 2});
	$('ul.logos-cms .php').tipTip({defaultPosition: 'top', edgeOffset: -14});
	$('ul.logos-cms .magento').tipTip({defaultPosition: 'top', edgeOffset: 4});
	$('ul.logos-cms .prestashop').tipTip({defaultPosition: 'bottom', edgeOffset: 0});
	$('ul.logos-cms .jquery').tipTip({defaultPosition: 'bottom', edgeOffset: -6});
	$('ul.logos-cms .less').tipTip({defaultPosition: 'bottom', edgeOffset: -12});
	$('ul#social-icons li a').tipTip({defaultPosition: 'bottom', edgeOffset: 3});
	*/
	
	// Desactive keyboard Nav
	$(document).keydown(function(e){
		if (e.keyCode == 37 || e.keyCode == 39) {
			return false;
		}
	});
	
	
	function projectCss() {
		$('img, a').removeAttr('title');
		$('ul.tags li:even').addClass('even'); // TAGS
		$('ul.tags li:odd').addClass('odd');
		$('.gallery .gallery-icon:last').addClass('last');
		$('#container').scrollTo( 0, 0 );
		$('#prev-wrapper').hide(); // desactivate prev
		$('#next-wrapper').show(); // activate next
		$('.project-content .project-nav').appendTo('#nav').hide().fadeIn(100);
	}
	
	//If the User resizes the window, adjust the #container height
	function resizeWindow() {
	
		//$('#container').scrollTo({top:0, left:0} , $durationNav, {easing: $easing});
		$('.gallery .gallery-icon img').each(function(){
		
			var newWindowWidth = $(window).width();
			var imgSrc = $(this).attr('src');
			var bigSrc = imgSrc.replace('-720x509', '');
			var smallSrc = imgSrc.replace('.jpg', '');
			var sizeImg = imgSrc.slice(-12);
					
			if(newWindowWidth >= $bigRes){
				// if image is already resized
				if(sizeImg == '-720x509.jpg'){
				
					var nbrImgs = $('.gallery li').size();
					$('.gallery').css({width: ((nbrImgs*$widthBigImgs) + $widthDesc)}); // resize width gallery
					
					$('#container').stop().animate({height: $containerBigHeight}, $speedOpenClose, $easing2); // resize #container height
					
					$('#prev-wrapper, #next-wrapper').hide().fadeIn(400, function(){
						$(this).css({height: ($containerBigHeight-1)}); // resize Nav Buttons height
						$('#prev-wrapper').hide();
					});
							
					$(this).removeAttr('width').removeAttr('height');
					$(this).fadeOut(400, function(){
						$(this).attr('src', bigSrc).fadeIn(400);
					});
				}
			} else if(newWindowWidth <= $smallRes){
				// if image is not resized
				if(sizeImg != '-720x509.jpg'){
				
					var nbrImgs = $('.gallery li').size();
					$('.gallery').css({width: ((nbrImgs*$widthSmallImgs) + $widthDesc)}); // resize width gallery
					
					$(this).removeAttr('width').removeAttr('height');
					$(this).fadeOut(400, function(){
						$(this).attr('src', smallSrc + '-720x509.jpg').fadeIn(400);
						$('#container').stop().animate({height: $containerSmallHeight}, $speedOpenClose, $easing2); // resize #container height
						$('#prev-wrapper, #next-wrapper').hide().fadeIn(400, function(){
							$(this).css({height: ($containerSmallHeight-1)}); // resize Nav Buttons height
							$('#prev-wrapper').hide();
						});
					});
				}
			}
		});
	} // End function resizeWindow()
	
	var resizeTimer = null;
	$(window).bind('resize', function() {
	    if (resizeTimer) clearTimeout(resizeTimer);
	    resizeTimer = setTimeout(resizeWindow, 200);
	});
	/* END RESIZE TIMER */
	
	// Window width variable
	
		
	// Update #container Height - BIG - SMALL - VIDEO
	function resizeContainerHeight() {

		var windowWidth = $(window).width();
		var videoExist = $('#post').hasClass('category-video'); // If video exist
		var videoID = $('#post').find('object').attr('id');
		
		if(videoExist == true){
			if(videoID == 'video-player-990'){
			var containerHeight = $containerBigVideo;
			} else if(videoID == 'video-player-720'){
			var containerHeight = $containerSmallVideo;
			}
		} else if(videoExist == false) {
			if(windowWidth >= $bigRes){
			var containerHeight = $containerBigHeight;
			} else if(windowWidth <= $smallRes){
			var containerHeight = $containerSmallHeight;
			}
		}
		
		/* Delay */
		$('#container').delay(400).animate({height: containerHeight}, $speedOpenClose, $easing2);
		$('#prev-wrapper, #next-wrapper').delay(400).animate({height: containerHeight - 1}, $speedOpenClose, $easing2);
		
	} // End function resizeContainerHeight()

	// THUMBS
	function thumbsAction() {
		
	
		$('#thumbs li .wp-post-image').css({opacity: 0.65});
	
		// Hover effect
		$('#thumbs li span.title').css({opacity: 0});
		$('#thumbs li a').hover(function(){
			$(this).find('.wp-post-image').stop().animate({opacity: 1}, 200);
			$(this).find('.title').stop().animate({top: 145, opacity: 1}, 200);
			$(this).find('.markup').stop().animate({opacity: 0}, 200);
		}, function(){
			$(this).find('.wp-post-image').stop().animate({opacity: .65}, 500);
			$(this).find('.title').stop().animate({top: 140, opacity: 0}, 200);
			$(this).find('.markup').stop().animate({opacity: 1}, 200);
		});

		// Window width variable
		var windowWidth = $(window).width();
						
		// Ajax Loading Project Page
		//$.ajaxSetup({cache:false});
		/*
		$('ul#thumbs li a').click(function(){
			
		
			$('#thumbs').fadeOut(500);
			$('.twtr-widget').fadeOut(100);
			$('#prev-wrapper, #next-wrapper').fadeOut(100);
			
			var postURL = $(this).attr('href');
			var videoExist = $(this).parent().hasClass('video');
			
			if(videoExist && (windowWidth >= $bigRes)){
				var containerHeight = $containerBigVideo;
			} else if(videoExist && (windowWidth <= $smallRes)){
				var containerHeight = $containerSmallVideo;
			} else if(windowWidth >= $bigRes){
				var containerHeight = $containerBigHeight;
			} else if(windowWidth <= $bigRes){
				var containerHeight = $containerSmallHeight;
			} else {
				var containerHeight = $containerBigHeight;
			}
			
			$('#container').stop().animate({height: containerHeight}, $speedOpenClose, $easing2, function(){
	
				$('#container').scrollTo( 0, 0 );
				
				$('#container').html('').load(postURL + ' #post', {data: windowWidth}, function() {
				
					$('#prev-wrapper, #next-wrapper').css({height: (containerHeight - 1)}).fadeIn(100);
					$(this).find('#post').css({opacity: 0}).stop().animate({opacity: 1}, $speedOpenClose, $easing2);
		  	  			projectCss();
		  	  			navButtons();
		  	  			launchVideo();
				});
			});
			return false;
		
		});
		*/
	} // End thumbsAction()
	
	
	function navButtons(){
	
		// Paragraph effect
		if (!$.browser.msie){ /* Not Enabled on IE */
			$('.project-content p').css({opacity: .65});
			$('.project-content').hover(function(){
	    		$(this).find('p').stop().animate({opacity: .99}, 600);
	    	}, function(){
	    		$(this).find('p').stop().animate({opacity: .65}, 300);
	    	});
    	}
    	
    	// INNER PREV & NEXT BUTTONS PROJECTS
    	// Hover
    	var allButtons = $('.project-nav .next-project, .project-nav .prev-project, .project-nav .go-website');
    	
    	$(allButtons).append('<span class="hover">hover</span>');
    	$(allButtons).find('.hover').css({opacity: 0});
    	
    	$(allButtons).hover(function(){
    		$(this).find('.hover').stop().animate({opacity: 1}, 500, $easing2);
    		$(this).stop().animate({width: 148}, 300, $easing2);
    	}, function(){
    		$(this).stop().animate({width: 43}, 600, $easing2, function(){
    			$(this).find('.hover').stop().animate({opacity: 0}, 200, $easing2);
    		});
    	});
    	
    	
    	// Window width variable
		var windowWidth = $(window).width();
    	
		// CLOSE HOVER
    	$('.project-nav .close, .project-nav .home').append('<span class="hover">hover</span>');
    	$('.project-nav .close, .project-nav .home').find('.hover').css({opacity: 0});
    	$('.project-nav .close, .project-nav .home').hover(function(){
    		$(this).find('.hover').stop().animate({opacity: 1}, 500, $easing2);
    	}, function(){
    		$(this).find('.hover').stop().animate({opacity: 0}, 200, $easing2);
    	});

    	// Close Project
		$('.project-nav .next-project, .project-nav .prev-project').click(function(){
		
			var postURL = $(this).attr('href');
			
			$('#nav .project-nav').fadeOut(200, function(){ $(this).remove(); });
			$('#prev-wrapper, #next-wrapper').fadeOut(200); // desactivate
				
			$('#post').stop().animate({opacity: 0}, $speedOpenClose, $easing, function(){
				
				$('#container').html('').load(postURL + ' #post',{data: windowWidth}, function() {
					
					$(this).find('#post').css({opacity: 0}).stop().animate({opacity: 1}, $speedOpenClose, $easing2);
					projectCss();
	  	  			navButtons();
	  	  			launchVideo();
	  	  			resizeContainerHeight();
				});
			});
			return false;
		})
		
		// Change width #container if smaller images
		var $galleryExist = $('.gallery').size();
		var nbrImgs = $('.gallery li').size();
			
		if($galleryExist == 1){
			var imgSrcName = $('.gallery li img').attr('src').slice(-12); // !important
			if(imgSrcName == '-720x509.jpg'){ // if Small images, resize .gallery
				$('.gallery').css({width: ((nbrImgs*$widthSmallImgs) + $widthDesc)});
			}
		}
		
		// Close Project
		/*
		$('#nav .close, #nav .home').click(function(){
		
			$('#nav .project-nav').fadeOut(100);
			$('#prev-wrapper, #next-wrapper').fadeOut(100); // Buttons Height
			$('#post').stop().animate({opacity: 0}, $speedOpenClose, $easing, function(){
			
				$('#container').stop().animate({height: 226}, $speedOpenClose, $easing, function(){
				
					$('#container').html('').load($siteURL + ' #thumbs', function() {
				
					$('#thumbs li').css({opacity: 0}).stop().animate({opacity: 1}, $speedOpenClose, $easing);
					$('#container').scrollTo({top:0, left:0} , $durationNav, {easing: $easing});
					$('#prev-wrapper, #next-wrapper').css({height: 225});
					$('#next-wrapper').fadeIn(100);
					$('.twtr-widget').fadeIn(100); // Twitter
					thumbsAction();
						
					if($('body').hasClass('single')){ // Reload home page if in single page
						$(location).attr('href', $siteURL);
					}

					});
				});
			});
		});*/
	}
	
	
	projectCss();
	thumbsAction();
	navButtons();
	
	
	// CLIENTS - Testify (News Ticker)
	var first = 0;
	var speed = 700;
	var pause = 12000;
	
	function removeFirst(){
		first = $('ul#listticker li:first').html();
		$('ul#listticker li:first').fadeOut(speed, function() {
			$(this).remove();
			});
		$('ul#listticker li:nth-child(2)').fadeOut(speed);
		addLast(first);
	}
	function addLast(first){
		last = first+'';
		$('ul#listticker').append('<li class="quote-wrapper">' + last + '</li>');
		$('ul#listticker li:nth-child(2)').fadeIn(speed);
	}
	
	interval = setInterval(removeFirst, pause);
		
	// BIG PREV - NEXT BUTTONS : HOVER
	if (!$.browser.msie) { /* Fix defaut alpha bug : IE7 IE8 */
		$('#next-wrapper, #prev-wrapper').css({opacity: $defautAlphaBigNav});
		$('#next-wrapper .bg-arrows, #prev-wrapper .bg-arrows').css({opacity: $bgNav});
	}
	
	$('#next-wrapper, #prev-wrapper').hover(function(){
		$(this).stop().animate({opacity: $hoverAlphaBigNav}, 300);
	}, function(){
		$(this).stop().animate({opacity: $defautAlphaBigNav}, 150);
	});	
		
	// HORIZONTAL SCROLLING
	$('#next-wrapper').click(function(){
		$('#prev-wrapper').show().stop().animate({opacity: $defautAlphaBigNav}, 600);
		
		var $leftScrollValue = $('#container').scrollLeft();
		var $thumbsExist = $('#thumbs').size();
		var $galleryExist = $('.gallery').size();
		
		// THUMBS	
		if($thumbsExist == 1){
			$('#thumbs li.current').removeClass('current').next().next().next().addClass('current');
			$('#container').scrollTo('li.current', $durationNav, {easing: $easing, axis: 'x'});
		}
		
		// GALLERY	
		if($galleryExist == 1){
			if(!$('.gallery li').hasClass('current')){
				$('#next-wrapper').fadeOut(100);
			} else {
				$('.gallery li.current').removeClass('current').next().addClass('current');
				$('#container').scrollTo('li.current', $durationNav, {easing: $easing, axis: 'x'});
			}
		} 

	});
	// END SCROLLING
	

	// NAV ARROWS - PREV / NEXT
	$('#prev-wrapper').click(function(){
	
		$('#next-wrapper').show();

		var $thumbsExist = $('#thumbs').size();
		var $galleryExist = $('.gallery').size();
		var thirdThumb = $('#thumbs li:eq(3)');
		
		// THUMBS	
		if($thumbsExist == 1){
			if(!$('#thumbs li').hasClass('current')){ // if not class current
				$('#thumbs li:last').addClass('current'); // add one to the last li
			} else if(!$(thirdThumb).hasClass('current')) { // if is not third li
				$('#thumbs li.current').removeClass('current').prev().prev().prev().addClass('current'); // continue scrolling left
				$('#container').scrollTo('li.current', $durationNav, {easing: $easing, axis: 'x'});
			} else if($(thirdThumb).hasClass('current')) { // if is the third li
				$('#container').scrollTo({top: 0, left: 0}, $durationNav, {easing: $easing, axis: 'x'}); // scoll to #container left:0
				$('#thumbs li').removeClass('current'); // remove all class 'current'
				$('#thumbs li:first').addClass('current'); // attribute 'current' class to first li
				$("#prev-wrapper").fadeOut(100);
			}
		}
		// GALLERY	
		if($galleryExist == 1){
			if($('.gallery li.current').hasClass('first')){
				$('#container').scrollTo({top: 0, left: 0}, $durationNav, {easing: $easing, axis: 'x'});
			} else {
				$('.gallery li.current').removeClass('current').prev().addClass('current');
				$('#container').scrollTo('li.current', $durationNav, {easing: $easing, axis: 'x'});
			}
		}
	});
	
	// NEXT & PREV BUTTONS FOLLOW THE MOUSE 
    $('#prev-wrapper').hide();
    
	// next
	$("#next-wrapper").mousemove(function(e){
		
		if($('.gallery li.current').hasClass('last')){
			$('#next-wrapper').fadeOut(100);
		}
	
		var secondSecondLast = $('#thumbs li:last').prev().prev(); 
		var secondLast = $('#thumbs li:last').prev();
		var last = $('#thumbs li:last'); 
	
		if(secondSecondLast.hasClass('current') || secondLast.hasClass('current') || last.hasClass('current')){
			$('#next-wrapper').fadeOut(100);
		}
		
		// Fix for scrolling #thumbs
		// calculate scrolls & width datas
        var $leftScrollValue = $('#container').scrollLeft();
		var $contentWidthThumbs = $('#thumbs').width();
		var $widthContainer = $('#container').width();
		var $paddingLeft = 40;
        
       	//  Hide NEXT WRAPPER if : width container + left scroll pos == width content
		if(($widthContainer + $leftScrollValue - $paddingLeft) == $contentWidthThumbs)
		{
			$('#next-wrapper').fadeOut(100);
		};

    });
       
    // prev
    $("#prev-wrapper").mousemove(function(e){
    
    	var $scrollListener = $('#container').scrollLeft();
    	// Hide #prev-wrapper if scrollLeft == 0
	    if($scrollListener == 0) 
		{
			$("#prev-wrapper").fadeOut(100);
		}
	    
    });
    
    // SKILLS - hover effect
    // SAVOIR-FAIRE - hover effect
    var allMenuHover = $('.ul-skills li, .logos-cms li, #social-icons li');
    $('.ul-skills li span').append('<span class="hover">hover</span>');
    $('.logos-cms li').append('<span class="hover">hover</span>');
    $('#social-icons li a').append('<span class="hover">hover</span>');
    
    $(allMenuHover).find('.hover').css({opacity: 0});
    
	$(allMenuHover).hover(function(){
		$(this).find('.hover').stop().animate({opacity: 1}, 100);
	}, function(){
		$(this).find('.hover').stop().animate({opacity: 0}, 500);
	});
			
});
