/**
 * @author imac
 * Document Jquery
 */

$(document).ready(function(){
        
    indice = 0;
    miniIndice = 0;
    tactile = false;
    resizePage = false;
    
	menuTopHeight = $('.menu-top').height();
	
	windowHeight = $(window).height();
	
	presenceFrise = $(document).find('.frise').html();
	presenceSlideshow = $(document).find('#slideshow').html();
	
	$('.navigation-frise div').each(function(){
		$(this).wrap('<span>')
	})
	
    var agent=navigator.userAgent.toLowerCase();
    
    codeRegex = /mobile/i;
    
    if(codeRegex.test(agent) == true){
        tactile = true;
    }
    
	else{
		
		if(presenceSlideshow != null){
			$('.menu-toolbar .home-navigate').append('<div class="btn-navigate shortcuts btn-deploy"><div class="deploy"><div class="cadre-deploy"><p>Use keyboard to slide :<span style="display:block;" class="hotkey-up"> : reset slideshow</span><span style="display:block;" class="hotkey-right"> : slide right</span><span style="display:block;" class="hotkey-left"> : slide left</span><span style="display:block;" class="hotkey-down"> : last slide</span></p></div></div></div>')
		}

		$('.fiche .home-navigate .btn-deploy').mouseenter(showContent);
		$('.fiche .home-navigate .btn-deploy').mouseleave(hideContent);
		
		if(presenceFrise != null){
			$('.menu-footer').after('<div class="btn-navigate btn-infos-shortcuts"><div class="hotkey-guides">Use keyboard to navigate :<div class="hotkey-up"> : slide up</div><div class="hotkey-down"> : slide down</div></div></div>');
		}
	}
	
	if(tactile == true){
		heightCadreSlideshow = 700;
		adaptFooter();
		$('.bloc-droite-home').addClass('mobile');
		$('.fiche .home-navigate .btn-deploy').click(showContent);
	}
	
	else{
		heightCadreSlideshow = $(window).height() - menuTopHeight - 70;
		footerFrise();
		
	}
		
	$('#cadre-slideshow').prepend('<div id="prev"></div><div id="next"></div>');
	
    function nameSlides(){
        indice = indice +1;
        
        $(this).attr('indice',indice);
        
        count = $(this).find('div').length;
        
        $(this).addClass('slide');
        
        if (count == '2'){
            $(this).find('div:first-child').addClass('slide-prev');
            $(this).find('div:last-child').addClass('slide-next');
        }
        if (count == '1'){
            $(this).find('div').addClass('slide-next').addClass('center-slide');
            
            if($(this).attr('indice') != '1' ){
                $(this).append('<div class="slide-prev invisible"><img src="design/img/bg/pixel-empty.png" /></div>');
                $(this).find('.invisible').hide();
            }
        }
    }
    $('#slideshow > div').each(nameSlides);
    
    function replaceSrc(){
        bigSrc = $(this).attr('src');
        thumbSrc = bigSrc.replace('b_','t_');
        
        $(this).attr('src',thumbSrc)
    }
    
    function nameThumbs(){
        miniIndice = miniIndice +1;
        
        $(this).attr('indice',miniIndice);
        
        $(this).empty();
        
        getContent = $('#slideshow div[indice = '+miniIndice+']').html();
        
        $(this).addClass('slide-to').append(getContent);
        
        $(this).find('img').each(replaceSrc);
        
        $(this).find('.invisible').remove();
        
        $(this).find('*').attr('class','').attr('style','');
        
        $(this).find('p').remove();
        
        $(this).css({
            fontSize:'0px'
        });
    }
    

    function placeElements(){
        
        ficheWidth = $(window).width();
        
        infoFicheWidth = $('.info-fiche').width();

        menuToolbarWidth = $('.menu-toolbar').width();

        widthCadreSlideshow = ficheWidth - infoFicheWidth - menuToolbarWidth;
        widthNavigation = ficheWidth -menuToolbarWidth;

        menuToolbarHeight = $('.menu-toolbar').height();

        nameHeight = $('h1.name').height();
        menuTopHeight = $('.menu-top').height();
        
        heightCadreSlideshow = $(window).height() - menuTopHeight - 70;

        forDecalMarge = widthCadreSlideshow - 4;
        
        widthSlideshow = $('#slideshow').width();
        heightSlideshow = $('#slideshow').height();
        
        widthBtn = $('#next').width();
        widthCadre = $('#cadre-slideshow').width();
        heightCadre = $('#cadre-slideshow').height();

        marginBtn = widthCadre - widthBtn;

        halfWidth = (widthCadreSlideshow * 50/100);
        
        maxHeightImg = heightCadreSlideshow - 40;
        maxWidthCenter = widthCadreSlideshow - 10;
        
        $('#navigation .cadre-deploy').append('<div/>')
        

        $('body').css({
            height:'100%'
        });

        
        $('.fiche').css({
            width: ficheWidth +'px',
            height: heightCadreSlideshow +'px'
        });

        $('.slide-to').css({
            fontSize: '0px',
        });
        
        $('.info-fiche').css({
			maxHeight: heightCadreSlideshow -80 +'px',
			overflowY:'auto'
		})
		
        $('#cadre-slideshow').css({
            overflow:'hidden',
            height: heightCadreSlideshow +'px',
            minHeight: menuToolbarHeight +'px',
            width: widthCadreSlideshow +'px'
        });
        
        $('#slideshow').css({
            overflow:'hidden',
            height: heightCadreSlideshow +'px',
            minHeight: menuToolbarHeight +'px',
            width: widthCadreSlideshow +'px'
        });

        $('#slideshow > div > .slide-next').css({
            width : halfWidth +'px',
            left : halfWidth +'px',
            height: heightCadreSlideshow +'px',
        });

        $('#slideshow > div > .slide-prev:not(.invisible)').css({
            width : halfWidth +'px',
            height: heightCadreSlideshow +'px',
            left : '0',
        });

        $('#slideshow > div > .center-slide').css({
            width : widthCadreSlideshow +'px',
            height: heightCadreSlideshow +'px',
            left : '0',
        });
        
		if(tactile == true){
	        $('#slideshow > div > div img').css({
	            maxWidth : halfWidth - 10 +'px',
	            maxHeight : 680 +'px'
	        });
		}

		else{
	        $('#slideshow > div > div img').css({
	            maxWidth : halfWidth - 10 +'px',
	            //maxHeight : maxHeightImg - 10 +'px'
				maxHeight : maxHeightImg -10  +'px'
	        });

		}
        
        $('#slideshow > div > div p').addClass('title-photo');

        
        if (resizePage == false){
			
            $('#slideshow') 
            .cycle({ 
                fx:     'scrollHorz', 
                speed:  800,
                nowrap: 1,
                timeout: 0,
                pager:  '#navigation .cadre-deploy div',
                pause:   1,
                next:   '.slide-next img',
                prev:   '.slide-prev img',
				after:   displayBtnSlideshow, 
                easing: 'swing',
            });
            
            $('#navigation .cadre-deploy a').each(nameThumbs);
            $('.cadre-deploy').append('<div class="clear"></div>');
			
			//$('#cadre-slideshow').prepend('<div id="prev"></div><div id="next"></div>')
        }


		function displayBtnSlideshow(){


			if($('#slideshow > div:first-child').css('display') == 'block'){
				$('#prev').fadeOut(250);
			}
			
			if($('#slideshow > div:first-child').css('display') == 'none'){
				$('#prev').fadeIn(250);
			} 
						
			if($('#slideshow > div:last-child').css('display') == 'block'){
				$('#next').fadeOut(250);
			}

			if($('#slideshow > div:last-child').css('display') == 'none'){
				$('#next').fadeIn(250);
			} 

		}
		
		
        resizePage = false;
    }
    
    function addTitle(){
        
        $('.btn-deploy').each(function(){
            thisCategorie = $(this).attr('class').replace(' btn-deploy','').replace('btn-navigate ','').replace('-',' ')

            if(tactile == true){
                $(this).find('.cadre-deploy').prepend('<h3 class="categorie">'+ thisCategorie +'</h3><div class="btn-close"></div>')
            }
            else{
                $(this).find('.cadre-deploy').prepend('<h3 class="categorie">'+ thisCategorie +'</h3>')
            }
        })
    }
        

    placeElements();
    adaptFooterSlideshow();
    addTitle();
    
    $(window).resize(function() {
		windowHeight = $(window).height();
		
        resizePage = true;
		footerFrise();
		
		adaptFooterSlideshow();
        placeElements();
		
		$('#next').css({
			height:  heightCadreSlideshow +'px'
		})
		
		$('#prev').css({
			height:  heightCadreSlideshow +'px'
		})
    });
		

    $('.deploy').css({
        opacity:'0',
        //overflow:'hidden',
        width:'0',
        //height:'auto',
        //maxHeight:'300px'
    });
    
    $('.deploy .slide-to').css({
        opacity:'0'
    })

    
    function showContent(){
        
		$(this).find('.slide-to').animate({
            opacity:'1'
        },250);
        
		$(this).css({
            backgroundColor:'#B32553'
		});
		
        $(this).animate({
            width:'51px',
        },125, function(){
            $(this).find('.deploy').css({
                display:'block',
                //maxHeight:'inherit'
            }).animate({
                width:'630px',
                opacity:'1'
            },250, "swing", function(){
                deployHeight = $(this).height();
                //$(this).css('height',deployHeight);
                
				presenceImg = $(this).find('img').attr('src');
				
				listWidth = $(this).find('ul').width();
				
				if(presenceImg == null){
					$(this).css('height',deployHeight);
					$(this).find('ul').css({
						width: listWidth + 'px'
					})
				}
				
                contentWidth = $(this).find('.cadre-deploy').width() + 2;
                $(this).css('width', contentWidth);
            });
        });
        
        
        if(tactile == true){
            $('.fiche .home-navigate .btn-deploy').not($(this)).each(hideContent);
        }
    }
    
    function hideContent(){
        $(this).find('img').css({
            opacity:'1;'
        })
        
        $(this).find('.deploy').animate({
            width:'0',
            opacity:'0',
            },250, "swing",function(){
                $(this).css({
                    display:'none'
                });
				
               $(this).parent().animate({
                    width:'30px',
					backgroundColor:'#B32553'
                },250,function(){
                    $(this).removeClass('hovered');
					$(this).attr('style','')
                });

        });
    }
    
    function hideContentMobile(){
        $('.fiche .home-navigate .btn-deploy').each(hideContent);
    }

    if(tactile == true){
        $('.fiche .home-navigate .btn-deploy .deploy .cadre-deploy .btn-close').click(hideContentMobile);
    }
	
	$('#prev').click(function(){
		goLeft();
	})
	
	$('#next').click(function(){
		goRight();
	})
    
	function autorizeShortcutsSlideshow(){
		presenceLonguePage = $('.cadre-text-long').html();
		
		if(presenceLonguePage == null){
		    shortcut.add("Up",function() {
		        goFirst();
		    });
		    
			shortcut.add("Down",function() {
		        goLast();
		    });
			
		    shortcut.add("Right",function() {
		        goRight();
		    });
		
		    shortcut.add("Left",function() {
		        goLeft();
		    });
		}
		
	}
	
	function goFirst(){
		$('#navigation a:first-child').trigger('click');
	}
	
	function goLast(){
		$('#navigation div div > a:last-child').trigger('click');
	}
	
	function goRight(){
		currentIndice = $('#navigation a.activeSlide').attr('indice');
		        $('#slideshow .slide[indice='+ currentIndice +'] .slide-next img').trigger('click');
	}
	
	function goLeft(){
		currentIndice = $('#navigation a.activeSlide').attr('indice');
		$('#slideshow .slide[indice='+ currentIndice +'] .slide-prev img').trigger('click');
	}
	
	autorizeShortcutsSlideshow();
	
/*
    $('#hotkey-icon').css({
		opacity:'0.5'
	})
*/
	
	function showHotkeyGuide(){
		$(this).animate({
			marginTop:'0px',
		},250, 'swing')
		
		$(this).find('.hotkey-guides').animate({
			opacity:'1'
		})
		
		$('#hotkey-icon').animate({
			opacity:0,
		},250);
	}
	
	function hideHotkeyGuide(){
		if($(this).parent().parent().hasClass('cadre-frise')){
			decalTop = -95;
		}
		
		else{
			decalTop = -120;
		}
		
		$(this).parent().animate({
			marginTop: decalTop + 'px',
		},250, 'swing')
				
		$(this).animate({
			opacity:'0.5'
		})

		$('#hotkey-icon').animate({
			opacity:1,
		},250);
	}
	
	$('#hotkeys').mouseenter(showHotkeyGuide)
	$('#hotkeys .hotkey-guides').mouseleave(hideHotkeyGuide)


	function footerFrise(){

		presenceSearchForm = $('#searchform').html()
		
		if (presenceSearchForm != null){
			positionFooter = 928;
		}
		
		else{
			positionFooter = 893;
		}
		
		if(presenceFrise != null){
			
			if (windowHeight <= positionFooter){
				$('.menu-footer').css({
					position:'static'
				})
				
				$('.btn-infos-shortcuts').css({
					position:'static'
				})
			}
			
			else{
				$('.menu-footer').css({
					position:'fixed',
					bottom:'20px'
				});

				$('.btn-infos-shortcuts').css({
					position:'fixed',
					bottom:'20px',
					right: '0'
				});
			} 
		}
		
	}

	function footerSlideshow(){
		//presenceSlideshow = $(document).find('#slideshow').html();

		if(presenceSlideshow != null){
			$('.menu-footer').hide();
			
			$('.menu-footer').clone().appendTo('.fiche');
			
			$('.fiche .menu-footer').show();

			decalFooter = heightCadreSlideshow + 100;
			
			$('.fiche .menu-footer').css({
				position:'absolute',
				top: decalFooter +'px',
			});
			
			$('body > .menu-footer').remove();
		}
	}
	
	function adaptFooterSlideshow(){
		if (presenceSlideshow != null) {

			if (windowHeight <= 700){
				$('.menu-footer').css({
					position:'static'
				})
			}
			
			else{
				$('.menu-footer').css({
					position:'fixed',
					bottom:'20px'
				})
			}
		}
	}
	
	function adaptFooter(){
		footerSlideshow();
		footerFrise();
	}
	
});

