/*
---
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: false,
	
	/*
	*	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: true,
	
	/*
	*	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: [
		'custom/images/BKG-Lines.png'
	]
}

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

	$('navigation').getElements('li').each(function(li){
		var indicator = new Element('div', {'class': 'indicator'}).inject(li);
		
		li.getElement('a').addEvents({
			'mouseenter': function(){
				if( ! li.hasClass('selected')) indicator.setStyle('left', 0);
				indicator.fade('in');
			},
			'mouseleave': function(){
				if( ! li.hasClass('selected')) indicator.fade('out');
			},
			'click': function(){
				//indicator.fade('in');
				//indicator.morph({'left': li.getSize().x - indicator.getSize().x});				
			}
		});
		
		if(li.hasClass('selected'))
		{
			indicator.fade('in');
			indicator.morph({'left': li.getSize().x - indicator.getSize().x});
		}
	});

	window.controller.addEvents({
		
		/* When the site is loaded and set up, but before any animation */
		'siteReady': function(){
			
		},
		
		/* When animation is complete */
		'postAnimation': function(){
			/* Add the header contact element */
			if(site_settings.items.header_contact_area && site_settings.items.header_contact_area.value){
				new Element('div', {'id': 'header-contact-area', 'html': site_settings.items.header_contact_area.value.replace('\n', '<br/>'), 'styles': {
					'opacity': 0
				}}).inject('site', 'bottom').tween('opacity', 1);
			}
		},
		
		'pageReady': function(page){
			switch(page.type)
			{
				case 'Gallery':
					handle_gallery(page);
					break;
				case 'Page':
					handle_page(page);
					break;
				case 'Contact':
					handle_contact(page);
					break;
			}
			
			var li = $('navigation').getElement('li.selected');
			var indicator = li.getElement('.indicator');
			indicator.fade('in');
			indicator.morph({'left': li.getSize().x - indicator.getSize().x});
			
			$('navigation').getElements('li').each(function(el){
				if( ! el.hasClass('selected'))
				{
					el.getElement('.indicator').fade('out');
				}
			});
		}
	});

});


// --------------------------------------------------------------------
	
/**
 * Handle a gallery
 * 
 */
function handle_gallery(page)
{
	// Add info link
	var info_button = new Element('a', {'href': '#', 'class': 'item-info', 'html': '&nbsp;'}).inject($('content').getElement('.gallery .navigation')).addEvent('click', function(e){
		e.stop();
		if(page.gallery.gallery.current_item().getElement('.meta .description').get('text').trim() == '') page.gallery.gallery.current_item().getElement('.meta').set('text', 'No info');
		page.gallery.gallery.current_item().getElement('.meta').fade('toggle');
	}.bind(this));
	
	// Center images vertically
	$('content').getElements('.gallery .item.photo').each(function(el){
		el.getElement('img').setStyle('margin-top', Math.floor(el.getSize().y - el.getElement('img').getSize().y) / 2);
		el.getElement('.meta').setStyle('opacity', 0);
	});
	
	// Scrollbar for thumbnail page.
	new Kemso.ScrollBar.Auto($('content').getElement('.gallery .item.overview'));
	
	// On current.
	var on_current = function(el, key){
		// Info Button
		if(key == 0 || ! this.current_item().getElement('.meta') || this.current_item().getElement('.meta').get('text').trim() == '') info_button.setStyle('visibility', 'hidden');
		else info_button.setStyle('visibility', 'visible');
	};
	
	page.gallery.gallery.addEvents({
		'current': on_current
	});
	
	// We got here late, so fire events
	if(page.gallery.gallery.current_item())
	{
		on_current.attempt([page.gallery.gallery.current_item(), page.gallery.gallery.current], page.gallery.gallery);
	}
}


// --------------------------------------------------------------------
	
/**
 * Handle a page
 * 
 */
function handle_page(page)
{
	new Kemso.ScrollBar.Auto($('content-container').getElement('.page-content'));
}


// --------------------------------------------------------------------
	
/**
 * Handle the contact page
 * 
 */
function handle_contact(page)
{

}
