local _G = getfenv(0)

function Skinner:MerchantFrames()
	if not self.db.profile.MerchantFrames or self.initialized.MerchantFrames then return end
	self.initialized.MerchantFrames = true

    if self.db.profile.TexturedTab then
        -- hook this to update tabs
    	self:SecureHook("MerchantFrame_Update", function()
--  		self:Debug("MerchantFrame_Update: [%s]", PanelTemplates_GetSelectedTab(MerchantFrame))
    		for i = 1, MerchantFrame.numTabs do
    			local tabName = _G["MerchantFrameTab"..i]
    			if i == MerchantFrame.selectedTab then 
       				self:setActiveTab(tabName) 
    			else
       				self:setInactiveTab(tabName) 
    			end
    		end
    		end)
    end
    
	self:keepRegions(MerchantFrame, {6, 7, 8}) -- N.B. regions 6-8 are text

	MerchantFrame:SetWidth(MerchantFrame:GetWidth() * FxMult)
	MerchantFrame:SetHeight(MerchantFrame:GetHeight() * FyMult)

	self:moveObject(MerchantNameText, nil, nil, "+", 6)
	self:moveObject(MerchantFrameCloseButton, "+", 28, "+", 8)

	self:moveObject(MerchantItem1, "-", 6, "+", 30)

	self:moveObject(MerchantPageText, "+", 12, "-", 59)
	self:moveObject(MerchantPrevPageButton, "-", 5, "-", 60)
	self:moveObject(MerchantNextPageButton, "-", 5, "-", 60)

	self:moveObject(MerchantRepairText, nil, nil, "-", 58)
	self:moveObject(MerchantRepairAllButton, nil, nil, "-", 58)
	self:moveObject(MerchantBuyBackItem, nil, nil, "-", 5)
	self:moveObject(MerchantMoneyFrame, "+", 30, "-", 58)


	for i = 1, MerchantFrame.numTabs do
		local tabName = _G["MerchantFrameTab"..i]
		self:keepRegions(tabName, {7, 8}) -- N.B. region 7 is text, 8 is highlight
        if self.db.profile.TexturedTab then self:applySkin(tabName, nil, 0)
        else self:applySkin( _G["MerchantFrameTab"..i]) end 		
        if i == 1 then
			self:moveObject(tabName, nil, nil, "-", 55)
			if self.db.profile.TexturedTab then self:setActiveTab(tabName) end 
		else
			self:moveObject(tabName, "+", 12, nil, nil)
			if self.db.profile.TexturedTab then self:setInactiveTab(tabName) end
		end
	end

	self:applySkin( MerchantFrame)

end

function Skinner:GossipFrame()
	if not self.db.profile.GossipFrame or self.initialized.GossipFrame then return end
	self.initialized.GossipFrame = true
	
	self:removeRegions(GossipFrame)
	
	GossipFrame:SetWidth(GossipFrame:GetWidth() * FxMult)
	GossipFrame:SetHeight(GossipFrame:GetHeight() * FyMult)
	
	self:moveObject(GossipFrameNpcNameText, nil, nil, "+", 15)
	self:moveObject(GossipFrameCloseButton, "+", 24, "+", 12)
	self:removeRegions(GossipFrameGreetingPanel)
	self:moveObject(GossipFrameGreetingGoodbyeButton, "+", 28, "-", 64)

	GossipGreetingText:SetTextColor(HTr, HTg, HTb)

	for i = 1, NUMGOSSIPBUTTONS do
		_G["GossipTitleButton"..i]:SetTextColor(BTr, BTg, BTb)
	end 

	self:moveObject(GossipGreetingScrollFrame, "-", 12, "+", 30)
	self:skinScrollBar(GossipGreetingScrollFrame)
	
	self:applySkin(GossipFrame)

end

