Scriptname KA_ElderScrollScript extends ObjectReference  
{script for Elder Scroll inventory object}

VisualEffect Property FXReadElderScrollEffect Auto
VisualEffect Property FXTimeTravelCamAttachEffect Auto
VisualEffect Property FXTimeWarpCamAttachEffect Auto
ImageSpaceModifier Property FXReadScrollsBlindImod Auto
ImageSpaceModifier Property FXReadScrollsImod Auto
ImageSpaceModifier Property FXTimeTravelImodStatic Auto
ImageSpaceModifier Property FXTimeTravelImodStatic01 Auto
ImageSpaceModifier Property FXTimeTravelImodStatic02 Auto
ImageSpaceModifier Property FXTimeTravelImodStatic03 Auto
ImageSpaceModifier Property FXTimeTravelImodStatic04 Auto
ImageSpaceModifier Property FadeToWhiteInOutImod Auto

Idle Property idleReadElderScroll  Auto  
Idle Property idleReadElderScrollLonger  Auto  
; TEMP?
Idle Property IdleStop  Auto  

;;Marks fx armor that is equipt temporarily each time you read the scroll
Armor Property ElderScrollHandAttachArmor Auto

MusicType Property MUSSpecialElderScrollSquence  Auto  

Sound Property OBJElderScrollBlindIn2D Auto
Sound Property OBJElderScrollBlindOut2D Auto
int soundInstance01
int soundInstance02
Sound Property QSTMQ206ElderScrollRead2DSound  Auto  
Sound Property QSTMQ206TimeTravel2DSound  Auto  

globalvariable property numberOfTimesRead auto

Armor Property Mask01 auto
Message Property MainMenuMESG auto
Message Property Story01 auto
Message Property Story02 auto
Message Property Story03 auto
Message Property power01mesg auto
Message Property power02mesg auto
Message Property power03mesg auto

spell property Power01 auto
spell property Power02 auto
spell property Power03 auto

GlobalVariable Property KA_PowerTimer  Auto  
GlobalVariable Property GameDaysPassed  Auto  

Event OnEquipped(Actor akActor)
; 	debug.trace(self + "OnEquipped " + akActor)
	if akActor == Game.GetPlayer() && akActor.GetSitState() == 0 && (KA_PowerTimer.GetValue() < GameDaysPassed.GetValue())
;		; does player have anything equipped in hands?
		bool bWaitForUnequip = ( akActor.GetEquippedItemType(0) > 0 || akActor.GetEquippedItemType(1) > 0 )
; 		debug.trace(self + "bWaitForUnequip = " + bWaitForUnequip )		
			debug.trace(self + " OnEquipped - time travel effects")
;		; close menu force fp, lock controls
			Game.DisablePlayerControls(abMovement = true, abFighting = true, abCamSwitch = true, abLooking = false, abSneaking = true, abMenu = true, abActivate = true, abJournalTabs = true)
			Game.ForceFirstPerson()
;		; trigger music
			MUSSpecialElderScrollSquence.Add()
; Use chant sound
;	; wait for player to sheath weapons
		if bWaitForUnequip
			utility.wait(2.0)							
		endif
;		; play read idle and scroll art animation			
		akActor.equipItem(ElderScrollHandAttachArmor, false, true)			
		akActor.PlayIdle(IdleReadElderScroll)				
		akActor.DispelAllSpells()
		utility.wait(0.5)
		; play reading sound
		QSTMQ206ElderScrollRead2DSound.Play(akActor)
		utility.wait(0.75)
;	; play time travel sound
		QSTMQ206TimeTravel2DSound.Play(akActor)
		utility.wait(0.75)
		FXTimeWarpCamAttachEffect.Play(akActor)
		utility.wait(1.25)
		MUSSpecialElderScrollSquence.Add()
;		utility.wait(1.75)
		FadeToWhiteInOutImod.Apply()
