-------------------------------------------------------------------------------------------
---																						---
---								 Lockout v. 0.9b										---
---			   				by Giloran of EU-Blackrock					 				---
---					                                        							---
---	  Special Thanks to Zûk of EU-Blackrock for countless duels for testing purposes  	---
---																						---
-------------------------------------------------------------------------------------------
local lockout = CreateFrame("Frame", lockout, UIParent)

local lockouttar = CreateFrame("Frame", lockouttar, lockout)
local lockoutfoc = CreateFrame("Frame", lockoutfoc, lockout)

SLASH_LO1 = "/lockout"
SLASH_LO2 = "/lo"


---- Global SavedVars ----

lockout_targetxoffset = 500													
lockout_targetyoffset = 400
lockout_focusxoffset = 500
lockout_focusyoffset = 600
lockout_showtarget = true
lockout_showfocus = true
lockout_vertical = false
lockout_horizontal = true
lockout_scale = 1

local lastUpdate = 0
local tarschool1free = true -- indicator, icon1 isn't used yet
local tarschool2free = true
local tarschool3free = true
local tarschool1time -- time when interrupt happened, GetTime() format
local tarschool2time
local tarschool3time
local tarschool1dur -- duration of interrupt
local tarschool2dur
local tarschool3dur
local focschool1dur
local focschool2dur
local focschool3dur
local tartemp1 
local tartemp2
local tartemp3
local focschool1free = true
local focschool2free = true
local focschool3free = true
local focschool1time
local focschool2time
local focschool3time
local foctemp1
local foctemp2
local foctemp3
local tarcaster -- caster whom's interrupted cast is displayed in icon1
local foccaster -- caster whom's interrupted cast is displayed in icon1
local tar1school -- locked out spellschool displayed in icon1
local tar2school
local tar3school
local foc1school -- locked out spellschool displayed in icon1
local foc2school
local foc3school
local tar1icon
local tar2icon
local tar3icon
local foc1icon
local foc2icon
local foc3icon

local counter = 0
local text

local time1list = {}
local time2list = {}
local time3list = {}
local dur1list = {}
local dur2list = {}
local dur3list = {}
local icon1list = {}
local icon2list = {}
local icon3list = {}
local school1list = {}
local school2list = {}
local school3list = {}
local guidlist = {}
local cleartimelist = {}



local hpally = {} -- List of Holy Paladins found
local rshaman = {} -- List of Resto Shamans found

lockouttar:SetFrameStrata("LOW")
lockoutfoc:SetFrameStrata("LOW")
lockouttar:SetClampedToScreen(true)
lockoutfoc:SetClampedToScreen(true)
lockouttar:SetWidth(96)
lockoutfoc:SetWidth(96)
lockouttar:SetHeight(32)
lockoutfoc:SetHeight(32)

local tarschool1 = CreateFrame("Frame", tarschool1, lockouttar)
local tarschool2 = CreateFrame("Frame", tarschool2, lockouttar)
local tarschool3 = CreateFrame("Frame", tarschool3, lockouttar)
local focschool1 = CreateFrame("Frame", focschool1, lockoutfoc)
local focschool2 = CreateFrame("Frame", focschool2, lockoutfoc)
local focschool3 = CreateFrame("Frame", focschool3, lockoutfoc)
local tarschool1text = tarschool1:CreateFontString("tarschool1text")
tarschool1text:SetFont("Fonts\\FRIZQT__.TTF", 17, "OUTLINE")
tarschool1text:SetPoint("CENTER", tarschool1, "CENTER")
local tarschool2text = tarschool2:CreateFontString("tarschool2text")
tarschool2text:SetFont("Fonts\\FRIZQT__.TTF", 17, "OUTLINE")
tarschool2text:SetPoint("CENTER", tarschool2, "CENTER")
local tarschool3text = tarschool3:CreateFontString("tarschool3text")
tarschool3text:SetFont("Fonts\\FRIZQT__.TTF", 17, "OUTLINE")
tarschool3text:SetPoint("CENTER", tarschool3, "CENTER")
local focschool1text = focschool1:CreateFontString("focschool1text")
focschool1text:SetFont("Fonts\\FRIZQT__.TTF", 17, "OUTLINE")
focschool1text:SetPoint("CENTER", focschool1, "CENTER")
local focschool2text = focschool2:CreateFontString("focschool2text")
focschool2text:SetFont("Fonts\\FRIZQT__.TTF", 17, "OUTLINE")
focschool2text:SetPoint("CENTER", focschool2, "CENTER")
local focschool3text = focschool3:CreateFontString("focschool3text")
focschool3text:SetFont("Fonts\\FRIZQT__.TTF", 17, "OUTLINE")
focschool3text:SetPoint("CENTER", focschool3, "CENTER")

