


jQuery(document).ready(function(){

var myInterval;
index_picture = 0;
index_slider = 0;

jQuery('.flech_gauche').hide();


  var settings = {
    thumbListId: "thumbs",
    imgViewerId: "viewer",
    activeClass: "active",
    activeTitle: "Photo en cours de visualisation",
    loaderTitle: "Chargement en cours",
    loaderImage: "fileadmin/templates/img/loader.gif"
  };

  var thumbLinks = $("#"+settings.thumbListId).find("a"),
      firstThumbLink = jQuery('.tumb_li')[0]; //thumbLinks.eq(0),
      highlight = function(elt){
      	jQuery('.tumb_li2').removeClass('tumb_li2');
      	jQuery(elt).addClass('tumb_li2');
        //thumbLinks.removeClass(settings.activeClass).removeAttr("title");
        //elt.addClass(settings.activeClass).attr("title",settings.activeTitle);
      },
      loader = $(document.createElement("img")).attr({
        alt: settings.loaderTitle,
        title: settings.loaderTitle,
        src: settings.loaderImage
    });

  highlight(firstThumbLink);
  
  var clickPicture = function(target){
      
      if (bigPic.attr("src") == target) return;
      highlight($this.parent());
      
      imgViewer.html(loader);
      bigPic
        .load(function(){
          imgViewer.html($(this).fadeIn(250));
        })
        .attr("src",target);
    }

  var imgViewer = $("#"+settings.imgViewerId),
      bigPic = imgViewer.children("img");

  thumbLinks
    .click(function(e){
    	clearTimeout(myInterval);
      var getIndexNodes = jQuery(".galery_tumb_img");
      
      for(var i=0;i<getIndexNodes.length;i++) {
      
         if(jQuery(getIndexNodes[i]).parent().attr("href") == $(this).attr("href")) {index_picture=i}
      }
    
    
      e.preventDefault();
       
      var $this = $(this),
          target = $this.attr("href");
      if (bigPic.attr("src") == target) return;
      highlight($this.parent());
      
      imgViewer.html(loader);
      bigPic
        .load(function(){
          imgViewer.html($(this).fadeIn(250));
          startInterval();
        })
        .attr("src",target);
      
    });


var goToPicture = function($this) {
	//e.preventDefault();
       
      	//alert($this.getAttribute('href'));
    	target = $this.attr("href");
      if (bigPic.attr("src") == target) return;
      highlight($this.parent());
      
      imgViewer.html(loader);
      bigPic
        .load(function(){
          imgViewer.html($(this).fadeIn(250));
        })
        .attr("src",target);
        startInterval();
}

scrollLeft2 = function() {
    clearTimeout(myInterval); 
    jQuery("#container_all").animate({"margin-left": "+=500px"}, "slow", function() {startInterval()});
    jQuery(".flech_droite").show();
    jQuery('.flech_gauche').hide();
    index_slider--;
    //startInterval();
}


scrollRight = function() {
    clearTimeout(myInterval);
	//jQuery("#container_all").css('margin-left','-500px');
    jQuery("#container_all").animate({"margin-left": "-=500px"}, "slow", function() {startInterval()});
    jQuery(".flech_droite").hide();
    jQuery('.flech_gauche').show();
    index_slider++;
}




jQuery(".class_texte_credits").fancybox({
		//'hideOnContentClick': false,
		//'centerOnScroll': false,
		'width':600,
		'height':250,
		//'overlayColor':'#141414',
		'titleShow':false,
		'opacity'	:true,
		'type'				: 'iframe'
		
	});
jQuery(".class_texte_metion_legale").fancybox({
		//'hideOnContentClick': false,
		//'centerOnScroll': false,
		'width':900,
		'height':560,
		//'overlayColor':'#141414',
		'titleShow':false,
		'type'				: 'iframe'
		
	});

    jQuery(".class_texte_liste").fancybox({
		//'hideOnContentClick': false,
		//'centerOnScroll': false,
		'width':700,
		'height':500,
		//'overlayColor':'#141414',
		'titleShow':false,
		'type'				: 'iframe'
		
    });



startInterval = function() {
	myInterval = window.setTimeout(function () {
		var pictures = jQuery('.galery_tumb_img');
		index_picture++;
		if(index_picture >= pictures.length) {
			index_picture = 0;
		}
		goToPicture(jQuery(pictures[index_picture]).parent());
        
        var pos = parseInt(index_picture/6);
        if(pos > index_slider) {
            scrollRight();
        } else if(pos < index_slider) {
            scrollLeft2();
        }
		
	}, 5000);
	
}

startInterval();


    
});
