$(document).ready(function() {
//	links();
	preview();
});

var pages = [];

function links() {
	
	var nav = document.getElementById('nav');
	nav.links = nav.getElementsByTagName('a');
	for (var i=0,j=nav.links.length; i<j; i++) {
		var link = nav.links[i];
		
		link.ajaxName = link.href.replace(document.location.protocol + '//' + document.location.hostname + '/','');
		pages[link.ajaxName] = link.href;
	}
	
}

function preview() {
	$('.portfolio').each(function(i) {
		var portfolio = $('.portfolio')[i];
		$(portfolio).find('a').each(function(j) {
			var preview = $(portfolio).find('a')[j];
			preview.portfolio = portfolio;
			$(preview).click(function(event) {
				event.preventDefault();
				
				var id = this.id + '_full';
				
				if (!document.getElementById(id)) {
					var full = document.createElement('div');
					$(this.portfolio.parentNode).prepend(full);
					full.portfolio = this.portfolio;
					full.style.visibility = 'hidden';
					full.style.position = 'absolute';
					full.id = this.id + '_full';
					full.className = 'portfolio_full';
					
					full.h2 = document.createElement('h2');
					full.appendChild(full.h2);
					full.h2.innerHTML = this.title;
					$(full.h2).hide();
					
					full.close = document.createElement('p');
					full.appendChild(full.close);
					full.close.className = 'portfolio_full_close';
					full.close.a = document.createElement('a');
					full.close.appendChild(full.close.a);
					full.close.a.href = '#' + this.id;
					full.close.a.innerHTML = 'Close';
					$(full.close).hide();
					
					full.img = new Image();
					full.appendChild(full.img);
					full.img.src = this.href;
					
					if (full.img.complete) {
						preview_show(full);
					}
					else {
						$(full.img).load(function() {
							preview_show(full);
						});
					}
					
					$(full).click(function(event) {
						event.preventDefault();
						preview_hide(this);
					});
				}
				else {
					var full = document.getElementById(id);
					preview_show(full);
				}
				
			});
		});
	});
}

function preview_show(full) {
	$(full.portfolio).slideUp('fast');
	$(full).hide();
	full.style.visibility = 'visible';
	full.style.position = 'relative';
	$(full).slideDown('slow');
	$(full.h2).fadeIn('fast');
	$(full.close).fadeIn('slow');
}

function preview_hide(full) {
	$(full).slideUp('medium',function() {
		$(full.close).hide();
		$(full.h2).hide();
	});
	$(full.portfolio).slideDown('medium');
}
