// Self-Executing Anonymous Function to avoid more globals
(function() {
	
// Home link isn't dynamic, so default set class name to it to match how dynamic classes work in WordPress
$(".home li.home").removeClass("home").addClass("current_page_item");

// Create the Spinner with options
var spinner = new Spinner({
  lines: 15, // The number of lines to draw
  length: 15, // The length of each line
  width: 20, // The line thickness
  radius: 10, // The radius of the inner circle
  color: '#FAFAFA', // #rgb or #rrggbb
  speed: 1.3, // Rounds per second
  trail: 50, // Afterglow percentage
  shadow: false // Whether to render a shadow
}).spin(document.getElementById("wrap")); // Place in DOM node called "ajaxContentHolder"

var 
	$mainContent     = $("#content"),
    $searchInput     = $("#s"),
    $allLinks        = $("a"),
    $el;
    
// Auto-clear search field
$searchInput.focus(function() {
    if ($(this).val() == "Search...") {
        $(this).val("");
    }
});

$('#searchform').submit(function() {  
	var s = $searchInput.val();
	if (s) {
		var query = '/?s=' + s;
		$.address.value(query);  
	}
	return false;
});

// If odd number of categories, add a fake one
if ($(".widget_categories li").length%2 != 0) {
    $(".widget_categories ul").append("<li><a>&nbsp;</a></li>");
}

// If odd number of tags, add a fake one
if ($(".widget_tag_cloud a").length%2 != 0) {
    $(".widget_tag_cloud").append("<a>&nbsp;</a>");
}

// URL internal is via plugin http://benalman.com/projects/jquery-urlinternal-plugin/
$('a:urlInternal').live('click', function(e) { 
	
	// Caching
	$el = $(this);

	if ((!$el.hasClass("comment-reply-link")) && ($el.attr("id") != 'cancel-comment-reply-link')) { 		
		var path = $(this).attr('href').replace(base, '');
		$.address.value(path);
		$(".current_page_item").removeClass("current_page_item");
		$allLinks.removeClass("current_link");
		$el.addClass("current_link").parent().addClass("current_page_item");
		return false;
	}
	
	// Default action (go to link) prevented for comment-related links (which use onclick attributes)
	e.preventDefault();
	
});  

// Fancy ALL AJAX Stuff
$.address.change(function(event) {  
	if (event.value) {
		spinner.spin(document.getElementById("wrap"));
		$mainContent
			.empty()
			.load(base + event.value + ' #main', function() {
				spinner.stop();
				$mainContent.fadeIn();
				
				//AJAX EMAIL INIT// 
				
				$('input.erase').each(function() {
					$(this)
						.data('default', $(this).val())
						.addClass('inactive')
						.focus(function() {
							$(this).removeClass('inactive');
							if ($(this).val() == $(this).data('default') || '') {
								$(this).val('');
							}
						})
						.blur(function() {
							var default_val = $(this).data('default');
							if ($(this).val() == '') {
								$(this).addClass('inactive');
								$(this).val($(this).data('default'));
							}
						});
					});

					$('#mc-embedded-subscribe-form').submit(function() { 
						$(this).ajaxSubmit();
						$('#email').val('Check Your Inbox!');
						return false; 
					});

					//MOSAIC OVERLAY INIT//
					
					$('.fade').mosaic();
					
					//COLORBOX INIT//
					
					$('.afgcolorbox').colorbox({
						slideshow: true,
					    slideshowAuto: false,
					    slideshowSpeed: 3500,
					    maxWidth: '90%',
					    maxHeight: '90%',
					    current: '{current} of {total}',
					});
					
					$('.videocolorbox').colorbox({iframe:true, innerWidth:600, innerHeight:360});
					
					//FB INIT//
					
					window.fbAsyncInit = function() {
					    FB.init({appId: '274717432560068', status: true, cookie: true,
					             xfbml: true});
					  };
					  (function() {
					    var e = document.createElement('script'); e.async = true;
					    e.src = document.location.protocol +
					      '//connect.facebook.net/en_US/all.js';
					    document.getElementById('fb-root').appendChild(e);
					  }());
				
					vimeoInit();
			});  
	} 

	var current = location.protocol + '//' + location.hostname + location.pathname;
	if (base + '/' != current) {
		var diff = current.replace(base, '');
		location = base + '/#' + diff;
	}
});

})(); // End SEAF
