var mainClass = {

	screenCurrent: "home",
	ajaxURL : "",
	
	hauteur_banniere: 0,
	
	tabMenu: Array(),
	
	initialize: function() {
	
		tabMenu= new Array();
		tabMenu['home'] = 0;
		tabMenu['profile'] = 1;
		tabMenu['contact'] = 2;
		
		this.clickMenu();
		this.thumbOver();
		this.clickThumb();
		this.pageChangeDetection();

		
		$("#projetsContainer").empty();
		$("#infos-projet").empty();
		$("#projetsContainer").hide();
		$('#menu a:first').addClass('courant');
		hauteur_banniere = $("#slider-banniere").height();
		pageChangeAction();
		
		$('#testo').nivoSlider({
			effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
			slices:1,
			animSpeed:300, //Slide transition speed
			pauseTime:8000,
			startSlide:0, //Set starting Slide (0 index)
			directionNav:false, //Next & Prev
			directionNavHide:false, //Only show on hover
			controlNav:false, //1,2,3...
			controlNavThumbs:false, //Use thumbnails for Control Nav
		    controlNavThumbsFromRel:false, //Use image rel for thumbs
			controlNavThumbsSearch: '.jpg', //Replace this with...
			controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
			keyboardNav:false, //Use left & right arrows
			pauseOnHover:false, //Stop animation while hovering
			manualAdvance:false, //Force manual transitions
			captionOpacity:0.8, //Universal caption opacity
			beforeChange: function(){},
			afterChange: function(){},
			slideshowEnd: function(){} //Triggers after all slides have been shown
		});
	},
	
	//GESTION MENU
	/////////////////////////////////////////////////////////////////////////////////////////
	clickMenu: function() {
		$('#menu a').click(function() {
		
			if (!$(this).hasClass('courant')) {
				var lien = $(this).attr('href');
				var provenance = mainClass.screenCurrent;
								
				if (mainClass.screenCurrent == "projet") {
					mainClass.screenCurrent = lien;
					closeProject();
				}
				else if (mainClass.screenCurrent == "home") {
					mainClass.screenCurrent = lien;
					closeHome();
				}
				else {
					mainClass.screenCurrent = lien;
					closeOther(lien,provenance);
				}
				location.hash = '/' + lien;
			}
			return false;
		});
	},
	
	pageChangeDetection: function() {
		$(window).hashchange( function(){
			pageChangeAction();	
		});
	},

	//GESTION THUMBS & PROJETS
	/////////////////////////////////////////////////////////////////////////////////////////
	clickThumb: function() {
		
		$(".thumb_container li a").click(function() {
			$('#menu a').each(function(i) {
				$(this).removeClass('courant');
			});	
			$(this).fadeTo(200, 0.5);
			$(this).parent().find("h3").fadeTo(200, 0.5);
			location.hash = '/projet/' + $(this).attr('href');
			
			mainClass.screenCurrent = "projet";
			
			var form_data = {
				lien: $(this).attr('href'),
				ajax: '1'
			};
			
			closeHome(form_data);
			return false;
		});
	},
	
	thumbOver: function() {
		$(".thumb_container li").hover(function() { //On hover...
			var thumbOver = $(this).find("img").attr("src"); //Get image url and assign it to 'thumbOver'
			 //Set a background image(thumbOver) on the <a> tag - Set position to bottom
			$(this).find("a.thumb").css({'background' : 'url(' + thumbOver + ') no-repeat 10px -130px'});
			//Animate the image to 0 opacity (fade it out)
			$(this).find("span").stop().fadeTo(200, 0 , function() {
				//$(this).hide() //Hide the image after fade
			});
			
			//$(this).find("a.thumb").stop().animate({ borderTopColor: "#000", borderLeftColor: '#000', borderRightColor: '#000'}, 200);
			//$(this).find("h3 a").stop().animate({backgroundColor:"#000000", color:"#fce100"}, 200);
		
			$(this).find("h3 a").stop().animate({color:"#fcd000"}, 200);
		
		
			} , function() { //on hover out...
				 //Fade the image to full opacity
				$(this).find("span").stop().fadeTo(200, 1).show();
		
				//$(this).find("a.thumb").stop().animate({ borderTopColor: "#a8a8a8", borderLeftColor: '#a8a8a8', borderRightColor: '#a8a8a8'}, 200);
				//$(this).find("h3 a").stop().animate({backgroundColor:"#a8a8a8",color:"#ffffff"}, 200);
		
				$(this).find("h3 a").stop().animate({color:"#ffffff"}, 200);
			});
		}
};

function chargeProjet(html) {
	$("#infos-projet").append(html.titre + html.presentation);
	preLoadImage(0,html);
}

function preLoadImage(i,html) {
	var imgTab = html.tabImages;
	var img = new Image();
	img.src = imgTab[i];
	img.onload = function() {
		if (i == imgTab.length-1) {
			$("#projetsContainer").append(html.images);
			$("#projetsContainer").css({ opacity: 0 });
			afficheProjet(imgTab.length);
		}
		else {
			i++;
			preLoadImage(i,html);
		}
	}
}

