/*
	@Class:		MMSGallery
	@Author:		Brandon Gray 2010
	@Brief:		Loads thumbnails from Flickr photo set
	@Requirements:	Mootools 1.2
*/


var MMSGallery = new Class({
					   
	Implements: [Options],

	options: {
		page: 1
	},
	
	initialize: function( options )  {
		
		// Set options
		this.setOptions( options );
		
		this.flickrData;
		this.photos;
		this.photoSet;
		this.total;
		this.timer;
		this.totalPages;
		
		this.url 		= window.location.href;
		this.urlSplit 	= this.url.split( '.com/' );
		//this.urlSplit 	= this.url.split( 'trunk/' );
		
		// Which photoset?
		if ( this.urlSplit[1] == 'index.cfm' || this.urlSplit[1] == '' ) {
			this.photoSet = '72157626070675493';
		} else if ( this.urlSplit[1] == 'architecture.cfm' ) {
			this.photoSet = '72157625090988678';
		} else if ( this.urlSplit[1] == 'anything-goes.cfm' ) {
			this.photoSet = '72157624064770445';
		};
		
		// Time to fetch the data from the Flickr API
		this.fetchData();
		
		// Pagination
		this.pagination();
		
	},
	
	fetchData: function() {
		
		this.setRequest = new Request.JSONP({
		
			url: 'http://api.flickr.com/services/rest/',
			data: {
				method:			'flickr.photosets.getPhotos',
				api_key: 			'4e8fb601fd9f49759de9782690c34c4e',
				photoset_id:		this.photoSet,
				format: 			'json',
				nojsoncallback:	'1',
				page: 			this.options.page,
				per_page: 		56
			},
			callbackKey: 'jsoncallback',
			onComplete: this.receiveData.bind( this )
			
		}).send();
		
	},
	
	receiveData: function( response ) {
		
		this.flickrData 	= response;
		this.photos 		= this.flickrData.photoset.photo;
		this.total		= this.flickrData.photoset.total;
		this.totalPages 	= this.flickrData.photoset.pages;

		/*
		HTML Structure
		
		<li>
			<a href="##"><img src="img/fpo/thumb_75x75.jpg" width="75" height="75" alt="fpo" /></a>
		</li>
		*/

		this.photos.each( function( element, i ) {
			
			var title 		= element.title;
			var titleArray 	= title.split( ':' );
			var thumbList 		= new Element ( 'li' );
			var imageAnchor 	= new Element( 'a', { 
				
				'href': 	'http://farm'  + element.farm + '.static.flickr.com/' + element.server + '/' + element.id + '_' + element.secret + '.jpg',
				'title': 	titleArray[0] + '::' + titleArray[1],
				'rel': 	'lightbox[set1]'
				
			});
			var thumbImg 		= new Element( 'img', {
			
				'width':	'75',
				'height': '75',
				'alt': 	element.title,
				'src':	'http://farm' + element.farm + '.static.flickr.com/' + element.server + '/' + element.id + '_' + element.secret + '_s.jpg'
			
			});
			
			thumbImg.inject( imageAnchor );
			imageAnchor.inject( thumbList, 'top' );
			thumbList.inject( $( 'gallery' ) );
				
		});
		
		( this.options.page > 1 ) ? $( 'pg_prev' ).setStyle( 'display', 'block' ) : $( 'pg_prev' ).setStyle( 'display', 'none' );
		( this.options.page < this.totalPages ) ? $( 'pg_next' ).setStyle( 'display', 'block' ) : $( 'pg_next' ).setStyle( 'display', 'none' );

		// Mediabox
		Mediabox.scanPage();
		
	},
	
	pagination: function() {
		
		$( 'pg_prev' ).addEvent( 'click', function( event ) {
			
			this.options.page--;
			this.reinitialize();
			
			event.stop();
			
		}.bind( this ));
		
		$( 'pg_next' ).addEvent( 'click', function( event ) {
			
			this.options.page++;
			this.reinitialize();
			
			event.stop();
			
		}.bind( this ));
		
	},
	
	reinitialize: function() {
		
		// Empty the current thumbnails
		$( 'gallery' ).empty();
		
		// Fetch the new data
		this.fetchData();
		
	}
	
});

window.addEvent( 'domready', function() {
	
	if ( $( 'gallery' ) ) theGallery = new MMSGallery({ page: 1 });
	
});
