// JavaScript Document
/********  FUNCTION GENERIQUE AJAX   ***********/

//affiche un message html dans une div
//data : un objet contenant une valeur 'msg' au format html
function see_response (data, div_response){
//	alert ('see_response'+data.msg+' div_response='+div_response);
	if (data.success == '1') {
			
		$(div_response).removeClass();
		

		if (data.cssclass) {			
			if (data.cssclass != 'none') {
				$(div_response).addClass(data.cssclass);
			}
		} else {			
			$(div_response).addClass('success');
		}
		
	} else if (data.success == '0'){
		
		$(div_response).removeClass();
		
		if (data.cssclass) {	
			if (data.cssclass != 'none') {
				$(div_response).addClass(data.cssclass);
			}
		} else {
			$(div_response).addClass('error');
		}
		
		
	} 
	
	$(div_response).html(data.msg);$(div_response).show();
	$(div_response).hide();
	$(div_response).fadeIn(500);
	
	
}








// url_file : nom du fichier à qui envoyer les variables
// datas : variables encodées en JSON
// div_response : id de l'élément HTML ou doit s'afficher la réponse
// func_after : String ou Array de noms fonction(s) à exécuter en cas de success. 
//              Cette ou ces fonctions reçevront en argument data (la réponse du serveur au format json)
//														     et div_response : ID de la div ou affiher

function ajax_send (url_file, datas, div_response, funct_after) {
	$.ajax({ // fonction permettant de faire de l'ajax
		   type: "POST", // methode de transmission des données au fichier php
		   url: url_file, // url du fichier php
		   data: datas, // données à transmettre	
		   dataType :"json",
		   beforeSend: function () {
			    $(div_response).removeClass();
				$(div_response).html('<img src="images/loader.gif">');
			},
		   
		   success: function(data){ // si l'appel a bien fonctionné
				//on execute la fonction prévue en success
				
				if (typeof(funct_after)=="object") {
					//alert ('array : '+funct_after);
					for (x in funct_after) {
						//alert (funct_after[x]);
						if (window[funct_after[x]]){
							window[funct_after[x]](data, div_response);
						}	
					}					
				} else {
						//alert ('string : '+funct_after);
						if (window[funct_after]){
							window[funct_after](data, div_response);
						}						
				}
				
				
		   },
			error: function () {
			   alert ('AJAX error');
			   see_response (data, div_response);
		   }
		});
}



/**** fonctions pour le slider de la home */

// JavaScript Document

function newsscoller(prev) {

	//Get the current selected item (with selected class), if none was found, get the first item
	var current_image = $('#gallery li.selected').length ? $('#gallery li.selected') : $('#gallery li:first');
	var current_excerpt = $('#excerpt li.selected').length ? $('#excerpt li.selected') : $('#excerpt li:first');

	//if prev is set to 1 (previous item)
	if (prev) {
		
		//Get previous sibling
		var next_image = (current_image.prev().length) ? current_image.prev() : $('#gallery li:last');
		var next_excerpt = (current_excerpt.prev().length) ? current_excerpt.prev() : $('#excerpt li:last');
	
	//if prev is set to 0 (next item)
	} else {
		
		//Get next sibling
		var next_image = (current_image.next().length) ? current_image.next() : $('#gallery li:first');
		var next_excerpt = (current_excerpt.next().length) ? current_excerpt.next() : $('#excerpt li:first');
	}

	//clear the selected class
	$('#excerpt li, #gallery li').removeClass('selected');
	
	//reassign the selected class to current items
	next_image.addClass('selected');
	next_excerpt.addClass('selected');

	//Scroll the items
	$('#mask-gallery').scrollTo(next_image, 800);		
	$('#mask-excerpt').scrollTo(next_excerpt, 800);					
	
}

function goto(item) {
   // console.log('goto:'+item);
	$('#mask-gallery').scrollTo(item, 800);		
	$('#mask-excerpt').scrollTo(item, 800);	
	$(item).addClass('selected');					
}

