jQuery(function($){
flag = false;
var docw = $(document).width() + 250;

$('<div id="img2" class="img" style="right:-'+docw+'px;"></div>').appendTo('body');
$('<img id="loader" src="#" />').appendTo('body').hide().load(function(){
	var imgsrc = $(this).attr('src');
	if (imgsrc != 
$('#img').css('background-image').replace('url(','').replace(')','').replace($('base').attr('href'),'')){
		$('#img2').css('background-image','url('+imgsrc+')');
		$('#img2').stop().animate({'right':0}, function(){
			$('#img').css('background-image','url('+imgsrc+')');
			$('#img2').css('right','-'+docw+'px');
		});
	}
});

$('.nav li a, #Content a[rel!=noajax]').live('click',function(){
	//if (!$(this).attr('href').match(/(http:)/){
	//	loc = $('base').attr('href') + $(this).attr('href'); }
	//else { loc = $(this).attr('href'); }

        flag = true;

	if ($(this).hasClass('url')) {
		$.address.value( this.href );
	}
	else {
		$.address.value($(this).attr('href'));
	}
	return false;
});

$.address.change(function(event){
	currentloc = window.location + '';
	changeurl = event.path;
	if (event.path == '' || (flag == false && event.path == '/')){ 
return false; }
	
	//if (currentloc.match(changeurl.replace('^http://allifoster.com',''))){return false;}
	if (currentloc.match('^'+ $('base').attr('href') + event.path.replace('^/','') +'$')){ return false; }	

	if (changeurl.match('/http://')){ changeurl = changeurl.replace('^/',''); }
	
	
//console.log(changeurl);
	
	$page = $('#Content');
	$page.data('oldpage',$page.html())
        //if (console.log){console.log(changeurl);}
t = setTimeout("$('#Content').animate({'height':'200px'},function(){$('#Content').html('<h4>Loading...</h5><img class=\"load c\" src=\"mysite/javascript/loader.gif\" />')});", 820);
	$.ajax({
		type: "GET",
		dataType: "html",
		url:  changeurl, 
		success: function(response){
		if (response.match('<div class="hide img">(.*)</div>')){
			var src = response.match('<div class="hide img">(.*)</div>')[1];
			if (src.match('\.(jp?g|png)')){
				$('img#loader').attr('src', src);
			}
		}		
		if (response.match('body')){
			//if (!changeurl.match('http://')){ changeurl += $.address.baseURL(); }
			window.location = changeurl;
		}		
		else {
			clearTimeout(t);
			$page.html(response);
			$page.css({'height':'100%'});
        		//$page.fadeIn('slow');
			if ($('a[href$='+changeurl+']')){
           			$('li.current').removeClass('current');
           			$('a[href$='+changeurl+']').blur().parent('li').addClass('current');
			}
                }
		},
                error: function(request,status){
			errc = request.status;
			if (errc == 302 && request.getResponseHeader('Location')){ window.location = request.getResponseHeader('Location'); }
			clearTimeout(t);
			if (errc == 500){ error = 'there has been an server error.'; }
			else if (errc == 404){ error = 'the page you have requested cannot be found.'; }
			else { error = 'an unknown error occured.'; }
			$page.html('<h4>Error Fetching Page</h4><p>Sorry, but '+error+'</p><p><a class="back">Go Back</a>.')
				.find('.back').click(function(){ 
					$page.html($page.data('oldpage'));
				});
		}//End onError
	});//End Ajax
});//End OnFragment Change
});//End OnReady
