/***
 * Theoscrib library.
 * Uses Prototype and Scriptaculous
 * Copyright (c) Theo Leloux
 * Version 1.o January 2009
 */

/* This function assigns the given className to the div identified by the given id */ 
function highlightDiv( aDivName, className ) { var aDiv = $( aDivName ); if ( aDiv ) {	aDiv.className += ' ' + className; } }

/* This function removes the given className from the div identified by the given id */
function delightDiv( aDivName, className ) { var aDiv = $( aDivName  ); if ( aDiv ) {aDiv.className = aDiv.className.replace(className, '');}}

/* Generates and assigns highlighting and dehighligting for the given elemType and partenDiv using given highligh className */
function assignHiglightFunctionsForElementType( elemType, parentDiv, className ) { var fields = $$( elemType ); var hlFun = function() { highlightDiv( parentDiv, className ); };var dlFun = function() { delightDiv( parentDiv, className ); };for (var i = 0; i < fields.length; i++) { fields[i].onfocus = hlFun; fields[i].onblur = dlFun; } }

/* Function to close the overlay voor Ajax loaded HTML */
function closeTheoscriboverlay( anId ) { Effect.SlideUp(anId,{duration:0.5}); return false; }

/* Function for showin the overlay and loading the url with ajax */
function showTheoscriboverlay( anId, anUrl, homeUrl ) { forceVisible( 'messageloading' ); forceHidden('messagecontent'); $('new_navigator').scrollTo();	var handleLoad = function() { loadContent( anUrl, homeUrl ); }; if ( $(anId ) && $(anId).visible() != true ) { Effect.SlideDown(anId,{duration:0.6, afterFinish: handleLoad }); } else { loadContent( anUrl ); } return false; }

/* Function for re-assigning a handler to an url that resides on "www.theoscrib.com" */
function assignHomeHandlers( homeUrl, aParent ) { var hrefs = $$( 'a' ); var par = $( aParent ); for ( var i=0; i < hrefs.length; i++ ) { var aRef = hrefs[ i ]; if ( aRef.descendantOf( par ) ) { var aRel = aRef.rel; if ( aRel.startsWith( 'internal-' ) ) { var oldRef = aRef.href; var oldHtml = aRef.innerHTML; aRef.replace( '<a href="javascript:void(0);" rel="' + aRel + '" onclick="loadSubPage( \'' + oldRef + '\' );">' + oldHtml + '</a>' ); } } } }

/* Function for loading a page within the overlay from theoscrib */
function loadSubPage( oldRef ) { forceHidden('messagecontent'); forceVisible( 'messageloading' ); loadContent( oldRef ); }

/* Calling the ajax engine of prototype and load the given page. The result will be shown in the overlay */
function loadContent( anUrl, homeUrl ) {new Ajax.Request( anUrl, {method:'get',onSuccess: function(transport){forceHidden( 'messageloading' ); forceVisible('messagecontent');var response = transport.responseText || "Sorry de pagina kon niet geladen worden";response = stripWordpressMeuk( response);$( 'messagecontent' ).update( response );},onFailure: function(){ alert("Sorry de pagina kon niet geladen worden") },onComplete: function() { assignHomeHandlers( homeUrl, 'messagecontent' ); }});}

/* Function for making anElement visible (inline css must contain "display") */
function forceVisible( anElement ) { var elem = $( anElement );	if ( elem ) { elem.show(); } }

/* Function for hiding anElement (inline css must contain "display") */
function forceHidden( anElement ) { var elem = $( anElement ); if ( elem ) { elem.hide(); } }

/* Function for removing the HTML wrapper generated by wordpress. Could have created an extra template but 
   by doing it this way the pages are still decorated if visited not with Ajax technology */
function stripWordpressMeuk( content ) {var result = content;var pos = result.indexOf( '<div id="left-column">' );if ( pos != -1 ) {var ePos = result.indexOf( '<div id="right-column">' );if ( ePos != -1 ) {ePos = result.lastIndexOf( '</div>', ePos );result = content.slice( pos + 22, ePos );}}return result;}

/* Function for replacing the given id and assign an onclick. No harm for javascript haters */
function scriptizeAnchor( anId, onclick ) { var anchor = $( anId ); if ( anchor ) { anchor.replace( '<a id="' + anId + '" href="javascript:void(0);" onclick="' + onclick + '">' + anchor.innerHTML + '</a>' );	} }