AttachEvent(window, 'load', aj_Init, false);

// the current url thati s in the browser address bar:
var ajCurrentHash;

// processing an ajax page / in the middle of loading. true or false
var aj_Busy = false;

// delay in miliseconds of the rate of checking if there is a new url
var ajDelay = 10;

/*
	Init first page, check if this has anchor, if so, redirect to real page:
*/

var cLoc = location.pathname;
var wLoc = retrieveAncor();


// if both the manual location is empty (/) AND the hash portion (#/...) is empty, redirect to home page:

if((!cLoc || cLoc == WSD_WEBROOT) && (!wLoc || wLoc == WSD_WEBROOT) && WSD_AJAX)
{
	var url_allready_in_bar = true;

	location.href = WSD_WEBROOT+'#'+REDIR_ROOT+'home.html';
}


// if there is some data in the manual location, convert this to anchor mode (#..)

else if(cLoc && cLoc != WSD_WEBROOT && WSD_AJAX)
{
	var url_allready_in_bar = true;

	location.href = WSD_WEBROOT+'#'+cLoc;
}


/*
	Initialize, check for any new anchor (#), and load that page..
*/

function aj_Init(e)
{
	ajTimer = setTimeout(aj_UpdatePage, ajDelay);
}


/*
	Refresf the timer
*/

function aj_UpdatePage(e)
{
	// get the value after #
	var wLoc = retrieveAncor();

	// first see if anchor value starts with #, then see if the page you're switching to is different as the one you are on now,
	if(wLoc.match(/^\//gi) && ajCurrentHash != wLoc && !aj_Busy)
	{
		if(url_allready_in_bar)
		{
			url_allready_in_bar = false;

			dn_MenuItemByURL(wLoc, true);
		}
		else
		{
			dn_MenuItemByURL(wLoc, false);
		}

		aj_Busy = true;
	}

	ajTimer = setTimeout(aj_UpdatePage, ajDelay);
}

function dn_MenuItemByURL(rawUrl, manual_url_redir)
{
	if(!WSD_AJAX) return true;

	E('ajLoading').style.display = 'block';

	if(rawUrl.match(/\?/gi))
	{
		urlLoc = rawUrl+'&fromAjax=yes';
	}
	else
	{
		urlLoc = rawUrl+'?fromAjax=yes';
	}

	createXMLHttpRequest();
	xmlHttp.onreadystatechange = function(){updateMenuRequest(false, false, false, false, rawUrl, manual_url_redir);};
	xmlHttp.open("GET", urlLoc, true);
	xmlHttp.send(null);

	return false;
}

function updateMenuRequest(itemCode, menuType, menuID, subID, urlVar, manual_url_enter)
{
    if(xmlHttp.readyState == 4)
	{
		if(xmlHttp.status == 404)
		{
			//alert("Page was not found ! You are now being redirected to our frontpage.");

			// done loading page, busy = false. the 'true' state prevents other ajax request from happening
			aj_Busy = false;

			// loading bar:
			E('ajLoading').style.display = 'none';

			// set current page in variable
			ajCurrentHash = urlVar;

			//location.href = '/#'+REDIR_ROOT+'home.html';

			dn_MenuItemByURL(REDIR_ROOT+'404.html');
		}
		else if(xmlHttp.status == 200)
		{
			//set_page();

			var dnContent = E('dnContent');

			var responseStatus = xmlHttp.responseText;
			var menuContent = responseStatus;	// Content
			dnContent.innerHTML = menuContent;

			// done loading page, busy = false. the 'true' state prevents other ajax request from happening
			aj_Busy = false;

			// loading bar:
			E('ajLoading').style.display = 'none';

			// set current page in variable
			ajCurrentHash = urlVar;

			// title
			document.title = E('aj_HeaderInfo').getAttribute('alt');
			
			if(!manual_url_enter)
			{
				location.href = WSD_WEBROOT+"#"+urlVar;
			}
		}
	}
}

var xmlHttp;

function createXMLHttpRequest()
{
    if (window.ActiveXObject) {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
       xmlHttp = new XMLHttpRequest();
    }

	return xmlHttp;
}

function generalAJAXStatus(xmlHttp, whatTodoNext)
{
    if(xmlHttp.readyState == 4)
	{
        if(xmlHttp.status == 200)
		{
			var responseStatus = xmlHttp.responseText;

			if(responseStatus != 'good')
			{
				alert(xmlHttp.responseText);
			}
			else
			{
				switch(whatTodoNext)
				{
				}
			}
		}
	}
}

