var debut_scroll_Y = false;
var debut_marginTop = 0;
var hauteur_scrollbar_auto = 100;

function fix_scroll_auto() {
	if(
		(navigator.userAgent.match(/iPhone|iPod|iPad/i))
	) {
		$(".scroll_auto").each(function() {
			var contenu = $(this).html();
			var start_time = 0;
			var last_decal = 0;
			var rapport = 1;
			var hauteur_max = 100;
			var hauteur_boite = 100;
			
			if ($(this).children(".wrapper").width() > 0) {
			} else {
				$(this)
					.html( "<div class='scrollbar_auto' style='position: absolute; top: 0px; right: 3px; -webkit-box-shadow: 0px 0px 2px rgba(255,255,255, 0.3); width: 5px; height: 100px; -webkit-border-radius: 3px; background-color: rgba(0,0,0,0.5); z-index: 100; display: none; '></div>"
								+ "<div class='wrapper' style='height: auto; -webkit-transition-property: margin-top; -webkit-transition-duration: 0.2s; -webkit-transition-timing-function: ease-out;'>" 
								+ contenu + "<div style='clear: both;'></div></div>"
								+ "<div class='bidon' style='position: absolute; opacity: 0;'></div>"
							)
					.css("position", "relative")
					.css("overflow", "hidden");
					
			}
	
			this.ontouchstart = function(evt) {
				debut_scroll_Y = evt.touches[0].pageY;
				var now = new Date().getTime();
			    start_time = parseInt(now, 10);
				last_decal = 0;
				debut_marginTop = parseInt($(this).children(".wrapper").css("marginTop"));
				
				rapport = $(this).outerHeight() / $(this).children(".wrapper").outerHeight();
				
				if (rapport < 1) {
					$(this).children(".scrollbar_auto").fadeIn();
					hauteur_scrollbar_auto = Math.floor(rapport * $(this).outerHeight());
					$(this).children(".scrollbar_auto").height(hauteur_scrollbar_auto);
				}
				hauteur_max = $(this).children(".wrapper").outerHeight() - $(this).outerHeight();
				hauteur_max = -1 * hauteur_max;
				hauteur_boite = $(this).outerHeight();
				
			};
			
			this.ontouchmove = function(event) {
				if (rapport > 1) return false;
				
				var y = event.touches[0].pageY;
				
				var decal = y - debut_scroll_Y;
				last_decal = decal;
				decal = decal + debut_marginTop;
				
				
				if (decal > 0) decal = 0;
				if (decal < hauteur_max) decal = hauteur_max;
				$(this).children(".wrapper").css("marginTop", decal);
				// Hack bizarre: dans certains cas, il faut ecrire quelque chose quelquepart pour que le scroll fonctionne en direct
				$(this).children(".bidon").html("bidon");
				
				var rapport_pos = (decal / hauteur_max);
				var vide = Math.round( rapport_pos * (hauteur_boite - hauteur_scrollbar_auto));
				
				$(this).children(".scrollbar_auto").css("marginTop", vide);
				
				
				return false;
									
			};
			
			this.ontouchend = function(event) {
				var now_end = new Date().getTime();
			    var end_time = parseInt(now_end, 10);
			    
			    var duree = end_time - start_time;
			
				var decal = Math.round(last_decal * ( 1 + 200/duree));
				decal = decal + debut_marginTop;
				
				
				if (hauteur_max < 0) {
					
					if (decal > 0) decal = 0;
					if (decal < hauteur_max) decal = hauteur_max;
					
					$(this).children(".wrapper").css("marginTop", decal);
					// Hack bizarre: dans certains cas, il faut ecrire quelque chose quelquepart pour que le scroll fonctionne en direct
					$(this).children(".bidon").html("bidon");
				}
			}
		});
	}
}



