
$(document).ready(function() {

        t = setTimeout("clickRealisation()",5000);

        /* Resize */
        $(window).resize(function() {
            testPositionBackgroundRightPart();
            setHauteurPage();
        });
        $(window).scroll(function() {
            testPositionBackgroundRightPart();
        });

        /* Blur/Focus */
        $(window).blur(function() {
                clearTimeout(t);
        }).focus(function() {
                t = setTimeout("clickRealisation()",5000);
        });

        /* PIE */
	$(function() {
		if (window.PIE) {
			$('.rounded').each(function() {
				PIE.attach(this);
			});
		}
	});

        hauteurPageBS = $(document).height();
        $('#blackScreenBook').css('height', hauteurPageBS);




        /* Popin Realisation*/
        $('#closePopin').click(function(){
            $('#blackScreenPopin').fadeOut('fast',function(){
                $('html').css('overflow','auto');
                $('html').css('overflow-x','hidden');
                testPositionBackgroundRightPart();
                $('#contenuPopin').empty();
                $('#wait').css('display','block');
            });
        });


        setHauteurPage();



	$('.realisationBook').unbind().click(function(){

            clickedRealisation = $(this);

            $('html,body').animate({scrollTop:0}, 'slow',function(){

		idRealisation = clickedRealisation.attr('id').split('_');
		idRealisation = idRealisation[1];
		scrollTop = $(window).scrollTop();
		$('#blackScreenPopin').css('top',scrollTop+'px');
		$('#blackScreenPopin').fadeIn();
		$('html').css('overflow','hidden');

		testPositionBackgroundRightPart();

		$.ajax({
			type: "POST",
			url: siteAccess+"/ezjscore/call/digitalconcept_front::getDetailRealisation",
			data: 'idRealisation='+idRealisation,
			dataType: 'json',
			success: function(msg){
				detailRealisation = '<h3>'+msg.content.titre+'</h3>';

                                detailRealisation += '<div id="conteneurPopinRepeat">'+msg.content.description+'</div>';

                                detailRealisation += '<div style="float: left;">';

                                if( msg.content.image.isLien != 0 ){
                                    detailRealisation += '<a href="'+msg.content.urlLien+'">';
                                }

                                if( msg.content.image.isLightbox ){
                                    detailRealisation += '<a href="'+msg.content.image.full+'" class="lightboxImg">'
                                }

				detailRealisation += '<img src="'+msg.content.image.medium+'" alt="'+msg.content.titre+'" class="illustrationRealisation" title="'+msg.content.titre+'" style="display: block; margin: 0px auto 0px auto;" />';

                                if( msg.content.image.isLien || msg.content.image.isLightbox ){
                                    detailRealisation += '</a></div>';
                                }


                                $('#wait').css('display','none');
				$('#contenuPopin').empty().append(detailRealisation);

                                $('.lightboxImg').lightBox({fixedNavigation:true});
			}
		});
            });
	});

	/* References */
	nbLi = $('#contenuSlider li').size();
	hauteurUl = $('#contenuSlider').height();
	hauteurLi = parseFloat( $('#contenuSlider li:first').height() ) + parseFloat( $('#contenuSlider li:first').css('margin-top') ) + parseFloat( $('#contenuSlider li:first').css('margin-bottom') );
	decale = 0;
	nbClick = 2;

	if( nbClick >= Math.ceil( nbLi/2 ) ){
		$('#allerBas').css({'background-position':'0px -29px','cursor':'default'});
	}
	$('#allerHaut').css({'background-position':'0px -29px','cursor':'default'});


	$('#allerBas').unbind().click(function(){

		if(  Math.ceil( (nbLi-1)/2 ) > nbClick ){
			decale = decale-hauteurLi;
			$('#contenuSlider').animate({top: decale},500,'easeInOutExpo',function(){});
			nbClick++;
		}
		if( Math.ceil( (nbLi-1)/2 ) == nbClick ){
			$('#allerBas').css({'background-position':'0px -29px','cursor':'default'});
		}
		if(nbClick>2){
			$('#allerHaut').css({'background-position':'0px 0px','cursor':'pointer'});
		}
		clearTimeout(t);
		t = setTimeout("clickRealisation()",5000);

	});

	$('#allerHaut').unbind().click(function(){

		if( nbClick > 2 ){
			decale = decale+hauteurLi;
			$('#contenuSlider').animate({top: decale},500,'easeInOutExpo',function(){});
			nbClick--;
		}
		if( nbClick == 2 ){$('#allerHaut').css({'background-position':'0px -29px','cursor':'default'});}
		if(  Math.ceil( (nbLi-1)/2 ) > nbClick ){$('#allerBas').css({'background-position':'0px 0px','cursor':'pointer'});}
		clearTimeout(t);
		t = setTimeout("clickRealisation()",5000);
	});





        /* Liste prestation book */
	listeLi = $('#listePrestations , #listePrestationsWeb , #listePrestationsMobile').find('li');

	$.each(listeLi,function(){
		if( trim( $(this).find('.previewParagrapheBloc').text() )=="" ){
			$(this).find('h4').css('cursor','default');
		}
	});

	$('#listePrestationsWeb .previewTitreBloc , #listePrestationsMobile .previewTitreBloc').click(function(){
		clickedItem = $(this);
		if(trim(clickedItem.closest('.contentBox').find('.previewParagrapheBloc').text())!=""){
			if(clickedItem.closest('.contentBox').find('.previewParagrapheBloc').is(':hidden')){
				clickedItem.closest('.contentBox').find('.previewParagrapheBloc').slideDown(500);
			}
			else{
				clickedItem.closest('.contentBox').find('.previewParagrapheBloc').slideUp(500);
			}
		}
		else{
			clickedItem.css('cursor','default');
		}


	});

	$('#btnWeb').click(function(){

		if( $('#listePrestationsWeb').is(':hidden') ){
			$('#listePrestationsMobile').hide();
			$('#listePrestationsWeb').show();
			$('#btnWeb').css('background-position','0px 0px')
			$('#btnMobile').css('background-position','0px -47px')
		}

	});

	$('#btnMobile').click(function(){

		if( $('#listePrestationsMobile').is(':hidden') ){
			$('#listePrestationsWeb').hide();
			$('#listePrestationsMobile').show();
			$('#btnWeb').css('background-position','0px -47px')
			$('#btnMobile').css('background-position','0px 0px')
		}

       });

	$('#listePrestations .previewTitreBloc').click(function(){
		clickedItem = $(this);

		if(trim(clickedItem.closest('.contentBox').find('.previewParagrapheBloc').text())!=""){
			if(clickedItem.closest('.contentBox').find('.previewParagrapheBloc').is(':hidden')){
				clickedItem.closest('.contentBox').find('.previewParagrapheBloc').slideDown(500);
			}
			else{
				clickedItem.closest('.contentBox').find('.previewParagrapheBloc').slideUp(500);
			}
		}
		else{
			clickedItem.css('cursor','default');
		}

	});


        /* Pagination actualitÃ©s */
        var nbPerPage = 4;
        var nb = $('.itemBookActus').length;

        if(nb > nbPerPage){

            //on crÃ©Ã© la pagination
            var nbPages = Math.ceil(nb / nbPerPage);
            for(i=1; i<=nbPages; i++){
              (i != 1) ? $('#pagination').append(' - ') : '';
              $('#pagination').append('<span>'+i+'</span>');
            }
            $('#pagination span:first').addClass('currentPage');

            //on cache tous les block sauf la premiere page
            $('.itemBookActus').each(function(index){
               if(index >= nbPerPage){$(this).css('display','none');}
            });

            //au click sur un numero
            $('#pagination span').not('current').click(function(){
                $('.currentPage').removeClass();
                $(this).addClass('currentPage');
                var pageNumber = $(this).text();
                $('.itemBookActus').each(function(index){
                    var rang = parseFloat(index+1);
                    var max = (nbPerPage*pageNumber)
                    var min = max - nbPerPage;
                    (pageNumber == 1 && rang == 1) ? rang = 2 : '';
                    if(rang > min && rang <= max ){
                        $(this).css('display','block');
                    } else {
                        $(this).css('display','none');
                    }
                });
            });

        }

        /* Book */
        var hauteurFenetre = window.innerHeight;
        var hauteurBook = $('#viewBook').height();

        if( hauteurFenetre > hauteurBook ){
            var margeHaut = ( hauteurFenetre - hauteurBook )/2;
        }
        else{
            var margeHaut =0;
        }

        var largeurLi = $('#sliderBook li:first').width();
        var nbLiBook = $('#sliderBook li').size();
        var largeurUl = largeurLi*nbLiBook;
        var decalage = 0;
        var nbClickBook = 1;

        $('#sliderBook').css('width',largeurUl);

        $('#viewBook').css('margin-top',margeHaut);

        $('span.book').click(function(){
            $('html,body').animate({scrollTop:0}, 'slow',function(){
                $('#blackScreenBook').fadeIn();
                //$('html').css('overflow','hidden');
            });

        });

        $('#goNextBook').click(function(){
                if( nbClickBook < nbLiBook ){
                        decalage = decalage - parseInt(largeurLi);
                        $('#sliderBook').animate({
                                marginLeft: decalage+'px'
                        },500,'easeInOutExpo',function(){});
                        nbClickBook++;
                }
        });
        $('#goPrevBook').click(function(){
                if( nbClickBook > 1 ){
                        decalage = decalage + parseInt(largeurLi);
                        $('#sliderBook').animate({
                                marginLeft: decalage+'px'
                        },500,'easeInOutExpo',function(){});
                        nbClickBook--;
                }
        });

        $('#closeBook').click(function(){
            $('#blackScreenBook').fadeOut();
            $('html').css('overflow','auto');
        });

       /* $('#blackScreenBook').live('click',function(){
            $('#blackScreenBook').fadeOut();
            $('html').css('overflow','auto')
        });

        $('#blackScreenPopin').click(function(){
            $('#blackScreenPopin').fadeOut();
            $('html').css('overflow','auto')
        });*/

        if ( $.browser.msie ) {
          if( parseFloat($.browser.version.substr(0, 1)) < 9){
              $('#backgroundVoeux').fadeTo(1, 0.8);
              $('#stage').css('display','none');
              $('#voeuxie').css('display','block');
              $('#voeuxie').css('top',$('#backgroundVoeux').height()/2 - 287 +'px');
              $('#voeuxie').css('left',$('#backgroundVoeux').width()/2 - 325 +'px');
          }
        }

         var top = 340;
         $('#stage').css('top',$('#backgroundVoeux').height()/2 - top +'px');

        $('#blockVoeux').click(function(){
            $('#blockVoeux').css('display','none');
            $('#backgroundVoeux').fadeOut();
            $('body').css('overflow','auto');
        });

        $('#stage_btn_close').click(function(){
            $('#blockVoeux').css('display','none');
            $('#backgroundVoeux').fadeOut();
            $('body').css('overflow','auto');
        });

        $('#voeuxie').click(function(){
            $('#blockVoeux').css('display','none');
            $('#backgroundVoeux').fadeOut();
            $('body').css('overflow','auto');
        });

});

