instanceSetters();
instanceGetScrollState();

if( typeof pageState == 'undefined' ) {
	pageState = new function() {
		var position_path = null;
		var current_transcription_name = null;
		var current_annotation_name = null;
		var new_annotation_id = null;
		var remi_mode = false;
		var selection_commands_active = false;
	}
}

if( typeof transcriptionStates == 'undefined' ) {
	transcriptionStates = new function() {
		keys = new Array();
		states = new Array();
		this.addTranscriptionState = new Function( 'transcriptionName', 'state', 'keys.push( transcriptionName ); states.push( state );' );
		this.getTranscriptionState = new Function( 'transcriptionName', 'return states[this.indexOf(transcriptionName)];' );
		this.indexOf = new Function( 'key', 'var index; for( var i = 0; i < keys.length; i++ ) { if( key == keys[i] ) { index = i; } }; return index;');
	}
}

if( typeof transcriptionState == 'undefined' ) {
	transcriptionState = function() {
		var transcriptionMode = null;
		var annotationMode = null;
		var scrollState = new Array(2);
		scrollState[0] = 0;
		scrollState[1] = 0;
		this.setTranscriptionMode = new Function( 'mode', 'this.transcriptionMode = mode;' );
		this.getTranscriptionMode = new Function( 'return this.transcriptionMode;' );
		this.setAnnotationMode = new Function( 'mode', 'this.annotationMode = mode;' );
		this.getAnnotationMode = new Function( 'return this.annotationMode;' );
		this.setScrollState = new Function( 'scrollState', 'this.scrollState = scrollState;' );
		this.getScrollState = new Function( 'return this.scrollState;' );
	};
}

urlQueryAndLocation = location.href.split( '?' );
if( typeof urlQueryAndLocation[1] != 'undefined' ) {
	urlQuery = urlQueryAndLocation[1].split( '#' );
	setCurrentTranscriptionName( urlQuery[1] );
	urlParameters = urlQuery[0].split( '&' );
	setNewAnnotationID( urlParameters[1].split( '=' )[1] );
}

function instanceSetters() {

	if( typeof setPositionPath == 'undefined' ) {
		setPositionPath = function( path ) {
			pageState.position_path = path;
		};
	}

	if( typeof getPositionPath == 'undefined' ) {
		getPositionPath = function() {
			return pageState.position_path;
		};
	}

	if( typeof setCurrentTranscriptionName == 'undefined' ) {
		setCurrentTranscriptionName = function( transcriptionName ) {
			pageState.current_transcription_name = transcriptionName;
		};
	}

	if( typeof getCurrentTranscriptionName == 'undefined' ) {
		getCurrentTranscriptionName = function() {
			return pageState.current_transcription_name;
		};
	}

	if( typeof setCurrentAnnotationName == 'undefined' ) {
		setCurrentAnnotationName = function( annotationName ) {
			pageState.current_annotation_name = annotationName;
		};
	}
	
	if( typeof getCurrentAnnotationName == 'undefined' ) {
		getCurrentAnnotationName = function() {
			return pageState.current_annotation_name;
		};
	}

	if( typeof setCurrentAnnotationiId == 'undefined' ) {
		setCurrentAnnotationId = function( annotationId ) {
			pageState.current_annotation_id = annotationId;
		};
	}
	
	if( typeof getCurrentAnnotationId == 'undefined' ) {
		getCurrentAnnotationId = function() {
			return pageState.current_annotation_id;
		};
	}

	if( typeof setNewAnnotationID == 'undefined' ) {
		setNewAnnotationID = function( annotationID ) {
			pageState.new_annotation_id = annotationID;
		};
	}
	
	if( typeof getNewAnnotationID == 'undefined' ) {
		getNewAnnotationID = function() {
			return pageState.new_annotation_id;
		};
	}

	if( typeof setRemiMode == 'undefined' ) {
		setRemiMode = function( remiMode ) {
			pageState.remi_mode = remiMode;
		};
	}

	if( typeof getRemiMode == 'undefined' ) {
		getRemiMode = function() {
			return pageState.remi_mode;
		};
	}
	
	if( typeof setSelectionCommandsActive == 'undefined' ) {
		setSelectionCommandsActive = function( editorId ) {
			pageState.selection_commands_active = editorId;
		};
	}

	if( typeof getSelectionCommandsActive == 'undefined' ) {
		getSelectionCommandsActive = function() {
			return pageState.selection_commands_active;
		};
	}

}

// JZ20050927 This function intended to query the annotation for which
// the user wants to add/edit metadata. We can't use
// getCurrentAnnotationName() because that's only set when an annotation is
// being edited, which may not be the case when the addMetaData button is
// clicked. 
if( typeof querySelectedAnnotationId == 'undefined' ) {
	querySelectedAnnotationId = function( selectedTranscriptionName ) {
		var annotationTextDocument = document.getElementById( selectedTranscriptionName + '.annotatorframe' ).contentWindow.document;
		var annotationId = null;
		try {
			annotationId = annotationTextDocument.location.href.split( '?' )[1].split( '=' )[1];
		} catch( exception ) {
			alert( 'No annotation found onto which metadata can be added.' );
		}
		return annotationId;
	};
}
	
// JZ20050928 Added state for metadatproperties, see eLaborateMetadata.js
if( typeof metadataPropertiesState == 'undefined' ) {
	metadataPropertiesState = new function() {
		var transcriptionName = null;
		var annotationId = null;
		var readyStateChangeAction = null;
		this.setTranscriptionName = new Function( 'name', 'this.transcriptionName = name;' );
		this.getTranscriptionName = new Function( 'return this.transcriptionName;' );
		this.setAnnotationId = new Function( 'id', 'this.annotationId = id;' );
		this.getAnnotationId = new Function( 'return this.annotationId;' );
		this.setReadyStateChangeAction = new Function( 'action', 'this.readyStateChangeAction = action;' );
		this.getReadyStateChangeAction = new Function( 'return this.readyStateChangeAction;' );
	};
}

function instanceGetScrollState() {
	if( typeof getScrollState == 'undefined' ) {
		getScrollState = function() {
			scrollState = null;
			if( getCurrentTranscriptionName() ) {
				transcriptionState = transcriptionStates.getTranscriptionState( getCurrentTranscriptionName() );
				if( transcriptionState != null ) {
					scrollState = transcriptionState.getScrollState();
				}
			}
			return scrollState;
		};
	}
}
