var pageMove;
var pageUrl;
var pageId;
var pageSize = 0;
var pageDifference = 0;
var pageOffscreen = 1000;

var calca = 0;
var calcb = 0;
var calcc = 0;

var makeChange = true;

var preload_image_a = new Image();
var preload_image_b = new Image();

/*
 * Get the height of a layer...
 */
function getHeight() {
	var b = document.body, e = document.documentElement;
	var esw = 0, eow = 0, bsw = 0, bow = 0, esh = 0, eoh = 0, bsh = 0, boh = 0;
	if (e) {
		esw = e.scrollWidth;
		eow = e.offsetWidth;
		esh = e.scrollHeight;
		eoh = e.offsetHeight;
	}
	if (b) {
		bsw = b.scrollWidth;
		bow = b.offsetWidth;
		bsh = b.scrollHeight;
		boh = b.offsetHeight;
	}

	// alert('compatMode: ' + document.compatMode
	// + '\n\ndocumentElement.scrollHeight: ' + esh
	// + '\ndocumentElement.offsetHeight: ' + eoh
	// + '\nbody.scrollHeight: ' + bsh + '\nbody.offsetHeight: ' + boh
	// + '\n\ndocumentElement.scrollWidth: ' + esw
	// + '\ndocumentElement.offsetWidth: ' + eow + '\nbody.scrollWidth: '
	// + bsw + '\nbody.offsetWidth: ' + bow);

	return esh;
}

function changePage(id, changePage, changeSize) {

	if (makeChange) {
		makeChange = false;

		pageId = id;
		pageUrl = changePage;
		pageDifference = pageSize - changeSize;

		calca = -pageSize;
		calcb = pageOffscreen + pageDifference;
		calcc = pageSize - pageDifference;

		pageSize = calcc;

		new Effect.Move(pageMove, {
			x : 0,
			y : calca,
			mode : 'absolute',
			transition : Effect.Transitions.sinoidal,
			duration : 1
		});

		new Effect.Appear('loading_background', {
			duration : 0.5
		});

		window.setTimeout('updatePage()', 1000);
	}
}

function updatePage() {
	// move the div completely out of view...
	new Effect.Move(pageMove, {
		x : 0,
		y : calca - pageOffscreen,
		mode : 'absolute',
		duration : 0
	});

	var url = pageUrl;
	
	if (url.indexOf('?')==-1) {
		url = url + '?ajx=true';
	} else {
		url = url + '&ajx=true';
	}
	
	var pars = '';
	var target = document.getElementById('page_body');
	var myAjax = new Ajax.Updater(target, url, {
		method : 'get',
		parameters : pars,
		onSuccess : function(oXHR, oJson) {
			// alert(oXHR.responseText);
		window.setTimeout('showPage()', 500);
	},
	onFailure : function(oXHR, oJson) {
		window.location = pageUrl;
	}
	});

}

function showPage() {
	// move the div just out of view...
	new Effect.Move(pageMove, {
		x : 0,
		y : (calca - pageOffscreen) + calcb,
		mode : 'absolute',
		duration : 0
	});

	new Effect.Move(pageMove, {
		x : 0,
		y : 0,
		mode : 'absolute',
		transition : Effect.Transitions.sinoidal,
		duration : 1
	});

	new Effect.Fade('loading_background', {
		duration : 0.5
	});
	window.setTimeout('makeChange = true', 2000);
	window.setTimeout('updateLinkOnReload()', 1000);
	// window.setTimeout('pageReset()', 1000);
}

// firefox will shit itself after a while... this at least resets the bug...
function pageReset() {
	new Effect.Move(pageMove, {
		x : 0,
		y : 0,
		mode : 'absolute',
		duration : 0
	});
}

function updateLinkOnReload() {
	if (pageId == 'index_image' || pageId == 'index') {
		updateLink('index-features', 1400);
		updateLink('index-how1', 900);
		updateLink('index-how2', 900);

		window.setTimeout('swfobject.registerObject("FlashID")', 250);
	}

	if (pageId = "faq") {
		updateLink('faq-how', 900);
		updateLink('faq-contact', 900);
	}
	
	if (pageId = "activities") {
		updateLink('activities-previous',1500);
		updateLink('activities-next',1500);
		updateLink('group-previous',1500);
		updateLink('group-next',1500);	
		
		for (var i=1; i < 7; i++) {			
			updateLink('group-text-' + i,1500);
			updateLink('group-image-' + i,1500);
		}		
	}

	window.setTimeout('swfobject.registerObject("loadingFlash")', 250);
}

function updateLink(id, height) {
	try {
		$(id).href = "javascript:changePage('" + id + "','" + $(id).href
				+ "', " + height + ");";
	} catch (e) {
		// alert(id + ' (' + e + ')');
	}
}

function preloadImages() {
	preload_image_a.src = "http://www.spedmo.com/images/small_box_background.png";
	preload_image_b.src = "http://www.spedmo.com/images/header_internal.png";
}

Event.observe(window, 'load', function() {
	initialise();

	pageSize = getHeight();
	if (pageSize == 0) {
		pageSize = 1300;
	}

	pageMove = $('page_overlay');
	pageReset();

	updateLink('index_image', 900);
	updateLink('index', 900);
	updateLink('howTo', 900);
	updateLink('features', 1400);
	updateLink('faq', 1200);	
	
	updateLink('activities', 1500);
	updateLink('activities-previous',1500);
	updateLink('activities-next',1500);
	updateLink('group-previous',1500);
	updateLink('group-next',1500);
	
	for (var i=1; i < 7; i++) {		
		updateLink('group-text-' + i,1500);
		updateLink('group-image-' + i,1500);
	}			
	
	updateLink('index-features', 1400);
	updateLink('index-how1', 900);
	updateLink('index-how2', 900);
	updateLink('faq-how', 900);
	updateLink('faq-contact', 900);

	updateLink('follow', 1300);
	updateLink('privacy', 800);
	updateLink('tou', 1400);
	updateLink('contact', 700);
	
	window.setTimeout('preloadImages()', 500);
});

