var index = 0 , margin = 0;
/******************************* slider **********************/
$(function() {
    //scrollpane parts
    var scrollPane = $( ".scroll-pane" ),
    scrollContent = $( ".scroll-content" );

    //build slider
    var scrollbar = $( ".scroll-bar" ).slider({});
		
    //append icon to handle
    var handleHelper = scrollbar.find( ".ui-slider-handle" )
    .mousedown(function() {
        scrollbar.width( handleHelper.width() );
    })
    .mouseup(function() {
        scrollbar.width( "100%" );
    })
    .append( "<span class='ui-icon ui-icon-grip-dotted-vertical'></span>" )
    .wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();
		
    //change overflow to hidden now that slider handles the scrolling
    scrollPane.css( "overflow", "hidden" );
		
    //size scrollbar and handle proportionally to scroll distance
    function sizeScrollbar() {
        var remainder = scrollContent.width() - scrollPane.width();
        var proportion = remainder / scrollContent.width();
        var handleSize = scrollPane.width() - ( proportion * scrollPane.width() );
        scrollbar.find( ".ui-slider-handle" ).css({
            width: handleSize,
            "margin-left": -handleSize / 2
        });
        handleHelper.width( "" ).width( scrollbar.width() - handleSize );
    }
		
    //reset slider value based on scroll content position
    function resetValue() {
        var remainder = scrollPane.width() - scrollContent.width();
        var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
        parseInt( scrollContent.css( "margin-left" ) );
        var percentage = Math.round( leftVal / remainder * 100 );
        scrollbar.slider( "value", percentage );
    }
		
    //if the slider is 100% and window gets larger, reveal content
    function reflowContent() {
        var showing = scrollContent.width() + parseInt( scrollContent.css( "margin-left" ), 10 );
        var gap = scrollPane.width() - showing;
        if ( gap > 0 ) {
            scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );
        }
    }
		
    //change handle position on window resize
    var w = $('.scroll-content .scroll-item').length-1 ;

    $( window ).resize(function() {
       
        if ($('#spikly_wrapper').attr('class')!='digital'){
            index = 0 ;
            w = $('.scroll-content .scroll-item').length -1 ;
            resetValue();
            sizeScrollbar();
            reflowContent();
            scrollContent.animate({
                marginLeft:  "0px"
            }, {
                duration: 500,
                queue: false,
                easing: 'swing'
            });
        }
    });
    //init scrollbar size
    setTimeout( sizeScrollbar, -scrollPane.width() );//safari wants a timeout
	
	myWidth=0;
	$('.scroll-content .scroll-item ').each(function(){
		myWidth = myWidth + $(this).width();
	});
    $('.scroll-content').width(myWidth);
  
    $('#next,#prev,.next-q').click( function() {
        var j=scrollbar.slider( "value" );
        var id = $(this).attr('id');
        var classId = $(this).attr('class');
        var m = scrollContent.css("margin-left").replace('px', '');

        scrollbar.bind( "slidechange", function( event, ui ) {
        
            if ( scrollContent.width() > scrollPane.width() ) {
       
                scrollContent.animate({
                    marginLeft:  parseInt(m)+margin+ "px"
                }, {
                    duration: 500,
                    queue: false,
                    easing: 'swing'
                });

                         
            } else {
                scrollContent.css( "margin-left", 0 );
            }
        });
        
        if ((id=='next' || classId=='next-q' ) && scrollContent.width() + parseInt(m)> scrollPane.width()){
            margin = -( $('.scroll-content .scroll-item:eq('+index+')').width());
            index++;
            scrollbar.slider( "value", j += (w*10) );
        }
        else if (index>0 && id=='prev' ) {
            index--;
            margin = ( $('.scroll-content .scroll-item:eq('+index+')').width());
            scrollbar.slider( "value", j -= (w*10) );
        }
        else if (index==-1) index=0;
		
        
        return false ;
    });
    /*****philo slider*///////////
	if ($('.philo').length){
		myWidth = $('.scroll-item').width();
		$('a.influence').click(function(){
			index = 1;
			scrollContent.animate({
				marginLeft:  "-"+ myWidth * index +"px"
			}, {
				duration: 500,
				queue: false,
				easing: 'swing'
			});
		});
		$('a.tech').click(function(){
			index = 2;
			scrollContent.animate({
				marginLeft:  "-"+ myWidth * index +"px"
			}, {
				duration: 500,
				queue: false,
				easing: 'swing'
			});
		});
		
		$('a.crea').click(function(){
			index = 0;
			scrollContent.animate({
				marginLeft:  "-"+ myWidth * index +"px"
			}, {
				duration: 500,
				queue: false,
				easing: 'swing'
			});
		});
	}
     /*****end philo slider*///////////
    
});
/******************************* end slider **********************/

