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 wLoc = retrieveAncor();

if(wLoc.match(/^\//gi) && ajCurrentHash != wLoc)
{
	// only redirect to real page if admin (for toggle editor)
	if(!WSD_AJAX)
	{
		location.href = wLoc;
	}
}


/*
	Initialize, called when page is done loading. sets the tiemr
*/

function aj_Init(e)
{
	ajTimer = setTimeout(aj_UpdatePage, ajDelay);
}


/*
	Refresf the timer
*/

function aj_UpdatePage(e)
{
	// get the value after #
	var wLoc = retrieveAncor();

	//E('ajtest').innerHTML = "ch: "+ajCurrentHash+", wlco: "+wLoc+" -- "+parseInt(Math.random() * 9999);
	
	// 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)
	{
		dn_MenuItemByURL(wLoc);
		aj_Busy = true;
	}

	ajTimer = setTimeout(aj_UpdatePage, ajDelay);
}

function dn_MenuItemByLangCode(langCode)
{
	if(!WSD_AJAX) return true;

	var qAddon = "?lang_code="+langCode;

	var urlVar = qAddon;

	createXMLHttpRequest();
	xmlHttp.onreadystatechange = function(){updateMenuRequest(false, false, false, false, urlVar);};
	xmlHttp.open("GET", WSD_WEBROOT+"dn-menu.php"+urlVar, true);
	xmlHttp.send(null);

	return false;
}

function dn_MenuItemByURL(rawUrl)
{
	E('ajLoading').style.display = 'block';

	var urlVar = rawUrl.replace(/.+index\.php(\?.*)/gi, "$1");

	createXMLHttpRequest();
	xmlHttp.onreadystatechange = function(){updateMenuRequest(false, false, false, false, urlVar);};
	xmlHttp.open("GET", WSD_WEBROOT+"dn-menu.php"+urlVar, true);
	xmlHttp.send(null);

	return false;
}

function dn_MenuItemByCode(itemCode, menuType, mainID, subID)
{
	if(!WSD_AJAX) return true;

	if(menuType == "sub")
		var qAddon = "?pageSub="+itemCode;
	else
		var qAddon = "?page="+itemCode;

	var urlVar = qAddon;

	createXMLHttpRequest();
	xmlHttp.onreadystatechange = function(){updateMenuRequest(itemCode, menuType, mainID, subID, urlVar);};
	xmlHttp.open("GET", WSD_WEBROOT+"dn-menu.php"+urlVar, true);
	xmlHttp.send(null);

	return false;
}

function dn_MenuItemByType(itemCode, menuType, mainID, subID)
{
	if(!WSD_AJAX)
	{
		return true;
	}

	if(menuType == "sub")
		var qAddon = "?typeSub="+itemCode;
	else
		var qAddon = "?type="+itemCode;

	var urlVar = qAddon;

	createXMLHttpRequest();
	xmlHttp.onreadystatechange = function(){updateMenuRequest(itemCode, menuType, mainID, subID, urlVar);};
	xmlHttp.open("GET", WSD_WEBROOT+"dn-menu.php"+urlVar, true);
	xmlHttp.send(null);

	return false;
}

function dn_ImageCategoryByCode(itemCode, menuType, mainID, subID)
{
	if(!WSD_AJAX)
	{
		return true;
	}

	var urlVar = "?imgCat="+itemCode;

	createXMLHttpRequest();
	xmlHttp.onreadystatechange = function(){updateMenuRequest(itemCode, menuType, mainID, subID, urlVar);};
	xmlHttp.open("GET", WSD_WEBROOT+"dn-menu.php"+urlVar, true);
	xmlHttp.send(null);

	return false;
}

function dn_NewsByCode(itemCode, menuType, mainID, subID)
{
	if(!WSD_AJAX)
	{
		return true;
	}

	var urlVar = "?news="+itemCode;

	createXMLHttpRequest();
	xmlHttp.onreadystatechange = function(){updateMenuRequest(itemCode, menuType, mainID, subID, urlVar);};
	xmlHttp.open("GET", WSD_WEBROOT+"dn-menu.php"+urlVar, true);
	xmlHttp.send(null);

	return false;
}

function dn_DJByCode(itemCode, menuType, mainID, subID)
{
	if(!WSD_AJAX)
	{
		return true;
	}

	var urlVar = "?dj="+itemCode;

	createXMLHttpRequest();
	xmlHttp.onreadystatechange = function(){updateMenuRequest(itemCode, menuType, mainID, subID, urlVar);};
	xmlHttp.open("GET", WSD_WEBROOT+"dn-menu.php"+urlVar, true);
	xmlHttp.send(null);

	return false;
}

function dn_VideoByCode(itemCode, menuType, mainID, subID)
{
	if(!WSD_AJAX)
	{
		return true;
	}

	var urlVar = "?video="+itemCode;

	createXMLHttpRequest();
	xmlHttp.onreadystatechange = function(){updateMenuRequest(itemCode, menuType, mainID, subID, urlVar);};
	xmlHttp.open("GET", WSD_WEBROOT+"dn-menu.php"+urlVar, true);
	xmlHttp.send(null);

	return false;
}

function updateMenuRequest(itemCode, menuType, menuID, subID, urlVar)
{
    if(xmlHttp.readyState == 4)
	{
        if(xmlHttp.status == 200)
		{
			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';

			// current location:
			ajCurrentHash = WSD_WEBROOT+"index.php"+urlVar;

			// title
			document.title = E('aj_HeaderInfo').getAttribute('alt');

			location.href = "#"+ajCurrentHash;

			if(wLoc.match(/page_home/gi) || wLoc.match(/page\=home/gi))
			{
				E('audioPlayer').style.display = 'none';
			}
			else
			{
				E('audioPlayer').style.display = 'block';
			}
		}
	}
}

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)
				{
				}
			}
		}
	}
}

