

var activateSubNav = function(links, tag) {
	links.removeClass('show');
	//var query = "#projects li[tags*=\"" + tag + "\"] a, #projects li[primary=\"" + tag + "\"] a";
	var query = "#projects a[tags*=\"" + tag + "\"], #projects a[primary=\"" + tag + "\"]";
	$(query).addClass('show');
};

var resetNav = function() {	
	$('#projects a').animate({color: '#fff'}, 7500).removeClass('show');	
	setTimeout("$('#projects a').removeAttr('style')",7500);	
	$('#project_types *').animate({color: '#EFCEBB', fontWeight: 'normal'}, 7500);	
	setTimeout("$('#project_types a').removeAttr('style').removeClass('active')",7500);
};


$(document).ready(function(){
	var body = $('body');
	var subNavLinks = $('#projects a');	

	/**
	 *	On properties pages, mark the primary tag and
	 *	show the related properties based on active item
	 */
	var activeItem = $('#active_item');
	var primary = "";
	var tags = "";
		
	if (activeItem != null && activeItem != undefined) {
		primary = activeItem.attr('primary');
		tags = activeItem.attr('tags');
	}
	
	if (primary != undefined && primary.length > 0) {
		var primary_query = "#project_types a[tag=\"" + primary + "\"]";
		$(primary_query).addClass('active');		
		activateSubNav(subNavLinks, primary);
	}	

	if (tags != undefined && tags.length > 0) {
		$(tags.split(' ')).each(function() {
			var query = "#project_types a[tag=\"" + this + "\"]";
			$(query).addClass('related');
		});
	}

	/**
	 *	Event delegation is used to get the clicks browsing properties.
	 */
	$('#project_types').click(function(e) {
		var el = $(e.target);
		if ( el.is('a') ) {
			e.preventDefault();		
			$('#project_types a').removeClass('active');
			el.addClass('active');
			activateSubNav(subNavLinks, el.attr('tag'));
			//setTimeout("resetNav()", 2500);
		}
	});

	/**
	 *	Event delegation is used to get the clicks browsing sub nav.
	 */
	$('#projects').click(function(e) {
		var el = $(e.target);
		if ( el.is('a') && !el.hasClass('show')) {			
			e.preventDefault();
		}
	});

	/**
	 *	Event delegation is used to get the clicks browsing people.
	 */
	$('#people_list').click(function(e) {
		var el = $(e.target);
		if ( el.is('a') ) {
			e.preventDefault();
			var personNumber = parseInt(el.attr('href').replace('#person', '')) - 1;
			
			// brian: this is not being used. i think we can improve this by using
			// what i did to handle all anchor links but i don't understand how
			// the nested slider controls work well enough to do so
			var query = "#inner_slider #people a[name='" + el.attr('href').replace('#', '') + "']";
			
			$('#slider_control').slider('moveTo', personNumber * 5);
			
			setTimeout(function() {
				var pos = -1 * personNumber * ($('#inner_slider').innerWidth() / 11);
				$('#inner_slider').animate( { left: pos } );
			}, 600);
		}
	});
	
	
	
	/**
	 *	Event delegation is used to get the anchor clicks from links
	 *	using the custom slider control.
	 */
	$('#slider').click(function(e) {
		var el = $(e.target);
		if ( el.is('a') ) {		
						
			var href = el.attr('href');			
			
			// only get involved if it's linking to an anchor tag
			// that is on this page (e.g. starts with #)
			if (href.indexOf('#') == 0 && _useSlider) {
				
				e.preventDefault();
				var anchor = "a[name='" + href.replace('#', '') + "']";					
				var left = $(anchor).offset().left;
				$('#slider_control').slider('moveTo', left);
			}
		}
	});	
	
});