/*********************************************/





 $(document).ready(function() {
		
		//alert ('yo');
    
    
     
     
     
		// pour rendre tous les tableaux qui ont la class tablesorter classables
		$("table.tablesorter").tablesorter(); 
		
		// pour rendre un tableau searchable

	   $('table.tablesearch tbody tr').quicksearch({
		  position: 'before',
		  attached: 'table.tablesearch',
		  stripeRowClass: ['odd', 'even'],
		  labelText: 'Filtrer dans ce tableau'
		});
	
	
		 //pour créer les tooltip avec jquerytools
		 //http://flowplayer.org/tools/tooltip/index.html
		 $(".tooltiped").tooltip(
     { 
     position: "top center", 
     opacity: 1  ,
     effect: 'slide',
	   slideOffset: 10,
	   bounce: true
     }
     );
	
	    $('#autres_sites').change(function(){
			//alert('url = ' + this.value );
			window.location.href = this.value;
		  })
	 
	    
      $('table.magic').each(function(){
 //switch unntableau avec une ligne et 2 cases
 //la case 2 est masquée
 //au passage de la souris elle apparait et la case 1 est masquée           
	             
           $("td:eq(1)", this).attr('style','color:#cc0000;');
           
           cel0 = $("td:eq(0)", this) ;
           cel1 = $("td:eq(1)", this) ;
           
             $("td:eq(1)", this).attr('style','display:none;'); 
             
             $("td:eq(0)", this).mouseenter(function(){              
                 
                    $(this).parent().children("td:eq(1)").attr('style','display:block;');
                    $(this).parent().children("td:eq(0)").attr('style','display:none;');                                                                        
                  
              });
              
           
              
              $("td:eq(1)", this).mouseleave(function(){      
              //alert('out'); 
               $(this).parent().children("td:eq(0)").attr('style','display:block;');
                    $(this).parent().children("td:eq(1)").attr('style','display:none;');
                 
              });
           
      });
      
      
      
       /*** boxgrid **/
       
       
       
       
       
       
       
                       //To switch directions up/down and left/right just place a "-" in front of the top/left attribute
 //Vertical Sliding
 $('.boxgrid.slidedown').hover(function(){
   $(".cover", this).stop().animate({top:'-260px'},{queue:false,duration:300});
   }, function() {
   $(".cover", this).stop().animate({top:'0px'},{queue:false,duration:300});
 });
 //Horizontal Sliding
 $('.boxgrid.slideright').hover(function(){
   $(".cover", this).stop().animate({left:'325px'},{queue:false,duration:300});
   }, function() {
   $(".cover", this).stop().animate({left:'0px'},{queue:false,duration:300});
 });
 //Diagnal Sliding
 $('.boxgrid.thecombo').hover(function(){
 $(".cover", this).stop().animate({top:'260px', left:'325px'},{queue:false,duration:300});
 }, function() {
 $(".cover", this).stop().animate({top:'0px', left:'0px'},{queue:false,duration:300});
 });
 //Partial Sliding (Only show some of background)
 $('.boxgrid.peek').hover(function(){
 $(".cover", this).stop().animate({top:'90px'},{queue:false,duration:160});
 }, function() {
 $(".cover", this).stop().animate({top:'0px'},{queue:false,duration:160});
 });
 //Full Caption Sliding (Hidden to Visible)
 $('.boxgrid.captionfull').hover(function(){
 $(".cover", this).stop().animate({top:'160px'},{queue:false,duration:160});
 }, function() {
 $(".cover", this).stop().animate({top:'260px'},{queue:false,duration:160});
 });
 //Caption Sliding (Partially Hidden to Visible)
 $('.boxgrid.caption').hover(function(){
 $(".cover", this).stop().animate({top:'160px'},{queue:false,duration:160});
 }, function() {
 $(".cover", this).stop().animate({top:'220px'},{queue:false,duration:160});
 }); 
       
       
       
       /*** */
	   /** home slider **/
	
		//Speed of the slideshow
	var speed = 5000;
	
	//You have to specify width and height in #slider CSS properties
	//After that, the following script will set the width and height accordingly
	$('#mask-gallery, #gallery li').width($('#slider').width());	
	$('#gallery').width($('#slider').width() * $('#gallery li').length);
	$('#mask-gallery, #gallery li, #mask-excerpt, #excerpt li').height($('#slider').height());
	
	//Assign a timer, so it will run periodically
//	var run = setInterval('newsscoller(0)', speed);	
	
	$('#gallery li:first, #excerpt li:first').addClass('selected');

	//Pause the slidershow with clearInterval
	$('#btn-pause').click(function () {
//		clearInterval(run);
		return false;
	});

	//Continue the slideshow with setInterval
	$('#btn-play').click(function () {
//		run = setInterval('newsscoller(0)', speed);	
		return false;
	});
	
	//Next Slide by calling the function
	$('#btn-next').click(function () {
		newsscoller(0);	
		return false;
	});	

	//Previous slide by passing prev=1
	$('#btn-prev').click(function () {
		newsscoller(1);	
		return false;
	});	
	
	//Mouse over, pause it, on mouse out, resume the slider show
	$('#slider').hover(
/*	
		function() {
			clearInterval(run);
		}, 
		function() {
			run = setInterval('newsscoller(0)', speed);	
		}
*/
	); 	
	
	
 $('#links_home_slider a').click(function () {  
     //stop the slide show  
 //    clearInterval(run);  
           
     //go to the item  
//	 console.log('click');
     goto($(this).attr('rel'));    
      return false;        
     //resume the slideshow  
//     run = setInterval('newsscoller(0)', speed);   
    
 }); 
 goto('.item0');
 /*************************************/      
       
       
   
	
		
 });