/******************************* form contact **********************/
$(document).ready(function(){
    $("#nom").click(function(){
        if ($(this).val()=="Nom Prénom") $(this).val('');
    });
    $("#email").click(function(){
        if ($(this).val()=="Adresse e-mail") $(this).val('');
    });
    $('.typeSubmit').click(function() {
        if ($("#nom").val()=="Nom Prénom") $("#nom").val('');
        if ($("#email").val()=="Adresse e-mail") $("#email").val('');
        
        $("#form-contact").validate({
            invalidHandler: function(form, validator) {
                var errors = validator.numberOfInvalids();
                if (errors) {
                    if ($("#email").val()=="") $("#email").val('Adresse e-mail');
                    if ($("#nom").val()=="") $("#nom").val('Nom Prénom');
                }
            },
            submitHandler: function(form) {
                $.post("ajax/mail.php", $("#form-contact").serialize(),function(data){
                    $('#form-contact').html(data);
                });
            }
        });
        
        $("#nom").rules("add", {
            required: true,
            messages: {
                required: ""
            }
        });
        $("#email").rules("add", {
            required: true,
            email: true,
            messages: {
                required: "",
                email:""
            }
        });
        $("#sujet").rules("add", {
            required: true,
            messages: {
                required: ""
            }
        });
        $("#message").rules("add", {
            required: true,
            messages: {
                required: ""
            }
        });
    });
	if ($('.colorbox').length){
		$('.colorbox').colorbox({
			width:"755px", 
			height:"370px", 
			iframe:true, 
			left : "195px", 
			top: "70px"
		});
	}
	if ($('.video-box').length){
		$('.video-box').colorbox({
			left : "195px", 
			top: "70px"
		});
	}
	
	if ($('.digital .content .scroll-content .scroll-item').length){
		myQuizSize= ($('.digital .content').width()-8) * $('.digital .content .scroll-content .scroll-item').length;
		maxQuizSize = 11100;
		if (myQuizSize > maxQuizSize) {
			myQuizSize = maxQuizSize;
			myQuizLiSize = parseInt(maxQuizSize / $('.digital .content .scroll-content .scroll-item').length);
			$('.digital .content').width(myQuizLiSize +"px");
		} else {
			myQuizLiSize = $('.digital .content').width()-8;
		}
		
		$('.digital .content .scroll-content .scroll-item').each(function(){
			$(this).width( myQuizLiSize +"px");
		});
		$('.digital .content .scroll-content').width((myQuizSize)+"px");
		
	}
	// Déplacement du background en même temps que les blocs
	if ($('.philo').length || $('.agence').length ){
		setInterval("depBackground()", 50);
	}
	//positionnement du footer
	posFooter();
});
$(window).resize(function() {
	posFooter();
});
/******************************* end form contact **********************/
function depBackground(){
	$('.custom').css('background-position', $('.scroll-content').css('marginLeft')+' 0');
}
function posFooter(){
	myHeight = $('#spikly_wrapper').height() + $('.footer').height();
	if ($(window).height() > myHeight){$('.footer').addClass('bottom');}else{$('.footer').removeClass('bottom');}
}
/******************************* horloge contact **********************/
function get_time_zone_offset( ) {
    var current_date = new Date( );
    var gmt_offset = current_date.getTimezoneOffset( ) / 60;
    return gmt_offset;
}

$(function($) {

    dts = get_time_zone_offset( );
    if (dts==-2) t=1; else t=0;
	
	if ($('.jclockParis').length){
		var optionsParis={
			utc: true,
			utc_offset: 1+t
		};
	
		$('.jclockParis').jclock(optionsParis);
	}
    
	if ($('.jclockGeneve').length){
		var optionsGeneve={
			utc: true,
			utc_offset: 1+t
		};
		$('.jclockGeneve').jclock(optionsGeneve);
	}
	
	if ($('.jclockMont').length){	
		var optionsMont={
			utc: true,
			utc_offset: -5+t
		};
		$('.jclockMont').jclock(optionsMont);
	}
	
	if ($('.jclockTelaviv').length){
		var optionsTel={
			utc: true,
			utc_offset: 2+t
		};
		$('.jclockTelaviv').jclock(optionsTel);
	}
	
	if ($('.jclockSaoPaulo').length){
		var optionsTel={
			utc: true,
			utc_offset: -2+t
		};
		$('.jclockSaoPaulo').jclock(optionsTel);
	}
});
/******************************* end horloge contact **********************/
var q=1;
var a=0,b=0,c=0;
$(function($) {
    $('#start-q.next-q').click(function(){
        questionId= '#q1';
        $(questionId + ' input').removeAttr( 'disabled' );
        $(this).removeClass('next-q');
        $(questionId + ' a').addClass('next-q');
    });
    $('.next-q').live('click',function(){
        r = $("input[name='q"+q+"']:checked").val();
        if (r){
            if (r==1) a++;
            if (r==2) b++;
            if (r==3) c++;
            questionId= '#q'+ (++q);
            var m = $('.scroll-content').css("margin-left").replace('px', '');
			myQuizzIndex = q - 1;
			margin = parseInt(m)-$('.scroll-content .scroll-item:eq('+ myQuizzIndex +')').width();
			
			
            $('.scroll-content').animate({
                marginLeft:  margin + "px"
            }, {
                duration: 500,
                queue: false,
                easing: 'swing'
            });
            $(questionId + ' input').removeAttr( 'disabled' );
            $(this).removeClass('next-q');
            if ($(questionId + ' a').attr('class')!='result quizz-box'){
                $(questionId + ' a').addClass('next-q');
            } 
        }
    });
    $('.quizz-box').live('click',function(){
        var page = '';
        if(a> b && a >c){ 	
            page = 'response1';
        }else  if(b > a && b > c){ 
            page = 'response2';
        }else{
            page = 'response3';
        }
        $.colorbox({
            href : "popins/"+page+".html", 
            left : "195px", 
            top: "70px"
        });		
    });
});
 
