$(document).ready(function(){
lienvideo();
pager = new Imtech.Pager();
pager.pagingContainer = $("[id*='contenuBlockHidden']");
pager.paragraphs = $('.blocData', pager.pagingContainer);
pager.showPage(1);
dropMenu();	
dropMenu_pictos();	
showLink();
closeLink();
drop();
customSelect();
customSelectSecteurActivite();
customSelectActiviteButton();
slide();
showMenu();
});

emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

function checkscript() {
    var noError = true;
    
    if(!isOneChecked("rdClientAxa")) {
        $("#lbClientAxa").fadeIn("normal");		
		noError = false;
	}	
	else {
	    $("#lbClientAxa").fadeOut("normal");
	}
    
    if(!isOneChecked("rdCivilite")) {
        $("#lbCivilite").fadeIn("normal");		
		noError = false;
	}	
	else {
	    $("#lbCivilite").fadeOut("normal");
	}
    
	if ($("[id*='tbNom']").val() == "") {
		$("#lbNom").fadeIn("normal");		
		noError = false;
	}	
	else {
	    $("#lbNom").fadeOut("normal");
	}
	
	if ($("[id*='tbPrenom']").val() == "") {
	    $("#lbPrenom").fadeIn("normal");
	    noError = false;
	}
	else {
	    $("#lbPrenom").fadeOut("normal");
	}	

    // 1: email vide
    // 2: email non conforme
    var emailPro = $("[id*='tbEmailPro']").val();
    if (emailPro == "") {
		$("#lbEmailPro1").fadeIn("normal");
		noError = false;
	}
	else if(!emailReg.test(emailPro)) {
	    $("#lbEmailPro1").fadeOut("normal");
	    $("#lbEmailPro2").fadeIn("normal");
	    noError = false;
	}
	else {
	    $("#lbEmailPro1").fadeOut("normal");
	    $("#lbEmailPro2").fadeOut("normal");
	}	
	
	// 1: email confirmation vide
	// 2: email confirmation non conforme
	// 3: email confirmation non egal email professionnel
	var emailConf = $("[id*='tbConfirmationEmail']").val();
	if (emailConf == "") {
		$("#lbConfirmationEmail1").fadeIn("normal");
		noError = false;
	}
	else if(emailConf!=emailPro){
	    $("#lbConfirmationEmail1").fadeOut("normal");
	    $("#lbConfirmationEmail2").fadeIn("normal");
	    noError = false;
	}
	else {
	    $("#lbConfirmationEmail1").fadeOut("normal");
	    $("#lbConfirmationEmail2").fadeOut("normal");
	}

    if ($("[id*='tbNomEntreprise']").val() == "") {
		$("#lbNomEntreprise").fadeIn("normal");
		noError = false;
	}
	else {
	    $("#lbNomEntreprise").fadeOut("normal");
	}
	
	if ($("[id*='tbSecteurActivite']").val() == "") {
		$("#lbSecteurActivite").fadeIn("normal");
		noError = false;
	}
	else {
	    $("#lbSecteurActivite").fadeOut("normal");
	}
	
	if ($("[id*='tbCodePostal']").val() == "") {
		$("#lbCodePostal1").fadeIn("normal");
		noError = false;
	}
	else if($("[id*='tbCodePostal']")[0].value.length!=5){
	    $("#lbCodePostal1").fadeOut("normal");	    
	    $("#lbCodePostal2").fadeIn("normal");
	    noError = false;
	}
	else {
	    $("#lbCodePostal1").fadeOut("normal");
	    $("#lbCodePostal2").fadeOut("normal");
	}

    if (!$("[id*='cbCondition1']")[0].checked) {
		$("#lbCondition1").fadeIn("normal");
		noError = false;
	}
	else {
	    $("#lbCondition1").fadeOut("normal");
	}

    if (!$("[id*='cbCondition2']")[0].checked) {
		$("#lbCondition2").fadeIn("normal");
		noError = false;
	}
	else {
	    $("#lbCondition2").fadeOut("normal");
	}

    if(!isOneCheckedClass()) {
		$("#lbTheme").fadeIn("normal");
		noError = false;
	}
	else {
	    $("#lbTheme").fadeOut("normal");
	}

    //au moins un pb sur le formulaire
    if(!noError){
        $(".errorList").fadeIn("normal");
        $('html, body').animate({scrollTop:0}, 'slow');        
    }

	return noError;
}

//verifie qu'au moins un element
//de name = nameElt est coche
function isOneChecked(nameElt)
{
    var result = false;
    for(i = 0 ; i < $("[name*='"+nameElt+"']").length ; i++)
    {
        if($("[name*='"+nameElt+"']")[i].checked)
            return true;
    }
    return result;
}

//verifie qu'au moins un element
//de name = nameElt est coche
function isOneCheckedClass()
{
    var result = false;
    for(i = 0 ; i < $(".checkboxTheme").length ; i++)
    {
        if($(".checkboxTheme")[i].checked)
            return true;
    }
    return result;
}