function afficheProjet(nbrePhotos) {

	$('#footer').fadeTo(200, 0);
	$('.thumb_container').fadeTo(200, 0, function() {
		$('.thumb_container').hide();

		//on remet l'opacité
		$(".thumb_container li a").each(function() {
			$(this).fadeTo(200, 1);
			$(this).parent().find("h3").fadeTo(200, 1);
		});
		
		$('#projetMain').show();
		$('#infos-projet').fadeTo(200, 1);
		$("#projetsContainer").fadeTo(200, 1);
		
		$('#footer').fadeTo(200, 1);
	
		if (nbrePhotos>1) {
			$('#slider').nivoSlider({
				effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
				slices:1,
				animSpeed:300, //Slide transition speed
				pauseTime:10000,
				startSlide:0, //Set starting Slide (0 index)
				directionNav:false, //Next & Prev
				directionNavHide:false, //Only show on hover
				controlNav:true, //1,2,3...
				controlNavThumbs:false, //Use thumbnails for Control Nav
			    controlNavThumbsFromRel:false, //Use image rel for thumbs
				controlNavThumbsSearch: '.jpg', //Replace this with...
				controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
				keyboardNav:true, //Use left & right arrows
				pauseOnHover:true, //Stop animation while hovering
				manualAdvance:false, //Force manual transitions
				captionOpacity:0.8, //Universal caption opacity
				beforeChange: function(){},
				afterChange: function(){},
				slideshowEnd: function(){} //Triggers after all slides have been shown
			});
		}
		$('#closeProject a').click(function() {
			closeProject();
			afficheHome();
		});
	});
}

function closeHome(form_data) {
	$('#slider-banniere').fadeTo(200, 0, function() {
	    $('#banniere').slideToggle('medium', function() {
	        $("#slider-banniere").hide();
	        
	        if (mainClass.screenCurrent == "projet") {
		    	$.ajax({
		    		url: mainClass.ajaxURL,
		    		type: 'POST',
		    		dataType: "json",
		    		data: form_data,
		    		success: function(html) {
		    			chargeProjet(html);
		    		},
		    		error: function() {
		    			alert("ajax error");
		    		}
		    	});
		    }
		    else {
		    	$('#footer').fadeTo(200, 0);
		    	$('.thumb_container').fadeTo(200, 0, function() {
	    			$('.thumb_container').hide();
	    			//on remet l'opacité
	    			$(".thumb_container li a").each(function() {
	    				$(this).fadeTo(200, 1);
	    				$(this).parent().find("h3").fadeTo(200, 1);
	    			});
    				afficheOther();
	    		});
	    	}
	    });
	});
}

function closeProject() {
	$("#projetsContainer").fadeTo(200, 0);
	$("#infos-projet").fadeTo(200, 0);
	if (mainClass.screenCurrent == "home") {
		afficheHome();
	}
	else {
		afficheOther();
	}
}

function closeOther(lien,provenance,projet) {
	var str = '#' + provenance + "Main";
	$('#footer').fadeTo(200, 0);
	$(str).fadeTo(200, 0, function() {
		$(str).hide();
		if (lien == "home") {
			afficheHome();
		}
		else if (lien == "projet") {
			$('#projetMain').show();
			$('#infos-projet').fadeTo(200, 1);
			$("#projetsContainer").fadeTo(200, 1);
			
			$('#footer').fadeTo(200, 1);
		}
		else {
			afficheOther();
		}
	});
}

function afficheHome() {
	$("#slider-banniere").show();
	mainClass.screenCurrent = "home";
	$('#banniere').slideToggle('medium', function() {		
		$('#slider-banniere').fadeTo(200, 1);
	});
	$('#footer').fadeTo(200, 0, function() {
		$('#projetMain').hide();
		$('.thumb_container').show();
		$('#footer').fadeTo(500, 1);
		$('.thumb_container').fadeTo(200, 1, function() {
			$("#projetsContainer").empty();
			$("#infos-projet").empty();
			
		});
	});
}

function afficheOther() {
	$('#footer').fadeTo(200, 0, function() {
		$('#projetMain').hide();
		var str = '#' + mainClass.screenCurrent + 'Main';
		$(str).show();
		$(str).fadeTo(200, 1);
		h = $("#gauche_contact").height();
		h = h - $("#adresse").height();
		$("#adresse").css({'margin-top' : h-40});
		
		$('#footer').fadeTo(500, 1);
	});
}

function pageChangeAction() {

	var str = location.hash.substring(2,location.hash.length);
	var i = str.indexOf("/");
	if (i<0) i=str.length;
	
	var page = str.substring(0,i);
	var projet = str.substring(i+1, str.length);
	if (page=='') page='home';
	
	if (mainClass.screenCurrent != page) {
	
		switch (mainClass.screenCurrent) {
			case 'projet':
				mainClass.screenCurrent = page;
				closeProject();
			break;
			case 'home':
				mainClass.screenCurrent = page;
				if (projet != "") {
					var form_data = {
						lien: projet,
						ajax: '1'
					};
					closeHome(form_data);
				}
				else {
					closeHome();
				}
			break;
			default:
				closeOther(page,mainClass.screenCurrent,projet);
				mainClass.screenCurrent = page;
			break;
		}
	}
	
	$('#menu a').each(function(i) {
		$(this).removeClass('courant');
	});
	var id = tabMenu[mainClass.screenCurrent];
	$("#menu a:eq("+id+")").addClass('courant');
}


