var cache = [];

function preLoadImages(images, callback) {
	toLoad = 0;

	loading = $('<div><p><img src="./design/images/loading.gif" /></p></div>').css({ 
		'text-align': 'center', 
		width: '100%', 
		margin: 'auto', 
		'top': 300,
		position: 'absolute',
		opacity: 0.6
		});

	$("#TableAnimation").remove();
	$("body").append(loading);
	
	$.each(images, function(key, value) {
	var img = new Image();
		toLoad = toLoad + 1;
		$(img).load(function () {
			toLoad = toLoad - 1;
			if(toLoad == 1) {

				$(loading).remove();
				if($.isFunction(callback)) {
					callback.call(this);	
				}
			}
		}).attr('src', value);
	});
}

  
$(document).ready(function() {

	loaded = false;
	animated = false;
	
	loadIndex(function() {
		
		$("#List li, #List h1").css({opacity: 0});
		$("#Site").css('visibility', 'visible').css({opacity: 0}).animate({opacity: 1}, function() {	
			showElements(1000);																						 
		});
		
		showBarres(true, false, true, true);
		
		$("#Menu a").hover(function() {
			$(this).css({opacity: 0.35});
		}, function() {
			if($(this).data('clicked') != true)
				$(this).animate({opacity: 1});
		});
		
		$("#Menu a").click(function(event) { 
			
			$("#Menu a").css({opacity: 1});
			$(this).css({opacity: 0.35}).data('clicked', true);
			var newPage = $(this).attr('rel');
	
			var current = $("body").data('currentPage');
			event.preventDefault();
				
			var href = document.location.href;
			if (href.indexOf("?") > 0) href = href.substring(0,href.indexOf("?"));
			if (href.indexOf("#") > 0) href = href.substring(0,href.indexOf("#"));
		
			document.location.href = href + '#'  + $(this).attr('href');
			
			if($('body').data('page') != newPage) {
				
				hideBarres(1000);
				$(".Button").fadeOut('slow');
				
				$.hideElements(1000, function() {
					loadPage(newPage, true);
				});
			}
		});	  
		
	});
});


function hideBarres(duration) {
	$("#Top .Hor .Barre").animate({height: 0, 'margin-top': 120, opacity: 0}, duration);
	$("#Bottom .Hor .Barre").animate({height: 0, opacity: 0}, duration);
	$("#Center .Right .Vert .Barre").animate({width: 0, opacity: 0}, duration);	
	$("#Center .Long .Barre").animate({ width: 0, 'margin-left': 620, opacity: 0}, duration);

}

function showBarres(top, centerRight, centerLeft, bottom) {
	
	var duration = 1000;
	
	if(top)
		$("#Top .Hor .Barre").show().css({opacity: 0, 'margin-top': 115}).animate({height: 115, 'margin-top': 0, opacity: 1}, duration);
	
	if(centerRight)
		$("#Center .Long .Barre").show().css({opacity: 0, 'margin-left': 620, height: 1}).animate({ width: 620, 'margin-left': 0, opacity: 1}, duration);
		
	if(centerLeft)
		$("#Center .Right .Vert .Barre").show().css({opacity: 0, width: 0}).animate({width: 115, opacity: 1}, duration);

	if(bottom)
		$("#Bottom").find(".Hor .Barre").show().css({opacity: 0, height: 0}).animate({height: 115, opacity: 1}, duration);	
}

function showElements(duration) {
	
	delay = 0;
	
	$("#Bottom").find('.Right').find('#List li, #List h1').each(function() {
		$(this).css({opacity: 0});
		
		if($(this).attr('name') !== undefined)
			$(this).delay(delay).animate({opacity: 0.35}, 300);
		else
			$(this).delay(delay).animate({opacity: 0.8}, 300);
			
		delay = delay + 50;
	});
	
	
	$("#Bottom").find("#List li").hover(
		function() {
			$(this).css({opacity: 0.8});	  
		}, 
		 
		function() {		
			if($(this).attr('rel') != 'clicked')
				 $(this).animate({opacity: 0.3}, 1200);
		}
	);

	showButtonsList();

	//$("#Bottom").find('.Left').css({opacity: 0}).animate({opacity: 1}, 1800);
}


function stopScroll() {
	clearInterval(intervalScroll);
	$('html').unbind('mouseup', stopScroll);
}

function showButtonsList() {

	if($("#Bottom .Right .Container #List").height() > $("#Bottom .Right .Container").height()) {			
		$('<div id="Up" class="Button"></div><div id="Down" class="Button"></div>').insertAfter($("#Bottom .Right .Container")).hide().delay(3000).fadeIn();
		
		$("#Up").mousedown(function() {
			$('html').bind('mouseup', stopScroll);
			intervalScroll = setInterval(monterListe, 1);
			monterListe();
		});
										
		$("#Down").mousedown(function() {
			$('html').bind('mouseup', stopScroll);
			intervalScroll = setInterval(descendreListe, 1);
			descendreListe();
		});
	}
}

