// Requires jQuery
$(function() {
	$('a.project, a.project-category, .project-text, .sub, a.press, a.press-category')
	.hide();	
	// For projects
	$('#project_template a.project-category, #project_template a.project').show();
	$('#project_template .top:last,#project_template .project-category:last').addClass('underline');
	// For practice
	$('#practice_template a.sub').show();
	$('#practice_template .top:last').addClass('underline');
	// For press
	$('#press_template a.press-category, #press_template a.press').show();
	$('#press_template .top:last,#press_template .press-category:last').addClass('underline');
	// Top menu
	$('.top').click(function(){
		$('a.project,a.project-category,a.sub,a.press,a.press-category').hide();	
		$('a').each(function(){
			$(this).removeClass('active');
		});
		$('.top:last').addClass('underline');
		$(this).addClass('active');
	});
	// Projects menu
	$('#projects').click(function(){
		$('.project-category').each(function(){
			if($.support.opacity)
				$(this).show('slow');
			else
				$(this).show();
		});
		return false;
	});
	// Projects category clicked
	$('.project-category').click(function(){
		$('.project').hide();
		$('.project-category').each(function(){
			$(this).removeClass('active');
		});
		$('.project-category:last').addClass('underline');
		$(this).addClass('active');
		var targets = $(this).attr('rel');
		$('.'+targets).each(function(){
			if($.support.opacity)
				$(this).show('slow');
			else
				$(this).show();					  
		});
		return false;
	});
	// Press menu
	$('#press').click(function(){
		$('.press-category').each(function(){
			if($.support.opacity)
				$(this).show('slow');
			else
				$(this).show();
		});
		return false;
	});
	// Press category clicked
	$('.press-category').click(function(){
		$('.press').hide();
		$('.press-category').each(function(){
			$(this).removeClass('active');
		});
		$('.press-category:last').addClass('underline');
		$(this).addClass('active');
		var targets = $(this).attr('rel');
		$('.'+targets).each(function(){
			if($.support.opacity)
				$(this).show('slow');
			else
				$(this).show();					  
		});
		return false;
	});
	// Practice menu
	$('#practice').click(function(){
		$('.sub-practice').each(function(){
			if($.support.opacity)
				$(this).show('slow');
			else
				$(this).show();
		});
		return false;
	});
	// Toggle the map on the contact page
	$('#viewmap').click(function(){
		if($('img.contact_2').css('display')=='none'){
			$('img.contact_1').hide();
			$('img.contact_2').show();
		} else {
			$('img.contact_2').hide();
			$('img.contact_1').show();			
		}
		return false;
	});
	// Image cycling using jQuery cycle lite
	// http://malsup.com/jquery/cycle/lite/
	$('.carousel-auto').cycle({ 
		timeout: 5000 
	});
	$('.carousel').cycle({ 
		prev:   '#arrow-left', 
		next:   '#arrow-right', 
		timeout: 0 
	});
	// Show/hide texts	
	$('#project_template .menu').bind('mouseleave',function(){
		if($.support.opacity)
			$(this).hide('fast');
		else
			$(this).hide();	
		if($.support.opacity)
			$('#project_template .project-text').show('slow');
		else
			$('#project_template .project-text').show();	
	});
	$('#project_template .project-text').bind('mouseenter',function(){
		if($.support.opacity)
			$(this).hide('fast');
		else
			$(this).hide();	
		if($.support.opacity)
			$('#project_template .menu').show('slow');
		else
			$('#project_template .menu').show();
	});
	// Set the width of the main content area
	setContentWidth();
	$(window).resize(function(){
		setContentWidth();
	});
	// Print disguised email addresses
	$('a.email').each(function(i) {
        var text = $(this).text();
        var address = text.replace("[at]", "@");
        $(this).attr('href', 'mailto:' + address);
        $(this).text(address);
    });
});
// Supporting functions
function setContentWidth() {
	if($('body').width()>1024) {
		$('.content').addClass('content-wide');
	}
	else $('.content').removeClass('content-wide');
}
function setCategory(i) {
	$('.project').hide();
	$('#projects').addClass('active');
	$('a[rel=p-cat-'+i+']').addClass('active');
	$('.p-cat-'+i).show();
}
function setPressCategory(i) {
	$('.press').hide();
	$('#press').addClass('active');
	$('a[rel=pr-cat-'+i+']').addClass('active');
	$('.pr-cat-'+i).show();
}