$(document).ready(function() {
	$("#twitter").getTwitter({
			userName: "michaeleverson",
			numTweets: 5,
			loaderText: "Loading tweets...",
			slideIn: true,
			showHeading: true,
			headingText: "Latest Tweets",
			showProfileLink: true
		});
	if (BrowserSupportsHistoryPushState())
	{
		//Add click event handler to all anchor tags
    	$("a").click(function(){
    		if ($(this).attr('href') == undefined) return true;
    		var dest = $(this).attr('href');
    		//if link is an #, explicitly states a protocol, or is in class no-ajax then do not try ajaxy magic
    		isExternalURL = new RegExp("^[a-z0-9_\-]+://|^#");
    		if ((isExternalURL.test(dest) == true) || ($(this).is('.no-ajax'))) return true;
    		NavigateToPage(dest, true);
    		pageTracker._trackPageview(dest);
           	return false;
    	});
    }
});

window.onpopstate = function(event) {
	NavigateToPage(document.location.pathname, false);
};

function NavigateToPage(pathname, pushHistoryState)
{
	//TODO:Add support for existing parameter strings to future proof (ie. append &ajax)
	$.ajax({
	    url: pathname + "?ajax",
	    type: 'GET',
	    dataType: 'json',
	    timeout: 1000,
	    error: function(){
	        alert('Error loading document!!!');
	    },
	    success: function(json)
	    {
	    	//TODO: Do and Undo effects of ajax loaded css and js
	    	
	    	document.title = json.articleTitle + " – michaeleverson.com";
//	        if ('extraResources' in json)
//	        {
//	        	var len = json['extraResources'].length >>> 0;
//	        	for (var i = 0; i < len; i++)
//	        	{
//	        		var resource = json['extraResources'][i];
//	        		switch (resource.type)
//	        		{
//	        			case "css":
	        				//Do something
//	        				break;
//	        			case "js":
	        				//Do something
//	        				break;
//	        			default:
	        				//Error! Unknown resource type
//	        		}
//	        	}
//	        }
	        if (pushHistoryState)
	        {
	        	var stateObj = { site: "michaeleverson.com" };
	        	history.pushState(stateObj, "", pathname);
	        }
	        //Navbar update
	      	$('a:not([href])[data-href!="'+ pathname +'"]').attr('href', function() {
	        	return $(this).attr("data-href");
	        });
	        $('a:[data-href="'+ pathname +'"][href]').removeAttr('href');
	        //Replace content
	        $('div#content').html(json.content);
	    }
	});
	$("p").append("<strong>Test!!!!!!</strong>");
}

function BrowserSupportsHistoryPushState()
{
	return ('pushState' in window.history) && window.history['pushState'] !== null;
}