var iteration = 0;
function descendreListe() {
	marginTop = $("#Bottom .Right #List").css('margin-top').replace('px', '');
	/*console.info(marginTop);
	console.info(- marginTop - -$("#Bottom .Right .Container").height());
	console.info($("#Bottom .Right #List").height()+30);*/
	/*+30*/
	if((- marginTop - -$("#Bottom .Right .Container").height()) < ($("#Bottom .Right #List").height())) {
		
		iteration = iteration + 1;
		if(iteration > 100)
			step = 3;
		else
			step = 2;
		$("#Bottom .Right ul#List").css({'margin-top':  marginTop - step});
	}
};


function monterListe() {
	marginTop = $("#Bottom .Right #List").css('margin-top').replace('px', '');
	if(marginTop < 0) {
		$("#Bottom .Right ul#List").css({'margin-top':  marginTop - (-2)});
	}
};



function loadPage(page, montrerBarres, clbk, id) {
	
	var pages = ['bio', 'work', 'contact', 'slideshow', 'ie7'];

	if(page == null) {
		var href = document.location.href;
		regex = /.*\#(.*)\.html/;
		var match = regex.exec(href);
	
		if(match != null)
			var page = match[1];
		else
			var page = 'work';
	}
    
	$('body').data('page', page);	
	
	$.ajax({
		
		url: 'loader.php',
		data: {page: page, id: id},
		timeout: 20000,
		dataType: 'html',
		
		beforeSend: function() {
			$("#Bottom").css({opacity: 0});
		},
		
		success: function(toInsert) {
			
			$(toInsert).find("#List li, #List h1").css({opacity: 0});
			$("#Bottom").html(toInsert).css({opacity: 1});
			
			if(page == 'contact') {
				showBarres(true, true, true, false);
				loadMail(email);	
			} else if(page == 'videos') {
				showBarres(true, true, true, false);	
			} else if(page == 'slideshow') {
				showBarres(true, true, true, false);
			} else {
				showBarres(true, false, true, true);
				$("#Top .Left").children().fadeOut('slow');
			}
			
			if(montrerBarres) {
				$("#Bottom").find("#List li, #List h1").css({opacity: 0});
				showElements(1000);
			}
			
			if($.isFunction(clbk)) {
				alert(clbk);
				clbk.call(this);	
			}
		},
		
		error: function(response, error) {

		}
	});
}


function loadMail(email) {
	
	if(typeof email !== undefined) {
		$("#Top .Left").html('<span id="email"><a href="mailto:' + email + '">' + email + '</a></span>').find('#email').hide().delay(1000).fadeIn('slow').hover(function() {
			$(this).animate({opacity: 1});
		 }, function() {
			$(this).animate({opacity: 0.35});
		});
	}
}

function randOrd() {
	return (Math.round(Math.random())-0.5); 
}

function loadIndex(callback) {
	
	var href = document.location.href;
	regexAnchor = /.*#([a-z]*)\.html$/;
	
	if(result = regexAnchor.exec(href))
		page = result[1]
	else
		page = 'work';
		
	$('body').data('animate', true);

	$("#Animation .Barre, .Title").show().css({opacity: 0});
	barres = [  2,9,21,10,4,15,5,11,6,17,0,7,18,13,1,16,12,19,20,14,3,8];
	//barres.sort(randOrd);

	barresSaved = barres.slice();
	delay = 0;
	for(i = 0; i <= 21; i = i + 1) {
		
		barreId = barres[0];
		barres.shift();
			delay = delay + 60;
		$("#Animation .Barre:eq(" + barreId + ")").delay(delay).animate({opacity: 0.7});

		if(i == 20) {
			delay = delay + 80;
			$("#Animation .Title").delay(delay).animate({opacity: 0.7}, function() { $('body').data('animate', false); });			
		}
	}
	
	$('#Animation .Title').click(function() {
		
		$('body').data('animate', true);
		barresSaved = barresSaved.reverse();
		delay = 0;
		
		i = 0;
		for(i = 0; i <= 21; i = i + 1) {

			if(i == 0) {
				$("#Animation .Title").delay(delay).animate({opacity: 0});	
				delay = delay + 80;
			}
			
			barreId = barresSaved[0];
			barresSaved.shift();	
			iteration = 0;
			$("#Animation .Barre:eq(" + barreId + ")").delay(delay).animate({opacity: 0}, function() { 
				if(iteration == 20) {
				   $('body').data('animate', false); 																							

				   $.ajax({
					  url: 'preloader.php',
					  dataType: 'json',
					  timeout: 15000,
					  success: function(data) {
						  
						  	if(page !== undefined) {
								
								email = data.data.email;
								if(page == 'contact') {
									loadMail(data.data.email);
								}
							}

							preLoadImages(data.images, function() {
								if(typeof callback == 'function')
									callback.call(this);							
							});							
						}
					});
				}
				
				iteration += 1;
			});
			
			delay = delay + 60;
		}			   
	});
	
	$(".SpacerHor, .SpacerVert").hover(function() {
		if(!$('body').data('animate'))
			$(this).clearQueue().animate({opacity: 0.35});															
	}, function() {
		if(!$('body').data('animate'))
			$(this).clearQueue().animate({opacity: 0.7});		

	});
		
	loadPage(page, false);
}

(function($) {
		  
$.fn.tagName = function() {
    return this.each(function() {
        return this.tagName;
    });
}

$.fn.invert = function() {
  return this.end().not(this);
};



});
