var contentWidth = 0;
var contentHeight = 0;
var minLeft = 0;
var maxLeft = 0;
var maxTop = 0;
var minTop = 0;
var imageWidth = 0;
var imageHeight = 0;
var leftWidth = 0;
var topHeight = 0;
var innerContentWidth = 0;
var innerContentHeight = 0;
var mouseUp = true;
var keepActive = false;
var mobileUser = false;

$(document).ready(function() 
{
	 //$('#baseline').append('<span class="test"></span>');
	
	//var userAgent = navigator.userAgent.toLowerCase();
	
	if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1) ) {
		$('body').addClass('mobile-device');
		mobileUser = true;
	}
	
	
    defineContentSizes();	
	$(window).resize(function() {
	  	defineContentSizes();
	});
	$('#fancybox-overlay').click(function(){
            defineContentSizes();
        });
/*  soumission formulaire
-------------------------------------------*/

$('#contact-form form').bind('submit', function(){
    
	if (checkForm($('#contact-form form'))) {
		return true;	
	} else {
		return false;	
	};
	
	
	/*
	var formOk = true;
    $('#contact-form form input:text').css({border: "1px solid #ddd"});
    $('#contact-form form textarea').css({border: "1px solid #ddd"});


    $('#contact-form form input:text').each(function(){
        if ( jQuery.trim($(this).val()).length==0 ) {
            $(this).css({border: "1px solid red"});
            formOk = false;
        }
    });

     if ( jQuery.trim($('#contact-form form textarea').val()).length==0 ) {
            $('#contact-form form textarea').css({border: "1px solid red"});
            formOk = false;
     }

     if (formOk) {
        return true; 
    } else {
        return false;
    }
	*/
});

