
var stepIndex = 1;
var firstStep = true;
var jogReady = true;
var track;

$(document).ready(function(){

	stepIndex = 1;
	var i = location.href.split( '#' );

	if(i.length>1)
	{
		i = i[i.length-1];
		if( !isNaN( i ) && i != '' )
		{
			if( i<track.steps.length )
			{
				stepIndex = i;
			}
		}
	}

	$(window).resize(function(){
  		resize();
	});
	resize();
	$(document).keyup( function( event) {
	switch( event.keyCode )
	{
		case 37 : 	prev();
					break;
		case 39 : 	next();
					break;
	}

	});
});

function initApp()
{
	loadReaderInterface();
	$('#readerDiv div').bind(
				'mouseover',
				function() {
					$(this).addClass( 'hover' );
				}
			);
	$('#readerDiv div').bind(
				'mouseout',
				function() {
					$(this).removeClass( 'hover' );
				}
		);

	selectStep( stepIndex );
	firstStep = false;
	markNextButton();
	try
	{
		appliLoaded();
	}
	catch(e) {};
}

function selectStep( newPosition )
{
	if( jogReady )
	{
		var oldPosition = stepIndex;
		if( !firstStep )
		{
			unmarkNextButton();
		}
		stepIndex = newPosition;
		onSelectStep( oldPosition );
		var url = track.steps[stepIndex-1].url;
		changeIFrameSource( url );
		changeStepProcess();
	}
}

function goToReferer()
{
	var ref = $('#ref').html().replace(/^\s+/g,'').replace(/\s+$/g,'');
	if( ref =='' )
		ref = '/';
	window.location = ref;
}

function prev()
{
	if( stepIndex>1 )
		selectStep( stepIndex - 1 );
}

function next()
{
	if( stepIndex<track.nSteps )
	{
		selectStep( stepIndex + 1 );
	}
	else
	{
		if( redirectAtEnd  )
		{
			goToReferer()
		}
	}
}

function closeReader()
{
	window.location = track.steps[stepIndex-1].url;
}

function refresh()
{
	if( jogReady )
	{
		changeIFrameSource( track.steps[stepIndex-1].url );
	}
}

function changeIFrameSource( url )
{
	$("#jogTheWebContentFrame").attr( 'src', config.urlWait );
	setTimeout( function(){	$("#jogTheWebContentFrame").attr( 'src', url  ) }, 200 );
}

