
(function($){

// TEAM CONTROLLER CLASS

Nike6.Team = new Class({
	
	Implements: Options,
	Binds: true,
	
	initialize: function(content, options, copy){
		this.setOptions(options);
		this.copy = copy;
		
		this.$content = $(content);
		
		JQ('#secondary div.toggle').live('click', function(){
			var $this = $(this);
			$this.siblings('.thumbs').fade('toggle');
			$this.find('span').toggle();
		});
		
		Nike6.hashChange(this.onChange);
	},
	
	onChange: function(event){
		
		Nike6.overlay(true);
		var sport = this.sport = event.pathNames[0] || 'bmx';
		var id = this.id = event.pathNames[1];
		
		if (id){
			
			this.$content.fetch(this.options.urls.bio, {athlete: id}, this.onFetch);
			
			// append this class so that the background shows
			$("#content-container").attr("class", "athletebio");

			
			Nike6.embed(this.options.bio_swf, {vars: {
				athlete: id,
				sport: sport,
				props: 'prop18:team,prop19:' + sport
			}});
			
		} else {
			
			this.$content.fetch(this.options.urls.sport, {sport: sport}, this.onFetch, function(){
				$(this).find('.thumb').each(function(){
					this.href = '#/{sport}/{athlete}'.substitute( Nike6.params(this.href) );
				});
			});
			
			Nike6.embedImage(this.options.imgs[sport]);
			
		}
		
		Nike6.track({
			pageName: '>' + sport + (id ? '>' + id : ''),
			prop18: sport,
			prop19: sport
		});
		
		// action tags on sport landing pages
		if (!id) {
			JQ(document).ready(function(){
				JQ("<img src='http://view.atdmt.com/action/NAS_FY10_" + sport + "'/>").insertAfter("body");
			});
		}

	},
	
	onFetch: function(){
		// local variable set up here for tracking purposes
		var sport = this.options.tags[this.sport];

		
		// snap to the top
		Nike6.scroll();
		Nike6.setTitle(this.$content.find('h1').text());
		Nike6.overlay(false);

		$('#news').fetch(this.options.urls.newsfeed, {
			sport: this.options.tags[this.sport],
			athlete: this.id ? this.id.replace('-', '_') : null // replace first dash
		}, function(){
			new Nike6.NewsFeed;
		});
		
		// Crossfade on hover-fade touts
		$('#content a.hover-fade img').hoverFade(600);
		
		// correct the tag for tout tracking
		if(sport == 'snowboard') {
			sport = 'snow';
		} else if (sport == 'freeride_bike') {
			sport = 'frb';
		}
		
		// add the open and close functionality to the interviews
		$(".toggle-btn").click(function(){
			var targetInterview = $(this).attr("target");
			$("#" + targetInterview).toggle(300);
			if ($(this).attr("class") == "toggle-btn open"){
				$(this).attr("class","toggle-btn close");
			} else {
				$(this).attr("class","toggle-btn open");
			}
		});
	}
	
});


})(jQuery);


