/* Author: 
	* Charles X. Morrissey
	* version: 091211
*/

// Global JS
(function($){
	
	var $animations = new Array();
	var $init = true;
	var $container = $('#main');
	
	var $current;

	$(function(){
		
		// Nav Accordian
		$('.nav-accordion').accordion({
			collapsible: true,
			autoHeight: false,
			animated: 'ease',
			active: false
		});
		
		
		// Collections
		var $title = document.title;
		
		// Init and change handlers
		$.address.init(function(event) {
			
			if($init && event.value !== '/') {
				$current = event.pathNames[0];
				
				$animations.length = 0;
				
				$container.load('_assets/includes/collections/' + event.pathNames + '.php', function(){
					// log('init: ', $current);
					$container.scrollTop(0);
					
					if(event.queryString === "") {
						showGallery(false, event.pathNames);
					}
					else {
						showGallery(true, event.pathNames, $.address.parameter('id'));
					}
				
					$("ul#image-grid img, .title").each(function() {
						$animations.push($(this));
					});
					
					doAnimation($animations.shift());
					
				});
				
			}
						
			
		}).change(function(event) {
			// log('change: ' + event.value);
			
			var names = $.map(event.pathNames, function(n) {
				return n.substr(0, 1).toUpperCase() + n.substr(1);
			}).concat(event.parameters.id ? event.parameters.id.split('.') : []);
			
			var links = names.slice();
			var match = (event.value !== '/') ? names[0].toLowerCase() : ''; // links.length ? links.shift() + links.join('.') : 'Home';
						
			$('.nav-column.collections a').each(function() {
				$(this).toggleClass('selected', $(this).attr('href') == match);
			});
			
			$.address.title([$title].concat(names).join(' - '));
			
			// On Collection
			if(event.value !== '/') {
				
				if(!$('body').hasClass('collections')) {
					$('body').removeClass('home').addClass('collections');
					$('#right-column').find('nav')
						.html('<a class="close-btn" href="#/">Close X</a>');
				}
				
				if($current !== event.pathNames[0]) {
					
					$current = event.pathNames.toString();
					
					$animations.length = 0;
					
					$container.load('_assets/includes/collections/' + event.pathNames + '.php', function(){
						/// log('change: ', $current);
						
						$container.scrollTop(0);
						
						if(event.queryString === "") {
							showGallery(false, event.pathNames);
						}
						else {
							showGallery(true, event.pathNames, $.address.parameter('id'));
						}
						
						$("ul#image-grid img, .title").each(function() {
							$animations.push($(this));
						});
					
						doAnimation($animations.shift());
					});
					
				}
				
			}
			
			// On Home
			else {
				$init = false;
				$current = undefined;
				$('body').removeClass('collections').addClass('home');
				$container.html('');
			}
				
			
			
		});
		
	});
	// End Document OnLoad
	
	function showGallery(b, url, index) {
		if(b) {
			$("a[rel=external]").fancybox({
				'index': index,
				overlayOpacity : 0,
				titlePosition : null,
				titleFormat : formatTitle,
				showNavArrows: false,
				onStart: function(selectedArray, selectedIndex, selectedOpts) {
					$.address.value(url + '?id=' + selectedIndex);
				},
				onComplete: initSharing,
				onClosed: function() {
					$.address.value(url);
				}
			});
			
			$("a[rel=external]").eq(index).trigger('click');
			
		}
		else {
			$("a[rel=external]").fancybox({
				overlayOpacity : 0,
				titlePosition : null,
				titleFormat : formatTitle,
				showNavArrows: false,
				onStart: function(selectedArray, selectedIndex, selectedOpts) {
					$.address.value(url + '?id=' + selectedIndex);
				},
				onComplete: initSharing,
				onClosed: function() {
					$.address.value(url);
				}
			});
			
		}
	}
	
	function initSharing() {
		//var script = 'http://s7.addthis.com/js/250/addthis_widget.js#pubid=ra-4d66e78a3e962981&domready=1';
		var script = 'http://s7.addthis.com/js/250/addthis_widget.js#domready=1';
		if ( window.addthis ) {
			window.addthis = null;
			// window._ate = null;
		}
		$.getScript(script, function(){
			addthis.init();
		});
	}
	


	
	function doAnimation(image) {
		
		image.fadeIn(0, 'linear', function() {
			
			if($animations.length > 0) {
				doAnimation($animations.shift());
			}
			else if($animations.length === 0) {
				// $loadComplete = true;
			}
		});
	}
	
	// Accordian Easing
	$.ui.accordion.animations.ease = function(settings) {
		this.slide(settings, {
			easing: 'linear',
			duration: 200
		});
	}
	
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
		html = '';
		html += '<div id="title-container">';
		html += (title && title.length ? '<p>' + title + '</p>' : '' );
		html += '<div class="gallery-navigation">';
		html += '<a class="prev-btn" href="javascript:;" onclick="$.fancybox.prev();">&lt;</a>';
		html += '<a class="next-btn" href="javascript:;" onclick="$.fancybox.next();">&gt;</a>';
		html += '</div>';
		html += '<div id="share-container">';
		html += '<a href="http://www.addthis.com/bookmark.php" class="addthis_button" addthis:url="http://rachelcomey.com/#/' + $current + '?id=' + currentIndex + '">Share +</a>';
		html += '</div>';
		html += '</div>';
		return html;
	}
	
	$("#right-column nav").click(function () {
      $('.close-btn').remove();
	});

	
})(jQuery);