function dropMenu(){
    $("ul.dropdown li").hover(function(){
        $(this).addClass("hover");
        $('ul:first',this).css('visibility', 'visible');
        
    }, function(){
        $(this).removeClass("hover");
        $('ul:first',this).css('visibility', 'hidden');
    });
    
    $("ul.dropdown li ul li:has(ul)").find("a:first").append(" &raquo; ");

}


function dropMenu_pictos() {
	 $("ul.dropdown_menu2 li").click(function(){    
        $(this).addClass("hover");		
        $('ul:first',this).css('visibility', 'visible');										    
    });    
    $("ul.dropdown_menu2 li ul li:has(ul)").find("a:first").append(" &raquo; ");
			
	$("ul.dropdown_menu2 li").hover(function(){        
		$('ul:first',this).css('visibility', 'hidden');	
		$(this).addClass("hover");
				
    }, function(){         		
		$(this).removeClass("hover");
        $('ul:first',this).css('visibility', 'hidden');    
    });
}

function showMenu(){
	$('.listTheme').next("ul").hide();
	$('.active.listTheme').next("ul").slideDown("normal");	
	
	$('.listTheme').click(
		function(){
			if($(this).next("ul:visible").length !=0 && !$(this).hasClass('active')){
				$(this).next("ul").slideUp("normal");	
			}
			else{
			$(this).next("ul").slideDown("normal");	
			}
		}
	);
}

function showLink(){
$('.toggle').hide();	
	$('.moreLink .savoirplusdecale').click(function (){											
	$(this).next(".toggle").slideDown("normal");
	$(this).next(".toggle").addClass("activ");
	$(this).hide();
	return false;
	}); 
}

function closeLink(){
	$('.moreLink a.close').click(function (){	
	$(this).closest('.activ').slideUp("normal");
	$(this).closest('.activ').prev('.savoirplusdecale').fadeIn();
	
	return false;
	}); 
}


function drop(){
	$("ul.secteur").imgDropDown({title:"Votre secteur d'activite"});
}

function customSelect(){
$("select.my-dropdown").sSelect();		
}

function customSelectSecteurActivite(){
var options = {
		defaultText: $("[id*='hfDefaultValue']").val(),
		animationSpeed: 0, //set speed of dropdown
		ddMaxHeight: '', //set css max-height value of dropdown
		containerClass: '' //additional classes for container div
	};
    $("select.my-dropdownSecteur").sSelect(options);
    			
}

function customSelectActiviteButton() {
    $('.my-dropdownSecteur').change(function () {
        $("[id*='buttonSecteurActivite']").removeAttr("disabled");
        $("[id*='buttonSecteurActivite']").removeClass("buttonDisable");
        $("[id*='buttonSecteurActivite']").addClass("button");
    });
}


//fonction utilisee pour la redirection newsletter
function getRedirectURL(url,inputName)
{   
    if (emailReg.test(('#' + inputName).val()))
    {
        return url + "?mail=" + ($('#' + inputName).val());
    }
    else
    {
        return url + "?mail=" + ""; 
    }
}

//fonction utilisee pour la redirection contacter un conseiller
function getRedirectURLCodePostal(url,inputName)
{
    var codePostalReg = /^[0-9][0-9A-Za-z]([0-9]{3})?$/;
    if (codePostalReg.test($('#' + inputName).val()))
    {
        return url + "?CodePostal=" + ($('#' + inputName).val());
    }
    else
    {
        return url + "?CodePostal=" + ""; 
    }
}


function rotate() {
$(".next02").click();
}

function slide(){

        Frequence = $("[id*='hfTime']").val();
        
        if(Frequence==null)
            Frequence = "3";
            
        Frequence = Frequence.replace(',','.');
        
		//rotation speed and timer
    var speed = Frequence*1000;
	var run = setInterval('rotate()', speed);  
     
    //grab the width and calculate left value
    var item_width = $('.actualites .actuBlock').outerWidth();
	var nb_item = $('.actualites .actuBlock').length;
    var left_value = item_width * (-1);
	var size = item_width * nb_item;

       $('.actualites').css({'width':size});   
	 $('.actualites .actuBlock:first').before($('.actualites .actuBlock:last'));  
     
    //set the default item to the correct position
    $('.actualites').css({'marginLeft' : left_value});
 
    //if user clicked on prev button
	 
    $('.prev').click(function() {
 		clearInterval(run);	
        //get the right position           
        var left_indent = parseInt($('.carousel .actualites').css('marginLeft')) + item_width;

        //slide the item           
        $('.carousel .actualites').animate({'marginLeft' : left_indent}, 400,function(){   
 
            //move the last item and put it as first item              
    $('.actualites .actuBlock:first').before($('.actualites .actuBlock:last'));       
 
            //set the default item to correct position
            $('.carousel .actualites').css({'marginLeft' : left_value});
         
        });
 
        //cancel the link behavior           
        return false;
             
    });
 
  
    //if user clicked on next button
    $('.next').click(function() {
        clearInterval(run);		
        //get the right position
        var left_indent = parseInt($('.actualites').css('marginLeft')) - item_width;
        //slide the item
        $('.actualites').animate({'marginLeft' : left_indent}, 400, function () {
             
            //move the first item and put it as last item
            $('.actuBlock:last').after($('.actuBlock:first'));                 
             
            //set the default item to correct position
            $('.actualites').css({'marginLeft' : left_value});
         
        });
                  
        //cancel the link behavior
        return false;
         
    });       
    
	 $('.next02').click(function() {
        	
        //get the right position
        var left_indent = parseInt($('.actualites').css('marginLeft')) - item_width;
        //slide the item
        $('.actualites').animate({'marginLeft' : left_indent}, 400, function () {
             
            //move the first item and put it as last item
            $('.actuBlock:last').after($('.actuBlock:first'));                 
             
            //set the default item to correct position
            $('.actualites').css({'marginLeft' : left_value});
         
        });
                  
        //cancel the link behavior
        return false;
         
    });       
    //if mouse hover, pause the auto rotation, otherwise rotate it
   
}

