function doNav(url)
{
	var frameUrl = "http://" + window.location.hostname + url + "?x=" + (new Date());
	Ext.get("pageLoader").dom.src = frameUrl;
}


function fixLinks()
{
	var links = Ext.query("A");
	
	var clickFn = function(evt, elem, opts)
	{
		var url = this.dom.href;
		var pos = url.indexOf("#"); 
		if(pos!=-1)
		{
			url = url.substring(pos+1);
			doNav(url);
		}
		
	};
	
	var baseUrl = "http://" + window.location.hostname + "/";
	for(var i=0; i<links.length; i++)
	{
		var a = links[i];
		
		if(a.href.indexOf(baseUrl)==0 && a.href.indexOf("#")==-1 && (a.target==null || a.target.length==0))
		{
			var url = a.href.substring(baseUrl.length-1);
			a.href = "#" + url;
			Ext.get(a).on("click", clickFn);			
		}
	}
}


function setContent(html)
{
	var elem = Ext.get("content");
	
	elem.hide(false);
	elem.dom.innerHTML = html;
	elem.show({duration: .5});
	window.setTimeout(setTitle, 1);	// hack around for IE messing up title by adding anchor name
}

function setTitle()
{
	document.title = "Danny Liston";
}

Ext.onReady(function()
{
	
	// check for anchor name on URL incase someone bookmarked this URL
	var pos = window.location.href.indexOf("#"); 
	if(pos!=-1)
	{
		var cont = Ext.get("content");
		cont.dom.innerHTML = "<div style='height: 600px;'>Loading...</div>";
		var url = window.location.href;
		url = url.substring(pos+1);
		doNav(url);
	}

	// if we are in an iframe, then we need to pass our content up to the parent for display.	
	if(parent != window)
	{
		var elem = Ext.get("content");
		parent.window.setContent(elem.dom.innerHTML);
		parent.window.fixLinks();
		if(typeof(pngfix) != "undefined")
		{
			window.setTimeout(pngfix, 1000);
		}
	}
	else
	{
		fixLinks();
	}
	
});