local dummytexttar = lockouttar:CreateFontString("dummytexttar")
dummytexttar:SetFont("Fonts\\FRIZQT__.TTF", 12, "OUTLINE")
dummytexttar:SetPoint("CENTER", lockouttar, "CENTER")	
dummytexttar:SetText("Target")
local dummytextfoc = lockoutfoc:CreateFontString("dummytextfoc")
dummytextfoc:SetFont("Fonts\\FRIZQT__.TTF", 12, "OUTLINE")
dummytextfoc:SetPoint("CENTER", lockoutfoc, "CENTER")
dummytextfoc:SetText("Focus")

tarschool1:SetWidth(32)
tarschool2:SetWidth(32)
tarschool3:SetWidth(32)
focschool1:SetWidth(32)
focschool2:SetWidth(32)
focschool3:SetWidth(32)

tarschool1:SetHeight(32)
tarschool2:SetHeight(32)
tarschool3:SetHeight(32)
focschool1:SetHeight(32)
focschool2:SetHeight(32)
focschool3:SetHeight(32)

local textarschool1 = tarschool1:CreateTexture()
local textarschool2 = tarschool2:CreateTexture()
local textarschool3 = tarschool3:CreateTexture()
local texfocschool1 = focschool1:CreateTexture()
local texfocschool2 = focschool2:CreateTexture()
local texfocschool3 = focschool3:CreateTexture()
local texdummytar = lockout:CreateTexture("texdummytar")
local texdummyfoc = lockout:CreateTexture("texdummyfoc")
local texbordertar1 = lockout:CreateTexture("texbordertar1", "BORDER")
local texbordertar2 = lockout:CreateTexture("texbordertar2", "BORDER")
local texbordertar3 = lockout:CreateTexture("texbordertar3", "BORDER")
local texborderfoc1 = lockout:CreateTexture("texborderfoc1", "BORDER")
local texborderfoc2 = lockout:CreateTexture("texborderfoc2", "BORDER")
local texborderfoc3 = lockout:CreateTexture("texborderfoc3", "BORDER")

tarschool1:SetPoint("CENTER", lockouttar, "CENTER", -32, 0)
tarschool2:SetPoint("CENTER", lockouttar, "CENTER", 0, 0)
tarschool3:SetPoint("CENTER", lockouttar, "CENTER", 32, 0)
focschool1:SetPoint("CENTER", lockoutfoc, "CENTER", -32, 0)
focschool2:SetPoint("CENTER", lockoutfoc, "CENTER", 0, 0)
focschool3:SetPoint("CENTER", lockoutfoc, "CENTER", 32, 0)

lockouttar:SetPoint("BOTTOMLEFT", UIParent, "BOTTOMLEFT", lockout_targetxoffset, lockout_targetyoffset)
lockoutfoc:SetPoint("BOTTOMLEFT", UIParent, "BOTTOMLEFT", lockout_focusxoffset, lockout_focusyoffset)

texdummytar:SetTexture(0,0,0,0.6)
texdummyfoc:SetTexture(0,0,0,0.6)
texbordertar1:SetTexture("Interface\\AddOns\\Lockout\\border.tga")
texbordertar2:SetTexture("Interface\\AddOns\\Lockout\\border.tga")
texbordertar3:SetTexture("Interface\\AddOns\\Lockout\\border.tga")
texborderfoc1:SetTexture("Interface\\AddOns\\Lockout\\border.tga")
texborderfoc2:SetTexture("Interface\\AddOns\\Lockout\\border.tga")
texborderfoc3:SetTexture("Interface\\AddOns\\Lockout\\border.tga")

