// create the namespace
var ALLABOUTJAMES 	= ALLABOUTJAMES || {};

// self executing function to make
// sure jQuery arrives intact
(function($){
	
	// create our bootstrap
	ALLABOUTJAMES.ajax = new function()
	{
		var SPEED			= 450;
		
		// variables 
		var sBase			= $.address.baseURL()+'/',
		fCurrentScroll		= 0,
		iLoadingOffset		= 0,
		$window				= $(window),
		$allLinks			= $('a'),
		$mainContent		= $('#ajax-container'),
		$ajaxLoader			= null,
		$hiddenThumbs		= null,
		iHiddenThumbsH		= 600;
		
			
		this.init = function()
		{
			ajaxLinks();
			on_ajaxAddressChange();
		}
		
		function insertLoader()
		{
			$mainContent.before("<img src='"+sBase+"wp-content/themes/allaboutjames/_img/loader.gif' id='ajax-loader' />");
			$('#ajax-loader')
			.fadeTo(0, 0)
			.fadeTo(SPEED, 1);
		}
		
		function staticCtrls()
		{
			var aCtrls		= $('ol#project-navigation a'),
				aWrapTxt	= $('#right-wrap'),
				iOffset		= 125,
				iTop		= $('#middle-wrap').offset().top + iOffset;
			
			if (aCtrls.length > 0)
			{
				$window.scroll(function() {
					if ($window.scrollTop() > iTop)
					{
						// changing the position to fixed will do all the hard work for us
						aCtrls.each(function() {
							$(this).css({'position':'fixed', 'margin-top':'-'+iOffset+'px'})
						});
						aWrapTxt.css({'position':'fixed'});
					} else {
						// when we're above the orginal placement switch back to our absolute values
						aCtrls.each(function() {
							$(this).css({'position':'absolute', 'margin-top':'0px'})
						});
						aWrapTxt.css({'position':'absolute'});
					}
				});
			}
		}
		
		function ajaxLinks()
		{
			$.address.crawlable(false);
			$.address.wrap(false);
			$.address.autoUpdate(true);
			$('a:urlInternal').live('click', function(e) { 
				$el = $(this);
				// remember our scroll position
				fCurrentScroll = $window.scrollTop();
				if ((!$el.hasClass("comment-reply-link")) 
					&& ($el.attr("id") != 'cancel-comment-reply-link') 
					&& ($el.attr("href") != '/blog/')) { 		
					//replace the domain in the link to create a neat hashtag
					var path = $(this).attr('href').replace(sBase, '');
					
					$.address.value(path);
					$(".current_page_item").removeClass("current_page_item");
					$allLinks.removeClass("current_link");
					$el.addClass("current_link").parent().addClass("current_page_item");
					$('#ajax-content').fadeOut(SPEED/2);
					return false;
					// Default action (go to link) prevented
					e.preventDefault();
				}
				
			});
		}
		
		function on_ajaxAddressChange()
		{
			$.address.change(function(e) {
				
				// when the hashed address changes, execute
				if (e.value) {
					$window.scrollTop(fCurrentScroll);
					
					//console.log($.address.baseURL()+"/" +  ' != ' +  $.address.value());
					if ($.address.baseURL()+"/" != $.address.baseURL() + $.address.value())
					{	
						// the #section-name will cause the window to return to the top of the page
						// we need to force the page scroll to our prev position
						insertLoader();
						
						$mainContent
						.load(e.value + ' #ajax-content', function() 
						{
							var $response = $('#ajax-content');
							// if there is an empty response do nothing
							if( $response.length < 1 ) return;
							// our content has loaded, transition in
							$response.css({'opacity':'0'});
							$('html,body').animate({ scrollTop: $('div.inner-wrap #portfolio').offset().top  }, 
									 SPEED, 
									 function() {
										$('#ajax-content')
										.css({'padding-top':iLoadingOffset+'px'})
										.animate({
											'opacity': 1,
											'padding-top': '0px'}, SPEED);
										// now hide the loader
										$('#ajax-loader').fadeOut(SPEED, function() {
											$('#ajax-loader').remove();
										});
									
							});
							
							
							if ($.address.baseURL()+"/#/index.php" == $(location).attr('href'))
							{
								// if we're reloading the index, kick off our home specific scripts 
								$('div#middle-wrap div#hidden-thumbs').height(0);
							}
							
							staticCtrls();
							
						});  
						
				 	}
				}
			}); 
		}
		
	}
	
	ALLABOUTJAMES.ajax.init();
	
})(jQuery);
  


