$(function() {
	var images = new Array('images/bg.gif', 'images/logo.png', 'images/ajax-loader.gif');
	var loaded = false;
	var active = false;
	var value;
	
	$('#nav a, #content a').live('mouseover mouseout', function(event) {
		if (event.type == 'mouseover')
			$(this).animate({color: '#333'}, 200);
		else
			if (!$(this).is('.selected'))
				$(this).animate({color: '#666'}, 200);
	});
	
	$('#nav a').click(function() {
		$.address.value($(this).attr('href'));
	});
	
	$('.thumb').live('mouseover mouseout', function(event) {
		if (event.type == 'mouseover') {
			$(this).find('.white').animate({opacity: 0}, 200);
			$(this).parent().animate({color: '#ff0000'}, 200);
		} else {
			$(this).find('.white').animate({opacity: .1}, 200);
			$(this).parent().animate({color: '#666'}, 200);
		}
	});
	
	preload_image(0);
	
	function preload_image(id) {
		var image = new Image();
		
		image.src = images[id];
		image.onload = function() {
			if (id + 1 < images.length) {
				id++;
				
				preload_image(id);
			} else {
				loaded = true;
				
				$('#loader').fadeOut(200);
				
				init();
			}
		}
	}
	
	function init() {
		$('#header-bg').delay(400).fadeIn(400);
		$('#bar').delay(400).animate({width: 197}, 400);
		$('#logo').delay(600).fadeIn(400);
		$('#nav a').delay(800).each(function(index) {
			$(this).delay(index * 20).fadeIn(200);				  
		});
		if ($(window).width() > 1000)
			$('#social').delay(1000).fadeIn(400);
		$('#footer').delay(1200).fadeIn(400);
		
		load_page();
	}
	
	$.address.change(function(event) {
		$('#nav a').each(function() {
			if ($(this).attr('href') == event.value) {
				$(this).addClass('selected');
			} else {
				$(this).removeClass('selected');
				$(this).animate({color: '#666'}, 200);
			}
		});
		
		value = event.value.replace('/', '');
		
		if (!window.location.hash)
			$.address.value('/web');
		
		if (loaded)
			load_page();
	});
	
	function load_page() {
		if (active) {
			var delay = 0;
		} else {
			var delay = 920;
			active = true;
		}
		
		$('#main').delay(delay).fadeOut(200, function() {
			if (value.indexOf('/') != -1) {
				var index = value.indexOf('/');
				var section = value.substring(0, index);
				var item = value.substring(index + 1, value.length);
				
				$('#main').load('item.php?section=' + section + '&item=' + item, function() {
					onResize();
					
					$('.item').each(function(index) {
						$(this).children('img').css('display', 'none');
					});
					
					$('#main').fadeIn(200);
					
					load_image(0, 'big');
				});
			} else {
				if (value == 'about' || value == 'resume')
					$('#main').load(value + '.php', function() {
						onResize();
						
						$(this).fadeIn(200);
					});
				else
					$('#main').load('section.php?section=' + value, function() {
						onResize();
						
						$('#main').show();
						
						$('.item').each(function(index) {
							$(this).css('display', 'none');
							$(this).delay(index * 40).fadeIn(200);
						});
						
						load_image(0, 'small');
					});
			}
		});
	}
	
	function load_image(index, size) {
		if (index < $('img').length)
			$('img').eq(index).one('load', function() {
				$(this).fadeIn(200);
				
				index++;
				
				load_image(index, size);
			}).each(function() {
				if (this.complete)
					$(this).trigger('load');
			});
        else if (size == 'big')
			$('#content-loader').fadeOut(200);
	}
});

$(window).resize(function() {
	onResize();
});

function onResize() {
	var width = $(window).width();
	var padding = Math.round(width * .05);
	var thumb_width = 200;
	var thumb_margin = 20;
	var items = Math.floor((width - padding) / (thumb_width + thumb_margin));
	var new_width = items * (thumb_width + thumb_margin) + thumb_margin;	
	
	if (width > 1000)
		$('#social').show();
	else
		$('#social').hide();
	
	$('#items').css('width', new_width);
	
	if (width > 800)
		$('#content').css('width', 640);
	else
		$('#content').css('width', '');
}