;	;do time travel scroll read fx. Read scroll and then have a lasting screen fx that turns off in timeTravelCleanup
		utility.wait(1.0)
		FXTimeWarpCamAttachEffect.Stop(akActor)
		utility.wait(4.0)
;============================
; MENUS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		story()
		menu()
;============================	

;			;set fx for back in time seq.			
;			FXTimeTravelCamAttachEffect.Play(akActor)
;			FXTimeTravelImodStatic.ApplyCrossFade(0.05)
;			utility.wait(4.0)
			akActor.PlayIdle(IdleStop)
;			;Marks remove elder scrolll from hands
			akActor.removeItem(ElderScrollHandAttachArmor, 1, true)			
;			;utility.wait(5.0)		
			utility.wait(1.0)		
;			;FXTimeTravelImodStatic02.ApplyCrossFade(5.0)
			game.EnablePlayerControls()

		else
			Game.DisablePlayerControls(abMovement = false, abFighting = false, abCamSwitch = false, abLooking = false, abSneaking = false, abMenu = true, abActivate = false, abJournalTabs = false)
			if akActor.GetSitState() == 0
				Game.ForceFirstPerson()
			endif			
			if akActor.GetSitState() == 0
				akActor.equipItem(ElderScrollHandAttachArmor, false, true)
				; play read idle and scroll art animation
				akActor.PlayIdle(idleReadElderScroll)		
				;akActor.PlaySubGraphAnimation("stage2")	
				utility.wait(1.05)
			endif		
			soundInstance01 = OBJElderScrollBlindIn2D.play(akActor) 
			utility.wait(0.5)
			game.ShakeCamera(afStrength = 0.5, afDuration = 1.5) 
			FXReadElderScrollEffect.Play(akActor, 8.1)	
			FXReadScrollsBlindImod.Apply()
			utility.wait(3.0)
			if akActor.GetSitState() == 0
				akActor.PlayIdle(IdleStop)
				akActor.removeItem(ElderScrollHandAttachArmor, 1, true)
			endif

			Game.EnablePlayerControls(abMovement = false, abFighting = false, abCamSwitch = false, abLooking = false, abSneaking = false, abMenu = true, abActivate = false, abJournalTabs = false)
			utility.wait(1.9)			
			soundInstance02 = OBJElderScrollBlindOut2D.play(akActor) 
				
	endIf
endEvent

int count  ;stores the number of times this object has been activated

Function Story()
count = count + 1
	if count == 1
   		Story01.show()
	elseif count == 2
   		 Story02.show()
	elseif count == 3
   		Story03.show()		
	else
	endif	
endFunction

Function Menu(Int aiMessage = 0, Int aiButton = 0, Bool abMenu = True)
	While abMenu
		If aiButton == -1 ; prevents problems if recycling aiButton

		ElseIf aiMessage == 0
			aiButton = MainMenuMESG.show()
				If aiButton == 0
					aiButton = Power01MESG.Show()
						If aiButton == 0
							aiButton = MainMenuMESG.Show()
						else
							Game.GetPlayer().removeSpell(Power02)
							Game.GetPlayer().removeSpell(Power03)
							Game.GetPlayer().addSpell(Power01)
							abMenu = False						
						endif							
				ElseIf aiButton == 1
					aiButton = Power02MESG.Show()
						If aiButton == 0
							aiButton = MainMenuMESG.Show()
						else
							Game.GetPlayer().removeSpell(Power01)
							Game.GetPlayer().removeSpell(Power03)
							Game.GetPlayer().addSpell(Power02)
							abMenu = False						
						endif
				ElseIf aiButton == 2
					aiButton = Power03MESG.Show()						
						If aiButton == 0
							aiButton = MainMenuMESG.Show()
						else
							Game.GetPlayer().removeSpell(Power01)
							Game.GetPlayer().removeSpell(Power02)
							Game.GetPlayer().addSpell(Power03)
							abMenu = False						
						endif
				Else
					abMenu = False
				EndIf
		endIf
	endWhile
endFunction