texdummytar:SetAllPoints(lockouttar)
texdummyfoc:SetAllPoints(lockoutfoc)
textarschool1:SetAllPoints(tarschool1)
textarschool2:SetAllPoints(tarschool2)
textarschool3:SetAllPoints(tarschool3)
texfocschool1:SetAllPoints(focschool1)
texfocschool2:SetAllPoints(focschool2)
texfocschool3:SetAllPoints(focschool3)
texbordertar1:SetAllPoints(tarschool1)
texbordertar2:SetAllPoints(tarschool2)
texbordertar3:SetAllPoints(tarschool3)
texborderfoc1:SetAllPoints(focschool1)
texborderfoc2:SetAllPoints(focschool2)
texborderfoc3:SetAllPoints(focschool3)

lockouttar.texture = texdummytar
lockoutfoc.texture = texdummyfoc
tarschool1.texture = textarschool1
tarschool2.texture = textarschool2
tarschool3.texture = textarschool3
focschool1.texture = texfocschool1
focschool2.texture = texfocschool2
focschool3.texture = texfocschool3



tarschool1:Hide()
tarschool2:Hide()
tarschool3:Hide()
focschool1:Hide()
focschool2:Hide()
focschool3:Hide()
texbordertar1:Hide()
texbordertar2:Hide()
texbordertar3:Hide()
texborderfoc1:Hide()
texborderfoc2:Hide()
texborderfoc3:Hide()

texdummytar:Hide()
texdummyfoc:Hide()
dummytexttar:Hide()
dummytextfoc:Hide()



lockout:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
lockout:RegisterEvent("PLAYER_TARGET_CHANGED")
lockout:RegisterEvent("PLAYER_FOCUS_CHANGED")
lockout:RegisterEvent("ADDON_LOADED")

function lockout:OnEvent(event, ...)

	if event == "COMBAT_LOG_EVENT_UNFILTERED" then
		local _, event = ...
		if event == "SPELL_INTERRUPT" then
			local timestamp, _, _, myName, _, tarGUID, tarName, _, myspellID, myspellName, _, tarspellID, tarspellName, tarSchool = ...
			local dur
			local _, _, icon = GetSpellInfo(tarspellID)
			dur = lockout.getdur(myspellID, tarGUID) -- Estimate Interrupt Duration
			lockout.interrupt(tarSchool, tarGUID, tarName, icon, GetTime(), dur) -- Determine interrupted Spellschool, set Icon and show it 			
		elseif event == "SPELL_CAST_START" or event == "SPELL_CAST_SUCCESS" then
			local _, _, guid, name, _, _, _, _, spellId, spellName, school = ...
			if spellId == 61295 or spellId == 61299 or spellId == 61300 or spellId == 61301 or spellId == 974 or spellId == 32593 or spellId == 32594 or spellId == 49283 or spellId == 49284 or spellId == 16190 then -- Detect Restoration Shaman according to used Spells (Riptide & Earth Shield & Mana Tide Totem)		
				for i=0, 1000 do 
					if rshaman[i] == name then -- Check whether Shaman has already been detected
						break
					elseif rshaman[i] == nil then -- Reached end of the List, adding Shaman
						rshaman[i] = name 
						break
					end
				end
			elseif spellId == 20473 or spellId == 48825 or spellId == 48824 or spellId == 33072 or spellId == 27174 or spellId == 20930 or spellId == 20929 or spellId == 53563 then -- Detect Holy Paladin according to used Spells (Holy Shock & Beacon of Light)
				for i=0, 1000 do 
					if hpally[i] == name then -- Check whether Paladin has already been detected
						break
					elseif hpally[i] == nil then -- Reached end of the List, adding Paladin
						hpally[i] = name
						break
					end
				end
			end
			lockout.checkfailedinterrupts(guid, school)
		end
	elseif event == "PLAYER_TARGET_CHANGED" then
		lockout.changedtar()
	elseif event == "PLAYER_FOCUS_CHANGED" then
		lockout.changedfoc()
	elseif event == "ADDON_LOADED" then
		local name = ...
		if name == "Lockout" then  -- Load SavedVars
		
			print("[Lockout] Addon loaded, type '/Lockout' or '/LO' for configuration")
			lockouttar:SetPoint("BOTTOMLEFT", UIParent, "BOTTOMLEFT", lockout_targetxoffset, lockout_targetyoffset)
			lockoutfoc:SetPoint("BOTTOMLEFT", UIParent, "BOTTOMLEFT", lockout_focusxoffset, lockout_focusyoffset)
	
			Lockout_Target_Check:SetChecked(lockout_showtarget)
			Lockout_Focus_Check:SetChecked(lockout_showfocus)
			if lockout_vertical == true and lockout_horizontal == true then -- Two Checkboxes that should be exclusive are checked at the same time. Disable one of them.
				lockout_vertical = false
			end
			if lockout_vertical == true then
				lockouttar:SetWidth(32)
				lockoutfoc:SetWidth(32)
				lockouttar:SetHeight(96)
				lockoutfoc:SetHeight(96)
				tarschool1:SetPoint("CENTER", lockouttar, "CENTER", 0, -32)
				tarschool2:SetPoint("CENTER", lockouttar, "CENTER", 0, 0)
				tarschool3:SetPoint("CENTER", lockouttar, "CENTER", 0, 32)
				focschool1:SetPoint("CENTER", lockoutfoc, "CENTER", 0, -32)
				focschool2:SetPoint("CENTER", lockoutfoc, "CENTER", 0, 0)
				focschool3:SetPoint("CENTER", lockoutfoc, "CENTER", 0, 32)
				Lockout_Vertical_Check:SetChecked(true)
				Lockout_Horizontal_Check:SetChecked(false)
			elseif lockout_horizontal == true then
				lockouttar:SetWidth(96)
				lockoutfoc:SetWidth(96)
				lockouttar:SetHeight(32)
				lockoutfoc:SetHeight(32)
				tarschool1:SetPoint("CENTER", lockouttar, "CENTER", -32, 0)
				tarschool2:SetPoint("CENTER", lockouttar, "CENTER", 0, 0)
				tarschool3:SetPoint("CENTER", lockouttar, "CENTER", 32, 0)
				focschool1:SetPoint("CENTER", lockoutfoc, "CENTER", -32, 0)
				focschool2:SetPoint("CENTER", lockoutfoc, "CENTER", 0, 0)
				focschool3:SetPoint("CENTER", lockoutfoc, "CENTER", 32, 0)
				Lockout_Horizontal_Check:SetChecked(true)
				Lockout_Vertical_Check:SetChecked(false)
			end

			Lockout_Scale_Slider:SetValue(lockout_scale*100)
			lockouttar:SetScale(lockout_scale)
			lockoutfoc:SetScale(lockout_scale)
		end
	end
