var j = jQuery.noConflict();
j(document).ready(function(){

//home > fond ecran > au rollover affichage des formats a telecharger
/*
	j('#footer_carousel .carousel li a').hover(
		function () {
			j(this).next().slideDown();
		}, 
		function () {
		   setTimeout(function() {j('.fondecran').fadeOut();}, 3000);
	});
*/

// PERSONNALISATION > Changement du background

	j("#personnalisation #perso_bloc_2 a").click(function () {
		var src = j(this).find('img').attr('src');
		switch(src){
			case 'images/fonds-ecran/fondecran1.jpg': j('body').css('background', 'url(images/bg.jpg) no-repeat top center #000');break;	
			case 'images/fonds-ecran/fondecran6.jpg': j('body').css('background', 'url(images/bg5.jpg) no-repeat top center #000');break;
			case 'images/fonds-ecran/fondecran2.jpg': j('body').css('background', 'url(images/bg3.jpg) no-repeat top center #000');break;
			case 'images/fonds-ecran/fondecran7.jpg': j('body').css('background', 'url(images/bg6.jpg) no-repeat top center #000');break;
			case 'images/fonds-ecran/fondecran9.jpg': j('body').css('background', 'url(images/bg2.jpg) no-repeat top center #000');break;
			case 'images/fonds-ecran/fondecran8.jpg': j('body').css('background', 'url(images/bg4.jpg)');break;
			default:j('body').css('background', 'url(images/bg4.jpg)');
		}
		
		j.ajax({
			type: "POST",
			url: "_ajax_changement_background.php",
			data: "src="+src,
			success: function(html){
			}
		});
		
		return false;
    });



// gestion personnalisation
    j("#personnalisation .bloc_onoff a").click(function () { 
        var src = j(this).find('img').attr('src');

        if(src == 'images/header/personnalisation-page.png') {
            j(this).find('img').attr('src', 'images/header/personnalisation-page-on.png');
            j("#personnalisation .bloc").slideDown();
        }
        
        if(src == 'images/header/personnalisation-page-on.png') {
            j(this).find('img').attr('src', 'images/header/personnalisation-page.png');
            j("#personnalisation .bloc").slideUp();
        }
    });
	
// Gestion home : partenaires defilement
	j('#partenaires2').innerfade({ animationtype: 'fade', speed: 'slow', timeout: 4000, type: 'sequence'}); 

// Gestion home : actus interview video
	
	//j("#featured > ul").tabs();
	j("#featured > ul").tabs({fx:{opacity: "toggle", duration:'fast'}}).tabs("rotate",4000, false);

// Gestion de l'inscription a la newsletter via le header
												 
	j("#header_newsletter form").submit(function(){ 
		j.ajax({
			type: "POST",
			url: "_ajax_insc_newsletter.php",
			data: "email="+j("#newsletter_email").val(),
			success: function(html){
				j("#newsletter_email").val(html);
			}
		});
	
		return false;
	});
	
	j("#newsletter_email").click(function(){
		j("#newsletter_email").val("");
	});
	
// Gestion tri joueurs page joueurs
	
	//tri par nom
	/*
	j("#joueur_rech_2 a").click(function(){ 
		
		var src = j("#joueur_rech_2 a").find('img').attr('src');
        if(src == 'images/int/joueur_rech_2.png') {
            j("#joueur_rech_2 a").find('img').attr('src', 'images/int/joueur_rech_2_asc.png');
			j("#joueur_rech_3 a").find('img').attr('src', 'images/int/joueur_rech_3.png');
			var order="asc";
        }
		if(src == 'images/int/joueur_rech_2_desc.png') {
            j("#joueur_rech_2 a").find('img').attr('src', 'images/int/joueur_rech_2_asc.png');
			j("#joueur_rech_3 a").find('img').attr('src', 'images/int/joueur_rech_3.png');
			var order="asc";
        }
		if(src == 'images/int/joueur_rech_2_asc.png') {
            j("#joueur_rech_2 a").find('img').attr('src', 'images/int/joueur_rech_2_desc.png');
			j("#joueur_rech_3 a").find('img').attr('src', 'images/int/joueur_rech_3.png');
			var order="desc";
        }
        
		j("div.fiche_joueur").tsort("div.fiche_joueur_nom",{order:order});
		return false;
	});
	*/
	
	//tri par classement
	j("#joueur_rech_3 a").click(function(){ 
		
		var src = j("#joueur_rech_3 a").find('img').attr('src');
        if(src == 'images/int/joueur_rech_3.png') {
            j("#joueur_rech_3 a").find('img').attr('src', 'images/int/joueur_rech_3_asc.png');
			j("#joueur_rech_2 a").find('img').attr('src', 'images/int/joueur_rech_2.png');
			var order="asc";
        }
		if(src == 'images/int/joueur_rech_3_desc.png') {
            j("#joueur_rech_3 a").find('img').attr('src', 'images/int/joueur_rech_3_asc.png');
			j("#joueur_rech_2 a").find('img').attr('src', 'images/int/joueur_rech_2.png');
			var order="asc";
        }
		if(src == 'images/int/joueur_rech_3_asc.png') {
            j("#joueur_rech_3 a").find('img').attr('src', 'images/int/joueur_rech_3_desc.png');
			j("#joueur_rech_2 a").find('img').attr('src', 'images/int/joueur_rech_2.png');
			var order="desc";
        }
        
		j("div.fiche_joueur").tsort("div.fiche_joueur_classement span",{order:order});
		return false;
	});

	
// Gestion joueurs home
	
  //a partir de la slide barre verticale
  j("#home_joueurs_liste ul li a").click(function(){ 
		j.ajax({
			type: "POST",
			url: "_ajax_aff_img_joueur.php",
			data: "url="+j(this).attr('href'),
			success: function(html){
				j("#img_joueur").attr('src',html);
			}
		});
		
		j.ajax({
			type: "POST",
			url: "_ajax_aff_txt_joueur.php",
			data: "url="+j(this).attr('href'),
			success: function(html){
				j("#txt_joueur").empty();
				j("#txt_joueur").append(html);
			}
		});
		
		return false;
  });
  
  
  //a partir de la liste deroulante	
  j("#choix_joueur").change(function(){ 
		j.ajax({
			type: "POST",
			url: "_ajax_aff_img_joueur.php",
			data: "id="+j("#choix_joueur").val(),
			success: function(html){
				j("#img_joueur").attr('src',html);
			}
		});
		
		j.ajax({
			type: "POST",
			url: "_ajax_aff_txt_joueur.php",
			data: "id="+j("#choix_joueur").val(),
			success: function(html){
				j("#txt_joueur").empty();
				j("#txt_joueur").append(html);
			}
		});
  });

// Gestion comparateur de joueur face a face

  j("select#choix_faf_joueur1").change(function(){ 
		j.ajax({
			type: "POST",
			url: "_ajax_aff_img_joueur_faf.php",
			data: "id="+j("#choix_faf_joueur1").val(),
			success: function(html){
				j("#img_faf_joueur1").attr('src',html);
			}
		});
		j.ajax({
			type: "POST",
			url: "_ajax_aff_txt_joueur_faf.php",
			data: "id="+j("#choix_faf_joueur1").val(),
			success: function(html){
				j("#home_faceaface_comparez_gauche").empty();
				j("#home_faceaface_comparez_gauche").append(html);
			}
		});
  });
  
  j("select#choix_faf_joueur2").change(function(){ 
		j.ajax({
			type: "POST",
			url: "_ajax_aff_img_joueur_faf.php",
			data: "id="+j("#choix_faf_joueur2").val(),
			success: function(html){
				j("#img_faf_joueur2").attr('src',html);
			}
		});
		j.ajax({
			type: "POST",
			url: "_ajax_aff_txt_joueur_faf.php",
			data: "id="+j("#choix_faf_joueur2").val(),
			success: function(html){
				j("#home_faceaface_comparez_droite").empty();
				j("#home_faceaface_comparez_droite").append(html);
			}
		});
  });




// Gestion du deroule actus interviews audio sur la home

    j("#header-offon a").click(function () { 
        var src = j(this).find('img').attr('src');

        if(src == 'images/home/header-on.png') {
            j(this).find('img').attr('src', 'images/home/header-off.png');
            j(this).parent().prev().prev().slideDown();
        }
        
        if(src == 'images/home/header-off.png') {
            j(this).find('img').attr('src', 'images/home/header-on.png');
            j(this).parent().prev().prev().slideUp();
        }
    });
	
// Gestion de accordion live sur la home

	j('#home_live_blocs').hoverAccordion({
		activateitem: '1',
		speed: 'fast'
	});

// Gestion liste deroulante joueurs sur la home

	j('#home_joueurs_carousel').jcarousel({
        vertical: true,
        scroll:3,
		visible:4
    });
    

	
// Gestion des carousels
    j('.newCarousel').each(function(){
        var animationActive = false;
        var idCarousel = '#'+j(this).attr('id')+' ';

        var elementWidth = j(idCarousel + '.carousel li').width();
        var numberElements = j(idCarousel + '.carousel li').length;
		var c;
		var d;
        j(idCarousel + '.carousel').css('width', (numberElements*2)*elementWidth).wrap(document.createElement('div')).parent().addClass('jcarousel-clipp');
        
        // Gestion de defilement arriere
        j(idCarousel + '.carouselPrev a').bind('click', function(){
            if(animationActive != true) {
                animationActive = true;
                
                j(idCarousel + '.carousel li:last').clone().insertBefore(idCarousel + '.carousel li:last');
                j(idCarousel + '.carousel li:last').insertBefore(idCarousel + '.carousel li:first');
                
                var marginEnd = (j(idCarousel + '.carousel').css('marginLeft'));
                var marginBegin = marginEnd.replace(/px/, '');
                marginBegin = marginBegin-elementWidth;
                marginBegin = marginBegin+'px';
                
                j(idCarousel + '.carousel').css('marginLeft', marginBegin);
                j(idCarousel + '.carousel').stop().animate({'marginLeft': marginEnd}, 'slow', function() {
                    j(idCarousel + '.carousel li:last').remove();
                    animationActive = false
                });
            }

			j('.navCarousel li.hidden').removeClass('hidden');
			j('.navCarousel ul li:last]').clone().insertBefore('.navCarousel ul li:first');
			j('.navCarousel ul li:last]').insertBefore('.navCarousel ul li:first');			
			j('.navCarousel ul li:last]').addClass('hidden');
            return false;
        });
        
        // Gestion de defilement avant
        j(idCarousel + '.carouselNext a').bind('click', function(){
            if(animationActive != true) {
                animationActive = true;
                
                j(idCarousel + '.carousel li:first').clone().insertAfter(idCarousel + '.carousel li:first');
				j(idCarousel + '.carousel li:first').insertAfter(idCarousel + '.carousel li:last');
                
                var marginEnd = (j(idCarousel + '.carousel').css('marginLeft'));
                var marginBegin = marginEnd.replace(/px/, '');
                marginBegin = marginBegin-elementWidth;
                marginBegin = marginBegin+'px';
                
                j(idCarousel + '.carousel').stop().animate({'marginLeft': marginBegin}, 'slow', function() {
                    j(idCarousel + '.carousel').css('marginLeft', marginEnd);
                    j(idCarousel + '.carousel li:first').remove();
                    animationActive = false
                });
            }
			
            return false;
        });
		
		
		// Gère l'affichage des item de la navigation du carousel
		j('.navCarousel ul li a').click(function(){
			var idNav = j(this).attr('href');
			if(j('.navCarousel ul li').hasClass('hidden')){
				j('.navCarousel ul li').removeClass('hidden');
			}
			j(this).parent().addClass('hidden');
			j(this).parent().insertAfter('.navCarousel ul li:last');
			j(idCarousel + ".carousel " + idNav).insertBefore(idCarousel + '.carousel li:first');
			
			return false;
		});	
		
        
        // Autoslide
        if (j(this).is('.autoSlide')){
			var a=0;
			var b;
			var c;
            var interval = setInterval(function() {
                j(idCarousel + '.carouselNext a').click();
				b = j('.newCarousel ul li:eq(1)').attr('id');
				j('.navCarousel li').removeClass('hidden');
				j('.navCarousel ul li a[href$="'+b+'"]').parent().addClass('hidden').insertAfter('.navCarousel ul li:last');
            }, 4000);
			j(idCarousel + '.carousel').bind('mouseover', function(){ clearInterval(interval); });
			j('.navCarousel').bind('mouseover', function(){ clearInterval(interval); });		  
        };		
	});

	function navCarouselUpdate(){
		b = j('.newCarousel ul li:eq(0)').attr('id');
		j('.navCarousel li').removeClass('hidden');
		j('.navCarousel ul li a[href$="'+b+'"]').parent().addClass('hidden').insertAfter('.navCarousel ul li:last');
	}
		
});



