/*
---
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)
...
*/

// Make the gallery container height match the height of the current image.
window.auto_gallery_height = true;
window.gallery_height_on_mouse_scroll = true;

window.addEvent('domready', function(){

	window.controller.addEvents({
	
		'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)
{

}