end

function lockout:OnUpdate(elapsed)

	lastUpdate = lastUpdate + elapsed
	
	if (lastUpdate > 0.01) then
		lastUpdate = 0
		counter = counter+1
		if counter >= 1000 then
			lockout.cleanlists()
		end
		if tarschool1free == false then -- First School locked
			tartemp1 = tarschool1time + tarschool1dur 
			text = ((math.floor((tartemp1-GetTime())*10))/10)
			tarschool1text:SetText(lockout.formattext(text))
			if GetTime() >= tartemp1 then -- Check Time
				tarschool1free = true -- If Interrupt Duration is exceeded, Hide Icon again
				texbordertar1:Hide()
				tarschool1:Hide()
			end
		end		
		if tarschool2free == false then
			tartemp2 = tarschool2time + tarschool2dur
			text = ((math.floor((tartemp2-GetTime())*10))/10)
			tarschool2text:SetText(lockout.formattext(text))
			if GetTime() >= tartemp2 then
				tarschool2free = true
				texbordertar2:Hide()
				tarschool2:Hide()
			end
		end		
		if tarschool3free == false then
			tartemp3 = tarschool3time + tarschool3dur
			text = ((math.floor((tartemp3-GetTime())*10))/10)
			tarschool3text:SetText(lockout.formattext(text))
			if GetTime() >= tartemp3 then
				tarschool3free = true
				texbordertar3:Hide()
				tarschool3:Hide()
			end
		end		
		if focschool1free == false then -- First School locked
			foctemp1 = focschool1time + focschool1dur 
			text = ((math.floor((foctemp1-GetTime())*10))/10)
			focschool1text:SetText(lockout.formattext(text))
			if GetTime() >= foctemp1 then -- Check Time
				focschool1free = true -- If Interrupt Duration is exceeded, Hide Icon again
				texborderfoc1:Hide()
				focschool1:Hide()
			end
		end		
		if focschool2free == false then
			foctemp2 = focschool2time + focschool2dur
			text = ((math.floor((foctemp2-GetTime())*10))/10)
			focschool2text:SetText(lockout.formattext(text))
			if GetTime() >= foctemp2 then
				focschool2free = true
				texborderfoc2:Hide()
				focschool2:Hide()
			end
		end		
		if focschool3free == false then
			foctemp3 = focschool3time + focschool3dur
			text = ((math.floor((foctemp3-GetTime())*10))/10)
			focschool3text:SetText(lockout.formattext(text))
			if GetTime() >= foctemp3 then
				focschool3free = true
				texborderfoc3:Hide()
				focschool3:Hide()
			end
		end
	end
