function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function verification_email(email){
    var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$', 'i');
    return reg.test(email);
}

function detectOS(string){
	return navigator.userAgent.toLowerCase().indexOf(string) + 1;
}

     if (detectOS('mac')) document.getElementsByTagName('html')[0].className += " macintosh";
else if (detectOS('win')) document.getElementsByTagName('html')[0].className += " windows";

(function($){
    $(document).ready(function(){
        
        // Supprimer le paragraphe ajouté automatiquement par WP autour des images
        $('p > img').each(function(){
            var $this = $(this);
            var $p= $this.parent();
            $p.before($this).remove();
        });
        
        // Slideshow des images de plus de 150x150 pixels
        $('.blog-article-preview-content img').wrap(function(){
            if ($(this).width() > 150 && $(this).height() > 150) {
                return '<a href="' + $(this).attr('src') + '" rel="lightbox[]" />';
            }
        });
        
        $('a[rel*=lightbox]').lightBox({
            overlayBgColor: '#000',
            overlayOpacity: 0.5,
            imageLoading:  '/wp-content/themes/eliance/js/jquery-lightbox/images/lightbox-ico-loading.gif',
            imageBtnClose: '/wp-content/themes/eliance/js/jquery-lightbox/images/lightbox-btn-close.gif',
            imageBtnPrev:  '/wp-content/themes/eliance/js/jquery-lightbox/images/lightbox-btn-prev.gif',
            imageBtnNext:  '/wp-content/themes/eliance/js/jquery-lightbox/images/lightbox-btn-next.gif',
            imageBlank:    '/wp-content/themes/eliance/js/jquery-lightbox/images/lightbox-blank.gif',
            containerResizeSpeed: 350,
            txtImage: 'Image',
            txtOf: 'sur'
        });
        
        // URL actuelle (sans hash ni paramètres)
        var currentHref = document.location.pathname;

        // Supprimer les sous-menus n'ayant qu'un item
    	$('#scroll-to-top').click(function(){
    	    $('body,html').animate({ scrollTop: 0 }, 500);
            return false;
    	});

        // Catégorie actuelle (blog)
        $('.current-cat a').addClass('selected');

        ///////////
        // MENUS //
        ///////////

        // Ouvrir le blog dans une nouvelle fenêtre (sauf si on est déjà sur le blog)
        if ((location.protocol+'//'+location.host+currentHref).indexOf(location.protocol+'//'+location.host+'/blog/') < 0)
    	   $('a[href^="'+location.protocol+'//'+location.host+'/blog/"]').attr('target', '_blank');

        // Supprimer les sous-menus n'ayant qu'un item
    	$('#menu-top .children,'+
    	  '.sitemap .children').each(function(){
    	    if ($(this).find('li').length < 2) {
    	        $(this).prev('a').attr('href', $(this).find('a').first().attr('href'));
    	        $(this).remove();
    	    } else {
    	        $(this).prev('a').attr('href', $(this).find('a').first().attr('href'));
    	    }
    	});

        // Supprimer les sous-menus n'ayant qu'un item
    	$('#menu-bottom .children').each(function(){
    	    if ($(this).find('li').length < 2) {
    	        $(this).prev('a').attr('href', $(this).find('a').first().attr('href'));
    	        $(this).remove();
    	    } else {
    	        $(this).prev('a').attr('href', $(this).find('a').first().attr('href'));
    	    }
	        $(this).remove();
    	});

        // Rollover menu-top
        $('#menu-top > ul > li').hover(function(){
            $(this).addClass('hover')
        }, function(){
            $(this).removeClass('hover');
        });

		// Boucler sur les liens du menu principal et appliquer 
		// la classe 'current' aux <li> de la hiérarchie affichée à l'écran
    	if (currentHref != '/') {
    		$('#menu-top a, #menu-left a').each(function(){
    			if ($(this).attr('href') == currentHref) $(this).addClass('selected');
    		});
    	}

        // Marquer avec des classes les premiers et derniers items des menus
    	$('#menu-top .page_item').first().find('a').addClass('first');
    	$('#menu-top .page_item').last().find('a').addClass('last');
    	$('#menu-top .cat-item').first().find('a').addClass('first');
    	$('#menu-top .cat-item').last().find('a').addClass('last');
    	$('#menu-left .page_item').first().find('a').addClass('first');
    	$('#menu-left .page_item').last().find('a').addClass('last');
    	$('#menu-bottom .page_item').first().find('a').addClass('first');
    	$('#menu-bottom .page_item').last().find('a').addClass('last');
    	$('#menu-top .current_page_item a,' +
    	  '#menu-left .current_page_item a').addClass('selected');

        // Faire apparaître le menu principal une fois tous les ajouts de class CSS effectués
    	$('#menu-top').fadeTo(1000, 1);

        // Validation du formulaire de contact
        window.checkForm = function(){
            if (document.contact.nom.value != "" && document.contact.societe.value != "" && document.contact.telephone.value != "") {
                return true;
            } else {
                alert("Veuillez remplir tous les champs obligatoires.");
                return false;
            }
        }

        var slidingImage = 0;
        var slideImages = $('.wd-slideshow img');
        slideImages.fadeOut(0);
        var toggleImages = function(){
            slideImages.fadeOut(250);
            setTimeout(function(){
                $(slideImages[slidingImage]).fadeIn(500)
                slidingImage++;
                if (slidingImage > slideImages.length-1) slidingImage = 0;
            }, 600);
        };
        toggleImages();
        setInterval(toggleImages, 4000);

    });
})(jQuery);

