//jQuery's noConflict mode
jQuery.noConflict();
//extension de Jquery pour trouver les positions d'un élément.
jQuery.fn.extend({
	findPos : function() {
       var obj = jQuery(this).get(0);
       var curleft = obj.offsetLeft || 0;
       var curtop = obj.offsetTop || 0;
       while (obj = obj.offsetParent) {
		 curleft += obj.offsetLeft
         curtop += obj.offsetTop
       }
       return {x:curleft,y:curtop};
	} 
});


jQuery(document).ready(function(){
	
	jQuery('.hover').hover(
		function (){
			hoverIn(this);
		},
		function (){
			hoverOut(this);
		}
	);
	
	jQuery('.hideMe').each(hideMe);
	
	jQuery('.emptyMe').focus(emptyMe);
	jQuery('.emptyMe').blur(fullMe);
	
	jQuery('#blocInfo').hover(overInfo, outInfo);
	
	jQuery('#aLaUne .liste .items .item').hover(overALaUne, outALaUne);
	
	OnChangeWtDirectory();
	
	manageBigNews();
	
	manageALaUne();
}); 


/*****
****** Fonctions  qui gere les actus à la une
*****/
var aLaUneChild;
var aLaUneEC = 0;
function manageALaUne(){
	var btnLeft = jQuery('#aLaUne .btnLeft');
	var btnRight = jQuery('#aLaUne .btnRight');
	var liste = jQuery('#aLaUne .liste');
	var items = liste.children('.items');
	aLaUneChild = items.children().length;
	
	items.css('width',((170*aLaUneChild)));
	items.css('position','absolute');
	items.css('float','none');
	liste.css('overflow','hidden');
	
	btnLeft.click(aLaUnePrev);
	btnLeft.css('visibility','hidden');
	
	btnRight.click(aLaUneNext);
	if((aLaUneEC+5) >= aLaUneChild)
		btnRight.css('visibility','hidden');
}

function aLaUneNext(){
	var btnLeft = jQuery('#aLaUne .btnLeft');
	var btnRight = jQuery('#aLaUne .btnRight');
	var liste = jQuery('#aLaUne .liste');
	var items = liste.children('.items');
	
	if((aLaUneEC+5) < aLaUneChild)
		aLaUneEC += 5;
		
	items.animate({left:'-'+(aLaUneEC*170)+'px'},800);
	
	if(aLaUneEC>0)
		btnLeft.css('visibility','visible');
	if((aLaUneEC+5) >= aLaUneChild)
		btnRight.css('visibility','hidden');
}

function aLaUnePrev(){
	var btnLeft = jQuery('#aLaUne .btnLeft');
	var btnRight = jQuery('#aLaUne .btnRight');
	var liste = jQuery('#aLaUne .liste');
	var items = liste.children('.items');
	
	if((aLaUneEC-5) > 0)
		aLaUneEC -= 5;
	else
		aLaUneEC = 0
		
	items.animate({left:'-'+(aLaUneEC*170)+'px'},800);
	
	if(aLaUneEC == 0)
		btnLeft.css('visibility','hidden');
	if((aLaUneEC+5) < aLaUneChild)
		btnRight.css('visibility','visible');
}


/*****
****** Fonctions  qui gere les 4 actus mises en avant a la une
*****/
var newsEC = -1;
var timerBigNews;
function manageBigNews(){
	jQuery('#mainActu .lstActus .item img').clone().prependTo('#mainActu .decor');
	jQuery('#mainActu .lstActus .item img').remove();
	jQuery('#mainActu .decor img').hide();
	doTimeBigNews();
}

function doTimeBigNews(){
	var len = jQuery('#mainActu .decor').children().length;
	if(len>0){
		if(newsEC!=-1){
			jQuery(jQuery('#mainActu .decor').children()[newsEC]).fadeOut();
			jQuery(jQuery('#mainActu .lstActus').children()[newsEC]).removeClass('active');
		}
		newsEC++
		if(newsEC>=len)
			newsEC = 0;
		
		jQuery(jQuery('#mainActu .decor').children()[newsEC]).fadeIn();
		jQuery(jQuery('#mainActu .lstActus').children()[newsEC]).addClass('active');
		
		timerBigNews = setTimeout('doTimeBigNews()',4000);
	}
}




