$(document).ready(function(){
	

	
	$(window).bind("load resize",function(){
		$("#navigateMenu").css("left",function(){

			return ($(window).width() - $("#collumn").width())/2 - 22;


		}).css("top","200px").show();
	});
	
	
	$('#menu li a').click(function(event){
		event.preventDefault();
		var target = $(this).attr('href');
		var scrolltarget = $(target).offset().top - 40;
		
		//console.log($(target).scrollTop() - 40);
		
		$.scrollTo(scrolltarget,500);
	})
	
	
	
	$("#scrollTop").click(function(){$.scrollTo(0,500)});
	
	$("#down").click(function(){
			
		//	if($(window).scrollTop() < $("#content").offset().top){
				var scroll= 3000000000;
		
				$(".ancre").each(function(){
						var temp =	$(this).offset().top - $(window).scrollTop()-40 ;
					if(temp<1){
						return
					}
					if(temp <= scroll ){
						scroll = temp;
					}
				})
				
				scroll = scroll;
				if(scroll != 3000000000){
					$.scrollTo('+='+scroll+'px',300);
					return
				}
		//	}
			
		/*	
			var scroll= 3000000000;
			alert("cas 2");
			$("#content .section").each(function(){
				
				var temp =	$(this).offset().top - $(window).scrollTop()-40 ;
				if(temp<1){
					return
				}
				if(temp <= scroll){
					scroll = temp;
				}
			})
			scroll = scroll;
			if(scroll != 3000000000)
				$.scrollTo('+='+scroll+'px',300);
		*/	
	
	
	})
	
	$("#up").click(function(){
			var scroll= 3000000000;
		
			$(".ancre").each(function(){
				var temp =	$(window).scrollTop() - $(this).offset().top +40;
				if(temp<1){
					return
				}
				if(temp <= scroll ){
					scroll = temp;
				}
			})
			
			scroll = scroll;
			if(scroll != 3000000000){
				$.scrollTo('-='+scroll+'px',300);
				return
			}
			
			/*if($(window).scrollTop() < $("#content").offset().top){
				var scroll= 3000000000;
				
				$(".page").each(function(){
					var temp =	$(window).scrollTop() - $(this).offset().top +40;
					if(temp<1){
						return
					}
					if(temp <= scroll){
						scroll = temp;
					}
				})
				
				scroll = scroll;
				if(scroll != 3000000000){
					$.scrollTo('-='+scroll+'px',300);
					return
				}
			}
			
			var scroll= 3000000000;

			$("#content .section").each(function(){
				var temp =	  $(window).scrollTop() - $(this).offset().top +40;
				if(temp <1){
					return
				}
				if(temp <= scroll){
					scroll = temp;
				}
			})
			scroll = scroll;
			if(scroll != 3000000000)
				$.scrollTo('-='+scroll+'px',300);
		*/
	})
	
	
	
	
	$(".section .rollImg").mouseover(function(){
		
		$(this).children(".imgOver").show().width(function(){
			return $(this).parent().children('img').width();
		}).height(function(){
			return $(this).parent().children('img').height();
		}).children('.tinypopin').css('marginTop',function(){
				return $(this).parent().height()/2 - $(this).height()/2;
		});
		
		
	}).mouseout(function(){$(".imgOver").hide()})
		
	
	
	
	$(".show").click(function(event){
		event.preventDefault();
		}).mouseup(function(){
		
		var source = $(this).children('img').attr('srchd');
		//console.log(source);
		
		var caption = $(this).parent().parent().parent().parent().children('.imgCaption').html();
		if(!caption || caption == null) caption = "";
		var text = $(this).parent().parent().children('p').html();
		if(!text || text == null) text = "";
		//console.log(text);
		
		
		var title;
		
		if(caption == ""){
			//title = text;
			var titreImage = source.split("/");
			var titreImage2 = titreImage[titreImage.length - 1];
			var titreImage3 = titreImage2.substr(0,titreImage2.length - 4);
			title = titreImage3;
		}
		else title = caption;
		
		var popinHtml = '<div id="popin" style="position:fixed;z-index:10">';
		popinHtml += '		<div id="cross">';
		popinHtml += '			<img src="cross.png"/>';
		popinHtml += '		</div>';
		popinHtml += '		<img src="'+ source + '"/>';
		if(title != ""){
			popinHtml += '	<p class="copyright">'+ title +'</p>';
		}
		popinHtml += '	</div>';
		
		$('body').append(popinHtml);
		
		var newImg = new Image();
		newImg.onload = function(){
			var imgHeight = newImg.height;
			var imgWidth = newImg.width;
			$('#popin').css('left',function(){
				return ($(window).width() / 2) - (imgWidth/2);
			}).css('top',function(){
				return ($(window).height() / 2)- (imgHeight/2);
			});
			$('#popin').fadeIn("slow");
		};
		newImg.src = source;
		
		$('#popin').css('left',function(){
			return ($(window).width() / 2) - (500/2);
		}).css('top',function(){
			return 50;
		}).css('display','none');
		
		
		$('#cross').click(function(){
			$('#popin').remove();
		});
		
		
	})
	
//	var pushActif = 1;
	
	
	
	$(".pushElement").hover(function(event){
		var numToActive = $(this).attr("number");
		if(numToActive != pushActif){
			oldImage = getMaxImage(pushActif);
			pushActif = numToActive;
			newImage = getMaxImage(numToActive);// $(this).children('img').attr('srcMax');
			
			enabledPushElements(numToActive);
			disabledPushElement($(this).children('.pushText'));
			
			//
			//$("#pushMaxImage").fadeOut("slow");
			newImage.fadeOut("fast");
			oldImage.fadeOut("slow", function() { 
				if (this.complete) {
					oldImage.css("display", "none");
					newImage.fadeIn("slow");
				 }
				
			});
			
			ArrowAnimation();
		}
	})
	
	$(".pushElement").click(function(event){
		window.location.replace("?id="+pushActif);
	})
	
	$("#pushImage").click(function(event){
		window.location.replace("?id="+pushActif);
	})

	
});

function getMaxImage(numero){
	var image = null;
	$("#pushImage").children().each(function(){
		
		if($(this).attr("class") == "pushMaxImage" && numero == $(this).attr("number")){
			image = $(this);
		}
	});
	return image;
}

function initScrollBar(){
	$("#pushScroll").scrollControl({
		mousewheel: true,
		cssCursor: { 
			width: "5px",
			height: "40px",
			background: "url(img/push/scrollBar.png)"
		}
	});	
}

function enabledPushElements(toActive){
		
	$("#pushScroll").children().each(function(){
		if($(this).attr("number") && toActive != $(this).attr("number")){
			$(this).children('.pushText').css('background-color','#efefef');
			$(this).children('.pushText').children(".date").css('color','#9a9a9a');
			$(this).children('.pushText').children(".date").children(".titre").css('color','#5b5b5b');
		}
	 });
}
	
function ArrowAnimation(){
	$('#arrow').animate({
			top: (pushActif*106)-63
	  }, 300, function() {
		// Animation complete.
	  });
}
	
	
function disabledPushElement(element){		
	element.css('background-color','#a8bb45');
	element.children(".date").css('color','#d9dbd3');
	element.children(".date").children(".titre").css('color','#ffffff');
}
	

function initPushs(toActive){
	pushActif = toActive;
	var cpt = 1;
	$("#pushScroll").children().each(function(){
		if(cpt == pushActif){
			disabledPushElement( $(this).children('.pushText') );
		}
		cpt++;
	});
	
//	ArrowAnimation();	
	$('#arrow').css('top', (pushActif*106)-63);
}
