var id_inst;
var id_inst2;

jQuery(document).ready(function(){

	$(' a.permalink').attr("href","#");
	$("p:has(img):first").css('padding' , '0');
	
	
	/* Gestion marquée & Gallery */
	$(".gallery a:has(img)").removeAttr("href");
	
	/*gestion position partenaires */
	if($('.post-471').length == 1){


		$('.post-471 img').each(function(){
			$(this).attr('id',baseName($(this).attr("src")));
		});

		
		var $target = $(window.location.hash);
		$target = $target.length && $target || $('[id=' + window.location.hash.slice(1) +']');
		if ($target.length) {
			var targetOffset = $target.offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 0);
		} 
		
	}
	

	

	/* marquee */
	
	if(($(".post-761").length == 0) && ($('.post-13').length == 0) && ($('.gallery').length > 0)){
		marqueeGallery("471");	
		marqueeGallery("761");
	}
	
	var currentItem = $("#access .current_page_item ");

	if($(currentItem).hasClass('cadeaux')){$(currentItem).find("a").css("background","url(/wp-content/themes/3suisses/images/btn-cadeaux.jpg)");}
	if($(currentItem).hasClass('cheques')){$(currentItem).find("a").css("background","url(/wp-content/themes/3suisses/images/btn-cheque.jpg)");}
	if($(currentItem).hasClass('skdo')){$(currentItem).find("a:first").css("background","url(/wp-content/themes/3suisses/images/btn-3skdo.jpg)");}
	if($(currentItem).hasClass('dispositif')){$(currentItem).find("a").css("background","url(/wp-content/themes/3suisses/images/btn-dispositif.jpg)");}
	
	var currentItem = $("#secondary .current_page_item a");
	if($(currentItem).parent().hasClass('ce')){$(currentItem).css("background","url(/wp-content/themes/3suisses/images/btn-ce.jpg)");}
	
	
	
	$('#menu-menu-footer li:first').css('border',0);
	$('.tblank a').attr("target","_blank");
	$(".menu-menu-gauche-container a").addClass("pngbg");
	
	$("#access .sub-menu >  li").prepend('<span style="color:#c4202c;float:left;line-height:24px;padding:0 5px;">&gt;</span>');

	var url = ($('#site-title a').attr('href')+"wp-content/themes/3suisses/images/");

	$('img.3skdo-web').hover(function(){
		$(this).attr("orig",$(this).attr("src"));
		$(this).attr("src",url+"btn1-hover.png");
	},function(){
		$(this).attr("src",$(this).attr('orig'));
	});
	$('img.3skdo-catalogue').hover(function(){
		$(this).attr("orig",$(this).attr("src"));
		$(this).attr("src",url+"btn3-hover.png");
	},function(){
		$(this).attr("src",$(this).attr('orig'));
	});
	$('img.3skdo-pass').hover(function(){
		$(this).attr("orig",$(this).attr("src"));
		$(this).attr("src",url+"btn2-hover.png");
	},function(){
		$(this).attr("src",$(this).attr('orig'));
	});
	

	//$('#marquee').wrap("<marquee id='metiers' direction='right' behavior='alternate' scrollamount='2'>");
	//$('marquee#metiers').wrap('<div id="container-marquee">');
	if($("#marquee").length >0){
	$('#marquee')
		.css("position","absolute")
		.css("overflow","hidden")
		.wrap('<div id="container-marquee">');
		
	$("#container-marquee")
		.css("height","20px")
		.css("line-height","1.5em")
		.css("position","relative")
		.css("width","750px;")
		.css("overflow","hidden");
	
	marqueeText('#container-marque',"#marquee",100);
	
	$("#container-marquee").append("<div class='gradient-img bgpng'>");
	}
	if(($.browser.msie)&(parseInt($.browser.version)<7)){
		$("img[src$='.png']").each(function(){$(this).addClass("png");});
	}
	
	$("li.lock > a").removeAttr("href").css('color','#999').css("text-decoration","none");
	$("img").removeAttr("title");
	
	
	
	/* event contact form */

		$(".dynamic").hide();
		$('.dynamic input').removeClass('wpcf7-validates-as-required');
		if($(".part-contact .chk-1 input:checked").length == 1)
			$('.email.dynamic').show().find('input').addClass('wpcf7-validates-as-required');
		
		if($(".part-contact .chk-2 input:checked").length == 1)
			$('.adresse.dynamic').show().find('input').addClass('wpcf7-validates-as-required');
		
		if($(".part-contact .chk-3 input:checked").length == 1)
			$('.message.dynamic').show().find('textarea').addClass('wpcf7-validates-as-required');
	
		
		

	$(".part-contact .chk-1").click(function(){
		if($(".part-contact .chk-1 input:checked").length == 1){	
			$('.email.dynamic').slideDown().find('input').addClass('wpcf7-validates-as-required');
		}else{
			$('.email.dynamic').slideUp().find('input').removeClass('wpcf7-validates-as-required');
		}
	});
	$(".part-contact .chk-2").click(function(){
		if($(".part-contact .chk-2 input:checked").length == 1){	
			$('.adresse.dynamic').slideDown().find('input').addClass('wpcf7-validates-as-required');
		}else{
			$('.adresse.dynamic').slideUp().find('input').removeClass('wpcf7-validates-as-required');
		}

	});
	
	$(".part-contact .chk-3").click(function(){
		if($(".part-contact .chk-3 input:checked").length == 1){	
			$('.message.dynamic').slideDown().find('textarea').addClass('wpcf7-validates-as-required');
		}else{
			$('.message.dynamic').slideUp().find('textarea').removeClass('wpcf7-validates-as-required');
		}
	});
	
	
	

});

