var oldPage = null;
hashCheck();
setInterval('hashCheck();', 250);
$("html").bind("ajaxSuccess",function(event, XMLHttpRequest, ajaxOptions){ ajaxResponse_Process(XMLHttpRequest.responseXML) });
 

function hashCheck()
{
	if (oldPage==null && window.location.hash=="")
	{
		window.location.hash = "index";
		oldPage = window.location.hash;
		passToControl("index");
	}
	if (window.location.hash != oldPage)
	{
		oldPage = window.location.hash;
		passToControl(oldPage);
	}
}

function passToControl(curHash)
{
	sHash = curHash.split('?',2);
	action = sHash[0];
	
	post = "";
	if (sHash[1])
	{
		post += "variables=" + Base64.encode(sHash[1]) + "&";
	}
	if (document.cookie)
	{
		post += "cookies=" + Base64.encode(document.cookie) + "&";
	}
	
	post += "action=" + action;

	$.post("control.php", post);

}

function refreshHash()
{
	if (window.location.hash==null)
	{
		window.location.hash="index";
	}
	sHash = window.location.hash.split('?',2);
	action = sHash[0];
	
	post = "";
	if (sHash[1])
	{
		post += "variables=" + Base64.encode(sHash[1]) + "&";
	}
	if (document.cookie)
	{
		post += "cookies=" + Base64.encode(document.cookie) + "&";
	}
	
	post += "action=" + action;

	$.post("control.php", post);

}

function ajaxResponse_Process(xml)
{
	$(xml).find('setCookie').each(function(){
			setCookie($(this).attr('name'),Base64.decode($(this).text()),$(this).attr('expires'));
			});
	
	$(xml).find('html').each(function(){
			$("#"+$(this).attr('divID')).html(Base64.decode($(xml).find('html').text()));
			if ($(this).attr('scrollID')>0)
			{
				scrollPage($(this).attr('scrollID'));
			}
			});
	
	$(xml).find('notice').each(function(){
			siteNotice(Base64.decode($(this).find('title').text()), Base64.decode($(this).find('content').text()));
			});
	
	$(xml).find('error').each(function(){
			siteError(Base64.decode($(this).find('title').text()), Base64.decode($(this).find('content').text()));
			});

	
	$(xml).find('javascript').each(function()
	{
		var fileref=document.createElement('script');
		fileref.setAttribute("type","text/javascript");
		fileref.innerHTML = Base64.decode($(this).text());
		document.getElementsByTagName("head")[0].appendChild(fileref);
	});
	
	$(xml).find('css').each(function()
	{
		var fileref=document.createElement('link');
		fileref.setAttribute("type","text/css");
		fileref.setAttribute("rel","stylesheet");
		fileref.innerHTML = Base64.decode($(this).text());
		document.getElementsByTagName("head")[0].appendChild(fileref);
	});
	
	$(xml).find('deleteElement').each(function(){
		$($(this).attr('id')).remove();
	});

}

function errorReturn(xhr, thrownError)
{
	siteError('Ajax Error','The AJAX Framework failed to connect to the remote server<br>'+xhr.status+'<br>'+thrownError);
}

function siteError(header, content)
{
	var errorPurr = '<div class="ajax_error" id="purr_container"><div class="notice">'
						+ '<div class="notice_body">'
						+ '<img src="../icons/error_big.png">'
						+ '<h4>'+header+'</h4>'
						+ '<p>'+content+'</p>'
						+ '</div></div></div>';

	$(errorPurr).purr({fadeInSpeed: 500,
					   fadeOutSpeed: 500,
					   removeTimer: 5000});
}

function siteNotice(header, content)
{
		var errorPurr = '<div class="ajax_notice" id="purr_container"><div class="notice">'
						+ '<div class="notice_body">'
						+ '<img src="../icons/error_big.png">'
						+ '<h4>'+header+'</h4>'
						+ '<p>'+content+'</p>'
						+ '</div></div></div>';

	$(errorPurr).purr({fadeInSpeed: 500,
					   fadeOutSpeed: 500,
					   removeTimer: 5000});
}

function debugMsg()
{
	alert("oldPage: "+oldPage+"\nHash: "+window.location.hash);
}