function Skinner:ClassTrainer()
	if not self.db.profile.ClassTrainer or self.initialized.ClassTrainer then return end
	self.initialized.ClassTrainer = true

	self:keepRegions(ClassTrainerFrame, {6, 7}) -- N.B. 6 & 7 are text regions
	
	ClassTrainerFrame:SetWidth(ClassTrainerFrame:GetWidth()* FxMult)
	ClassTrainerFrame:SetHeight(ClassTrainerFrame:GetHeight() * FyMult)

	self:moveObject(ClassTrainerNameText, nil, nil, "+", 6)
	self:moveObject(ClassTrainerGreetingText, "-", 35, nil, nil)
	self:moveObject(ClassTrainerFrameCloseButton, "+", 28, "+", 8)

	self:removeRegions(ClassTrainerExpandButtonFrame)
	self:moveObject(ClassTrainerExpandButtonFrame, nil, nil, "+", 10)

	self:keepRegions(ClassTrainerFrameFilterDropDown, {4}) -- N.B. 4 is the text region
	self:moveObject(ClassTrainerFrameFilterDropDown, nil, nil, "+", 10)
	self:moveObject(ClassTrainerSkill1, nil, nil, "+", 10)
	
	self:removeRegions(ClassTrainerListScrollFrame)
	self:moveObject(ClassTrainerListScrollFrame, "+", 35, "+", 10)
	self:skinScrollBar(ClassTrainerListScrollFrame)

	self:removeRegions(ClassTrainerDetailScrollFrame)
	self:skinScrollBar(ClassTrainerDetailScrollFrame)

	self:moveObject(ClassTrainerMoneyFrame, nil, nil, "-", 74)
	self:moveObject(ClassTrainerTrainButton, "-", 10, "+", 10)
	self:moveObject(ClassTrainerCancelButton, "-", 10, "+", 10)
	
	self:applySkin(ClassTrainerFrame)

end

function Skinner:TaxiFrame()
	if not self.db.profile.TaxiFrame or self.initialized.TaxiFrame then return end
	self.initialized.TaxiFrame = true
	
	self:keepRegions(TaxiFrame, {6, 7}) -- N.B. region 6 is TaxiMerchant, 7 is the TaxiMap overlay

	TaxiFrame:SetWidth(TaxiFrame:GetWidth() * FxMult)
	TaxiFrame:SetHeight(TaxiFrame:GetHeight() * FyMult)

	self:moveObject(TaxiMerchant, nil, nil, "+", 6)
	self:moveObject(TaxiCloseButton, "+", 28, "+", 8)
	self:moveObject(TaxiMap, "+", 12, "+", 25)
	self:moveObject(TaxiRouteMap, "+", 12, "+", 25)
	
	self:applySkin(TaxiFrame)

end

function Skinner:QuestFrame()
	if not self.db.profile.QuestFrame or self.initialized.QuestFrame then return end
	self.initialized.QuestFrame = true

	-- hook OnShow methods to change text colour
	self:SecureHook("QuestFrameRewardPanel_OnShow")
	self:SecureHook("QuestFrameProgressPanel_OnShow")
	self:SecureHook("QuestFrameGreetingPanel_OnShow")
	self:SecureHook("QuestFrameDetailPanel_OnShow")

	self:removeRegions(QuestFrame)
	
	QuestFrame:SetWidth(QuestFrame:GetWidth() * FxMult)
	QuestFrame:SetHeight(QuestFrame:GetHeight() * FyMult)
	
	self:moveObject(QuestFrameNpcNameText, nil, nil, "+", 15)
	self:moveObject(QuestFrameCloseButton, "+", 24, "+", 12)

	-- QF Reward Panel
	self:removeRegions(QuestFrameRewardPanel)
	self:moveObject(QuestFrameCancelButton, "+", 28, "-", 64)
	self:moveObject(QuestFrameCompleteQuestButton, "-", 10, "-", 64)
	self:moveObject(QuestRewardScrollFrame, "-", 12, "+", 30)
	self:skinScrollBar(QuestRewardScrollFrame)
	
	-- QF Progress
	self:removeRegions(QuestFrameProgressPanel)
	self:moveObject(QuestFrameGoodbyeButton, "+", 28, "-", 64)
	self:moveObject(QuestFrameCompleteButton, "-", 10, "-", 64)
	self:moveObject(QuestProgressScrollFrame, "-", 12, "+", 30)
	self:skinScrollBar(QuestProgressScrollFrame)
	
	-- QF Detail Panel
	self:removeRegions(QuestFrameDetailPanel)
	self:moveObject(QuestFrameDeclineButton, "+", 28, "-", 64)
	self:moveObject(QuestFrameAcceptButton, "-", 10, "-", 64)
	self:moveObject(QuestDetailScrollFrame, "-", 12, "+", 30)
	self:skinScrollBar(QuestDetailScrollFrame)

-- QF Greeting Panel	
	self:removeRegions(QuestFrameGreetingPanel)
	self:moveObject(QuestFrameGreetingGoodbyeButton, "+", 28, "-", 64)
	self:moveObject(QuestGreetingScrollFrame, "-", 12, "+", 30)
	self:skinScrollBar(QuestGreetingScrollFrame)

	self:applySkin( QuestFrame)

end

