/*
	@Class:		PreviewTrack
	@Author:		Brandon Gray for O3 World 2009
	@Brief:		Handles placement and removal of flash audio preview for tracks.  See comments for more details.
	@Requirements:	Mootools 1.2			
*/
var PreviewTrack = new Class({

	initialize: function( ) {
		
		// ID of element the flash preview will be embedded within
		this.containerID;
		this.tracks = $$( '#music li' );
		
		for ( i = 0; i < this.tracks.length; i++ ) {
			
			var listID 	= this.tracks[ i ].get( 'id' );
			var playBtn 	= this.tracks[ i ].getElement( 'a.play' );
			
			playBtn.addEvent( 'click', this.removePreview.bindWithEvent( this, [ listID ] ) );
			
		}
		
	},
	
	removePreview: function( event, listID, flashListID ) {
					
		// Loop through all 'tracks' and if there is an OBJECT element, remove it from the DOM and redisplay info DIV element
		for ( i = 0; i < this.tracks.length; i++ ) {
		
			if ( this.tracks[ i ].getElement( 'object' ) ) {
				
				// Remove any OBJECT element from the DOM
				var flashObject = this.tracks[ i ].getElement( 'object' );
				this.tracks[ i ].removeChild( flashObject );
				
				// Redisplay the play button
				this.tracks[ i ].getChildren( 'a.play' ).setStyle( 'display', 'block' );
				
			}
			
		}
		
		this.initPreview( listID );
	
		event.stop();
		
	},
	
	initPreview: function( listID ) {
				
		// Hide the play button
		$( listID ).getChildren( 'a.play' ).setStyle( 'display', 'none' );
		
		// Set the ID of the element flash preview will be embedded within
		this.containerID = listID + '_preview';
		
		// Create element that flash preview will be embedded within
		var containerEl = new Element( 'div', { 'id': this.containerID } );
		
		// Inject the container as the first element into the list element
		containerEl.inject( $( listID ), 'top' );
		
		this.embedFlash( listID );
		
	},
	
	embedFlash: function( listID ) {
		
		// Variable to hold the track path/file being previewed
		var trackPath = $( listID ).getElement( 'a.play' ).get( 'rel' );
		
		var flashvars 			= {};
		flashvars.songPath		= trackPath;
		flashvars.listElement	= listID;		// important to pass the listID so we can remove the proper preview upon flash callback
		var params    			= {};
		params.wmode 			= 'transparent';
		params.menu 			= 'false';
		params.allowscriptaccess = 'always';
		var attributes 		= {};
		attributes.id			= 'preview_object';
		swfobject.embedSWF( 'flash/audio_preview.swf', this.containerID, '120', '30', '9.0.0', '', flashvars, params, attributes );
		
	}

});

/*
	@Class:		NavInterface
	@Author:		Brandon Gray for O3 World 2010
	@Brief:		Handles visual transitions for main navigation
	@Requirements:	Mootools 1.2			
*/
var NavInterface = new Class ({
	
	initialize: function() {
		
		menuLinks = $$( '#nav a' );
		
		menuLinks.each( function( element, i ) {
			
			element.set({
					  
				tween:  { 
					duration: 250
				},
				events: {
					'mouseover': function() {
						if( element.get( 'class' ) != 'active' ) element.tween( 'width', '159' );
					},
					'mouseout' : function() {
						if( element.get( 'class' ) != 'active' ) element.tween( 'width', '149' );
					},
					'click': function() {
						for ( var j = 0; j < menuLinks.length; j++ ) {
							menuLinks[ j ].removeClass( 'active' );
							menuLinks[ j ].setStyle( 'width', '149px' );
						}
						this.addClass( 'active' );
						this.setStyle( 'width', '159px' );
					}
				}
				
			});
			
		});
		
	}
	
});