end

lockout:SetScript("OnEvent", lockout.OnEvent)
lockout:SetScript("OnUpdate", lockout.OnUpdate)

function lockout.formattext(text)
	if text%2 == 0 or text%2 == 1 then -- text is integer
		text = (tostring(text)..".0")
	end
	return tostring(text)
end

function lockout.cleanlists()
	for i = 0, 1000 do
		if cleartimelist[i] and GetTime() > cleartimelist[i] then
			time1list[i] = nil
			time2list[i] = nil
			time3list[i] = nil
			dur1list[i] = nil
			dur2list[i] = nil
			dur3list[i] = nil
			icon1list[i] = nil
			icon2list[i] = nil
			icon3list[i] = nil
			school1list[i] = nil
			school2list[i] = nil
			school3list[i] = nil
			guidlist[i] = nil
			cleartimelist[i] = nil
		end
	end
	
end

function lockout.interrupt(school, unitGUID, name, icon, t, d)
lockout.insertinlists(t, d, icon, school, unitGUID)
	if UnitGUID("target") == unitGUID and lockout_showtarget == true then
		tarcaster = unitGUID
		if tarschool1free == true then
			textarschool1:SetTexture(icon)
			tar1icon = icon
			tarschool1time = t -- pass over time of interrupt
			tarschool1dur = d -- pass over duration of interrupt
			texbordertar1:Show()
			tarschool1:Show()
			tar1school = school
			tarschool1free = false
		elseif tarschool2free == true then
			textarschool2:SetTexture(icon)
			tarschool2time = t
			tarschool2dur = d
			tarschool2:Show()
			texbordertar2:Show()
			tar2school = school
			tarschool2free = false
		elseif tarschool3free == true then
			textarschool3:SetTexture(icon)	
			tarschool3time = t
			tarschool3dur = d
			tarschool3:Show()
			texbordertar3:Show()
			tar3school = school
			tarschool3free = false
		end
	end
	if UnitGUID("focus") == unitGUID and lockout_showfocus == true then 
		foccaster = unitGUID
		if focschool1free == true then
			texfocschool1:SetTexture(icon)
			focschool1time = t -- pass over time of interrupt
			focschool1dur = d -- pass over duration of interrupt
			focschool1:Show()
			texborderfoc1:Show()
			foc1school = school
			focschool1free = false
		elseif focschool2free == true then
			texfocschool2:SetTexture(icon)
			focschool2time = t
			focschool2dur = d
			focschool2:Show()
			texborderfoc2:Show()
			foc2school = school
			focschool2free = false
		elseif focschool3free == true then	
			texfocschool3:SetTexture(icon)
			focschool3time = t
			focschool3dur = d
			focschool3:Show()			
			texborderfoc3:Show()
			foc3school = school
			focschool3free = false	
		end
	end
end

function lockout.insertinlists(t, dur, icon, school, guid)


	local j = -1
	local lon
	for i=0, 1000 do
		if guidlist[i] == guid then -- Checker whether the interrupted target already is in the List
			j = i
			break
		end
	end
	if j == -1 then -- interrupted target is not in the list yet.		
		for i=0, 1000 do
			if guidlist[i] == nil then -- Find the first free listlost
				j = i
				break
			end
		end
	end

	guidlist[j] = guid
	
	if time1list[j] == nil then
		time1list[j] = t
		dur1list[j] = dur
		icon1list[j] = icon
		school1list[j] = school
	elseif time2list[j] == nil then
		time2list[j] = t
		dur2list[j] = dur
		icon2list[j] = icon
		school2list[j] = school	
	elseif time3list[j] == nil then
		time3list[j] = t
		dur3list[j] = dur
		icon3list[j] = icon
		school3list[j] = school	
	end
	
	lon = t + dur
	if cleartimelist[j] and cleartimelist[j] < lon then
		cleartimelist[j] = lon
	elseif cleartimelist[j] == nil then
		cleartimelist[j] = lon
	end
			