function Skinner:QuestFrameRewardPanel_OnShow()

	QuestRewardTitleText:SetTextColor(HTr, HTg, HTb)
	QuestRewardText:SetTextColor(HTr, HTg, HTb)
	QuestRewardRewardTitleText:SetTextColor(HTr, HTg, HTb)
	QuestRewardItemChooseText:SetTextColor(HTr, HTg, HTb)
	QuestRewardItemReceiveText:SetTextColor(HTr, HTg, HTb)
	QuestRewardSpellLearnText:SetTextColor(HTr, HTg, HTb)

	for i = 1, MAX_NUM_ITEMS do
		_G["QuestRewardItem"..i]:SetTextColor(BTr, BTg, BTb)
	end 

end

function Skinner:QuestFrameProgressPanel_OnShow()

	QuestProgressTitleText:SetTextColor(HTr, HTg, HTb)
	QuestProgressText:SetTextColor(HTr, HTg, HTb)
	QuestProgressRequiredItemsText:SetTextColor(HTr, HTg, HTb)
	QuestProgressRequiredMoneyText:SetTextColor(HTr, HTg, HTb)

	for i = 1, MAX_REQUIRED_ITEMS do
		_G["QuestProgressItem"..i]:SetTextColor(BTr, BTg, BTb)
	end 

end

function Skinner:QuestFrameGreetingPanel_OnShow()

	GreetingText:SetTextColor(HTr, HTg, HTb)
	CurrentQuestsText:SetTextColor(HTr, HTg, HTb)
	AvailableQuestsText:SetTextColor(HTr, HTg, HTb)

	for i = 1, MAX_NUM_QUESTS do
		_G["QuestTitleButton"..i]:SetTextColor(BTr, BTg, BTb)
	end	

end

function Skinner:QuestFrameDetailPanel_OnShow()

	QuestTitleText:SetTextColor(HTr, HTg, HTb)
	QuestDescription:SetTextColor(HTr, HTg, HTb)
	QuestDetailObjectiveTitleText:SetTextColor(HTr, HTg, HTb)
	QuestObjectiveText:SetTextColor(HTr, HTg, HTb)

	QuestDetailRewardTitleText:SetTextColor(HTr, HTg, HTb)
	QuestDetailItemChooseText:SetTextColor(HTr, HTg, HTb)
	QuestDetailItemReceiveText:SetTextColor(HTr, HTg, HTb)
	QuestDetailSpellLearnText:SetTextColor(HTr, HTg, HTb)

	for i = 1, MAX_NUM_ITEMS do
		_G["QuestDetailItem"..i]:SetTextColor(BTr, BTg, BTb)
	end 

end

function Skinner:Battlefields()
	if not self.db.profile.Battlefields or self.initialized.Battlefields then return end
	self.initialized.Battlefields = true

	self:keepRegions(BattlefieldFrame, {6, 7, 8}) -- N.B. regions 6, 7 & 8 are text
	
	BattlefieldFrame:SetWidth(BattlefieldFrame:GetWidth() * FxMult)
	BattlefieldFrame:SetHeight(BattlefieldFrame:GetHeight() * FyMult)
	
	self:moveObject(BattlefieldFrameFrameLabel, nil, nil, "+", 6)
	self:moveObject(BattlefieldFrameCloseButton, "+", 26, "+", 6)
	self:moveObject(BattlefieldFrameCancelButton, "-", 10, "+", 10)

	local xOfs, yOfs = 12, 20
	self:moveObject(BattlefieldFrameNameHeader, "-", xOfs, "+", yOfs)
	self:moveObject(BattlefieldZone1, "-", xOfs, "+", yOfs)
	self:moveObject(BattlefieldFrameZoneDescription, "-", xOfs, "+", yOfs)
	self:removeRegions(BattlefieldListScrollFrame)
	self:moveObject(BattlefieldListScrollFrame, "+", 32, "+", yOfs)
	self:skinScrollBar(BattlefieldListScrollFrame)

	BattlefieldFrameZoneDescription:SetTextColor(BTr, BTg, BTb)

	self:applySkin(BattlefieldFrame)

end

function Skinner:ArenaFrame()
	if not self.db.profile.ArenaFrame or self.initialized.ArenaFrame then return end
	self.initialized.ArenaFrame = true

	self:keepRegions(ArenaFrame, {6, 7, 8, 10}) -- N.B. regions 6-8 & 10 are text
	
	ArenaFrame:SetWidth(ArenaFrame:GetWidth() * FxMult)
	ArenaFrame:SetHeight(ArenaFrame:GetHeight() * FyMult)
	
	self:moveObject(ArenaFrameCloseButton, "+", 26, "+", 6)
	self:moveObject(ArenaFrameCancelButton, "-", 10, "+", 10)
	self:moveObject(ArenaFrameNameHeader2, "+", 40, "-", 40)

	ArenaFrameZoneDescription:SetTextColor(BTr, BTg, BTb)

	self:applySkin(ArenaFrame)