function trim (myString){
    return myString.replace(/^\s+/g,'').replace(/\s+$/g,'');
}

function clickRealisation(){
	if($('#allerBas').css('cursor')=='pointer'){
		$('#allerBas').click();
	}
	else{
		$('#allerHaut').click();
	}
}

//cale le div backgroundRightPart
/*function getPositionBackgroundRightPart(){

    var hauteurScreen = $(window).height();
    var hauteurBody = $('#bodyPage').height();

    var hauteurMax = Math.max(hauteurScreen,hauteurBody);

    $('#leftBar').height((hauteurMax-20)+'px');

    //var largeurScreen = $(window).width();
    //var offset = $('#contenu').offset();
    //var leftPosition = offset.left;
    //var largeur = largeurScreen - leftPosition;
    hauteurBody > hauteurScreen ? hauteur = hauteurBody : hauteur = hauteurScreen;
    $('#backgroundRightPart').css({'height':hauteur+'px','width':largeur+'px','left':leftPosition+'px'});
}*/

//verifie si le div backgroundRightPart est calÃ©, et le recale le cas Ã©chÃ©ant
function testPositionBackgroundRightPart(){
    var offset = $('#contenu').offset();
    var leftPosition = offset.left;
    var verif = $('#backgroundRightPart').css('left');
    //if(leftPosition != verif) {getPositionBackgroundRightPart();}
}

function setHauteurPage(){
    hauteurPage = $(document).height();
    $('#bodyPage').css('height',hauteurPage+'px');
}