/************ DATE HEURE ************/
function tS(){
    x=new Date();
    x.setTime(x.getTime());
    return x;
}
function lZ(x){
    return (x>9)?x:'0'+x;
}
function dT(){
    if(fr==0){
        fr=1;
        document.write(" " + '<span id="tP">'+eval(oT)+'</span>');
    }
    document.getElementById("tP").innerHTML =  eval(oT);
    setTimeout('dT()',1000);
}
var fr=0;
oT="tS().getHours()+':'+lZ(tS().getMinutes())+':'+lZ(tS().getSeconds())";
/************ FIN DATE HEURE ************/


//----------------- Live scoring --------------------------------
function openLSWindowTandR(year, wkno, lang, tabno, eventid, ref_file, width, height, hasScrollBars) {
	// ADD NAME FIELD and make sure it get's focus!!!
	var theWidth = width;
	var theHeight = height;
	var scrollBars = "scrollbars";
	if (hasScrollBars == false) scrollBars = "scrollbars=0";
	if ((theWidth == "")||(theWidth == null)) theWidth =500;
	if ((theHeight == "")||(theHeight == null)) theHeight =668;
	var theLeft = (screen.availWidth - theWidth)/2;
	var theTop = (screen.availHeight - theHeight)/2;
	var strCheckRef = escape(ref_file);

	var lsURL = "http://www.protennislive.com/frameset.asp?year="+year+"&wkno="+wkno+"&lang="+lang+"&tabno="+tabno+"&eventid="+eventid+"&ref="+strCheckRef;
	var popupWin = window.open(lsURL, '_' + Math.round(Math.random() * 1000000),'top='+theTop+',left='+theLeft+',menubar=0,toolbar=0,location=0,directories=0,status=0,'+scrollBars+',width='+theWidth+', height='+theHeight);
}