end

function Skinner:ArenaRegistrar()
	if not self.db.profile.ArenaRegistrar or self.initialized.ArenaRegistrar then return end
	self.initialized.ArenaRegistrar = true

-->>-- Arena Registrar Frame
	self:removeRegions(ArenaRegistrarFrame, {1, 2, 3, 4, 5}) -- N.B. other regions are required

	ArenaRegistrarFrame:SetWidth(ArenaRegistrarFrame:GetWidth() * FxMult)
	ArenaRegistrarFrame:SetHeight(ArenaRegistrarFrame:GetHeight() * FyMult)

	self:moveObject(ArenaRegistrarFrameNpcNameText, nil, nil, "+", 15)
	self:moveObject(ArenaRegistrarFrameCloseButton, "+", 24, "+", 12)
	self:removeRegions(ArenaRegistrarGreetingFrame, {3}) -- N.B. regions 1 & 2 are text
	AvailableServicesText:SetTextColor(HTr, HTg, HTb)
	ArenaRegistrarButton1:SetTextColor(BTr, BTg, BTb)	
	ArenaRegistrarButton2:SetTextColor(BTr, BTg, BTb)	
	ArenaRegistrarButton3:SetTextColor(BTr, BTg, BTb)	
	RegistrationText:SetTextColor(HTr, HTg, HTb)
	ArenaRegistrarButton4:SetTextColor(BTr, BTg, BTb)	
	ArenaRegistrarButton5:SetTextColor(BTr, BTg, BTb)	
	ArenaRegistrarButton6:SetTextColor(BTr, BTg, BTb)	
	self:moveObject(ArenaRegistrarFrameGoodbyeButton, "+", 28, "-", 64)
	ArenaRegistrarPurchaseText:SetTextColor(BTr, BTg, BTb)
	self:moveObject(ArenaRegistrarFrameCancelButton, "+", 30, "-", 64)
	self:moveObject(ArenaRegistrarFramePurchaseButton, "-", 10, "-", 64)
	self:skinEditBox(ArenaRegistrarFrameEditBox)
	self:applySkin(ArenaRegistrarFrame)
	
-->>-- PVP Banner Frame	
	self:keepRegions(PVPBannerFrame, {6, 17, 18, 19, 20, 21, 22}) -- N.B. region 6 is the background, 17 - 20 are the emblem, 21, 22 are the text

	PVPBannerFrame:SetWidth(PVPBannerFrame:GetWidth() * FxMult)
	PVPBannerFrame:SetHeight(PVPBannerFrame:GetHeight() * FyMult)

	self:moveObject(PVPBannerFrameCloseButton, "+", 28, "+", 8)
	self:moveObject(PVPBannerFrameEmblemTopRight, "-", 20, nil, nil)
	self:moveObject(PVPBannerFrameNameText, nil, nil, "-", 28)
	self:moveObject(PVPBannerFrameBackground, "-", 8, "+", 10)
	self:moveObject(PVPBannerFrameCostFrame, "-", 5, "+", 10)
	self:removeRegions(PVPBannerFrameCustomizationFrame)
	self:moveObject(PVPBannerFrameCustomization1, "+", 35, "-", 73)
	self:keepRegions(PVPBannerFrameCustomization1, {4,5}) -- N.B. regions 4 & 5 are text
	self:keepRegions(PVPBannerFrameCustomization2, {4,5}) -- N.B. regions 4 & 5 are text
	self:moveObject(PVPBannerFrameMoneyFrame, "-", 30, "-", 75)
	self:moveObject(PVPBannerFrameAcceptButton, "-", 10, "+", 10)
	--	N.B. there are two PVPBannerFrameCancelButton entries
	self:moveObject(self:getChild(PVPBannerFrame, 4), "-", 10, "+", 10)
	
	self:applySkin(PVPBannerFrame)

end

