
var resize;
var width = '';
$(document).ready(function(){
	resizeContact();
});

$(window).load(function() {

	//Test si on est dans la category expertise ou la page contact
	if($(".container-actweb").hasClass('category-expertises') || $(".container-actweb").hasClass('post-266')) {
		width = $(window).width() - $('.content-actweb').outerWidth();
	}

	$(".reseaux-sociaux").mouseenter(function() {
		$(this).find('.left').stop().animate({'margin-left' : 0}, 500);
	});
	$(".reseaux-sociaux").mouseleave(function() {
		$(this).find('.left').stop().animate({'margin-left' : '-87px'}, 500);
	});

	$(".link-visit-website").hover(
		function() {
			var index = Math.floor((Math.random()) * 5);
			var tab_size = ["-14px","-28px","-42px","-56px","-70px"];
			var tab_color = ["#c80035","#007cb4","#f19300", "#7ab41d","#b1ab99"];
			$(this).css("background-position", "100% "+tab_size[index]);
			$(this).css("color", tab_color[index]);
		},
		function() {
			$(this).css("background-position", "100% 0px");
			$(this).css("color", "#000");
		}
	);
	
	resize = new JResize('.item-actweb');	
	resize.resizeGrid( '#content', '.item-actweb');
	
	//$('#wallpapper img').load(function() {
		resize.resizeBackground( '#wallpapper img', width);
		//On affiche limage apres quelle soit chargé et redimenssione
		$('#wallpapper img:first').fadeIn(300);
	//});
	
	$('.item-actweb').mouseenter(function() {
		$(this).find('.meta-info').stop().animate({
			'top': '35%'
		}, 500);
	});
	$('.item-actweb').mouseleave(function() {
		$(this).find('.meta-info').stop().animate({
			'top': '100%'
		}, 500);
	});
	
	$('.container-actweb .open').click( function() {
		if(parseInt($(this).parent().css('right')) == 0) {
			$(this).parent().animate({
				'right' : '-390px'
			}, 500, function() { $(this).find('.open').addClass('close'); } );
		} else {
			$(this).parent().animate({
				'right' : 0
			}, 500, function() { $(this).find('.open').removeClass('close'); });
		}
	});
	
	
	$('.content-mousemove').mouseleave(function() {
		$('.nav-next').fadeOut(200);
		$('.nav-previous').fadeOut(200);
	});
	$('.open').mouseenter(function() {
		$('.nav-next').fadeOut(200);
		$('.nav-previous').fadeOut(200);
	});
	$('.content-mousemove').mousemove(function(e) {
		var mouseX = e.pageX;
		var mouseY = e.pageY;
		var widthContent = $(this).width();
		var heightContent = $(this).height();
		var position = $(this).position();
		/*$('body .tmp').remove();
		$('body').prepend('<span class="tmp">'+mouseX+' '+mouseY +' top : '+position.top+' left : '+position.left+'</span>');*/
		
		if(mouseY < position.top || mouseX < position.left || mouseX > (position.left + widthContent) || mouseY > (position.top + heightContent - 80)) {
			$('.nav-next').fadeOut(200);
			$('.nav-previous').fadeOut(200);
		} else {
			//if(mouseY > (parseInt(heightContent) / 2)) {
			if(mouseX > (parseInt(widthContent) / 2)) {
				$('.nav-next').css({
					top : mouseY - 20,
					left : mouseX - (parseInt($('.nav-next').width()) / 2)
				});
				$('.nav-next').fadeIn(200);
				$('.nav-previous').fadeOut(200);
			} else {
				$('.nav-previous').css({
					top : mouseY - 20,
					left : mouseX - (parseInt($('.nav-previous').width()) / 2)
				});
				$('.nav-previous').fadeIn(200);
				$('.nav-next').fadeOut(200);
			}
		}
	});
	
	setTimeout('moveContent()', 3500);
	
	if($('#wallpapper img').size() > 1) {
		setTimeout('resize.slideWallpapper("#wallpapper")', 5000);
	}
	
	//On kill l'evenement sur les boutons 'open' des expertises
	$('.category-expertises .open').unbind('click');
	
	//Fancybox for the showreel
	$('#btn-showreel').live('click', function() {
		$.fancybox({
			content 	: '<iframe src="http://player.vimeo.com/video/29249387?byline=0&amp;portrait=0&amp;color=ffffff&amp;autoplay=1" width="1001" height="563" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>',
			overlayColor	: '#000000',
			transitionIn	: 'elastic'
		});
	});
});	

$(window).resize(function() { 
	resizeContact();
	resize.resizeGrid( '#content', '.item-actweb');
	//Test si on est dans la category expertise ou la page contact
	if($(".container-actweb").hasClass('category-expertises') || $(".container-actweb").hasClass('post-266')) {
		width = $(window).width() - $('.content-actweb').outerWidth();
	}
	resize.resizeBackground( '#wallpapper img', width);
});

function moveContent() {
	if(!$("body").hasClass('page') && !$(".container-actweb").hasClass('category-expertises') || $(".container-actweb").hasClass('post-64') ) {
		$('.container-actweb').animate({
			'right' : '-390px'
		}, 500, function() { $(this).find('.open').addClass('close'); } );
	}
}

function in_array (needle, haystack, argStrict) {
    // Checks if the given value exists in the array  
    // 
    // version: 1102.614
    // discuss at: http://phpjs.org/functions/in_array    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // +   input by: Billy
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
    var key = '',        strict = !! argStrict;
 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {                return true;
            }
        }
    } else {
        for (key in haystack) {            if (haystack[key] == needle) {
                return true;
            }
        }
    } 
    return false;
}

function resizeContact()
{
	var heightContactContainer = $('.contact .content-actweb .actweb').outerHeight() + $('.contact .content-actweb .actweb').css('margin-top') + $('.contact .content-actweb .actweb').css('margin-bottom');
	var heightBlockShare = $('.block-reseaux-sociaux').outerHeight();
	var heightWindow = $(window).height();
	
	//Resize the container contact
	if( heightContactContainer > ( heightWindow - heightBlockShare) )
	{
		$('.block-reseaux-sociaux').css({'width' : 328, 'right' : 16});
		$('.contact .content-actweb').css({ 'height' : ( heightWindow - heightBlockShare), 'padding-bottom' : heightBlockShare });
	}
}

function resizeContact()
{
	var heightContactContainer = parseInt($('.contact .content-actweb .actweb').outerHeight());
	var heightBlockShare = parseInt($('.block-reseaux-sociaux').outerHeight());
	var heightWindow = parseInt($(window).height());
	
	//Resize the container contact
	if( heightContactContainer > ( heightWindow - heightBlockShare) )
	{
		$('.block-reseaux-sociaux').css({'width' : 328, 'right' : 16});
	}
	else 
	{
		$('.block-reseaux-sociaux').css({'width' : 344, 'right' : 0});
	}
}
