-----------------------------------------------------------------------------
--[[
This script was made by Meiya Stormsinger of http://ownedcore.com/	  
This script may be edited and changed at will, but credits must be provided.
Thank you.
]]--						

-----------------------------------------------------------------------------
-- Addon declaration
local VIPChat = LibStub("AceAddon-3.0"):NewAddon("VIPChat", "AceConsole-3.0", "AceEvent-3.0", "AceTimer-3.0", "LibSink-2.0")
local W = LibStub("AceLocale-3.0"):GetLocale("VIPChat", false)
local LSM = LibStub("LibSharedMedia-3.0")
local LDB = LibStub("LibDataBroker-1.1", true)
local LDBIcon = LDB and LibStub("LibDBIcon-1.0", true)
VIPChat.version = GetAddOnMetadata("VIPChat", "Version")
VIPChat.versionstring = "VIPChat v"..GetAddOnMetadata("VIPChat", "Version")
_G["VIPChat"] = VIPChat



-----------------------------------------------------------------------------
-- Keybinding globals
BINDING_HEADER_VIPCHAT = VIPChat.versionstring
BINDING_NAME_VIPCHATTOGGLEFOCUS = "Focus VIPChat"

VIPChat_Frame = CreateFrame("FRAME", nil, UIParent)
VIPChat_Frame:SetMovable(true)
VIPChat_Frame:EnableMouse(true)
VIPChat_Frame:SetWidth(440)
VIPChat_Frame:SetHeight(30)
VIPChat_Frame:SetPoint("CENTER", DEFAULT_CHAT_FRAME, "BOTTOM", 0, -40)
VIPChat_Frame:SetBackdrop({bgFile = "Interface/Tooltips/UI-Tooltip-Background", edgeFile = "Interface/Tooltips/UI-Tooltip-Border", tile = true, tileSize = 16, edgeSize = 16, insets = { left = 4, right = 4, top = 4, bottom = 4 }})
VIPChat_Frame:SetBackdropColor(0,0,0,1)
VIPChat_Frame:Show()

function Chat_StopMoving()
	if VIPChat_Frame.isMoving then
		VIPChat_Frame:StopMovingOrSizing()
		VIPChat_Frame.isMoving = false
	end
end

function Chat_StartMoving()
	if arg1 == "RightButton" or "LeftButton" then
		VIPChat_Frame:StartMoving()
		VIPChat_Frame.isMoving = true
	end
end

function SendMessage()
	local text = VIPChat_Frame.editbx:GetText()
	local StrLe = strlen(text)
	if StrLe == 0 then
		VIPChat_Frame.editbx:ClearFocus()
	else
		SendChatMessage(".chat " ..text, "GUILD")
		VIPChat_Frame.editbx:SetText("")
		VIPChat_Frame.editbx:ClearFocus()
	end
end

function VIPChat:ToggleFocus()
		VIPChat_Frame.editbx:SetFocus()
end

function ClearChat()
	VIPChat_Frame.editbx:SetText("")
end

VIPChat_Frame.titletxt = VIPChat_Frame:CreateFontString(nil, "OVERLAY", "GameTooltipText")
VIPChat_Frame.titletxt:SetText("World Chat")
VIPChat_Frame.titletxt:SetPoint("CENTER", VIPChat_Frame, "LEFT", 50, 0)

VIPChat_Frame.editbx = CreateFrame("EDITBOX", nil, VIPChat_Frame, "InputBoxTemplate ")
VIPChat_Frame.editbx:SetWidth(280)
VIPChat_Frame.editbx:SetHeight(10)
VIPChat_Frame.editbx:SetPoint("CENTER", VIPChat_Frame, 20, 0)
VIPChat_Frame.editbx:SetAlpha(1)
VIPChat_Frame.editbx:Show()
VIPChat_Frame.editbx:SetAutoFocus(disable)

VIPChat_Frame.sendbtn = CreateFrame("BUTTON", nil, VIPChat_Frame, "UIPanelButtonTemplate ")
VIPChat_Frame.sendbtn:SetWidth(45)
VIPChat_Frame.sendbtn:SetHeight(20)
VIPChat_Frame.sendbtn:SetText("Send")
VIPChat_Frame.sendbtn:SetPoint("RIGHT", VIPChat_Frame, -10, 0)
VIPChat_Frame.sendbtn:SetAlpha(1)
VIPChat_Frame.sendbtn:Show()

VIPChat_Frame.editbx:SetScript("OnEnterPressed", SendMessage)
VIPChat_Frame.editbx:SetScript("OnEditFocusLost", ClearChat)
VIPChat_Frame.sendbtn:SetScript("OnClick", SendMessage)
VIPChat_Frame:SetScript("OnMouseDown", Chat_StartMoving)
VIPChat_Frame:SetScript("OnMouseUp", Chat_StopMoving)