
var stepIndex = 1;
var firstStep = true;
var jogReady = true;
var track;

var prevent_bust = true;
var anchor_event = false;
var warning_framekiller = false;

window.onbeforeunload = framebustbust;

function framebustbust( e )
{
	e = e || window.event;

	if( mustPrevent() )
	{
		if(e)
		{
			e.returnValue = framebust_message;
		}
		return framebust_message;
	}
	anchor_event = false;
}

function mustPrevent()
{
	if( anchor_event )
	{
		return false;
	}

	if( prevent_bust )
	{
		return true;
	}
	else
	{
		return false;
	}
}


function stopBustPrevent()
{
	prevent_bust = false;
}

function restartBustPrevent()
{
	prevent_bust = true;
}


function ieReaderAnchor()
{
	anchor_event = true;
}

function readerAnchor( element )
{
	var url = element.attr( 'href' );
	if( url.indexOf( 'javascript:' ) == -1 )
	{
		anchor_event = true;
	}
}

$(document).ready(function(){

	stepIndex = 1;
	var i = location.href.split( '#' );

	if(i.length>1)
	{
		i = parseInt( 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;
	}

	});

	if( $.browser.msie )
	{
		$('a').live(
				'click',
				function() {
					ieReaderAnchor();
				}
			);
	}
	else
	{
		$('a').live(
				'click',
				function() {
					readerAnchor( $(this) );
				}
			);
	}

});

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()
{
	stopBustPrevent();
	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( 'yes' == options.redirectionAtEnd )
		{
			goToReferer()
		}
	}
}

function closeReader()
{
	stopBustPrevent();
	window.location = track.steps[stepIndex-1].url;
}

function refresh()
{
	if( jogReady )
	{
		changeIFrameSource( track.steps[stepIndex-1].url );
	}
}

function changeIFrameSource( url )
{
	$("#jogTheWebContentFrame").attr( 'src', options.urlWait );
	setTimeout( function(){	$("#jogTheWebContentFrame").attr( 'src', url  ) }, 200 );
}

function refreshReader()
{
	var u = window.location.toString().split( '#' );
	stopBustPrevent();
	window.location = u[0];
}