end

function lockout.getdur(id, guid)
	local basedur
	if id == 72 then -- Shield Bash
		basedur = 6
	elseif id == 1766 then -- Kick
		basedur = 5
	elseif id == 2139 then -- Counterspell
		basedur = 8
	elseif id == 6552 then -- Pummel
		basedur = 4
	elseif id == 16979 then -- Feral Charge - Bear
		basedur = 4
	elseif id == 19244 then -- Spell Lock Rank1
		basedur = 5
	elseif id == 19647 then -- Spell Lock Rank2
		basedur = 6			
	elseif id == 26090 then -- Pummel (Pet)
		basedur = 2
	elseif id == 47528 then -- Mind Freeze
		basedur = 4
	elseif id == 57994 then -- Wind Shear
		basedur = 2
	else
		basedur = 3
		if not id == 15487 and not id == 10308  and not id == 5589 and not id == 5588 and not id == 853 and not id == 34490 and not id == 25046 and not id == 28730 and not id == 50613 then
			print("[Lockout] Unknown Interrupt: "..GetSpellInfo(id))
		end
	end
	
	local targuid = UnitGUID("target")
	local focguid = UnitGUID("focus")
	if guid == targuid then
		basedur = basedur*lockout.getdurmod("target") -- Determine possible Duration-Reduces
	end
	if guid == focguid then
		basedur = basedur*lockout.getdurmod("focus")
	end
	
	return basedur -- Interrupt Duration Estimated as well as possible depending on Buffs, Talents and Interrupt Ability
end

function lockout.changedtar()
	
	tarschool1free = true
	tarschool2free = true
	tarschool3free = true
	texbordertar1:Hide()
	tarschool1:Hide()
	texbordertar2:Hide()
	tarschool2:Hide()
	texbordertar3:Hide()
	tarschool3:Hide()
	
	for i=0, 1000 do
		if guidlist[i] and guidlist[i] == UnitGUID("target") and lockout_showtarget == true then --search current target in List
			tarschool1time = time1list[i]
			tarschool2time = time2list[i]
			tarschool3time = time3list[i]
			tarschool1dur = dur1list[i]
			tarschool2dur = dur2list[i]
			tarschool3dur = dur3list[i]
			tar1icon = icon1list[i]
			tar2icon = icon2list[i]
			tar3icon = icon3list[i]
			tar1school = school1list[i]
			tar2school = school2list[i]
			tar3school = school3list[i]
			tarcaster = guidlist[i]
			if tarschool1time then
				textarschool1:SetTexture(tar1icon)
				texbordertar1:Show()
				tarschool1:Show()
				tarschool1free = false
			end
			if tarschool2time then
				textarschool2:SetTexture(tar2icon)
				texbordertar2:Show()
				tarschool2:Show()
				tarschool2free = false
			end
			if tarschool3time then
				textarschool3:SetTexture(tar3icon)
				texbordertar3:Show()
				tarschool3:Show()
				tarschool3free = false
			end			
			break
		end
	end
end

function lockout.changedfoc()
	
	focschool1free = true
	focschool2free = true
	focschool3free = true
	texborderfoc1:Hide()
	focschool1:Hide()
	texborderfoc2:Hide()
	focschool2:Hide()
	texborderfoc3:Hide()
	focschool3:Hide()
	
	for i=0, 1000 do
		if guidlist[i] and guidlist[i] == UnitGUID("focus") and lockout_showfocus == true then --search current focus in List
			focschool1time = time1list[i]
			focschool2time = time2list[i]
			focschool3time = time3list[i]
			focschool1dur = dur1list[i]
			focschool2dur = dur2list[i]
			focschool3dur = dur3list[i]
			foc1icon = icon1list[i]
			foc2icon = icon2list[i]
			foc3icon = icon3list[i]
			foc1school = school1list[i]
			foc2school = school2list[i]
			foc3school = school3list[i]
			foccaster = guidlist[i]
			if focschool1time then
				texfocschool1:SetTexture(foc1icon)
				texborderfoc1:Show()
				focschool1:Show()
				focschool1free = false
			end		
			if focschool2time then
				texfocschool2:SetTexture(foc2icon)
				texborderfoc2:Show()
				focschool2:Show()
				focschool2free = false
			end		
			if focschool3time then
				texfocschool3:SetTexture(foc3icon)
				texborderfoc3:Show() 
				focschool3:Show()
				focschool3free = false
			end
			break
		end
	end
