var currentHRef = '';
var currentPage = '';

function updateHRef()
{
	var href = window.location.hash.substring(1);
	if( href != currentHRef )
	{
		currentHRef = href;

		var params = {ajax: 'CONTENT,PAGE:TITLE,PAGE:REDIRECT,PAGE:CPM'};

		document.fire("ajax:loading");

		return new Ajax.Request( '/' + href, {
			method: 'get',
			parameters: params,
			requestHeaders: {Accept: 'application/json'},
			onSuccess: function(transport){
				var json = transport.responseText.evalJSON(true);
				if( typeof(json.REDIRECT) != "undefined" && json.REDIRECT != '' ) {
					var redirect = json.REDIRECT;
					if( redirect.substring(0, 5) != "http:" ) {
						if( redirect.substring(0, 1) == '#' ) {
							window.location.hash = json.REDIRECT;
						} else {
							if( redirect.substring(0, 1) != '/' ) redirect = '/' + redirect;
							window.location.href = window.location.protocol + '//' + window.location.host + redirect;
						}
					} else {
						window.location.href = json.REDIRECT;
					}
					return;
				}
				$('content').update(json.CONTENT);
				if( typeof(json.CPM) != "undefined" && json.CPM != '' )
					currentPage = json.CPM;
				else
					currentPage = href;
				if( typeof(json.TITLE) != "undefined")
					document.title = json.TITLE;
				document.fire("ajax:load");
				initLinks( $('content') );
			},
			onFailure: function(transport){ 
				var json = transport.responseText.evalJSON(true);
				if( typeof( json.CONTENT ) != "undefined" )
				{
					$('content').update(json.CONTENT);
					if( typeof(json.TITLE) != "undefined")
						document.title = json.TITLE;
					document.fire("ajax:load");
					initLinks( $('content') );
				}
				else
				{
					$('content').update('An error has occured...');
					document.title = 'An error has occured...';
				}
				if( typeof(json.CPM) != "undefined" && json.CPM != '' )
					currentPage = json.CPM;
				else
					currentPage = href;
				document.fire("ajax:load");
			}
		});
	}
}

new PeriodicalExecuter(updateHRef, 0.2);

function initLinks(start)
{
	var search = 'a';
	if( start )
		search = '#' + start + ' a';

	$$(search).each(function(element){
		if( element.hasClassName('dal') )
		{
			element.removeClassName('dal');
			element.setAttribute("href", "/#" + element.readAttribute("href").substring(1).replace(/\.html/,"") );
		}
		
		if( element.readAttribute('href').indexOf('#') == 0 && element.readAttribute('rel') == null )
		{
			dest = element.readAttribute('href').substring( element.readAttribute('href').indexOf('#')+1 );
			if( dest.length > 0 )
			{
				element.setAttribute('href', '#');
				element.setAttribute('dest', dest);
				element.observe('click', function(e){
					scrollToAnchor(this.readAttribute('dest')); 
					e.stop();
					return false;
				});
			}
		}
	});
}


function oLink(URL)
{
	if( URL.indexOf('?') === -1 ) {
		URL += '?ajax';
	} else {
		URL += '&ajax';
	}

	var params = {ajax: 'CONTENT,PAGE:TITLE'};

	document.fire("ajax:loading");

	new Ajax.Request( URL, {
		method: 'get',
		parameters: params,
		requestHeaders: {Accept: 'application/json'},
		onSuccess: function(transport){
			var json = transport.responseText.evalJSON(true);
			$('content').update(json.CONTENT);
			document.title = json.TITLE;
			document.fire("ajax:load");
			initLinks( $('content') );
		},
		onFailure: function(transport){ 
			var json = transport.responseText.evalJSON(true);
			if( typeof( json.TITLE ) != "undefined" )
			{
				$('content').update(json.CONTENT);
				document.title = json.TITLE;
				document.fire("ajax:load");
				initLinks( $('content') );
			}
			else
			{
				$('content').update('An error has occured...');
				document.title = 'An error has occured...';
			}
		}
	});

	return false;
}

function easeInOut(t,b,c,d)
{
	return c/2 * (1 - Math.cos(Math.PI*t/d)) + b;
}

var scrollInt;
var scrTime, scrSt, scrDist, scrDur, scrInt;

function scrollPage()
{
	scrTime += scrInt;
	if (scrTime < scrDur) {
		window.scrollTo( 0, easeInOut(scrTime,scrSt,scrDist,scrDur) );
	} else {
		window.scrollTo( 0, scrSt+scrDist );
		clearInterval(scrollInt);
	}
}

function scrollToAnchor(aname)
{
	var anchors, i, ele;

	if (!document.getElementById)
		return;

	ele = document.getElementById(aname);
	if( !ele )
		return;

	if (window.scrollY)
		scrSt = window.scrollY;
	else if (document.documentElement.scrollTop)
		scrSt = document.documentElement.scrollTop;
	else
		scrSt = document.body.scrollTop;

	scrDist = ele.offsetTop - scrSt;
	scrDur = 500;
	scrTime = 0;
	scrInt = 10;

	clearInterval(scrollInt);
	scrollInt = setInterval( scrollPage, scrInt );
}

document.observe("dom:loaded", function() {
	initLinks(); 
});

