/*
 * Scripting common for all pages
 */

function extractContent(data) {
    return data.replace(/^[\S\s]*<body[^>]*>([\S\s]*)<\/body>[\S\s]*$/i, '$1');
}

function processNewContent(data, status, request) {
    newData = extractContent(data);
    newContentData = jQuery(newData);

    scriptify(newContentData);
    //positionImages(newContentData);

    jQuery('#main')
	.slideUp(250)
	.queue(function() {
		jQuery('#title').css('overflow', 'hidden').replaceWith(newContentData.find('#title'));
		jQuery('#content').css('overflow', 'hidden').replaceWith(newContentData.find('#content'));
		jQuery('#navigation').replaceWith(newContentData.find('#navigation'));
		jQuery(this).dequeue();
	    })
	.slideDown(250)
	.queue(function() {
		tidyLayout(document);
		jQuery(this).dequeue();
	    });

}

function updateContent(e) {
    updateLocalURL(e.target.href);
    jQuery.get(e.target.href, processNewContent);
    //    updateLocalURL(e.target.href);
    return false;
}

function updateLocalURL(path) {
    basePath = getBasePath();

    if (path.match("^" + basePath) == basePath) {
	fragment = path.substr(basePath.length);

	window.location.hash = fragment;
    }
}

function getBasePath() {
    base = jQuery(document).find('base');
    if (base && base.attr('href')) {
	return base.attr('href');
    }
    // else
    return window.location.protocol +'//' +window.location.host + '/';

    // basePathArr = window.location.pathname.split("/");
    // lastIndex = basePathArr.length -1;
    // if (basePathArr.length == 0 || basePathArr[lastIndex].indexOf(".html") != -1) {
    //  basePathArr.splice(lastIndex, 1);
    // }
    // basePath = basePathArr.join('/');

}

function positionImages(what) {
    what = jQuery(what);

    var content = what.find('#content');

    if (content.find('#centreImage').length > 0) {
	return; // We don't move the images in this case
    }

    var rightImage = content.find('#rightImage').hide();
    var haveRightImage = (rightImage.length > 0);

    var leftImage = content.find('#leftImage').hide();
    var haveLeftImage = (leftImage.length > 0);

    var initialContentHeight = content.outerHeight(true);

    var leftSpacer = null;
    var rightSpacer = null;

    if (haveLeftImage) {
	content.prepend(leftImage.show());
    }

    if (haveRightImage) {
	content.prepend(rightImage.show());
    }

    if (haveRightImage) {
	content.prepend('<div id="rightImageSpacer"></div>');
	rightSpacer = jQuery('#rightImageSpacer').css('height', Math.max(0, initialContentHeight - rightImage.outerHeight(true)));
    }

    if (haveLeftImage) {
	content.prepend('<div id="leftImageSpacer"></div>');
	leftSpacer = jQuery('#leftImageSpacer').css('height', Math.max(0, initialContentHeight - leftImage.outerHeight(true)));
    }

    if (haveLeftImage || haveRightImage) {
	var done = false;
	var attempts = 0;
	var delta = 0;
	var movement = 0;

	do {
	    if (haveLeftImage) {
		delta = content.outerHeight(true) - (leftSpacer.height() + leftImage.outerHeight(true));
	    } else {
		delta = content.outerHeight(true) - (rightSpacer.height() + rightImage.outerHeight(true));
	    }

	    if (delta > 2) {
		movement = (delta/2);
	    } else if (delta < -2) {
		movement = - (delta/2);
	    } else {
		done = true;
	    }

	    if (!done) {
		if (haveLeftImage) {
		    leftSpacer.height(Math.max(0, leftSpacer.height() + movement) );
		}
		
		if (haveRightImage) {
		    rightSpacer.height(Math.max(0, rightSpacer.height() + movement) );
		}
	    }

	    attempts ++;
	} while (!done && attempts < 10);
    }	    

    return;

    if (rightImage.length > 0) {
	content.prepend(rightImage).prepend('<div id="rightImageSpacer"></div>');
	var spacer = jQuery('#rightImageSpacer').css('height', content.height() - rightImage.height());

	var done = false;
	var attempts = 0;

	do {
	    var delta = content.height() - (spacer.height() + rightImage.height());

	    if (delta > 1) {
		spacer.height(spacer.height() - (delta/2));
	    } else if (delta < -1) {
		spacer.height(spacer.height() + (delta/2));
	    } else {
		done = true;
	    }

	    attempts ++;
	} while (!done && attempts < 10);
    }	    

}

function tidyLayout(what) {
    what = jQuery(what);
    contentObj = what.find('#content');
    contentObj.css('overflow', 'auto');

    //positionImages(what);

    contentObj.height(what.find('#main').css('overflow', 'hidden').height() - contentObj.position().top);
}

function scriptify(what) {
    what = jQuery(what);
    what.find('#navigation a').click(updateContent);
}

function getNewURL(fragment) {
    basePath = getBasePath();
    if (fragment.substr(0,1) == '/') {
	fragment = fragment.substr(1);
    }
    return basePath + fragment;
}

function getPreferredURL(defaultURL) {
    pref = window.location.hash;
    if (pref.length > 1) {
	pref = pref.substr(1);
	return pref;
    }
    // else
    return defaultURL;
}

function checkRedirect() {
    pref = getPreferredURL(null);

    if (pref) {
	window.location = getNewURL(pref);
    }
}
	

jQuery.noConflict();
jQuery(document).ajaxError(function(event, XMLHttpRequest, ajaxOptions, thrownError) { alert('Sorry: There was a problem loading the content.') });