//empeche submit sur touche Entree
function process_keyPress(event)
{
    if(event==null)
    {
        event = window.event;
    }
    
    return (event.keyCode!=13);           
}

function processKeyPressSearch(event)
{
    if(event==null)
    {
        event = window.event;
    }
    
    if(event.keyCode == 13)
    {
        $("[id*='btnSearch']").click();
    }
}

var Imtech = {};
Imtech.Pager = function() {
	this.paragraphsPerPage = $("[id*='hfNombreRessource']");
	this.currentPage = 1;
	this.pagingControlsContainer = $("[id*='pageNav']");
	this.pagingContainerPath = $("[id*='contenuBlockDisplay']");
	
	this.numPages = function() {
		var numPages = 0;
		if (this.paragraphs != null && this.paragraphsPerPage != null) {
			numPages = Math.ceil(this.paragraphs.length / this.paragraphsPerPage[0].defaultValue);
		}
		
		return numPages;
	};
	
	this.showPage = function(page) {
		this.currentPage = page;
		var html = "";
		if(this.paragraphsPerPage[0] !=null )
		{
		    for (var i = (page-1)*this.paragraphsPerPage[0].defaultValue; i < ((page-1)*this.paragraphsPerPage[0].defaultValue) + this.paragraphsPerPage[0].defaultValue; i++) {
			    if (i < this.paragraphs.length) {
				    var elem = this.paragraphs.get(i); 
				    html += "<" + elem.tagName + ">" + elem.innerHTML + "</" + elem.tagName + ">";
			    }
		    }

            var obj = $(this.pagingContainerPath);
            var objElement = obj.get()[0];
            objElement.innerHTML = html;
    		
		    renderControls(this.pagingControlsContainer, this.currentPage, this.numPages());
		}
	};
	
	var renderControls = function(container, currentPage, numPages) {
		pagingControls = "<ul>";
		currentPageSelect = currentPage;
		if (currentPageSelect > 1) {
		    var newCurrentPage = currentPageSelect - 1;
		    pagingControls += "<li><a href='#' style=\"text-decoration:underline;\" onclick='pager.showPage(" + newCurrentPage + "); return false;'> &lt </a></li>";
		}
		
		if (numPages != 1)
		{
		    for (var i = 1; i <= numPages; i++) {
			    if (i != currentPageSelect) {
				    pagingControls += "<li><a href='#' style=\"text-decoration:underline;\" onclick='pager.showPage(" + i + "); return false;'>" + i + "</a></li>";
			    } else {
				    pagingControls += "<li class=\"liSelect\">" + i + "</li>";
			    }
		    }
		}

		if (currentPageSelect < numPages) {
		    var newLastCurrentPage = currentPageSelect + 1;
		    pagingControls += "<li><a href='#' style=\"text-decoration:underline;\" onclick='pager.showPage(" + newLastCurrentPage + "); return false;'> &gt </a></li>";
		}
		
		
		pagingControls += "</ul>";
		
		$(container).html(pagingControls);
	}
}

//// ouvre une popup pour lancer la video
function openvideo(video)
{
    hauteur=Math.round((screen.availHeight-500)/2);
    largeur=Math.round((screen.availWidth-450)/2);
    window.open(video,'_blank', 'height=500,width=450,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,modal=yes,top='+hauteur+',left='+largeur+'');_spformonsubmitcalled = false;_spsuppressformonsubmitwrapper=true;
}

function lienvideo ()
{
    if ($('a[href*="/popup-video.aspx?u"]').attr('href')!= null && $('a[href*="/popup-video.aspx?u"]').attr('href') != "#")
    {
        var url = $('a[href*="/popup-video.aspx?u"]').attr('href');
        var elems = url.split("/Pages/");
        var newurl;
        if (elems != null)
        {
            if (elems[1] != null)
            {
                newurl = elems[1];
            }
        }
        $('a[href*="/popup-video.aspx?u"]').attr("tmpurl",newurl);
    }

        $('a[tmpurl]').click(function()
        {
            openvideo($('a[tmpurl]').attr("tmpurl"));
            return false;
        });
        $('a[href*="/popup-video.aspx?u"]').attr("href","#");
}