/*****
****** Fonctions  Génériques qui gère  le hover des bloc a la une
*****/
function overALaUne(){
	jQuery(this).addClass('over');
}
function outALaUne(){
	jQuery(this).removeClass('over');
}


/*****
****** Fonctions  Génériques qui gère  le hover des informations
*****/
function overInfo(){
	hoverIn(jQuery(this).children('.btnInfo').children('img'));
	jQuery(this).children('#divInfo').show();
}

function outInfo(){
	hoverOut(jQuery(this).children('.btnInfo').children('img'));
	jQuery(this).children('#divInfo').hide();
}

/*****
****** Fonctions  Génériques qui gère  le hover des images de class '.hover'qui gère  le hover des images de class '.hover'
*****/
function fillRgThumbnail(){
	jQuery('.SS_withTumbnail .rgslideshow .rgsnest').removeAttr('style');
	var tabImg = new Array();
	jQuery('.SS_withTumbnail #slideshownewsteaser a img').each(function(){
		tabImg.push(jQuery(this).parent().html());
	});
	var cpt=0;
	jQuery('.SS_withTumbnail .rgslideshownav .rgsnav2 .rgsbrowser .rgsbrowserEl').each(function(){
		jQuery(this).html(tabImg[cpt]);
		jQuery(this).children('img').removeAttr('style');
		cpt++;
	});
	tabImg = new Array();	
}

/*****
****** Fonctions  Génériques qui gère  le hover des images de class '.hover'qui gère  le hover des images de class '.hover'
*****/
function hoverIn(el){
   if(!jQuery(el).hasClass('active')){
      var srcName = jQuery(el).attr('src');
   	srcName = srcName.replace('off','over');
   	jQuery(el).attr({src:srcName});	
   }
}

function hoverOut(el){
   if(!jQuery(el).hasClass('active')){
   	var srcName = jQuery(el).attr('src');
   	srcName = srcName.replace('over','off');
   	jQuery(el).attr({src:srcName});
   }
}



/*****
****** Fonction  Générique qui cache les champs
*****/
function hideMe(){
   jQuery(this).hide();
}



/*****
****** Fonctions  Génériques qui vide et reremplit les éléments de formulaire
*****/
var tmp_empty='';
function emptyMe(){
   tmp_empty = jQuery(this).val();
	jQuery(this).val('')
}
function fullMe(){
   if(jQuery(this).val()=='')
      jQuery(this).val(tmp_empty);
}

function OnChangeWtDirectory() {
   jQuery('select.category').change(
      function (){
         if(jQuery(this).val() != '0'){
            var jQuery_id = jQuery(this).attr('id');
            var lvlParent = (''+jQuery(this).attr('class')+'').replace('category lvl','');
            jQuery.ajax({
				type: "GET",
				url: "/typo3conf/ext/wt_directory/ajax/catFilter.php",
				data: "catChoose="+jQuery(this).val()+"&catLvl="+(parseInt((''+jQuery(this).attr('class')+'').replace('category lvl',''))+1),
				success: function(ret){
                  jQuery('.wtdirectory_filter_multi_div select').unbind();
                  jQuery('.wtdirectory_filter_multi_div input').remove();
                  jQuery('.wtdirectory_filter_multi_div select').each(function (e){
                     var lvlChild = (''+jQuery(this).attr('class')+'').replace('category lvl','');
                     if (lvlParent < lvlChild){
                     jQuery('.lvl'+lvlChild).remove();
                  }
               });
               jQuery('#'+jQuery_id).after(''+ret+'');
               OnChangeWtDirectory();
            }
         });
      }
   });
}

