/*
---
Custom Portfolio Javascript. You can add custom javascript to your site's portfolio section here (Galleries and pages).
The allows you to make additions and tweaks without modifying the site's core files.

To Activate this file, change the filename from Portfolio-sample.js to Portfolio.js

copyrights:
  - [Kemso, Ember](http://kemso.com, http://emberpack.com)

licenses:
  - [MIT License](http://emberpack.com/license.txt)
...
*/

/*
*	You can override the default portfolio options here.
*	You can also create new fields in your Site Settings section for any of these options.
*/
window.portfolio_default_options = {
	
	/*
	*	Make the gallery resize to fit the current image
	*/
	auto_gallery_height: true,
	
	/*
	*	Enable mouse wheel scrolling. (Note this only works in browsers that support horizontal scrolling (mostly just on Macs).
	*/
	mouse_scroll: true,
	
	/*
	*	If mouse_scroll is on, snap to closet image when scroll is finished.
	*/
	mouse_scroll_snap: true,
	
	/*
	*	Make the gallery height pop to the height of the largest item while mouse scrolling
	*/
	gallery_height_on_mouse_scroll: false,
	
	/*
	*	Hide gallery navigation while scrolling
	*/
	mouse_scroll_hide_navigation: false,
	
	/*
	*	Use vertical arrow keys to change the current gallery
	*/
	change_page_with_arrows: true,
	
	/*
	*	Wait to load images until they enter the screen.
	*	If you have a "lazy_load" custom field in your galleries, it will override this setting
	*/
	lazy_load: true,
	
	/*
	*	Number of images to load ahead of the current image, (when lazy_load is turned on).
	*/
	lazy_load_count: 2,
	
	/*
	*	Preload any images before starting up the site?
	*/
	assets_to_preload: [
	]
}

// Page events.
window.addEvent('domready', function(){

	window.controller.addEvents({
		
		/* When the site is loaded and set up, but before any animation */
		'siteReady': function(){
		
		},
		
		/* When animation is complete */
		'postAnimation': function(){
		
		},
		
		'pageReady': function(page){
			switch(page.type)
			{
				case 'Gallery':
					handle_gallery(page);
					break;
				case 'Page':
					handle_page(page);
					break;
				case 'Contact':
					handle_contact(page);
					break;
			}
		}
	});

});


function handle_gallery(page)
{
	
	// Info button
	var info_button = new Element('a', {'href': '#', 'class': 'info', 'text': 'Info'}).inject($('content').getElement('.navigation'));
	info_button.addEvent('click', function(e){
		e.stop();
		if(page.gallery.gallery.current_item().getElement('.meta').get('text').trim() == '') page.gallery.gallery.meta.set('text', 'No info');
		if(page.gallery.gallery.meta.getStyle('opacity') == 0) page.gallery.gallery.meta.fade('in'); else page.gallery.gallery.meta.fade('out');
	}.bind(this));
	
	// Gallery sets image meta's to hidden to retain the height. We want them all the way off
	page.gallery.gallery.items.each(function(el){
		if(el.getElement('.meta')) el.getElement('.meta').setStyle('display', 'none');
	});
	
	
	/*
	*	Gallery events
	*/
	var on_current = function(el, index){
		// Info Button
		if(index == 0 || ! this.current_item().getElement('.meta') || this.current_item().getElement('.meta').get('text').trim() == '') info_button.off();
		else info_button.on();
	};
	
	var on_transition_end = function(){
		// update meta
		if(this.current_item().getElement('.meta'))
		{
			// Reset position of the meta area to the bottom of the image
			this.meta.setStyle('width', this.current_item().getElement('img').getSize().x - 30);
			this.meta.position({relativeTo: this.current_item().getElement('img'), position: 'bottomLeft', edge: 'bottomLeft'});
			
			// Stop any transition
			this.meta.get('morph').cancel()
			this.meta.get('tween').cancel();
		}
		
		// Thumbnail button
		if(this.current == 0) $('content').getElement('.gallery .navigation .title').off(); 
		else $('content').getElement('.gallery .navigation .title').on();
		
	};
	
	page.gallery.gallery.addEvents({
		'current': on_current,
		'transitionEnd': on_transition_end
	});
	
	
	// We got here late, so the current event has likely already been fired on the current image
	on_current.attempt([page.gallery.gallery.current_item(), page.gallery.gallery.current], page.gallery.gallery);
	on_transition_end.attempt([], page.gallery.gallery);
}


function handle_page(page)
{

}

function handle_contact(page)
{

}