$('#comment-form #edit-submit').click(function(){
	if (checkForm($('#comment-form'))) {
		return true;	
	} else {
		return false;	
	};
});


	/* focus subscribe
	----------------------------------------------------------------*/
	$('#keep-in-touch form input').focus(function(){
		if ($(this).val() == 'email') {
			$(this).attr('value', '');
		}	
	});
	$('#keep-in-touch form input').blur(function(){
		if ($(this).val() == '') {
			$(this).attr('value', 'email');
		}
	});
	
	/* click contact-btn
	----------------------------------------------------------------*/
	$('#keep-in-touch .contact-form-btn').click(function(){
		$('#contact-form').css('opacity',0);
		$('#contact-form').css('display','block');
		$('#contact-form').animate({'opacity':1}, 'normal');
	});
	$('#splash-contact-form').click(function(){
		$('#contact-form').animate({'opacity':0}, 'normal', function(){
			$('#contact-form').css('display','none');
		});
	});
	/* hover QRCode
	----------------------------------------------------------------*/
	$('.region-qr img').hover(function(){
		$(this).animate({'opacity':1},'fast');
		$(this).animate({'top':0},'fast');
	}, function() {
		$(this).animate({'opacity':0.5},'normal');
		$(this).animate({'top':'-30px'},'normal');
	});
	
	
	/* hover des images homepage
	----------------------------------------------------------------*/
	$('.content-block ul li').hover(
		function(){
			$(this).find('.thumbnails .nb').animate({'opacity':0},'fast'); 
			$(this).find('.header .date').css('color', '#7D378A');
			$(this).find('h3 a').css('color', '#7D378A');
			$(this).find('h3').css('color', '#7D378A');
			$(this).find('.home-img-container').css({
				'border-bottom': '1px solid #7D378A',
				'border-top': '1px solid #7D378A'
			})
                        //$(this).animate({'backgroundColor':'#d3d3d3'},'normal'); 
                        $(this).css('background','#fff');
                        $(this).find('.home-img-container a ').animate({'opacity':0.5},'normal');
		}, 
		function() {
			$(this).find('.thumbnails .nb').animate({'opacity':1},'normal'); 
			$(this).find('.header .date').css('color', '#EE820F'); 
			$(this).find('h3 a').css('color', '#EE820F'); 
			$(this).find('h3').css('color', '#EE820F'); 
			$(this).find('.home-img-container').css({
				'border-bottom': '1px solid #EE820F',
				'border-top': '1px solid #EE820F'
			})
                        //$(this).animate({'backgroundColor':'#ffffff'},'normal'); 
                        $(this).css('background','none');
                        $(this).find('.home-img-container a ').animate({'opacity':0},'normal');
		});


        /* hover des images en visu projets
	----------------------------------------------------------------*/
	$('.project-teaser').hover(
		function(){
			$(this).find('.thumbnails .nb').animate({'opacity':0},'fast'); 
			$(this).find('.header .date').css('color', '#7D378A');
			$(this).find('h3 a').css('color', '#7D378A');
			$(this).find('h3').css('color', '#7D378A');
			$(this).find('.home-img-container').css({
				'border-bottom': '1px solid #7D378A',
				'border-top': '1px solid #7D378A'
			})
                        $(this).css('background-color','#e1e1e1');
                        $(this).find('.home-img-container a ').animate({'opacity':0.5},'normal');
		}, 
		function() {
			$(this).find('.thumbnails .nb').animate({'opacity':1},'normal'); 
			$(this).find('.header .date').css('color', '#EE820F'); 
			$(this).find('h3 a').css('color', '#EE820F'); 
			$(this).find('h3').css('color', '#EE820F'); 
			$(this).find('.home-img-container').css({
				'border-bottom': '1px solid #EE820F',
				'border-top': '1px solid #EE820F'
			})
                        $(this).css('background-color','#fff');
                        $(this).find('.home-img-container a ').animate({'opacity':0},'normal');
		});

	
	/* hover keep-in-touch
	----------------------------------------------------------------*/
	$('#keep-btn h2').hover(function(){
		if (!keepActive) {
			$('#keep-in-touch').animate({'right':0},'normal', function(){ keepActive = true});
		}		
	});
	$('#keep-in-touch').hover(function(){
	},
	function(){
		$(this).animate({'right':'-279px'},'normal', function(){ keepActive = false});
	});
		
	/* hover du menu
	----------------------------------------------------------------*/
	$('#left .menu li a').hover( function(){
			if (mobileUser == false) {
				if ($(this).hasClass('active')) { } else {
					$(this).animate({'backgroundColor':'#EE820F'},'fast'); 	
				}
			}
		}, 
		function() {
			if (mobileUser == false) {
				if ($(this).hasClass('active')) { } else {
					$(this).animate({'backgroundColor':'#7D378A'},'normal'); 
				}
			}
		});

       /* gestion sous-menu actif
	----------------------------------------------------------------*/
        parentMenu = $('#left .menu .active').parent().parent().parent().html();
        if ($('#left .menu .active').parent().parent().parent().hasClass('expanded')) {
              $('#left .menu .active').parent().parent().parent().find('a:first').addClass('active');
        }

        /* gestion du menu Blog
	----------------------------------------------------------------*/
        var pathname = $(location).attr('href');
        if(pathname.indexOf('le-blog') >= 0) {
            $('#left .menu a[href*="blog"]').addClass('active');
        }

		
	/* placement du sous-menu blog
	----------------------------------------------------------------*/
        var titleHeightFr = $('#block-views-categories-blog-francais-block').height();
		var titleHeightEn = $('#block-views-categories-blog-anglais-block').height();
        $('#block-menu-menu-liens-blog-fr-').css('top', titleHeightFr+'px');
        $('#block-menu-menu-liens-blog-en-').css('top', titleHeightEn+'px');

	/*
	----------------------------------------------------------------*/
	$('#fb-block a').hover(function(){
		$(this).parent().css('background-position','right');
	}, function() {
		$(this).parent().css('background-position','left');
	});
	
    /* accordeon adresses
	----------------------------------------------------------------*/
	$('.full-adress .agency-title').click(function(){
		
		if ($(this).parent().find('.agency-content').css('display') == 'none'){
			$(this).parent().find('.agency-content').fadeIn('normal');
			$(this).parent().css('background-position', '-17px 0');
		} else {
			$(this).parent().find('.agency-content').fadeOut('fast');
			$(this).parent().css('background-position', '0 -17px');	
		}

	});


	/* gestion du défilement mouseover
	----------------------------------------------------------------*/
	$('#content').mouseup(function(){
		 mouseUp = true;
	}).mousedown(function(){
		 mouseUp = false;
	});
	$('#content').mousemove(			
			function(e){
				if (mobileUser == false) {
					if (mouseUp) {
						//alert('leftWidth = '+leftWidth+' -- contentWidth = '+contentWidth+' --- innerContentWidth = '+innerContentWidth);
						var posX =  (-(e.pageX-leftWidth)/contentWidth)*innerContentWidth + (contentWidth/2) + leftWidth;
						if (innerContentWidth > contentWidth) {
							if (posX<0) {
								$('.inner-content').css({left:posX});
							} else {
								$('.inner-content').css({left:0});
							}
						}
						var posY =  (-(e.pageY-topHeight)/contentHeight)*innerContentHeight + (contentHeight/2) + topHeight;
						if (innerContentHeight > contentHeight) {
							if (posY<0) {
								$('.inner-content').css({top:posY});
							} else {
								$('.inner-content').css({top:0});
							}
						}
					}
				}
			}); 	
});