function Skinner:GuildRegistrar()
	if not self.db.profile.GuildRegistrar or self.initialized.GuildRegistrar then return end
	self.initialized.GuildRegistrar = true

	self:removeRegions(GuildRegistrarFrame, {1, 2, 3, 4, 5}) -- N.B. other regions are required

	GuildRegistrarFrame:SetWidth(GuildRegistrarFrame:GetWidth() * FxMult)
	GuildRegistrarFrame:SetHeight(GuildRegistrarFrame:GetHeight() * FyMult)

	self:moveObject(GuildRegistrarFrameNpcNameText, nil, nil, "+", 15)
	self:moveObject(GuildRegistrarFrameCloseButton, "+", 24, "+", 12)
	self:removeRegions(GuildRegistrarGreetingFrame, {2}) -- N.B. region 1 is text

	AvailableServicesText:SetTextColor(HTr, HTg, HTb)

	self:moveObject(GuildRegistrarFrameGoodbyeButton, "+", 28, "-", 64)

	GuildRegistrarButton1:SetTextColor(BTr, BTg, BTb)	
	GuildRegistrarButton2:SetTextColor(BTr, BTg, BTb)	
	GuildRegistrarPurchaseText:SetTextColor(BTr, BTg, BTb)

	self:moveObject(GuildRegistrarFrameCancelButton, "+", 30, "-", 64)
	self:moveObject(GuildRegistrarFramePurchaseButton, "-", 10, "-", 64)

	self:skinEditBox(GuildRegistrarFrameEditBox)

	self:applySkin(GuildRegistrarFrame)

end

function Skinner:Petition()
	if not self.db.profile.Petition or self.initialized.Petition then return end
	self.initialized.Petition = true
	
	self:removeRegions(PetitionFrame, {1, 2, 3, 4, 5}) -- N.B. other regions are required

	PetitionFrame:SetWidth(PetitionFrame:GetWidth() * FxMult)
	PetitionFrame:SetHeight(PetitionFrame:GetHeight() * FyMult)

	self:moveObject(PetitionFrameNpcNameText, nil, nil, "+", 15)
	self:moveObject(PetitionFrameCloseButton, "+", 24, "+", 12)
	self:moveObject(PetitionFrameCancelButton, "+", 28, "-", 64)
	self:moveObject(PetitionFrameSignButton, "-", 15, "-", 64)
	self:moveObject(PetitionFrameRequestButton, "-", 15, "-", 64)

	PetitionFrameCharterTitle:SetTextColor(HTr, HTg, HTb)
	PetitionFrameCharterName:SetTextColor(BTr, BTg, BTb)
	PetitionFrameMasterTitle:SetTextColor(HTr, HTg, HTb)
	PetitionFrameMasterName:SetTextColor(BTr, BTg, BTb)
	PetitionFrameMemberTitle:SetTextColor(HTr, HTg, HTb)
	for i = 1, 9 do
		_G["PetitionFrameMemberName"..i]:SetTextColor(BTr, BTg, BTb)
	end
	PetitionFrameInstructions:SetTextColor(BTr, BTg, BTb)
	
	self:applySkin(PetitionFrame)

end

function Skinner:Tabard()
	if not self.db.profile.Tabard or self.initialized.Tabard then return end
	self.initialized.Tabard = true

	self:keepRegions(TabardFrame, {6, 17, 18, 19, 20, 21, 22}) -- N.B. region 6 is the background, 17 - 20 are the emblem, 21, 22 are the text

	TabardFrame:SetWidth(TabardFrame:GetWidth() * FxMult)
	TabardFrame:SetHeight(TabardFrame:GetHeight() * FyMult)

	self:moveObject(TabardFrameCloseButton, "+", 28, "+", 8)
	self:moveObject(TabardFrameEmblemTopRight, "-", 20, nil, nil)
	self:moveObject(TabardFrameNameText, nil, nil, "-", 28)
	self:moveObject(TabardFrameBackground, "-", 8, "+", 10)
	self:moveObject(TabardModel, nil, nil, "-", 60)

	TabardCharacterModelRotateLeftButton:Hide()
	TabardCharacterModelRotateRightButton:Hide()
	self:makeMFRotatable(TabardModel)
	
	self:moveObject(TabardFrameCostFrame, "-", 5, "+", 10)
	self:removeRegions(TabardFrameCustomizationFrame)
	self:moveObject(TabardFrameCustomization1, "+", 35, "-", 73)
	for i = 1, 5 do
		self:keepRegions(_G["TabardFrameCustomization"..i], {4,5}) -- N.B. regions 4 & 5 are text
	end
	self:moveObject(TabardFrameMoneyFrame, "-", 30, "-", 75)
	self:moveObject(TabardFrameAcceptButton, "-", 10, "+", 10)
	self:moveObject(TabardFrameCancelButton, "-", 10, "+", 10)
	
	self:applySkin(TabardFrame)

end