function getOverlay(id){
	$("."+id).modal({
	opacity:80,
	overlayCss: {backgroundColor:"#000"}
	});
}
function getOverlayContact(id){
		$('.modal-contact').append( '<div class="close"><a href="#" class="simplemodal-close">Fermer X</a></div>');
		$(".modal-contact").modal({
						opacité:80,
						overlayCss: {backgroundColor:"#000"}
					});
}


function checkEmail(email) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {
		return false;
	}else{
		return true;
	}
}


function baseName(str)
{

   var base = new String(str).substring(str.lastIndexOf('/') + 1); 
    if(base.lastIndexOf(".") != -1)       
       base = base.substring(0, base.lastIndexOf("."));
   return base;
}


function getReseau(dept){


	if(dept==""){
		var	dept = $('input#codep').val();

		if((dept.length >5) || (isNaN(dept))){
			alert("Code postal non valide");	
			return;
		}		
	}
	$('.reseau-content').fadeOut("500");
	$('.reseau-content').each(function(){
		if(dept.length>2)
			dept = dept.substr(0,2);
		if($(this).hasClass(dept)){
			$(this).fadeIn("500");
		}
	});
	
	
}

function marqueeGallery(id){

		var elt = jQuery(".galleryid-"+id).addClass("go-left");
		jQuery(elt).addClass("marquee");
		jQuery(elt).wrap('<div class="container-gallery container-gallery-'+id+' ">').wrap("<div class='marquee-wrapper marquee-wrapper-"+id+"'>");
		jQuery('.marquee-wrapper-'+id).before("<span class='leftarrow pngbg'></span>");
		jQuery('.marquee-wrapper-'+id).after("<span class='rightarrow pngbg'></span>");
		
		jQuery('.marquee-wrapper-'+id).find("img").show();
	
		jQuery('.galleryid-471 a').each(function(){
			if($(this).find('img').attr("src"))
				$(this).attr('href','/partenaires#'+baseName($(this).find('img').attr("src")));
		});
		
	
		marquee('.marquee-wrapper-'+id,".galleryid-"+id,100,id);
		$(".galleryid-"+id).animate({left : "-=10px"});
		$('.container-gallery-'+id).find(".marquee-wrapper").hover(function(){
			if(id=="471")
				clearTimeout(id_inst);
			else
				clearTimeout(id_inst2);
//	alert(1);				
		},function(){
			marquee('.marquee-wrapper-'+id,".galleryid-"+id,100,id);
		});					
		$('.container-gallery-'+id+' .leftarrow').click(function(){
			$(this).parent().find(".marquee").animate({left : "-=100px"});
		});
		$('.container-gallery-'+id+' .rightarrow').click(function(){
			$(this).parent().find(".marquee").animate({ left : "+=100px"});
		});	

}




function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};












/* old */


/* MArquee Jquery */
function marquee(idWrapper,idMarquee,vitesse,idgallery)
/*
* idWrapper : l'identifiant du div autour du span contenant le texte à faire défiler
*	idMarquee : l'identifiant du span autour du texte
*	vitesse : nombre de millisecondes entre chaque déplacement de 1px
*/
{


	var oIdWrapper=$(idWrapper);
	var oIdMarquee=$(idMarquee);

	var width=oIdMarquee.width();
	var width2=oIdWrapper.width();

	var marge = (width2-60);
	
	if(idgallery=="471")
		id_inst=setTimeout(function() {marquee(idWrapper,idMarquee,vitesse,idgallery)},vitesse);
	else
		id_inst2=setTimeout(function() {marquee(idWrapper,idMarquee,vitesse,idgallery)},vitesse);

	var l=parseInt(oIdMarquee.css('left'));
	
	if(oIdMarquee.hasClass('go-left')){

		oIdMarquee.css({left:(l-2)+'px'});	
		//alert((-parseInt(oIdMarquee.css('left')))+" - "+(width-650));
		if((-parseInt(oIdMarquee.css("left")))>=(width-(width2-100))){
		//if((wWidth-wLeft) >= (limitLeft)){
			$(oIdMarquee).fadeOut(function(){
				oIdMarquee.css({left:(width2-650)+'px'},1000);
				$(oIdMarquee).fadeIn();
			});
			

			//oIdMarquee.addClass("go-right");
			//oIdMarquee.removeClass("go-left");

		}	
	}
	else{
		oIdMarquee.css({left:(l+2)+'px'});
		if(-parseInt(oIdMarquee.css('left')) <= 0){

			oIdMarquee.addClass("go-left");
			oIdMarquee.removeClass("go-right");

		}
	}
}



function marqueeText(idWrapper,idMarquee,vitesse)
/*
* idWrapper : l'identifiant du div autour du span contenant le texte &agrave; faire d&eacute;filer
*	idMarquee : l'identifiant du span autour du texte
*	vitesse : nombre de millisecondes entre chaque d&eacute;placement de 1px
*/
{


	var oIdWrapper=$(idWrapper);
	var oIdMarquee=$(idMarquee);

	var width=oIdMarquee.width();
	var width2=oIdWrapper.width();

	

	id_inst_mText=setTimeout(function() {marqueeText(idWrapper,idMarquee,vitesse)},vitesse);


	var l=parseInt(oIdMarquee.position().left);
	
	

		oIdMarquee.css({left:(l-2)+'px'});	
		//alert((-parseInt(oIdMarquee.css('left')))+" - "+(width-650));
		if((-parseInt(oIdMarquee.css("left")))>=(width)){
		//if((wWidth-wLeft) >= (limitLeft)){
			$(oIdMarquee).fadeOut(function(){
				oIdMarquee.css({left:(width2-650)+'px'},1000);
				$(oIdMarquee).fadeIn();
			});
			

			//oIdMarquee.addClass("go-right");
			//oIdMarquee.removeClass("go-left");

		}	
	
}




