/*
 *  Original code:
 *  klof  |  innovative web technologies
 *  workshop@klof.nl
 *
 *  Implemented by Trentt
 */

// Easy movement
function glidePath( nTime, nStart, nEnd, nDelay )
{
	if ( ( nTime /= nDelay / 2) < 1)
		return nEnd / 2 * Math.pow( nTime, 3 ) + nStart
	else
		return nEnd / 2 * ( ( nTime -= 2 ) * Math.pow( nTime, 2 ) + 2) + nStart;
}

// Slider object constructor
function slider( sLayer )
{
	this._ancestor = dynamicElement
	this._ancestor( sLayer );

	this._time         = 0;
	this._slideSpeed   = 30;
	this._visible      = false;

	return this;
}
// Assign new position to slider
slider.prototype.reposition = function( nX, nY )
{
	this._time        = 0;
	this._slideXstart = this._x;
	this._slideYstart = this._y;
	this._slideXend   = nX - this._x;
	this._slideYend   = nY - this._y;
	this.slide();
}
// Slide the slider object towards position provided with slider.reposition method
slider.prototype.slide = function()
{
	if ( ++this._time < this._slideSpeed )
	{
		var nY = parseInt( glidePath( this._time, this._slideYstart, this._slideYend, this._slideSpeed ) );
		var nX = parseInt( glidePath( this._time, this._slideXstart, this._slideXend, this._slideSpeed ) );
		this.move( nX, nY );
		this._timer = setTimeout( this._self + ".slide()", 30 );
	}
	else
	{
		this.move( this._slideXstart + this._slideXend, this._slideYstart + this._slideYend );
		clearTimeout( this._timer );
	}
}


/*
 *  Site specific code added by Trentt
 *  R.Spieker@Trentt.com
 */

// Extract an anchor from an URL
function extractAnchor( sURL )
{
	return sURL.substr( sURL.indexOf( "#" ) + 1, sURL.length - sURL.indexOf( "#" ) );
}

// Find the clicked object inside the navigation div
// (this is done, so the same method for sliding to content can be used outside the navigation, so inline in
// the content itself, while still highlighting the correct navigation element)
function findActiveNavigationElement( sAnchor )
{
	// if we sport the DOM
	if ( document.getElementById && document.getElementsByTagName )
	{
		// reference to the navigation div
		var oNavigation = document.getElementById( "navigation" );
		// all A-elements from the navigation object
		var aLink       = oNavigation.getElementsByTagName( "a" );
		// walk thru the A-elements
		for ( var i = 0; i < aLink.length; ++i )
			// Do the anchors match?
			if ( extractAnchor( aLink[ i ].href ) == sAnchor )
				return aLink[ i ];
	}
	return false;
}

// Switch to content identified by an anchor
function showContent( oLink )
{
	// check the current activated link and de-activate it
	if ( oActive != null )
		oActive.style.fontWeight = "normal";
	// Reposition the slider object
	oContent.reposition( aContentPosition[ extractAnchor( oLink.href ) ], 0 );

	// activate the current link
	oActive = findActiveNavigationElement( extractAnchor( oLink.href ) );
	oActive.style.fontWeight = "bold";
	return false;
}

// Zoom an image in a seperate window (exactly sized)
function zoomImage( oImage )
{
	var sImage = oImage.src.replace( /t\.jpg/g, ".jpg" );
	oZoom      = window.open( sImage, "oZoom", "width=" + ( ( oImage.width * 2 ) + 20 ) + ",height=" + ( ( oImage.height * 2 ) + 20 ) + ",scrollbars=no,menubar=no,locationbar=no" );
	oZoom.focus();
}

function init()
{

	// construct the actual slider object
	oContent = new slider( "mover" );

	// Set the default URL
	var sAnchor = "intro";
	// check if an anchor was provided, if so, move ( _no slide_ ) towards the provided anchor
	if ( document.location.href.indexOf( "#" ) >= 0 )
	{
		sAnchor = extractAnchor( document.location.href );
		oContent.move( aContentPosition[ sAnchor ], 0 );
	}
	// Find the active navigation element, and highlight it
	oActive = findActiveNavigationElement( sAnchor );
	if ( oActive )
		oActive.style.fontWeight = "bold";
}

var aContentPosition = new Array();
	// define anchor positions
	aContentPosition[ "intro" ]          = 0;
	aContentPosition[ "news" ]           = -410;
	aContentPosition[ "webapplication" ] = -820;
	aContentPosition[ "webcast" ]        = -1230;
	aContentPosition[ "elearning" ]      = -1640;
	aContentPosition[ "cdrom" ]          = -2050;
	aContentPosition[ "dvl" ]            = -2460;
	aContentPosition[ "showreel" ]       = -2870;
	aContentPosition[ "contact" ]        = -3280;
	aContentPosition[ "login" ]          = -3690;

var oActive = null;
var oContent;

window.onload = init;

