$(document).ready(function() {
	
	/* Roll hover images */
	var preload_array = new Array();
	var preload = function(src) {
		var img = new Image();
		img.src = src;
		preload_array.push(img);
	}

	$('img.roll').each(function (i) {
		//let's preload
		preload(this.src.replace(/([_-])off([._-])/, '$1on$2'));
		$(this).hover(
			function () { // over
				$(this).attr('src',this.src.replace(/([_-])off([._-])/, '$1on$2'));
			},
			function () { // out
				$(this).attr('src',this.src.replace(/([_-])on([._-])/, '$1off$2'));
			}
			);
	});

	// projets slide show
	if($(document.body).attr('class') == 'projets') {
		var scrolls = $(".scroll");
		scrolls.scrollable({circular: true});
		setInterval(function() {
			var rand = Math.floor(Math.random() * 4);
			scrolls.eq(rand).data("scrollable").next();
		}, 2500);
	} else if ($(document.body).attr('class') == 'atelier') {
		$('#tech').ImageMenu({
			'saveOnClick':true,
			'keepOpen':true,
			'init':0,
			'extendBy':315,
			click: function(me, options) {
				$('.inner').hide();
				$('#' + $(me).attr('id') + '-content').slideDown();
			}
		});
	} else if ($(document.body).attr('class') == 'nouvelles') {
		$(".inner").scrollable({
			vertical: true,
			mousewheel: true
		});
	} else if ($(document.body).attr('class') == 'nouvelle') {
		$(".inner").scrollable({
			vertical: true,
			mousewheel: true
		});
	} else if ($(document.body).attr('class') == 'projects-listing') {
		$(".project-list").scrollable();
		
		// Get the Scrollable control
 		var scrollable = $(".project-list").data("scrollable");

		// Set to the number of visible items
		var size = 3;

		// Handle the Scrollable control's onSeek event
		scrollable.onSeek(function(event, index) {
			// Check to see if we're at the end
			if (this.getIndex() >= this.getSize() - size) {
			  // Disable the Next link
			  $(".next").addClass("disabled");
			}
		});
		
		// Handle the Scrollable control's onBeforeSeek event
	 	scrollable.onBeforeSeek(function(event, index) {
			// Check to see if we're at the end
			if (this.getIndex() >= this.getSize() - size) {
			// Check to see if we're trying to move forward
			if (index > this.getIndex()) {
				// Cancel navigation
				return false;
			}
		}
	
	  });
	} else if ($(document.body).attr('class') == 'projet-detail') {
		$(".images-list").scrollable();
		
		// Get the Scrollable control
 		var scrollable = $(".images-list").data("scrollable");

		// Set to the number of visible items
		var size = 8;

		// Handle the Scrollable control's onSeek event
		scrollable.onSeek(function(event, index) {
			// Check to see if we're at the end
			if (this.getIndex() >= this.getSize() - size) {
			  // Disable the Next link
			  $(".next").addClass("disabled");
			}
		});
		
		// Handle the Scrollable control's onBeforeSeek event
	 	scrollable.onBeforeSeek(function(event, index) {
			// Check to see if we're at the end
			if (this.getIndex() >= this.getSize() - size) {
			// Check to see if we're trying to move forward
			if (index > this.getIndex()) {
				// Cancel navigation
				return false;
			}
		}
	
	  });
	}

        /***************** SLIDESHOW EQUIPE *****************/
        if($('#tech')){

       
            var maxMove = $('.equipe_desc').length;
            

            $('.active_btn_right').click(function(){

                $('#tech').stop();
                if(pos_bg > (-size_img*(maxMove-1))){
                    
                   moveTo('left');
                    // We're displaying the info of the person, with a beautiful fadeOut/fadeIn
                    $('.equipe_desc').each(function(ind, e){
                        if($('#'+e.id).is(':visible')){
                            $('#'+e.id).fadeOut(500, function(){
                                $('#atelier-'+currentpic+'-content').fadeIn(500);
                            });
                        }
                    });
                }
            });
            $('.active_btn_left').click(function(){


                $('#tech').stop();
               if(pos_bg < (size_img*maxMove) && pos_bg != 0){
                   moveTo('right');
                   $('.equipe_desc').each(function(ind, e){
                        if($('#'+e.id).is(':visible')){
                            $('#'+e.id).fadeOut(500, function(){
                                $('#atelier-'+currentpic+'-content').fadeIn(500);
                            });
                        }
                    });

               }
            });

            $('.equipe span').click(function(e){

                if($('#tech').is(':animated')){
                    $('#tech').stop();
                }
                    var id = e.currentTarget.id.split('-');
                    id = id[1];

                    moveTo(id);
                    $('.equipe_desc').each(function(ind, e){
                        if($('#'+e.id).is(':visible')){
                            $('#'+e.id).fadeOut(500, function(){
                                $('#atelier-'+currentpic+'-content').fadeIn(500);
                            });
                        }
                    });
            });
        }
});

var pos_bg = 0;
var size_img = 480; // img + whitespace
var currentpic = 1;

// Direction is used for left, right or an integer that says which picture we want to go to.
function moveTo(direction){

    var px;
    if(direction == 'right'){
        px = parseInt(pos_bg,10)+parseInt(size_img,10);
        currentpic--;
    }else{
        if(direction == 'left'){
            px = parseInt(pos_bg,10)-parseInt(size_img,10);
            currentpic++;
        }else{
            px = size_img * (direction-1);
            if(px > pos_bg){
                px = px*-1;
            }
            currentpic = direction;
        }
    }
    $('#tech').animate({backgroundPosition: px+'px 0'}, 1500, "easeOutExpo");

    // Hide or show sidebar
    if(!$('#atelier_btn_right').is(':visible')){
        $('#atelier_btn_right').fadeIn();
    }

    if(!$('#atelier_btn_left').is(':visible')){
        $('#atelier_btn_left').fadeIn();
    }
    if(currentpic == $('.equipe span').length){
        $('#atelier_btn_right').fadeOut();
    }else{
        if(currentpic == 1){
            $('#atelier_btn_left').fadeOut();}
    }


    $('.span_actif').removeClass('span_actif');
    $('#equipe-'+currentpic).addClass('span_actif');
    pos_bg = px;

}


// Slideshow
// ---------------------------------------------------------------------------------					 
$(document).ready(function() {
/*
	//Get size of images, how many there are, then determin the size of the image reel.
	var images = $(".slideshow img");
	var imageSum = images.size();
	var currentImage = $(images[0]);
	var slideId = 0;
	
	images.css({
		position: 'absolute',
		display: 'none'
	});
	currentImage.fadeIn();
	
	//Adjust the image reel to its new size

	//Paging + Slider Function
	rotate = function() {
		currentImage.fadeOut();
		
		if (++slideId >= imageSum) slideId = 0;
		
		currentImage = $(images[slideId]);
		currentImage.fadeIn();
	}; 
	
	//Rotation + Timing Event
	rotateSwitch = function(){		
		play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
			rotate(); //Trigger the paging and slider function
		}, 5000); //Timer speed in milliseconds (3 seconds)
	};
	
	rotateSwitch(); //Run function on launch
	
	//On Hover
	$(".slideshow a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation
	});	*/

});

function print_mail_to_link(part1, part2) {
   document.write("<a href=\"mailto");
   document.write(":" + part1 + "@");
   document.write(part2 + "\">" + part1 + "@" + part2 + "<\/a>");
}