/* -------------------------------------------------
	functions
---------------------------------------------------*/

function defineContentSizes (){
	var windowHeight = $(window).height();
	var windowWidth = $(window).width();
	
	var titleWidth = 200;
	//if ($('.homepage').length >0 ) {titleWidth = 203; } else {titleWidth = 250;}
			
	if (mobileUser == false || mobileUser == true) {
			leftWidth = $('#left').width();
			topHeight = $('#top').height();
			
			/*  on teste la taille du navigateur (1024 x 600 min)
			--------------------------------------------------------------------------------------------------------*/
			if (windowWidth < 1025) { 
					$('body').css('width','1024px');
					windowWidth = 1024; 
					$('body').css('overflow-x','auto');
				} else {
					$('body').css('width','');
					 $('body').css('overflow-x','hidden');
				};
			if (windowHeight < 451) { windowHeight = 450; };
		
			/*  on juste la taille du splash-page pour le contact-form
			--------------------------------------------------------------------------------------------------------*/
				$('#contact-form').css('height',windowHeight);
				$('#splash-contact-form').css('height',windowHeight);
		
				$('.inner-content').css('left',	0);
			
			/*  test si scrollbar ou non
			--------------------------------------------------------------------------------------------------------*/
			if(windowHeight < 451){
				$('body').css('overflow-y','auto');
						contentWidth = windowWidth - leftWidth - 30;
			} else {
				$('body').css('overflow-y','hidden');
					   contentWidth = windowWidth - leftWidth;
			} 
			
			if ($('#toolbar').length > 0) { contentWidth -= 10;}
			contentHeight = windowHeight - topHeight;
				$('#content').css('width',contentWidth);
			$('#content').css('height',contentHeight);
			$('#content .inner-content').css('width', '100000px');
			$('#keep-in-touch').css('height', windowHeight);
			
			innerContentWidth = 0;        
		
			if( $('.is-page').length > 0) {
				innerContentWidth = $('#content .inner-content .is-page').width() + 200;
					   
			}
			else 
			{
				if( $('.project-full').length > 0) {
					tempWidth = titleWidth;
					$('.project-full').each(function(){
						tempWidth += $(this).width() + 20;
					 });
					 if (tempWidth > innerContentWidth) {
							innerContentWidth = tempWidth;
					}
				} else {	
					$('.content-block').each(function(){
						tempBlock = $(this);
						tempWidth = titleWidth;
						tempBlock.find('.content-block-li').each(function() {
							tempWidth += $(this).width() + 20;
						});
						if (tempWidth > innerContentWidth) {
							innerContentWidth = tempWidth;
						}
					});
				}
			}
			if (innerContentWidth < contentWidth) {
				innerContentWidth = contentWidth-10;
						 if (innerContentWidth<870) { 
							 innerContentWidth = 870; 
						 }
			}
				
			$('#content .inner-content').css('width', innerContentWidth);		
			innerContentHeight = $('#content .inner-content').height();
			
			var imageWidth = $( ".inner-content" ).outerWidth();
			var imageHeight = $( ".inner-content" ).outerHeight();
		
			if (imageWidth < contentWidth) {
				minLeft = 0;
			} else {
				minLeft = (0 - imageWidth + contentWidth);
			}
			maxTop = 0;
			if (imageHeight < contentHeight) {
				minTop = 0;
			} else {
				minTop = (0 - imageHeight + contentHeight);
			}
			if(mobileUser == true) { $('#keep-in-touch').css('width', innerContentWidth+254); }
			imgHeight = windowHeight - 50;
			$('#background-image img').attr('height', imgHeight);
			
				 //$('#baseline .test').html('windowWidth = '+windowWidth+ ' -- leftWidth= '+leftWidth+' -- contentWidth = '+contentWidth+' -- windowHeight = '+windowHeight+ '-- contentHeight = '+contentHeight+' --documentHeight = '+$('body').height());
	}
};


function checkForm (theForm) {
	var formOk = true;
	theForm.find('input:text').css({border: "1px solid #ddd"});
    theForm.find('textarea').css({border: "1px solid #ddd"});


    theForm.find('input:text').each(function(){
        if ( jQuery.trim($(this).val()).length==0 ) {
            $(this).css({border: "1px solid red"});
            formOk = false;
        }
    });

     if ( jQuery.trim(theForm.find('textarea').val()).length==0 ) {
            theForm.find('textarea').css({border: "1px solid red"});
            formOk = false;
     }

     if (formOk) {
        return true; 
    } else {
        return false;
    }	
	
}


