if (!window.console) {
	window.console = {
		log:function(){},
		time:function(){}
	}
}
(function(){
	
	// Elements
	var $cartouche = $('.cartouche'),
		$img,
		$slide = $(document.getElementById('slide')),
		$photos = $slide.find('.photos'),
		$cartouche = $slide.find('.cartouche'),
		next = $slide.find('.droite'),
		nextDiapo = $slide.find('.next-diapo'),
		prev = $slide.find('.gauche'),
		prevDiapo = $slide.find('.prev-diapo');
	
	
	// Namespace
	var NW = {
		loader: {},
		slider: {
			current: 0,
			delta: 0,
			infos: [],
			nextInfos: [],
			infos: {},
			enableResize: true
		},
		verfiForm: {},
		newsTicker: {
			
		}
	};
	
/********************** BEGIN - LOADER ***********************/
	NW.loader = function(tab, fn){
		
		var temp = {},
			loaded = 0,
			len = tab.length;
		var pathname = window.location.pathname;
		
		console.log(pathname);
		
		if (pathname != '/notre_savoir_faire/') {
			$('#loader').show();
		}
		
		function simpleLoad(){
			if (loaded < len) {
				var img = new Image();
				$(img).load(function(){
					loaded++;
					simpleLoad();
				}).attr('src', tab[loaded][0]);
				//}).attr('src', 'images/infographie/' + tab[loaded][0]);
			}
			else {
				fn.apply(NW.slider);
			}
		}
		
		simpleLoad();
		
	};


/********************** BEGIN - RESIZER **********************/
	NW.resizer = function(){
		
		if (!NW.slider.enableResize) return;
		
		/*Centre l'image horizontalement et verticalement*/	
		var $photo = $slide.find('.photos img');
		var winWidth = $(window).width();
		var winHeight = $(window).height() - 105;
		
		$slide.height(winHeight);
		
		var imgWidth = $photo.width();
		var imgHeight = $photo.height();
		
		if (imgWidth < winWidth) {
			$photo.css({
				width: '100%',
				height: 'auto'
			});
		}
		else if (imgHeight < winHeight) {
			$photo.css({
				width: 'auto',
				height: '100%'
			});
		}
		
		$photo.css({
			top: (winHeight - $photo.height()) / 2,
			left: (winWidth - $photo.width()) / 2
		});
		/*Centre l'image horizontalement et verticalement ------- END*/
		
		if (winHeight < 663) {
			$cartouche.find("p").hide();
		} else {
			$cartouche.find("p").show();
		}
		
		NW.slider.delta = imgWidth;
		
	};
	

/********************** BEGIN - SLIDER **********************/
	NW.slider.init = function(){
		this.infos = images;
		$photos.append("<img src='"+ this.infos[this.current][0] +"' alt="+ this.infos[this.current][0] +">");
		NW.slider.updateInfos();
		
		$img = $photos.find('img');
		setTimeout(NW.resizer,250);
		
		
		/*Gestion du hover sur les flèhes du slider*/
		//if (this.current!=images.length-1) {
			$(next).hover(function(){
				nextDiapo.stop().animate({ right: 0 }, 500);
			}, function(){
				nextDiapo.stop().animate({ right: -290 }, 500);
			});
		//}
		
		//if (this.current>0) {
			$(prev).hover( function() {
				prevDiapo.stop().animate({ left: 0 }, 500);
			}, function() {
				prevDiapo.stop().animate({ left: -290 }, 500);
			});
		//}
		/*Gestion du hover sur les flèhes du slider ------- END*/
		
		$('.droite:not(.animated)', $slide).live('click', function(){
			//console.log($(this));
			NW.slider.move($(this));
			return false;
		});
		$('.gauche:not(.animated)', $slide).live('click', function(){
			NW.slider.move($(this));
			return false;
		});
		
		
		$(document).keydown(function(event) {
	        var code = event.keyCode ? event.keyCode : event.which;
	        switch(code){
	            case 37 : // Flèche gauche
	                $('.gauche', $slide).trigger('click');
	            break;
	 
	            case 39 : // Flèche droite
	                $('.droite', $slide).trigger('click');
	            break;
	        }
	    });
		
		
		
		//$('#loader .gif img').css('visibility', 'hidden');
		$('#loader .gif img').fadeOut(2000, function(){
			$('#loader .gif p').fadeIn(1000);
		});
		
		$('#loader .gif p').live('click', function(){
			$('#loader').fadeOut(2000, function(){
				$(this).remove();
			});
		});
		
		setTimeout(function(){
			$('#loader').fadeOut(2000, function(){
				$(this).remove();
			});
		},7000);
	
		
		
	};
	
	NW.slider.move = function(elem){
		NW.slider.enableResize = false;
		
		//console.log('event.keyCode ' + elem);
		
		var autorise = false;
		var goLeft = false;
		var delta = NW.slider.delta;
		var insert = 'before';
		var $img = $photos.find('img');
		var w = $img.width();
		var h = $img.height();
		var myleft = parseInt($img.css('left'), 10);
		var l = parseInt($img.css('left'), 10) + w;
		var t = $img.css('top');
		var m = 0;
		var temp = this.infos.length;
		
		if ((elem.hasClass('droite') || elem==39) && NW.slider.current+1 < this.infos.length) {
			delta = -delta;
			NW.slider.current++;
			insert = 'after';
			autorise = true;
		}
		else if(elem.hasClass('gauche') && NW.slider.current != 0) {
			NW.slider.current--;
			insert = 'before';
			autorise = true;
			goLeft = true;
			m = -w;
			l = w;
			$img.css({ left: w });
		}
		
		if(autorise) {
			
			if (goLeft){
				$img[insert]('<img src="' + this.infos[NW.slider.current][0] + '" style="left:' + myleft + 'px;top:'+ t +';" width="' + w + '" height="' + h + '" />');
			} else {
				$img[insert]('<img src="' + this.infos[NW.slider.current][0] + '" style="left:'+ l +'px;top:'+ t +';" width="' + w + '" height="' + h + '" />');
			}
		
			$(elem).addClass("animated");		
			
			$photos.find('img').css({
				width: w,
				height: h
			});
			$photos.css({
				position: 'relative',
				width: 2*w,
				height: h,
				marginLeft: m
			})
			
			var end = parseInt($photos.css('marginLeft'), 10) + delta + myleft*2;
			
			if (goLeft){
				end = parseInt($photos.css('marginLeft'), 10) + delta - myleft*2;
			}
			
			$cartouche.fadeOut("fast", function(){
				$(prev).removeClass("hide");
				$(next).removeClass("hide");
				
				if (NW.slider.current==0) {
					$(prev).addClass("hide");
				}
				if (NW.slider.current+1 == temp) {
					$(next).addClass("hide");
				}
				$photos.animate({ marginLeft: end }, 1000, 'easeOutQuint', function() {
					$photos.removeAttr('style').find('img').removeAttr('style');
					$photos.find('img').css({left: l-w,top: t});
					$(elem).removeClass("animated");
					NW.slider.updateInfos();
					$cartouche.fadeIn("slow");
					$img.remove();
					NW.slider.enableResize = true;
					NW.resizer();
				});			
			});
		}
		/*
else if (NW.slider.current==0) {
			$(prev).addClass("hide");
		}
		else if (NW.slider.current+1 > this.infos.length) {
			$(next).addClass("hide");
		}
*/
		
		
		
		/*
function toto(p1, p2){
			
		}
		toto.apply(this, ['tutu', 5]) => this == NW.slider
		
		toto('tutu', 5) => this == window
*/
		
	};
	
	
	NW.slider.infos.init = function(){
		$cartouche.fadeOut(500, function(){
			NW.slider.updateInfos()
		})
	};
	NW.slider.updateInfos = function() {
		var previewLeft = 0;
		var previewRight = 0;
	
		$cartouche.find("h2").html(this.infos[this.current][1]);
		$cartouche.find("h3").html(this.infos[this.current][2]);
		$cartouche.find("h4").html(this.infos[this.current][3]);
		$cartouche.find("p").html(this.infos[this.current][4]);
		//$cartouche.find("a.website").html(this.infos[this.current][5]);
		if (this.infos[this.current][5] != '') {
			$cartouche.find("a.website").attr('href', this.infos[this.current][5]);
			$cartouche.find("a.website").show();
		} else {
			$cartouche.find("a.website").hide();
		}
		
		
		if (this.current>0) {
			previewRight=1;
		}
		if (this.current!=this.infos.length-1) {
			previewLeft=1;
		}
		
		nextDiapo.find('img').attr('src',this.infos[this.current + previewLeft][0]);
		nextDiapo.find("p").html(this.infos[this.current + previewLeft][1]);
		prevDiapo.find('img').attr('src',this.infos[this.current - previewRight][0]);
		prevDiapo.find("p").html(this.infos[this.current - previewRight][1]);
	};
	
	window.NW = NW;
	
	
	// INIT
	$(function(){
		NW.loader(images, NW.slider.init);
		$(window).resize(NW.resizer);		
	});

})();