end

function lockout.getdurmod(unitid)
	local modifier = 1
	for i=1, 40 do
		local name, rank, _, _, _, _, _, unitCaster, _, _, spellId = UnitAura(unitid, i)
		if spellId == 19746 or spellID == 48942 or spellID == 48947 or spellID == 48945 or spellID == 48943 or spellID == 54043 or spellID == 32223 then -- all Paladin Auras max Rank
			for i = 0, 1000 do
				if hpally[i] == GetUnitName(unitCaster) then -- Probably imp. Concentration Aura
					if modifier > 0.7 then
						modifier = 0.7
					end
					break
				elseif hpally[i] == nil then -- The Paladin casting the Aura hasn't been detected as a Holy Paladin, so probably no imp. Concentration Aura
					break
				end
			end
		elseif spellId == 14743 then -- Matyrdom Rank1
			if modifier > 0.9 then
				modifier = 0.9
			end
		elseif spellId == 27828 then -- Matyrdom Rank2
			if modifier > 0.8 then
				modifier = 0.8
			end
		end
	end
	for i = 0, 1000 do
		if GetUnitName(unitid) == rshaman[i] then -- Interrupted Target is a Resto Shaman, so probably has 3/3 Focused Mind
			if modifier > 0.7 then
				modifier = 0.7
			end
		end
	end
	return modifier
end

function lockout.checkfailedinterrupts(guid, school)
	
	local j = -1
	
	if guid == tarcaster and school == tar1school then -- A cast has been recognized, that should have been locked out. Interrupt either never happened, or school isn't locked out anymore.
		tarschool1free = true 
		texbordertar1:Hide()
		tarschool1:Hide()
		tar1school = nil
	end
	if guid == tarcaster and school == tar2school then -- A cast has been recognized, that should have been locked out. Interrupt either never happened, or school isn't locked out anymore.

		tarschool2free = true 
		texbordertar2:Hide()
		tarschool2:Hide()
		tar2school = nil
	end
	if guid == tarcaster and school == tar3school then -- A cast has been recognized, that should have been locked out. Interrupt either never happened, or school isn't locked out anymore.
		tarschool3free = true 
		texbordertar3:Hide()
		tarschool3:Hide()
		tar3school = nil
	end
	if guid == foccaster and school == foc1school then -- A cast has been recognized, that should have been locked out. Interrupt either never happened, or school isn't locked out anymore.
		focschool1free = true 
		texborderfoc1:Hide()
		focschool1:Hide()
		foc1school = nil
	end
	if guid == foccaster and school == foc2school then -- A cast has been recognized, that should have been locked out. Interrupt either never happened, or school isn't locked out anymore.
		focschool2free = true 
		texborderfoc2:Hide()
		focschool2:Hide()
		foc2school = nil
	end
	if guid == foccaster and school == foc3school then -- A cast has been recognized, that should have been locked out. Interrupt either never happened, or school isn't locked out anymore.
		focschool3free = true 
		texborderfoc3:Hide()
		focschool3:Hide()
		foc3school = nil
	end
	
	for i=0, 1000 do
		if guidlist[i] == guid then
			j = i
			break
		end
	end
	
	if j > -1 then                          -- Remove any false list entries
		if school1list[j] == school then
			time1list[j] = nil
			dur1list[j] = nil
			icon1list[j] = nil
			school1list[j] = nil
		elseif school2list[j] == school then
			time2list[j] = nil
			dur2list[j] = nil
			icon2list[j] = nil
			school2list[j] = nil
		elseif school3list[j] == school then
			time3list[j] = nil
			dur3list[j] = nil
			icon3list[j] = nil
			school3list[j] = nil
		end
	end
end


---------- GUI Options ------------

function Lockout_Target()
	if Lockout_Target_Check:GetChecked() == 1 then
		lockout_showtarget = true

	else
		lockout_showtarget = false
	end
end

