/**
 * This is re-called after ajax loads to initialise new content too
 */

function initJS(){
	Cufon.replace('#research, #projects, #studio, #contact, h2, h3, h4', {
	    fontFamily: 'Rotis Sans Serif Std'
	});
}

initJS();



/**
 * Function to trigger whenever the page loads or a history-enabled link is clicked
 * @param {Object} hash - the URL to load
 */

function pageLoad(hash) {
	if(hash) {
		// Load the location specified after the hash.
		$('#popup_loading').show();
		$("#popup_ajax").hide().load(hash,function(){
			initJS();
			$(this).fadeIn();
			$('#popup_loading').fadeOut();
			initCloseButton();
		});
		$("#popup").show();
		$('#screen_trigger').css('display','block');
		$('#screen_trigger').animate({opacity: "0.5"},"normal");
		$(".project").hide();
	} else {
		// Don't load anything. 
		//$("#popup_ajax").empty();
	}
}

$(document).ready(function() {
    
	$.historyInit(pageLoad);
	
	$(".second a").click(function(event){
		$('.second a').each(function(){
			if($(this).get(0) != event.currentTarget) {
				$(this).removeClass('selected');
			} else {
				$(this).addClass('selected');
			}
		})
		var hash = $(this).attr('href');
		$.historyLoad(hash);
		return false;
	});
	
	$("#projects_list a, .pagination a").live("click",function(event){
		var hash = $(this).attr('href');
		$.historyLoad(hash);
		return false;
	});
    
    initCloseButton();
    
    $('.nav > li ul').hide();

    
    $('li.top').click(function(event) {
        event.preventDefault();
		$('li.top').each(function(){
			if($(this).get(0) != event.currentTarget) {
				$(this).children('ul').hide();
			}
		})
        $(this).children('ul:hidden').show('blind', 'normal');
    });

});

function initCloseButton() {
	$('#popup_close').click(function() {
        $('#popup').fadeOut('normal');
        $('#projects_menu a').removeClass('selected');
        $('#screen_trigger').animate({opacity: "0"}, "fast",'linear',function(){$(this).css('display','none')});
    });
}