jQuery(document).ready(function() {

//menu gauche
			var duration = 500;

			jQuery('.menug-el li').hoverIntent(
				function(){ jQuery(this).find('div.jq-bulle:hidden').show("blind", { direction: "horizontal"}, 500);
				   },

				function(){ jQuery(this).find('div.jq-bulle:visible').hide(); //.fadeOut(50)
				   }
			);

			jQuery('.menug-el li a').hover(
				function(){ 
				$(this).children("span").fadeIn(duration);   },

				function(){
				$(this).children("span").hide();   }  //.fadeOut(300)
			);


//clavier
			jQuery('.clavier-el li').hoverIntent(
				function(){ var tpo= jQuery(this).find('div.accueil-bulle:hidden');
					    if (tpo.attr("name")=="cld") tpo.show("blind", { direction: "horizontal"}, 500);	
						if (tpo.attr("name")=="clb") tpo.show("blind", { direction: "vertical"}, 500);
						if (tpo.attr("name")=="clh") tpo.show("blind", { direction: "horizontal",mode:"show"}, 500);//tpo.show("clip", { direction: "vertical"}, 500);
						if (tpo.attr("name")=="clg") tpo.show("blind", { direction: "horizontal",mode:"show"}, 500);
				   },

				function(){ jQuery(this).find('div.accueil-bulle:visible').hide(); //.fadeOut(50)
				   }
			);

			jQuery('.clavier-el li').hover(
				function(){ 
				$(this).children("span").fadeIn(duration);   },

				function(){
				$(this).children("span").hide();   }  //.fadeOut(300)
			);

// menu top
	$("ul#topnav li").hoverIntent(function() { //Hover over event on list item
		//$(this).css({ 'background' : '#1376c9 url(topnav_active.gif) repeat-x'}); //Add background color and image on hovered list item
		var apo= $(this).find("span"); //Show the subnav
		//apo.fadeIn(500);
		apo.show();
		//apo.show("blind", { direction: "horizontal"}, 700);



	} , function() { //on hover out...
		//$(this).css({ 'background' : 'none'}); 
		var apo= $(this).find("span");
		//apo.fadeOut(100);
		apo.hide();

	});

		$("#accordion").accordion({active:false,animated:false,autoheight:false});

//Shawdobox
            var xHeight = jQuery(document).height();
            var xWidth = jQuery(window).width();

             //Dimensionnement du masque recouvrant la page
             //jQuery('#page').css({'width':xWidth,'height':xHeight});
             //Apparition du masque
             //jQuery('#page').show();//fadeIn();

//Attribution à celui-ci d’une transparence de
//façon à laisser la page légèrement visible     

             //jQuery('#page').fadeTo("fast",0.6);    
             var xH = jQuery(window).height();
             var xW = jQuery(window).width();

     //Centrage de la shadow box
     //jQuery("#dialog").css('top', xH/2-jQuery("#dialog").height()/2);
     //if($.browser.msie) jQuery("#dialog").css('left', xW/2-jQuery("#dialog").width()/2);

     //Apparition de la shadow box
//jQuery("#dialog").fadeIn();


jQuery('.voirarguslefilm').click(function () {
        jwplayer().stop();

	jQuery('.window').show();
	jwplayer().playlistItem(0);//when the popup is already opened
	jwplayer().onReady(function(){ jwplayer().playlistItem(0); });//when the popup is closed, must wait onready
             });


jQuery('.window .close').click(function () {
            //jQuery('#page').hide();
            //jQuery('.window').remove();
        jwplayer().stop();	//jwplayer().playlistItem(1);

	//jwplayer().onReady(function(){ jwplayer().playlistItem(1);
					//jwplayer().pause();
	// });


	jQuery('.window').hide();
             });

jQuery('.window2 .close').click(function () {
        jwplayer().stop();
        jQuery('.window2').hide();
             });

jQuery("#videovisu").click(function () {
	jQuery("#dialog2").fadeIn();
            //jQuery('.window').remove();
             });

jQuery("#videoculturerp").click(function () {
        //jwplayer().stop();
	jQuery('.window').show();
//jwplayer().load({file:"http://91.121.81.97/~argusvid/culture-rp-480x270_baseline.mp4",image:"jwplayer/image-rp.jpg"});
	jwplayer().playlistItem(1);
	jwplayer().onReady(function(){ jwplayer().playlistItem(1); });
});

jQuery("#videopolerh").click(function () {
	jQuery('.window').show();
	jwplayer().playlistItem(2);
	jwplayer().onReady(function(){ jwplayer().playlistItem(2); });
});


//Rollover du bouton visualiser
$("#videovisu").hoverIntent(function() {
		var apo= $(this).find("span"); //Show the subnav
		apo.show();
	} , function() { //on hover out...
		//$(this).css({ 'background' : 'none'}); 
		var apo= $(this).find("span");
		apo.hide();

	});

fix_scroll_auto();

		});


$(document).bind("touchend touchcancel", function(){
        $(".scrollbar_auto").fadeOut();
});

function checkemail(email) {
if( email.value.indexOf( '.', 0 )==-1 ) return false;
var pos = email.value.indexOf( '@', 0 ); if( pos==-1 ) return false;
if( email.value.indexOf( '@', pos+1 ) != -1) return false;
return true;}

function Etrecontacte(fm)
{
fields = {first_name:"prénom",last_name:"nom",company:"société",zip:"code postal",city:"ville",email:"email",phone:"téléphone"};
for (var i in fields) { if (fm[i].value.length==0) {alert('Vous devez entrer votre '+ fields[i]);fm[i].focus();return false;} }
if (checkemail(fm.email)==false) {alert('L\'email n\'est pas valide !');fm.email.select();fm.email.focus();return false;}
if (fm['00ND0000003Cbye'].value=='') {alert('Vous devez entrer votre fonction');return false;}
if (fm['00ND0000003Cbyt'].value=='') {alert('Vous devez entrer votre secteur d\'activité');return false;}
return true;
 }

function inscription_culturerp(fm)
{
fields = {societe:"Société",nom:"nom",prenom:"prénom",email:"email"};
for (var i in fields) { if (fm[i].value.length==0) {alert('Vous devez entrer votre '+ fields[i]);fm[i].focus();return false;} }
if (checkemail(fm.email)==false) {alert('L\'email n\'est pas valide !');fm.email.select();fm.email.focus();return false;}
return true;
 }

function inscription_journalistes(fm)
{
fields = {nom:"nom",prenom:"prénom",telephone:"téléphone",email:"email",support:"support",rubrique:"Rubrique rédactionnelle"};
for (var i in fields) { if (fm[i].value.length==0) {alert('Vous devez entrer votre '+ fields[i]);fm[i].focus();return false;} }
if (checkemail(fm.email)==false) {alert('L\'email n\'est pas valide !');fm.email.select();fm.email.focus();return false;}
return true;
 }
