 $(document).ready(function(){ 
    
    if( typeof window.console == 'undefined'){
        window.console = {
            log:function(){}
        };
    }
    
    playBtPos();
    
    siteContent = new full('content');
    halo = new full('halo');
    moroMap = new full('morrowMap');
    
    /* menu */
    (function(){
        
        var e ={
            elm : {
                lm : $('#lm'),
                li : $('#lm li.adLi'),
                m : $('#m'),
                bgW : $('#bgW')
            },
            w : {
                m : $('#m').width() + 108
            }
        }
        
        e.elm.li.hover(function(){
            var self = $(this);
            slide(self,true);
        },function(){
            var self = $(this);
            slide(self);    
        });
        
        function slide(elm,y,callBack){
            if(y) elm.stop(true,true).animate({marginTop:0},'slow',function(){if(callBack) callBack();});
            else elm.stop(true,true).animate({marginTop:-25},'fast',function(){if(callBack) callBack();});
        }
        
        var h = window.location.href,
            pattern = /home/i,
            res = pattern.test(h);
            
        if(res) setWidthBg(e.w.m,e.elm.bgW,true);
        else setWidthBg(e.w.m,e.elm.bgW);
        
        if(isiPad() || isiPhone()){
            $(window).bind('resize',function(){
                if(getWidth() > getHeight()) console.log('landscape');    
                else setWidthBg(e.w.m,e.elm.bgW);
            });   
        }
        
    })();
    
    
    /* about page */
    (function(){
        
        
        
        var a = {
                elm : {
                    adWelcom : $('#adWelcom'),
                    abBox : $('.ab-box'),
                    abSecNav : $('#ab-secNav')
                },
                w : {
                    abBox : 243
                }
            }
            
         if(!isiPad() || !isiPhone()) setWidth(a.elm.abSecNav,a.w.abBox*3);
        
        a.elm.abBox.each(function(index,elm){
            var self = $(this)
                self.css({left:a.w.abBox*index});
        });
            
        function welcom(callBack){
            a.elm.adWelcom.animate({left:0},800,'easeInOutQuart',function(){if(callBack) callBack(); });
        }
        
        function showBoxs(callBack){
            if(isiPad() || isiPhone()){
                a.elm.abBox.animate({marginLeft:0},800,'easeInOutQuart',function(){ if(callBack) callBack(); });   
            }else{
                a.elm.abBox.animate({top:0},800,'easeInOutQuart',function(){ if(callBack) callBack(); });   
            }
        }
        
        welcom(showBoxs);
 
    })();
    
    
    
    
    //news
    (function(){
        
        var news = {
            dom:{
                newsMask : $('.newsMask'),
                newsSlider : $('.newsSlider'),
                newsBox : $('.newsBox'),
                newsArr : $('.arrNav'),
                next : $('.navNext'),
                prev : $('.navPrev'),
                newsContent : $('#newsContent'),
                newsContContainer : $('#newsContContainer'),
                nContainer : $('#news-container'),
                newsCurrent : 1,
                firstNews : $('.newsBox:eq(0)').children(),
                bNews : $('.b-news'),
                bgNewsContainer : $('#bgNewsContainer'),
                bgNewsSlider : $('#bgNewsSlider'),
                bgNews : $('.bgNews'),
                bgNewsImgs : $('.bgNews img'),
                bNewsVisible : $('.b-news:visible').index(),
                adThumb : $('.adThumb'),
                thumbCurrent : $('#thumbCurrent')
            },
            
            lth:{
                allBoxs : $('.newsBox').length,
                allBgNews : $('.bgNews').length
            },
            
            w:{
                boxWidth : 410,
                wArrow : 42,
                wSecTitle : 105,
                wContNews : 400
            },
            
            bool : {
                hideSliderNews : false  
            }
        }
        
        
        setWidth(news.dom.nContainer,getWidth()-news.w.wSecTitle);
        $(window).bind('resize',function(){
            setWidth(news.dom.nContainer,getWidth()-news.w.wSecTitle);
        });
        
        news.slide = function(){
            
            var i = 0, left,l = ((news.w.boxWidth)),sliderLeft;
            //
            setPos(news.dom.newsSlider,l);
            news.dom.prev.hide();
            news.dom.bNews.hide();
            news.dom.bNews.eq(0).show();
            news.dom.firstNews.hide(); // hide First news 
            //
            
            news.dom.adThumb.each(function(index,elm){
                var self = $(this);
                self.bind('click',function(){
                     go(index);
                }); 
            });
            
            
            if(isiPad() || isiPhone()){
                news.dom.newsMask.swipable();
                news.dom.newsMask.bind('swipe.left',function(){
                    if(i >= (news.lth.allBoxs -1) ) return;
                    goNext();    
                });
                news.dom.newsMask.bind('swipe.right',function(){
                    if(i < 1) return;
                    goPrev();
                });  
            }
            
            
            news.dom.next.bind('click',{'next':true},goNext);
            news.dom.prev.bind('click',{'prev':true},goPrev);
            
            function go(index){
                var c = news.dom.thumbCurrent.val();
                if(c>index) goPrev();
                else goNext();
                console.log('go :: i = ' + i );
            }
            
            function goNext(){
                news.resize(i+1);
                left = ((news.dom.newsCurrent) * news.w.boxWidth) - l;
                news.dom.newsArr.fadeIn(300);
                hideContentBox();
                if(news.dom.newsCurrent >= news.lth.allBoxs - 1) news.dom.next.fadeOut(200);
                animateSlider(-left,i+1,hideNewsBox(i,showContentBox(i+1)));
                i++; news.dom.newsCurrent++;
                news.dom.thumbCurrent.val(i);
            }
            
            function goPrev(){
                news.dom.newsCurrent--; i--;
                news.resize(i);
                left = ((news.dom.newsCurrent - 1) * news.w.boxWidth) - l;
                news.dom.newsArr.fadeIn(300);
                hideContentBox();
                if(news.dom.newsCurrent <= 1) news.dom.prev.fadeOut(200);
                animateSlider(-left,i,hideNewsBox(i-1,showContentBox(i)));
                news.dom.thumbCurrent.val(i);
            }
            
            function animateSlider(left,i,callBack){
                news.dom.newsSlider.animate({marginLeft:left},'slow',function(){if(callBack) callBack();});
                news.dom.bgNewsSlider.animate({marginLeft:-(getWidth()*i)},'slow',function(){if(callBack) callBack();});
            }
            
            function hideNewsBox(index,callBack){
                news.dom.newsBox.children().fadeIn(300);
                $('.newsBox:eq('+(index+1)+')').children().fadeOut(300,function(){if(callBack) callBack();});
            }
            
            function showContentBox(index){
                news.dom.bNews.hide();
                news.dom.newsContent.fadeIn(200,function(){
                    $('.b-news:eq('+index+')').show();
                    news.dom.newsContContainer.stop().animate({marginLeft:0},300,function(){});     
                });
            }
            
            function hideContentBox(){
                news.dom.newsContent.fadeOut(400,function(){
                    news.dom.newsContContainer.css({marginLeft:-(getWidth()-300)/2});
                });
            }
        
        }
        
        
        news.allowSlide = function(){
            var w = 1230;
            if(getWidth() <= w){
                makeVisible(true);
                news.dom.newsMask.width(getWidth()-220);
            }else{
                makeVisible();
                news.dom.newsMask.width(w);
            }
            //
            function makeVisible(y){
                if(y) news.dom.newsSlider.css({'visibility':'hidden'});
                else news.dom.newsSlider.css({'visibility':'visible'});
            }
        }
        
        
        news.resize = function(i){
            $(window).bind('resize',function(){
                var l = parseInt(news.dom.newsSlider.css('margin-left'));
                setPos(news.dom.newsSlider,l);
                //
                news.allowSlide();
                var wBox = 410;
                setWidth(news.dom.bgNewsContainer,getWidth());
                setWidth(news.dom.bgNews,getWidth());
                setWidth(news.dom.bgNewsImgs,getWidth());
                setWidth(news.dom.bgNewsSlider,(getWidth() * news.lth.allBgNews) +10);
                if(i) news.dom.bgNewsSlider.css({marginLeft:-(getWidth()*i)});
                news.dom.newsMask.css({marginTop:getHeight()/3});
                fixPosNewsContent();
            });
        }
        
        news.init = function(){
            news.allowSlide();
            var wBox = 410;
            setWidth(news.dom.newsSlider,(wBox + 2) * news.lth.allBoxs);
            setWidth(news.dom.bgNewsContainer,getWidth());
            setWidth(news.dom.bgNews,getWidth());
            setWidth(news.dom.bgNewsSlider,(getWidth() * news.lth.allBgNews) +10);
            setWidth(news.dom.bgNewsImgs,getWidth());
            news.dom.newsMask.css({marginTop:getHeight()/3});
            news.slide();
            news.resize();
            fixPosNewsContent();
        }
        
        function fixPosNewsContent(){
            var wCont = news.dom.newsMask.outerWidth(true),
                wNewsC = news.dom.newsContent.outerWidth(true),
                left = (wCont-wNewsC)/2;
            news.dom.newsContent.css({left:left});
        }
        
        news.init();
        
    })();
    
    
     /*work + videos*/
    (function(){
        
        itemsArray = [];
        
        var site = {
        
                bool : {
                    hover : true,
                    move : false,
                    playVideo : false,
                    animated : false  
                },
            
                taille  : {
                    menuWidth : $('.secTitle').outerWidth(true),
                    liWidth   : $("ul#callMe li").outerWidth(true),
                    windowHeight : $(window).height(),
                    maxWidth  : getWidth() - 201,
                    liBgWidth : $('.li').outerWidth(true),
                    itemWidth : $('.item:eq(0)').outerWidth(true), //290 bug to fix ;)
                    maskWidth : $('.mask').outerWidth(true)
                },
            
                elm : {
                    firstBlock : $("ul#callMe li:eq(0)"),
                    li : $("ul#callMe li"),
                    container : $('#accordion'),
                    boxContact : $('#boxContact'),
                    bc : $('.bc'),
                    box : $('.box'),
                    items : $('.item').length,
                    mask : $('.mask'),
                    item : $('.item'),
                    workVideo : $('#workVideo'),
                    allContent : $('#allContent'),
                    secTitle : $('.secTitle'),
                    boxReel : $('.reelBoxTitle'),
                    backBtn : $('.back'),
                    accTitle : $('#accTitle'),
                    accTitleSlider : $('#accTitleSlider'),
                    mouse : $('.mouse'),
                    workNav : $('.workNav'),
                    nLeft : $('.workNav:eq(0)'),
                    nRight : $('.workNav:eq(1)'),
                    navArr : $('#nav'),
                    vimeo : $('.vimeo'),
                    iframe : $('.myVimeo iframe'),
                    myVimeo : $('.myVimeo'),
                    callMe : $('#callMe')
                },
                posElm : {
                    boxPos : ($('.item').length * $('.item:eq(0)').outerWidth(true)) - (getWidth() - 200) ,
                    defaultPos : 0
                }
        }
        
        //kjbfkef k rkge rgkergkeg eg i kbefkwefkfwek kbfb
        
        
        
        site.go = function(){
                var lang = $('#myLang').val();
                site.resize();
                site.elm.li.each(function(index,elm){
                    $(this).bind('click',
                        function(e){
                            slideTitle(index);
                            var $rel = elm.getAttribute('rel');
                            console.log(elm.id + ' rel = ' + $rel);
                            e.preventDefault();
                            $(site.elm.firstBlock).stop()
                                                .animate({width: site.taille.liWidth},
                                                    {
                                                     queue:false,
                                                     duration:600,
                                                     complete: function(){
                                                         $(this).find('.box').hide();
                                                         $('.ajax').show();
                                                         $.ajax({
                                                             url: '/inc/html/ajax/'+$rel+'.php?lang='+lang,
                                                             success: function(html){
                                                                 site.bool.hover = true; 
                                                                 $('.ajax').hide();
                                                                 var $mask = $('.mask');
                                                                 $mask.empty().hide().html(html).appendTo('.slideBoxs:eq('+index+') .box');
                                                                 site.elm.navArr.prependTo('.slideBoxs:eq('+index+') .box');
                                                                 var items = $('.item').length,
                                                                    vimeo = $('.vimeo');
                                                                arr = [];
                                                                site.elm.items = items;
                                                                itemsArray.push(items);
                                                                $('.myVimeo').each(function(index,e){
                                                                    var html = $(e).html();
                                                                    arr.push(html);
                                                                    $(e).empty();
                                                                });     
                                                                 site.posElm.boxPos = (items * site.taille.itemWidth) < 0 ? (getWidth() - 200) : (items * site.taille.itemWidth) - (getWidth() - 200) ;
                                                                 site.init();
                                                                 site.showVideoDesc($mask);
                                                                 site.playVideo(vimeo);
                                                                 //bug FF, Chrome, Safari fadeIn
                                                                 //$mask.css({width:(items*site.taille.itemWidth)}).fadeIn('slow').fadeTo(100,1);
                                                                 //
                                                                 $mask.css({width:(items*site.taille.itemWidth)}).show();
                                                             }    
                                                         });
                                                     }
                                             });                         
                            $(this).addClass('boxActive').stop()
                                    .animate({width: site.taille.maxWidth},{
                                        queue:false,
                                        duration:600,
                                        complete: function(){
                                            $(this).find('.box').fadeIn('slow');       
                                        }
                                    });
                            site.elm.firstBlock = this;
                        }
                    ); 
                });
                //
                function slideTitle(index){
                    var h = 83;
                    site.elm.accTitleSlider.stop().animate({top:-(h*index)},'slow');
                }
            }
            
        
        site.navigate = function(items){
                site.allowNavigation(items);
                perNav = getOrientation() == 'land' ? 3 : 2;
                $(window).bind('resize',function(){
                    perNav = getOrientation() == 'land' ? 3 : 2; 
                });
                
                console.log('navigate :: perNav = ' + perNav);
                
                if(isiPad() || isiPhone()){
                    var perSwip = perNav;
                    site.elm.callMe.swipable();
                    site.elm.callMe.bind('swipe.left',function(){
                        var l = itemsArray.length,
                            currentItem = (l == 0 ? items : itemsArray[l-1]);
                        if(currentItem <= perSwip) return;
                        allowAnim();
                        goLeft(denyAnim);
                    });
                    site.elm.callMe.bind('swipe.right',function(){
                        var l = itemsArray.length,
                            currentItem = (l == 0 ? items : itemsArray[l-1]);
                        if(currentItem <= perSwip) return;
                        allowAnim();
                        goRight(denyAnim);
                    });
                }
            
                site.elm.nLeft.bind('click',function(){
                    allowAnim();
                    goRight(denyAnim);
                });
                
                site.elm.nRight.bind('click',function(){
                    allowAnim();
                    goLeft(denyAnim);
                });
                
                function allowAnim(){
                    site.bool.animated = false; 
                }
                
                function denyAnim(){
                    site.bool.animated = true;
                }
                
                function goLeft(callBack){
                    if(site.bool.animated) return;
                    site.elm.mask.stop().animate({left:-(site.taille.itemWidth*(perNav*2))},'slow',function(){
                        if(callBack) callBack();
                        $('.item:lt('+perNav+')').appendTo('.mask');
                        $(this).css({left:-site.taille.itemWidth*perNav});
                        site.bool.animated = false;
                    }); 
                }
                
                function goRight(callBack){
                    if(site.bool.animated) return;
                    var items = $('.item').length - (perNav+1);
                    site.elm.mask.stop().animate({left:0},'slow',function(){
                        if(callBack) callBack();
                        $('.item:gt('+items+')').prependTo('.mask');
                        $(this).css({left:-site.taille.itemWidth*perNav});
                        console.log('goRight :: items = ' + items + ' perNav = ' + perNav);
                    });
                }
                
                function getOrientation(){
                    return  getWidth() > getHeight() ? 'land' : 'port';
                }
        }
        
        
        site.allowNavigation = function(items){
            
            if(!isiPad()) site.elm.navArr.hide();
            
            if(site.getReelWidthMask(items)) canNavigate();
            else cannnotNavigate();
            //initMouseNavigate();
            
            function canNavigate(){
                if(isiPad() || isiPhone()) site.elm.mask.css({left:-(290*3)});
                else site.elm.mask.css({left:0});    
                    
                site.elm.workNav.show();
                // initMouseNavigate();
				initMouseScroll();
            }
            
            function cannnotNavigate(){
                site.elm.mask.css({left:0});
                site.elm.workNav.hide();
            }
			
			function initMouseScroll(){			
				if(isiPad()) return;

				var relativeDuration = true;
				var speed = 30; // 0 à 10
				var cel = 1.0009;
				var dMax = 10000;
				var duration = 7000;
				var padding = 20; 
				var targetedX = 0;
				
				$(".box").unbind('mousemove').bind('mousemove', function(event) {
	 				var mousePosX = event.clientX;
					targetedX = maskScroll(targetedX, this, mousePosX);
				}).bind('mouseleave', function() {
					var mousePosX = 0;
					targetedX = maskScroll(targetedX, this, mousePosX);
				});
				maskScroll = function (targetedX, box, mousePosX){
					var $box = $(".box");
					var $mask = $box.find('.mask');
					var boxWidth = $box.width();
					var maskWidth = $mask.width();
					var boxPos = $box.position();
					var boxLeft = boxPos['left'];
					var maskPos = $mask.position();
					var maskLeft = maskPos['left'];
					
					if( boxWidth < maskWidth ){
						var $item = $box.find(".item:first");
						var centre = $(window).width()/2;
						// var safety = $item.width()* 3/2;
						var safety = $(window).width()*15/100;
						var safeAreaLeft =  centre - safety;
						var safeAreaRight = centre + safety;
						
						// tracelog(mousePosX,limitRight,limitLeft,maskPos,maskLeft,maskWidth,boxLeft,boxWidth);
						if( (  mousePosX > safeAreaRight && maskLeft > (boxWidth-maskWidth) ) || ( mousePosX < safeAreaLeft && maskLeft < 0 ) ) {
							// console.log("safeAreaLeft = "+safeAreaLeft );
							// console.log("safeAreaRight = "+safeAreaRight );
							// console.log("MOUSE LEAVE ??"
							// +"\n mousePosX==0"+Boolean(mousePosX==0)
							// +"\n ( mousePosX > safeAreaLeft )"+Boolean( mousePosX > safeAreaLeft )
							// +"\n ( mousePosX < safeAreaRight )"+Boolean( mousePosX < safeAreaRight )
							// +"\n ( mousePosX > safeAreaLeft && mousePosX < safeAreaRight)"+Boolean(( mousePosX > safeAreaLeft && mousePosX < safeAreaRight))
							// );
							if(mousePosX==0 || ( mousePosX > safeAreaLeft && mousePosX < safeAreaRight) ){
								// mouse leave
								// console.log("MOUSE LEAVE mousePosX==0"+Boolean(mousePosX==0));
								var f = 1;
								// console.log("targetedX >= maskLeft : "+targetedX+" >= "+maskLeft+" ? "+Boolean(targetedX>=maskLeft));
								if(targetedX>=maskLeft){
									f = 1;
								}else{
									f = -1;
								}
								// console.log(
								// "\n(maskLeft+(f*50)) > (boxWidth-maskWidth) : " +(maskLeft+(f*50)) + " > "+(boxWidth-maskWidth)+" ? \t "+ Boolean( (maskLeft+(f*50)) > (boxWidth-maskWidth) )+
								// "\n((maskLeft+(f*50)) < 0) : " +(maskLeft+(f*50))+ "< 0 ? \t "+ Boolean( (maskLeft+(f*50)) < 0 )
								// );
								if( (maskLeft+(f*50)) > (boxWidth-maskWidth) && (maskLeft+(f*50)) < 0 && maskLeft != (boxWidth-maskWidth) ){
									targetedX = maskLeft+(f*50);
									// console.log("targetedX : " +targetedX);
									// console.log("maskLeft : " +maskLeft);
									duration=500;
									$mask.stop(true, false).animate({'left': targetedX }, {'duration': duration }, "easeOutCirc");
								}
								return targetedX;
							}else{
								var targetedX = 0; // + padding 					
								if( mousePosX <= safeAreaLeft ) {
									targetedX= 0
								}else if( mousePosX >= safeAreaLeft ) {
									targetedX =  Math.round( ( -boxWidth ) * ( (maskWidth-boxWidth) / (boxWidth) ) ); // + padding 
								}
								var dist = Math.abs(maskLeft-targetedX);
								// console.log("dist : ="+dist);
								if( relativeDuration ){
									duration = Math.round( (dist-Math.abs(mousePosX-centre)/(speed/10) )/(speed/100) );
								}
								// console.log("duration : ="+duration);
								// console.log("limitRight : ="+limitRight);
								// console.log("boxWidth : ="+boxWidth+" / maskWidth : ="+maskWidth+" / padding : ="+padding);
								// console.log("targetedX : "+targetedX + "targetedX : "+targetedX);
								// if(duration<100){ duration=0; }
								$mask.stop(true, false).animate({'left': targetedX }, {'duration': duration }, "easeOutCirc");
								return targetedX;
							}
						}else if( mousePosX > safeAreaLeft || mousePosX < safeAreaRight) {
							$mask.stop();
						}
					}else{
						return targetedX;
					}
				}
			}
            
            function initMouseNavigate(){
                if(isiPad()) return;
                // console.log("initMouseNavigate");
				var relativeDuration = true;
                var speed = 3; // 0 à 10
                var cel = 1.0009;
                var dMax = 10000;
                var duration = 7000;
				var padding = 20; 
				
				var targetedX = 0;
				
                $(".box").unbind('mousemove').bind('mousemove', function(event) {
	 				var mousePosX = event.clientX;
					targetedX = maskAnimate(targetedX, this, mousePosX);
                }).bind('mouseleave', function() {
					var mousePosX = 0;
					targetedX = maskAnimate(targetedX, this, mousePosX);
					// animate({'left': targetedX }, {'duration': duration }, "easeInQuart");
				});
				
				maskAnimate = function (targetedX, box, mousePosX){
					var $box = $(box);
					var $mask = $box.find('.mask');
					var boxWidth = $box.width();
					var maskWidth = $mask.width();
					var boxPos = $box.position();
					var boxLeft = boxPos['left'];
					var maskPos = $mask.position();
					var maskLeft = maskPos['left'];
					
					if( boxWidth < maskWidth ){
						var $item = $box.find(".item:first");
						var limitLeft = (boxLeft + $item.width()* 1/2);
						var limitRight = (boxLeft + boxWidth -$item.width()* 1/2);
						padding = ($item.width()) /2 * ((maskWidth-boxWidth)/boxWidth);
						maskWidth = maskWidth + 12;
						// padding = ($item.width() -margin) /2 * ((maskWidth-margin*n)/boxWidth);
						// maskWidth = maskWidth + 2*padding; //

						// tracelog(mousePosX,limitRight,limitLeft,maskPos,maskLeft,maskWidth,boxLeft,boxWidth);
						if( (  
								maskLeft < 0 && maskLeft > (boxWidth-maskWidth)
							) || ( 
								mousePosX > limitRight && maskLeft != (boxWidth-maskWidth) 
							) || ( 
								mousePosX < limitLeft && maskLeft != 0 
							)
							) {

							if(mousePosX==0 ){
								// console.log("mousePosX==0"+Boolean(mousePosX==0));
								var f = 1;
								// console.log("targetedX >= maskLeft : "+targetedX+" >= "+maskLeft+" ? "+Boolean(targetedX>=maskLeft));
								if(targetedX>=maskLeft){
									f = 1;
								}else{
									f = -1;
								}
								// console.log(
									// "\n(maskLeft+(f*50)) > (boxWidth-maskWidth) : " +(maskLeft+(f*50)) + " > "+(boxWidth-maskWidth)+" ? \t "+ Boolean( (maskLeft+(f*50)) > (boxWidth-maskWidth) )+
									// "\n((maskLeft+(f*50)) < 0) : " +(maskLeft+(f*50))+ "< 0 ? \t "+ Boolean( (maskLeft+(f*50)) < 0 )
								// );
								if( (maskLeft+(f*50)) > (boxWidth-maskWidth) && (maskLeft+(f*50)) < 0 && maskLeft != (boxWidth-maskWidth) ){
									
									targetedX = maskLeft+(f*50);
									// console.log("targetedX : " +targetedX);
									// console.log("maskLeft : " +maskLeft);
									duration=500;
									$mask.stop(true, false).animate({'left': targetedX }, {'duration': duration }, "easeOutCirc");
								}
								return targetedX;
							}else{
								var n 			= $box.find(".item").length;
								var margin 		= Number($item.css('margin-left').replace("px","")) + Number($item.css('margin-right').replace("px",""));
								var targetedX 	=  Math.round( ( boxLeft-mousePosX ) * ( (maskWidth-boxWidth) / (boxWidth) ) ); // + padding 					
								var dist 		= Math.abs(maskLeft-targetedX);

								if( relativeDuration ){
									duration = Math.round( dMax * ( 1-Math.pow( cel, (-dist) ) ) );
								}

								if( mousePosX <= limitLeft ) {
									targetedX= 0
								}else if( mousePosX >= limitRight ) {
									targetedX =  Math.round( ( -boxWidth ) * ( (maskWidth-boxWidth) / (boxWidth) ) ); // + padding 
								}
								// console.log(" Math.abs(maskLeft-targetedX) < boxWidth : "+Math.abs(maskLeft-targetedX)+" < "+boxWidth+" : "+Boolean( Math.abs(maskLeft-targetedX) < boxWidth ) );
								if( Math.abs(maskLeft-targetedX) < (boxWidth/2) ){
									// duration = 2000;
								}else{
									// duration = 8000;
								}
								// console.log("duration : ="+duration);
								// console.log("limitRight : ="+limitRight);
								// console.log("boxWidth : ="+boxWidth+" / maskWidth : ="+maskWidth+" / padding : ="+padding);
								// console.log("targetedX : "+targetedX + "targetedX : "+targetedX);
								if(duration<100){ duration=0; }
								$mask.stop(true, false).animate({'left': targetedX }, {'duration': duration }, "easeInQuart");
								return targetedX;
							}
						}
                    }else{
                        return targetedX;
                    }
				}
				function tracelog(mousePosX,limitRight,limitLeft,maskPos,maskLeft,maskWidth,boxLeft,boxWidth){
					console.log(
						"\n maskPos : " + maskPos +
						"\n maskLeft : " + maskLeft +
						"\n maskWidth : " + maskWidth +
						"\n boxLeft : " + boxLeft + 
						"\n boxWidth : " + boxWidth +
						"\n\n maskLeft < 0 && maskLeft > (boxWidth-maskWidth) : " + Boolean( maskLeft < 0 && maskLeft > (boxWidth-maskWidth) ) +
						"\n\t\t maskLeft < 0  : \t\t\t" +maskLeft+ " < 0:\t" + Boolean( (maskLeft < 0) ) +
						"\n\t\t maskLeft > (boxWidth-maskWidth) : \t" +maskLeft+ ">" +(boxWidth-maskWidth)+ ":\t" +  Boolean( maskLeft > (boxWidth-maskWidth) ) +
												
						"\n\n mousePosX > limitRight && maskLeft != (boxWidth-maskWidth) : \t" + Boolean( mousePosX > limitRight && maskLeft != (boxWidth-maskWidth) ) +
						"\n\t\t mousePosX > limitRight : \t\t" + Boolean( mousePosX > limitRight ) +
						"\n\t\t maskLeft != (boxWidth-maskWidth) : \t\t" +maskLeft+ "!= "+(boxWidth-maskWidth)+" :\t" + Boolean( maskLeft != (boxWidth-maskWidth) ) +
						
						"\n\n mousePosX < limitLeft && maskLeft != 0 : \t" + Boolean( mousePosX < limitLeft && maskLeft != 0 ) +
						"\n\t\t mousePosX < limitLeft : \t\t" +mousePosX+ "<" +limitLeft+ ":\t" + Boolean( mousePosX < limitLeft ) +
						"\n\t\t maskLeft != 0  : \t" +maskLeft+ "!= 0 :\t" + Boolean( maskLeft != 0 ) +
						"\n "
					);
				}
			}
			function easeInCirc(t, b, c, d) {
				t /= d;
				return -c * (Math.sqrt(1 - t*t) - 1) + b;
			};
        }
        
        
        site.getReelWidthMask = function(items){
            return (items * site.taille.itemWidth) > getWidth();
        }
        
        site.showVideoDesc = function(mask){
            var arr = [];
            $('.item').each(function(index,elm){
                if(site.bool.hover){
                    $(this).stop(true,true).hover(function(){
                        site.bool.hover = true;
                        $(this).addClass('pointer').find('.itemInfo').fadeIn(100).removeClass('hideMe');
                    },function(){
                        hideInfoDesc();
                    });     
               }
                
                site.reelBoxHover();//for reels video
                
                var url = $(this).find('.play').attr('rel'); 
                arr.push(url);
                
                $(this).find('.play').bind('click',function(e){
                    e.preventDefault();
                    var rel = $(this).attr('rel');
                });
            });
        }
        
        
        site.videoFull = function(html){
            site.elm.workVideo.animate({marginLeft:0},1000,'easeInOutExpo',function(){
                site.bool.playVideo = true;
                $(document).ready(function(){
                    site.elm.backBtn.show();
                    site.elm.secTitle.animate({right:-170},500);
                    
                    if(site.elm.boxContact.is(':visible')) {
                        site.elm.boxContact.animate({right:-400},500);
                        site.elm.bc.removeClass('currentBox');
                    }    
                    
                    site.elm.secTitle.animate({right:-170},500);
                    var v = $('#video');
                    v.html(html);
                    var f = document.querySelector('iframe');
                    if(!isiPad() || !isiPhone()){
                        setWidth(f,getWidth());
                        setHeight(f,getHeight());   
                    }else{
                        setWidth(f,400);
                        setHeight(f,270);
                    }
                    
                    isReady(f);
                    
                    //console.log('videoFull :: vimeo = ' + html);
                    
                    
                    function isReady(iframe){
                        var self = iframe;
                        $f(self).addEvent('ready', function(){
                            
                            /*$(self).bind('click',function(){
                                $f(self).api("api_play");
                                console.log('click :: vimeo ready ');
                            });
                            $(self).click(); */
                            
                            $f(self).addEvent('finish',function(){
                                //console.log('vimeo finished');
                                site.kill();
                             });
                         });
                    }
                    
                });
                var x,y,backTimer,handlerBack;
                if(site.bool.playVideo){
                    site.elm.backBtn.bind('click',function(){
                        site.kill();
                    });
                    if(isiPad() || isiPhone()){
                        handlerBack = new HandlerBack(site.elm.backBtn);
                        handlerBack.show();
                    }else{
                        site.elm.backBtn.hover(function(e){
                            var self = $(this);
                            handlerBack = new HandlerBack(self);
                            handlerBack.show();
                            self.unbind('mousemove').bind('mousemove',function(e){
                                clearTimeout(backTimer);
                                handlerBack.show();
                                x =  e.pageX;
                                y =  e.pageY;
                                console.log(' before :: x = ' + x + ' y = ' + y);
                                backTimer = setTimeout(function(){
                                    console.log('AFter :: x = ' + x + ' y = ' + y);
                                    if(x == x) handlerBack.hide();  
                                },3000); 
                            });
                        },function(){
                            var self = $(this);
                            handlerBack = new HandlerBack(self);
                            handlerBack.hide();
                        });        
                    }
                }  
                
            });
            
            function HandlerBack(elm){
                this.elm = elm;
                this.show = function(){this.elm.find('img').show();}
                this.hide = function(){this.elm.find('img').hide();}
                return this;
            }
        }
        
        
        var arr = [];
        site.elm.myVimeo.each(function(index,e){
            var html = $(e).html();
            arr.push(html);
            $(e).empty();
        });  
        
        
        site.playVideo = function(elm){
            elm.each(function(index,e){
                var self = $(this);
                self.bind('click',function(){
                    site.videoFull(arr[index]);
                });
            });    
        }
        
        
        
        site.kill = function(){
            site.bool.playVideo =  site.bool.move = false;
            site.elm.backBtn.hide();
            site.elm.workVideo.animate({marginLeft:-getWidth()},1000,'easeInOutExpo',function(){
                $('#workVideo').empty().append('<div id="video" />');
                site.elm.secTitle.animate({right:0},500);        
                console.log('kill :: called');
            });
        }
        
        
        site.reelBoxHover = function(){
            site.elm.boxReel.stop().hover(function(){
                var elm = $(this);
                fn(elm);
            },function(){
                var elm = $(this);
                fn(elm,true);
            });
            
            function fn(elm,t){
                if(t) elm.parents('.ad-reel-box').find('.img:eq(0), .reelFakeBg').fadeIn(200).end().find('.toHide').fadeIn(200).next().hide();
                else elm.parents('.ad-reel-box').find('.img:eq(0), .reelFakeBg').fadeOut(200).end().find('.toHide').hide().next().fadeIn(200);
            }
        }
        
        site.resize = function(){
            $(window).bind('resize',function(){
                var items = $('.item').length;
                site.taille.maxWidth = getWidth() - 201;
                site.posElm.boxPos = (items * $('.item:eq(0)').outerWidth(true)) - (getWidth() - 200);
                site.allowNavigation(items);
                //
                
                setWidth($('iframe'),getWidth());
                setHeight($('iframe'),getHeight());
                setWidth(site.elm.firstBlock,site.taille.maxWidth);
                setWidth(site.elm.box,(site.taille.maxWidth - site.taille.liBgWidth));
                setWidth(site.elm.allContent,(getWidth()*2));
                setWidth(site.elm.boxReel,(getWidth()  - 105) / 3);
                setWidth($('.boxImg, .img'),(getWidth()  - 105) / 3);
                //
                site.elm.container.css({top:getHeight()/2});
                site.elm.accTitle.css({top:(getHeight()/2) - 73});
                //
                if(site.bool.playVideo) site.elm.workVideo.css({width:getWidth(),height:getHeight()});
                else site.elm.workVideo.css({width:getWidth(),height:getHeight(),marginLeft:-getWidth()});
                $('#site2').css({width:getWidth(),height:getHeight()});
                //    
                hideSecTitle();
            });        
        }
        
        
        site.init = function(){
            var items = $('.item').length;
            site.go();
            site.showVideoDesc(site.elm.mask);
            site.resize();
            site.allowNavigation(items);
            site.playVideo(site.elm.vimeo);
            //
            setWidth($('iframe'),getWidth());
            setHeight($('iframe'),getHeight());
            setWidth(site.elm.firstBlock,site.taille.maxWidth);
            setWidth(site.elm.box,(site.taille.maxWidth - site.taille.liBgWidth));
            setWidth(site.elm.mask,(site.taille.itemWidth * site.elm.items));
            //
            setWidth(site.elm.workVideo,getWidth());
            setWidth(site.elm.allContent,(getWidth()*2));
            //
            site.elm.workVideo.css({marginLeft:-getWidth(),width:getWidth(),height:getHeight()});
            site.elm.container.css({top:getHeight()/2});
            site.elm.accTitle.css({top:(getHeight()/2) - 73});
            //
            setWidth(site.elm.boxReel,(getWidth()  - 105) / 3);
            setWidth($('.boxImg, .img'), (getWidth()  - 105) / 3);
            $('#site2').css({width:getWidth(),height:getHeight()});
            
            hideSecTitle();
        }
        
        site.init();
        site.kill();
        site.navigate(site.elm.items);
        
        
        function hideSecTitle(){
            return;
            var w = getWidth();
            if(w < 1200) site.elm.secTitle.hide();                
            else site.elm.secTitle.show();    
        }
        
        
        return site;
        
    })();
    
    
    //contact
    (function(){
        var c = {
            dom : {
                boxContact : $('#boxContact'),
                adClose : $('.adClose'),
                infoBoxContact : $('.infoBoxContact'),
                bc : $('.bc'),
                toValidate : $('.toValidate'),
                send : $('#send'),
                reset : $('#reset'),
                form : $('#fm'),
                fContainer : $('#form'),
                afterSend : $('#afterSend')
            },
            
            o : {
                collapse : -500,
                expand : 107
            }
        }
        
        
        c.dom.bc.each(function(index,elm){
            $(this).bind('click',function(e){
                var id =  e.currentTarget.id;
                setCuurentBg(id);
                expand(switchView(id));
            });    
        });
        
        c.dom.adClose.bind('click',function(){
            collapse(function(){
                c.dom.boxContact.hide();
                c.dom.bc.removeClass('currentBox');
                console.log('contact box : click : ok'); 
            });        
        });
        
        
        
        function switchView(id){
            if($('#info-'+id).hasClass('ba')) return;
            collapse(function(){
                c.dom.infoBoxContact.removeClass('ba').hide();
                $('#info-'+id).addClass('ba').fadeIn('slow',function(){
                    expand(function(){});    
                }); 
            });
        }
        
        function setCuurentBg(id){
            c.dom.bc.removeClass('currentBox');
            $('#'+id).addClass('currentBox');
        }
        
        function expand(callBack){
           animate(c.o.expand,callBack);
           console.log('contact box : expand : ok'); 
        }
        
        function collapse(callBack){
           animate(c.o.collapse,callBack);
           console.log('contact box : collapse : ok');
        }
        
        function animate(r,callBack){
            c.dom.boxContact.show().animate({right:r},800,'easeInOutQuint',function(){if(callBack) callBack(); });
        }
        
        function validate(){
            
            console.log('validate ready');
            
            var $v = [];
            var lang = $('#mLang').val();
            
            c.dom.send.bind('click',function(e){
                
                c.dom.toValidate.each(function(index,elm){
                    $v.push(elm.value);
                    if(isEmpty(elm.value))
                        displayError($('#'+elm.id),'required',$v[index]);
                });    
            
                var f = {
                    dom : {
                        name : $('#name'),
                        email : $('#email'),
                        c_name : $('#c_name'),
                        tel : $('#phone'),
                        mess : $('#mess')
                    },
                    
                    v : {
                        vname : $('#name').val(),
                        vemail : $('#email').val(),
                        vc_name : $('#c_name').val(),
                        vtel : $('#phone').val(),
                        vmess : $('#mess').val()
                    }
                }
                    
                if(isEmpty(f.v.vname) || f.v.vname == $('#name').attr('default')){
                    var t = {}
                        t.fr = f.v.vname +' Champ obligatoire !!!',
                        t.en = f.v.vname +' Mandatory field  !!!';
                    displayError(f.dom.name,t,f.v.vname);    
                }    
                else if(!isEmailValid(f.v.vemail)){
                    var t = {}
                        t.fr = 'Votre adresse courriel est invalide',
                        t.en = 'Your email address is invalid ';
                    displayError(f.dom.email,t,f.v.vemail);
                    
                }else if(!isPhoneNumber(f.v.vtel)){
                    var t = {}
                        t.fr = 'Votre téléphone est invalide',
                        t.en = 'Your phone number is invalid';
                    displayError(f.dom.tel,t,f.v.vtel);
                }else{
                    var data = c.dom.form.serialize();
                    $.ajax({
                        type : 'POST',
                        data : data,
                        url:'/inc/html/form/send.php',
                        success:function(html){
                            if(html=='yes')
                                successMail();
                        }
                    }); 
                }    
                 
                return false;
                
            });
                
            c.dom.toValidate.each(function(index,dom){$v.push(dom.value);})    
                            .bind('focusin focusout',function(e){
                                var t = e.type,$val,elm = $(this);
                                $.each($v,function(index,value){
                                    switch(t){
                                        case 'focusin' :
                                            $val = elm.val();
                                            if($val == value) elm.val('');
                                        break;
                                        case 'focusout' :
                                            if(isEmpty(elm.val())) elm.val(elm.attr('default'));
                                        break;    
                                    }
                                });
                            });
                            
            var timer;
            function successMail(){
                c.dom.fContainer.slideUp('slow',function(){
                    c.dom.afterSend.fadeIn('slow');
                    timer = setTimeout(function(){
                        collapse(function(){
                            reset();
                            clearTimeout(timer);    
                        });    
                    },1000);
                });
            }
            
            function reset(){
                c.dom.fContainer.show();
                c.dom.afterSend.hide();
                c.dom.reset.click();
                c.dom.bc.removeClass('currentBox');
            }
            
            function displayError(elm,t,v){
                elm.val('').prev('span').text(t[lang]).fadeIn('slow',function(){
                    $(this).delay(600).fadeOut(600,function(){elm.val(v);});
                });
            }
            
        }
        
        c.init = function(){
             validate();   
        }
        
        return c.init();
        
    })();
    
    
    
    //artists
    var artist = (function(){
        var a = {
            dom : {
                abMask : $('.aboutMask'),
                abSlider : $('.aboutSlider'),
                abBox : $('.aboutBox'),
                abSn : $('.sn'),
                abBoxPer : $('.per'),
                abArr : $('.abArr'),
                abPrev : $('.abPrev'),
                abNext : $('.abNext'),
                adBoxInfo : $('.adBoxInfo'),
                slideBox : $('#slideBox'),
                sliderBgInfo : $('.sliderBgInfo'),
                sliderInfo : $('.sliderInfo'),
                arContainer : $('#artist-container'),
                adReadMore : $('.adReadMore'),
                artFullDesc : $('#artFullDesc'),
                adBoxFull : $('.adBoxFull'),
                artClose : $('.artClose'),
                snCurrent : $('#adSnCurrent')
             },
            
            lth : {
                abBox : $('.sn').length
            },
            
            w : {
               abBox : $('.aboutBox').outerWidth(true)
            },
            
            h : {
                sbg : $('.sbg').outerHeight(true) - 10
            }
        }
        
        a.dom.abSn.not('.currentBox').scale('.9');
         
        //
        var i = 1, currentPos = 2;
        
        
        if(isiPad()){
            a.dom.abSlider.swipable();
            a.dom.abSlider.bind('swipe.left',function(){
                if(i >= a.lth.abBox -1) return;
                navLeft();
            });
            a.dom.abSlider.bind('swipe.right',function(){
                navRight(true);
            });
            
        }
        a.dom.abNext.bind('click',navLeft);
        a.dom.abPrev.bind('click',navRight);
        
        setWidth(a.dom.arContainer,getWidth() - 105);
        $(window).bind('resize',function(){
            setWidth(a.dom.arContainer,getWidth() - 105);
        });
        
        
        a.dom.adReadMore.each(function(index,elm){
            var self = $(this);
            self.bind('click',function(){
                showFullDesc(index);
            });
        });
        
        a.dom.artClose.bind('click',hideFullDesc);
        
        
        //
        a.init = function(){
            var w = (getWidth() - 206);
            setHeight(a.dom.abBoxPer,getHeight());
            setWidth(a.dom.abMask,(a.w.abBox * 3));
            setWidth(a.dom.abSlider,(a.w.abBox * a.lth.abBox));
            a.dom.abArr.css({top:(getHeight() - 300)});
            a.dom.sliderBgInfo.css({top:-a.h.sbg});
            a.resize();
        }
        
        a.resize = function(){
            $(window).bind('resize',function(){
                var w = (getWidth() - 206);
                var l = parseInt(a.dom.abSlider.css('left'));
                setHeight(a.dom.abBoxPer,getHeight());
                a.dom.abArr.css({top:(getHeight() - 300)});
            });
        }
        
        a.dom.abSn.bind('click',function(e){
            e.preventDefault();
            var self = $(this),
                index = self.index();
            if(i == index) return;
            if(i > index) navRight();
            else navLeft();    
        });
        
        function navLeft(){    
            setScale();
            slideBoxArtist(i,function(){
                scale(i);
                switchCurrentBox(i);
            });
            hideArrow(i+1);
            SlideInfo(i+1);
            i++; currentPos++;
        }
        
        function navRight(y){
            i--; currentPos--;
            if(y){
                if(i < 0) return;
            }
            setScale();
            slideBoxArtist(i-1,function(){
                scale(i);
                switchCurrentBox(currentPos-1);   
            });
            SlideInfo(i);
            hideArrow(i);
        }
        
        function showFullDesc(index){
            a.dom.artFullDesc.fadeIn('slow',function(){
                a.dom.adBoxFull.hide();
                $('.adBoxFull:eq('+index+')').fadeIn('slow');
            });
        }
        
        function hideFullDesc(){
            a.dom.adBoxFull.fadeOut('slow',function(){
                a.dom.artFullDesc.fadeOut('slow');
            });
        }
        
        function scale(index){
            $('.sn:eq('+index+')').animate({scale: '1'}, 600);
        }
        
        
        function setScale(callBack){
            a.dom.abSn.removeClass('currentBox');
            $('.sn').animate({scale: '0.9'}, 600,function(){
                if(callBack) callBack();
            });
        }
        
        //
        function slideBoxArtist(index,callback){
            a.resize();
            a.dom.abSlider.animate({left:-(a.w.abBox*index)},1000,'easeInOutQuad',function(){
                if(callback) callback();afterSlide();
            }); 
        }
        //
        function switchCurrentBox(index){
            setTimeout(function(){
                $('.sn:eq('+index+')').addClass('currentBox');
            },0);
        }
        //
        function hideArrow(index){
            a.dom.abArr.fadeIn(200);
            if(index < 1) a.dom.abPrev.fadeOut(200);
            else if(index >= a.lth.abBox-1) a.dom.abNext.fadeOut(200);
        }
        //
        function SlideInfo(index){
            a.dom.slideBox.animate({marginTop:-200},300,'easeInOutQuad',function(){
                slideBgInfo(index);
                a.dom.adBoxInfo.hide();
                $('.adBoxInfo:eq('+index+')').show();
                if(a.dom.artFullDesc.is(':visible')){
                    a.dom.adBoxFull.hide();
                    $('.adBoxFull:eq('+index+')').fadeIn('slow');
                }    
            });
        }
        //
        function afterSlide(){
            a.dom.slideBox.animate({marginTop:0},400,'easeInOutQuad',function(){});
        }
        //
        function slideBgInfo(index){
            a.dom.sliderBgInfo.animate({top:-a.h.sbg*index},400,'easeInOutQuad',function(){});
        }
        //        

        return a;
    })();
    //
    artist.init(); 
});