function Lockout_Focus()
	if Lockout_Focus_Check:GetChecked() == 1 then
		lockout_showfocus = true
	else
		lockout_showfocus = false
	end
end

function Lockout_Vertical()
	if Lockout_Vertical_Check:GetChecked() == 1 then
		if Lockout_Horizontal_Check:GetChecked() == 1 then
			Lockout_Horizontal_Check:SetChecked(false)
		end
		lockout_vertical = true
		lockout_horizontal = false
		lockouttar:SetWidth(32)
		lockoutfoc:SetWidth(32)
		lockouttar:SetHeight(96)
		lockoutfoc:SetHeight(96)
		tarschool1:SetPoint("CENTER", lockouttar, "CENTER", 0, -32)
		tarschool2:SetPoint("CENTER", lockouttar, "CENTER", 0, 0)
		tarschool3:SetPoint("CENTER", lockouttar, "CENTER", 0, 32)
		focschool1:SetPoint("CENTER", lockoutfoc, "CENTER", 0, -32)
		focschool2:SetPoint("CENTER", lockoutfoc, "CENTER", 0, 0)
		focschool3:SetPoint("CENTER", lockoutfoc, "CENTER", 0, 32)
	else
		if Lockout_Horizontal_Check:GetChecked() == nil then
			Lockout_Horizontal_Check:SetChecked(true)
		end
		lockout_vertical = false
		lockout_horizontal = true
	end
end

function Lockout_Horizontal()
	if Lockout_Horizontal_Check:GetChecked() == 1 then
		if Lockout_Vertical_Check:GetChecked() == 1 then
			Lockout_Vertical_Check:SetChecked(false)
		end
		lockout_horizontal = true
		lockout_vertical = false
		lockouttar:SetWidth(96)
		lockoutfoc:SetWidth(96)
		lockouttar:SetHeight(32)
		lockoutfoc:SetHeight(32)
		tarschool1:SetPoint("CENTER", lockouttar, "CENTER", -32, 0)
		tarschool2:SetPoint("CENTER", lockouttar, "CENTER", 0, 0)
		tarschool3:SetPoint("CENTER", lockouttar, "CENTER", 32, 0)
		focschool1:SetPoint("CENTER", lockoutfoc, "CENTER", -32, 0)
		focschool2:SetPoint("CENTER", lockoutfoc, "CENTER", 0, 0)
		focschool3:SetPoint("CENTER", lockoutfoc, "CENTER", 32, 0)
	else
		if Lockout_Vertical_Check:GetChecked() == nil then
			Lockout_Vertical_Check:SetChecked(true)
		end
		lockout_horizontal = false
		lockout_vertical = true
	end
end

function Lockout_UpdateScale()
	lockout_scale = Lockout_Scale_Slider:GetValue()/100
	lockouttar:SetScale(lockout_scale)
	lockoutfoc:SetScale(lockout_scale)
end

function Lockout_Close()
	Lockout_Frame:Hide()
	texdummytar:Hide()
	texdummyfoc:Hide()
	dummytexttar:Hide()
	dummytextfoc:Hide()
	lockouttar:EnableMouse(false)
	lockouttar:SetMovable(false)
	lockoutfoc:EnableMouse(false)
	lockoutfoc:SetMovable(false)
end

SlashCmdList["LO"] = function(param) 
	Lockout_Frame:Show()
	texdummytar:Show()
	texdummyfoc:Show()
	dummytexttar:Show()
	dummytextfoc:Show()
	lockouttar:EnableMouse(true)
	lockouttar:SetMovable(true)
	lockoutfoc:EnableMouse(true)
	lockoutfoc:SetMovable(true)
end

lockouttar:SetScript("OnMouseDown", function (self, button)
lockouttar:StartMoving()end)
lockouttar:SetScript("OnMouseUp", function (self, button)
lockouttar:StopMovingOrSizing()
lockout_targetxoffset = lockouttar:GetLeft()
lockout_targetyoffset = lockouttar:GetBottom()
end)

lockoutfoc:SetScript("OnMouseDown", function (self, button)
lockoutfoc:StartMoving()end)
lockoutfoc:SetScript("OnMouseUp", function (self, button)
lockoutfoc:StopMovingOrSizing()
lockout_focusxoffset = lockoutfoc:GetLeft()
lockout_focusyoffset = lockoutfoc:GetBottom()
end)