//FOOTER FLICKR API CALLBACK HANDLER
function footPhotosHandler( data ) {
	
	var columns	= 4;
	var photo 	= data.photos.photo;
	
	photo.each( function( element, i ) {
		
		var thumbURL 		= 'http://farm' + element.farm + '.static.flickr.com/' + element.server + '/' + element.id + '_' + element.secret + '_s.jpg';
		var thumbList 		= new Element ( 'li' );
		var thumbAnchor 	= new Element( 'a', {
		
			'href':	'http://farm'  + element.farm + '.static.flickr.com/' + element.server + '/' + element.id + '_' + element.secret + '.jpg',
			'rel':	'lightbox[footPhotos]',
			'id':	element.id
			
		});
		
		var thumbIMG 		= new Element( 'img', {
		
			'height': '75',
			'width':	'75',
			'alt': 	element.title,
			'src':	thumbURL
		
		});
		
		thumbIMG.inject( thumbAnchor );
		thumbAnchor.inject( thumbList );
		thumbList.inject( $( 'photos' ) );
			
	});
	
	window.Mediabox.scanPage();
	
}

// FOOTER FLICKR API REQUEST
function footerFlickrRequest() {
	
	var setRequest = new Request.JSONP({
		
		url: 'http://api.flickr.com/services/rest/',
		data: {
			method:		'flickr.people.getPublicPhotos',
			api_key: 		'7e77b051f92a2566fce981cd8e1499cf',
			user_id: 		'47911147@N02',
			per_page:		8,
			format: 		'json',
			jsoncallback: 	'footPhotosHandler'
		}
		
	}).send();
	
}

// TWITTER REQUEST
function relativeTime( x ) {
	
	var y = x.split( ' ' );
	x = Date.parse( y[ 1 ] + ' ' + y[ 2 ] + ', ' + y[ 5 ] + ' ' + y[ 3 ]);
	y = ( arguments.length > 1 ) ? arguments[ 1 ] : new Date();
	x = parseInt( ( y.getTime() - x ) / 1000 );
	x = x + ( y.getTimezoneOffset() * 60 );
	
	if ( x < 60 ) { 
		return 'less than a minute ago'; 
	} else if ( x < 120 ) { 
		return 'about a minute ago';
	} else if ( x < ( 60 * 60) ) { 
		return ( parseInt( x / 60 ) ).toString() + ' minutes ago';
	} else if ( x < ( 120 * 60 ) ) {
		return 'about an hour ago';
	} else if ( x < ( 24 * 60 * 60 ) ) {
		return 'about ' + ( parseInt( x / 3600 ) ).toString() + ' hours ago';
	} else if ( x < ( 48 * 60 * 60 ) ) {
		return '1 day ago';
	}
	
	return ( parseInt( x / 86400 ) ).toString() + ' days ago';
	
}
function twitterCallback( twttr ) {
	
	var a = [];
	for ( var i = 0; i < twttr.length; i++ ) {
		
		var usr = twttr[ i ].user.screen_name;
		var st = twttr[ i ].text.replace( /((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function ( url ) {
			return '<a href="' + url + '">' + url + '</a>';
		}).replace( /\B@([_a-z0-9]+)/ig, function ( reply ) {
			return reply.charAt( 0 ) + '<a target="_blank" href="http://www.twitter.com/' + reply.substring( 1 ) + '">' + reply.substring( 1 ) + '</a>';
		});
		
		if ( i != 0 ) { a.push('<li class="rule"></li>'); }
		a.push( '<li>' + st + '<span>' + relativeTime( twttr[ i ].created_at ) + '</span></li>' );
		//<a href="http://twitter.com/' + usr + '/statuses/' + twttr[i].id + '" target="_blank">x</a>
	}
    
	// document.getElementById( 'twitter_nomad' ).innerHTML = a.join( '\n' );
	$( 'twitter_nomad' ).set( 'html', a.join( '\n' ) );
}

window.addEvent( 'domready', function() {
	
	// IE6 doesn't support fixed position elements so the nav is set to position absolute and the 'top' value needs to be updated with the scroll position.
	if ( Browser.Engine.trident4 ) {
		
		var scroll = window.getScroll();
		$( 'nav' ).setStyle( 'top', scroll.y + 73 );
		
		window.addEvent( 'scroll', function() {
									 
			var scroll = window.getScroll();
			$( 'nav' ).setStyle( 'top', scroll.y + 73 );


		});
		
	}
	
	new SmoothScroll( { duration: 700 }, window );
	
	var navSetup = new NavInterface();
	var tracks = new PreviewTrack();
	
});

window.addEvent( 'load', function() {

	// Insert Flickr photos
	footerFlickrRequest();
	
});
