// JavaScript Document
function pageLoad(hash) {
	// alert("pageload: " + hash);
	// hash doesn't contain the first # character.
	if(hash) {
		// restore ajax loaded state
		if($.browser.msie) {
			// jquery's $.load() function does't work when hash include special characters like aao.
			hash = encodeURIComponent(hash);
		}
		hash = hash.split('/');
		section = hash[0];
		subsection = hash[1];
		switch(section) {
			case 'about':
			width = '20em';
			break;
			case 'blog':
			width = '45em';
			break;
			case 'projects':
			width = 190;
			break;
			case 'contact':
			width = 170;
			break;
		}
		//Reveal Section
		$this = $('#'+section);
		$content = $('#'+section+'Content');;
		$('.sectionContent:visible').not($content).slideUp(500);
		$('#about, #blog, #projects, #contact').each(function() {
			$(this).text($(this).text().replace(/[–<]/,'+'));
		});
		if (section=='blog') {
			if (subsection) {
				$('#blog').text('< Blog');
				blogLoad('blog/' + subsection);
			} else {
				$this.text($this.text().replace('<','–'));
				blogLoad('blog');
			}
			$('#images').fadeTo(500,0.5);
		} else {
			if ($('#images').css('opacity') != 1) {
				$('#images').fadeTo(500,1);
			}
			if (section == 'projects' && subsection) {
				var winHeight = $(window).height();
				var $active = $('#images ul.active');;
				var $next =  $('#images #' + subsection);
				if (!$('#images #' + subsection).hasClass('active')) {
					getInfo(subsection);
					$next.css({top:winHeight, height: 0}).addClass('active').animate({top:0, height:'100%'}, 500);
					$active.animate({top:-winHeight}, 500, function() {
						$(this).removeClass('active').hide();
						$('.active', $active).removeClass('active').hide();
						$('li:first', $active).addClass('active').show();
					});
				}
			}
		}
		$this.text($this.text().replace('+','–'));
		if (!$content.isVisible()) {
			$('#sections').animate({width:width}, 500);
			$content.delay(500).slideDown(500);
		}
	} else {
		//Close Sections
		if ($('#images').css('opacity') != 1) {
			$('#images').fadeTo(500,1);
		}
		$('h1 a').each(function() {
			$(this).text($(this).text().replace('–','+'))
		});
		$('.sectionContent:visible').slideUp(500);
		$('#sections').delay(500).animate({width:'7.5em'}, 500);
	}
}
function nextImage() {
	var $project = $('#images ul.active');;
    var $active = $('li.active', $project);
	if (!$active.next().length) {
		var winHeight = $(window).height();
		var $nextProject =  $project.next().length ? $project.next() : $('#images ul:first');
		subsection = $nextProject.attr('id');
		getInfo(subsection);
		$nextProject.css({top:winHeight, height: 0}).addClass('active').show().animate({top:0, height:'100%'}, 500);
		$('li:first', $nextProject).css({left: 0}).addClass('active').show();
		$project.animate({top:-winHeight}, 500, function() {
			$(this).removeClass('active').hide();
    		$active.removeClass('active').hide();
    		$('li:first', $project).addClass('active').show();
		});
	} else {
		var winWidth = $(window).width();
		var $next = $active.next();
		$next.css({left: winWidth}).addClass('active').show().animate({left: 0}, 500, function() {
			$active.removeClass('active');
		});
	}
}
function prevImage() {
	var $project = $('#images ul.active');;
    var $active = $('li.active', $project);
	if (!$active.prev().length) {
		var winHeight = $(window).height();
		var $nextProject =  $project.prev().length ? $project.prev() : $('#images ul:last');
		subsection = $nextProject.attr('id');
		getInfo(subsection);
		$('.active', $nextProject).removeClass('active').hide();
		$('li:last', $nextProject).css({left: 0}).addClass('active').show();
		$nextProject.css({top:-winHeight}).addClass('active').show().animate({top:0, height: '100%'}, 500);
		$project.animate({top:winHeight, height: 0}, 500, function() {
			$(this).removeClass('active').hide();
		});
	} else {
		var winWidth = $(window).width();
		var $next =  $active.prev();
		$next.addClass('active').show();
		$active.animate({left: winWidth}, 500, function() {
			$(this).removeClass('active');
		});
	}
}
function resizeImage() {
	var $image = $('#images img');
	var $content = $('.sectionContent');
	var winWidth = $(window).width();
	var winHeight = $(window).height();
	maxHeight = winHeight - 160;
	$content.maxSize({height: maxHeight});
	var aspect = winWidth / winHeight;
	if (aspect > (14/9)) {
		var newHeight = (winWidth / 14) * 9;
		var newWidth = winWidth;
	} else {
		var newWidth = (winHeight / 9) * 14;
		var newHeight = winHeight;
	}
	newTop = 0 - ((newHeight - winHeight) / 2);
	newLeft =  0 - ((newWidth - winWidth) / 2);
	$image.css({top: newTop, left: newLeft});
	$image.css({height: newHeight, width: newWidth});
}
function blogSuccess(data) {
	$('#loading').delay(500).fadeOut();
	$('#blogContent').css('height',$('#blogContent').height()).animate({height:1000},1000);
	$('#blogAjax').html(data).delay(1000).fadeIn(500);
}
function blogLoad(url) {
	if($('#blogAjax').isVisible()) {
		$('#blogAjax').hide();
		$('#loading').fadeIn();
	}
	$.get(url, { ajax: "" }, blogSuccess);
}
function getInfo(subsection) {
	$.get('info.php',{project:subsection}, function(data) {
		if (data == '') {
			$('#infoText').text('');
			$('#info').hide();
		} else {
			$('#infoText').html(data);
			$('#info').fadeIn();
		}
	});
}
function interact() {
	$.historyInit(pageLoad, "index.php");
	//$('#blogContent').scrollbarPaper();
	// Remove dotted line in IE
	$('a').click(function() {
  		this.blur();
	});
	setTimeout("resizeImage()",1);
	setTimeout("resizeImage()",100);
	$('#about').click(function() {
		if ($('#aboutContent').isVisible()) $.historyLoad('');
		else $.historyLoad('about');
		return false;
	});
	$('#blog').click(function() {
		if ($(this).text() == '– Blog') $.historyLoad('');
		else $.historyLoad('blog');
		return false;
	});
	$('#projects').click(function() {
		if ($('#projectsContent').isVisible()) $.historyLoad('');
		else $.historyLoad('projects');
		return false;
	});
		$('#contact').click(function() {
		if ($('#contactContent').isVisible()) $.historyLoad('');
		else $.historyLoad('contact');
		return false;
	});
	$('#next').click(function() {
		nextImage();
		return false;
	});
	$('#prev').click(function() {
		prevImage();
		return false;
	});
	$(document).keydown(function(key) {
		if (key.which == 37) {
			prevImage();
		} else if (key.which == 39) {
			nextImage();
		}
	});
	$('#blogAjax a.article').live('click', function() {
		url = $(this).attr('href').split('/');
		article = url[url.length - 2];
		$.historyLoad('blog/' + article);
		return false;
	});
	$('#blogAjax a.live').live('click', function() {
		$('#blog').text('< Blog');
		blogLoad($(this).attr('href'));
		return false;
	});
	$('#commentform').live('submit', function() {
		$('#blogAjax').hide();
		$('#loading').fadeIn();
		$.post($(this).attr('action'), $(this).serialize(), blogSuccess);
		return false;
	});
	$('#projectlist li a').click(function() {
		project = $(this).attr('href');
		$.historyLoad(project);
		return false;
	});
	$('#projectlist li').hover(function() {
		$('.projectinfo', this).show().css({opacity:0.5});
	}, function() {
		$('.projectinfo', this).hide();
	});
	$('#uniquesite').hover(function() {
		$(this).stop().fadeTo(500, 1);
	}, function() {
		$(this).stop().fadeTo(500, 0.25);
	});
	$('#info .i').bind('mouseenter', function() {
		$('#infoText').stop().fadeTo(500,1);
	});
	$('#infoText').bind('mouseleave', function() {
		$('#infoText').stop().fadeTo(500,0);	
	});
	$('#info .i').click(function() {
		return false;
	});
}
$(window).resize(function(){
	resizeImage();
 });
$(document).ready(function() {interact();});
