--------------------------------------------------
-- 					Init
--------------------------------------------------
defaultFont = "fonts/fontkingthingsserifique.mvec";
defaultgameplayfont = "fonts/fontdesignosauritalic.mvec";

DefaultColor = Color(50,3,25,255);
DefaultShadowColor = Color(226,212,187,255);
BlackColor = Color(0,0,0,255);
RedColor = Color(255,0,0,255);
BlueColor = Color(77,115,178,255);
DarkBlueColor = Color(40,60,120,255);
WhiteColor = Color(255,255,255,255);
GlobalBorderColor = Color(1,69,1,255);
GlobalButtonColor = Color(245,255,216,255);

kIllegalNameChars = "!@#$%^&*()><\\\"\'[]{}|?/+=~`.,;:-";
kDefaultButtonSound = "audio/sfx/buttonclick.ogg";
kMenuButtonFontSize = 22;
kDefaultFontSize = 30;
kDefaultButtonFontSize = 34;
kDefaultButtonFontXOffset = 13;
kDefaultButtonFontYOffset = 2;
kDefaultButtonFontWidth = 200;
kDefaultButtonFontHeight = 50;
kDefaultButtonFontShadowSize = 1;

--------------------------------------------------
-- 					Styles
--------------------------------------------------
-- Graphics
StandardButtonGraphics = 
{
	"buttons/buttonup",
	"buttons/buttondown",
	"buttons/buttonrollover"
};

PauseButtonGraphics = 
{
	"buttons/gameplay/pause/buttonup",
	"buttons/gameplay/pause/buttondown",
	"buttons/gameplay/pause/buttonrollover"
};

SkipButtonGraphics = 
{
	"buttons/gameplay/skip/buttonup",
	"buttons/gameplay/skip/buttondown",
	"buttons/gameplay/skip/buttonrollover"
};

ChangePlayerButtonGraphics =
{
	"buttons/changeplayer/buttonup",
	"buttons/changeplayer/buttondown",
	"buttons/changeplayer/buttonrollover"
}

StandardButtonMetaGraphics = 
{
	"buttons/metamap/buttonmetamapup",
	"buttons/metamap/buttonmetamapdown",
	"buttons/metamap/buttonmetamaprollover"
};

StandardButtonMetaInactiveGraphics = 
{
	"buttons/metamap/buttonmetamapinactive",
	"buttons/metamap/buttonmetamapinactive",
	"buttons/metamap/buttonmetamapinactive"
};

LongButtonGraphics = 
{
	"buttons/buttonlongup.png",
	"buttons/buttonlongdown.png",
	"buttons/buttonlongover.png"
};

CheckboxButtonGraphics = 
{
	"buttons/buttoncheckup",
	"buttons/buttoncheckdown",
	"buttons/buttoncheckover",
	"buttons/buttoncheckdownover"
};

MetamapArrowButtonGraphics =
{
	"buttons/buttonarrowdown.png",
	"buttons/buttonarrowup.png",
	"buttons/buttonarrowover.png",
}

-- Fonts
StandardButtonFont = 
{
  defaultFont,
  kDefaultButtonFontSize,
  DefaultColor
};

DialogTitleFont = 
{
  defaultFont,
  20,
  DefaultColor
};

DialogTitleIntroFont = 
{
  defaultFont,
  20,
  DefaultColor
};

ErrorTitleFont = 
{
  defaultFont,
  20,
  RedColor
};

DialogBodyFont = 
{
  defaultFont,
  kDefaultFontSize,
  DefaultColor
};

DefaultStyle = 
{
	font = DialogBodyFont
};

DescMedalsTextStyle =
{
	font = 
	{
		defaultgameplayfont,
		20,
		DefaultColor
	}
};

DescMedalsTitleTextStyle =
{
	font = 
	{
		defaultgameplayfont,
		30,
		DefaultColor
	}
};


DialogTitleText = 
{
	parent = DefaultStyle,
	x = 16,
	y = 12,
	w = kMax,
	h = 30,
	font = DialogTitleFont,
	flags = kVAlignCenter + kHAlignLeft,
};

DialogBodyText = 
{
	parent = DefaultStyle,
	x = 4,
	y = 46,
	w = kMax - 4,
	h = kMax - 60,
	font =  
		{
			defaultgameplayfont,
			35,
			DefaultColor
		},
	flags = kVAlignCenter + kHAlignCenter,
};

SliderStyle =
{
	parent = DefaultStyle,
	railtop = "buttons/slider/buttonsliderrailtop",
	railtopback = "buttons/slider/buttonsliderrailbacktop",
	railmid = "buttons/slider/buttonsliderrailmid",
	railmidback = "buttons/slider/buttonsliderrailbackmid",
	railbot = "buttons/slider/buttonsliderrailbot",
	railbotback = "buttons/slider/buttonsliderrailbackbot",
	sliderimage = "buttons/slider/buttonsliderknobdown",
	sliderrollimage = "buttons/slider/buttonsliderknobrollover",
	slider = "buttons/slider/slider",
	yoffset = 3, -- scoot the slider down by one pixel to center it.
};

SliderFont =
{
  defaultFont,
  30,
  DefaultColor
};

ButtonStyle = 
{
	parent = DefaultStyle,
	font = StandardButtonFont,
	sound = kDefaultButtonSound,
	type = kPush,
	graphics = StandardButtonGraphics,
};

PauseButtonStyle = 
{
	parent = DefaultStyle,
	font = StandardButtonFont,
	sound = kDefaultButtonSound,
	type = kPush,
	graphics = PauseButtonGraphics,
};

SkipButtonStyle = 
{
	parent = DefaultStyle,
	font = StandardButtonFont,
	sound = kDefaultButtonSound,
	type = kPush,
	graphics = SkipButtonGraphics,
};

ChangePlayerButtonStyle = 
{
	parent = DefaultStyle,
	font = StandardButtonFont,
	sound = kDefaultButtonSound,
	type = kPush,
	graphics = ChangePlayerButtonGraphics,
};

ButtonMetaStyle = 
{
	parent = DefaultStyle,
	font = 
		{
			defaultFont,
			70,
			DefaultColor
		};
	sound = kDefaultButtonSound,
	type = kPush,
	graphics = StandardButtonMetaGraphics,
};

CheckboxButtonStyle = 
{
	parent = DefaultStyle,
	font = 
		{
			defaultFont,
			30,
			DefaultColor
		},
	type = kToggle,
	sound = kDefaultButtonSound,
	graphics = CheckboxButtonGraphics,
};

SetDefaultStyle(DefaultStyle);

--------------------------------------
--				Functions
--------------------------------------
-- Create a TButton with a label
--
-- We're overriding the default "Button" here with
-- some custom behavior. Specifically, we're layering
-- a Text{} object on top of every button, and it's placed
-- on all layers (up, down, roll-over).
function Button( button )
	return function()
		-- Grab the label from the button or current style
		if (GetTag(button,"label")) 
		then
			-- Look for text dimensions
			local tx = GetTag(button,"tx");
			local ty = GetTag(button,"ty");
			local tw = GetTag(button,"tw");
			local th = GetTag(button,"th");

			-- Set defaults
			if (not tx) 
			then
				tx = 0;
			end
			if (not ty) 
			then
				ty = 0;
			end
			if (not tw) 
			then
				tw = kMax ;
			end
			if (not th) 
			then
				th = kMax ;
			end

-- 4.0.15 -- Begin new hotkey support
			-- Get the label
			local label = GetTag(button,"label");
			local hotkey = GetTag(button,"hotkey");

			label,labelkey = StripKeyFromLabel( GetString(label) );

			-- If no hotkey was specified explicitly, then
			-- use the translated key.
			if not hotkey 
			then
				-- change our local
				hotkey = labelkey
			end;

			RegisterHotkey( hotkey, GetTag(button,"name") );
-- 4.0.15 -- End new hotkey support (more below!)

			local defflags = kPushButtonAlignment ;
			if (GetTag(button,"type") == kToggle) 
			then
				defflags = kToggleButtonAlignment ;
			elseif (GetTag(button,"type") == kRadio) 
			then
				defflags = kRadioButtonAlignment ;
			end

			local textfactory = GetTag(button,"textfactory");
			if (not textfactory) 
			then
				textfactory = "_text"
			end
			table.insert(button,
				SelectLayer( kAllLayers )
			);
			-- Grow our window to encompass any children we've added
			-- already.
			table.insert(button,
				FitToChildren()
			);

			table.insert(button,
				AppendStyle
				{ 
					font = button.font; 
					flags = button.flags; 
				}
			);

			table.insert(button,
				Text
				{
-- 4.0.15 -- Begin new hotkey support
					label = "#"..TagHotKey( hotkey, label ),
-- 4.0.15 -- End new hotkey support
					x = tx, 
					y = ty, 
					w = tw, 
					h = th,
					name = 'label',
					defflags = defflags
				}
			)
		end

		-- Type name of window to create; in our case, "Button".
		-- If you want to create a derived button type, you can do that here.
		button.typename = 'Button';
		DoWindow( button )
	end
end

-- Create several bits of text that combine to make a blended shadow
function ColorShadowText( x,y,size, label, color )

	return Group(
		{
		-- Add these traits to the current style
			AppendStyle
			{ 
				x = x, 
				y = y, 
				w = kMax, 
				h = kMax, 
				label = label 
			},
			AppendStyle
			{ 
				font = 
				{ 
					defaultFont, 
					size, 
					Color(0,0,0,40)
				} 
			},
			Text
			{ 
				y = y + 4 
			},
			AppendStyle
			{ 
				font = 
				{ 
					defaultFont, 
					size, 
					Color(0,0,0,255)
				} 
			},
			Text
			{ 
				y = y + 2 
			},
			AppendStyle
			{ 
				font = 
				{ 
					defaultFont, 
					size, 
					color 
				} 
			},
			Text
			{
			},
		}
	)
end

function MenuButtonLabel( label, color, colorHighlight )

	if not color 
	then
		color = Color(192,207,255,255);	-- the default colors
		colorHighlight = Color(255,255,255,255)
	end

	return Group
	{
		Bitmap
		{
			image = "elements/elementbar",
			x = 0,
			y = 0,
			scale = 1
		},
		SelectLayer(0),
		ColorShadowText(5,8,kMenuButtonFontSize,label,color),
		SelectLayer(1),
		ColorShadowText(5,8,kMenuButtonFontSize, label, colorHighlight ),
		SelectLayer(2),
		ColorShadowText(5,8,kMenuButtonFontSize, label, colorHighlight )
	}

end

-- Create a more limited-use button type here; the label is defined inline
-- with MenuButtonLabel above
function MenuButton( button )
	return function()
		-- Type name of window to create; in our case, "Button".
		-- If you want to create a derived button type, you can do that here.
		button.typename = 'Button';
		DoWindow( button )
	end
end

function showPopUp(aTitle,aText,dialog)
	DisplayDialog
	{
		dialog,
		title = "#"..aTitle,
		body =  "#"..aText,
	};
end;

-- ADDED BY KEFSENSEI
function ColorShadowColorText( w,h,x,y,size, label, color, shColor, name, shSize, flags, afont)
	
	return Group(
		{
			-- Add these traits to the current style
			--[[ FIRST APPEND NECESARY ATTRIBUTES: SIZE, ETC ]]
			AppendStyle{ x=x, y=y, w=w, h=h, label=label, flags=flags },
			--------------[[ MAKE SHADOWS ]]---------------------------
			AppendStyle{ font = { afont, size, shColor} },
			Text{ name = string.format('%s',name.."right"), x=x+shSize },
			Text{ name = string.format('%s',name.."left"), x=x-shSize },
			Text{ name = string.format('%s',name.."up"), y=y-shSize },
			Text{ name = string.format('%s',name.."down"), y=y+shSize },
			--------------[[ MAKE BASE TEXT ]]---------------------------
			AppendStyle{ name = name, font = { afont, size, color } } ,
			Text{},
		}
	)
end

function DefaultButtonLabel(aLabel)
	
	return ColorShadowColorText(kDefaultButtonFontWidth,
								kDefaultButtonFontHeight,
								kCenter + kDefaultButtonFontXOffset,
								kCenter + kDefaultButtonFontYOffset,
								kDefaultButtonFontSize,
								aLabel, 
								DefaultColor, 
								DefaultShadowColor,
								aLabel,
								kDefaultButtonFontShadowSize,
								kHAlignCenter + kVAlignCenter,
								defaultFont);
end

function ButtonLabel(aLabel, aSize, aXOffSet, aYOffSet)
	
	return ColorShadowColorText(kDefaultButtonFontWidth,
								kDefaultButtonFontHeight,
								kCenter + aXOffSet,
								kCenter + aYOffSet,
								aSize,
								aLabel, 
								DefaultColor, 
								DefaultShadowColor,
								aLabel,
								kDefaultButtonFontShadowSize,
								kHAlignCenter + kVAlignCenter,
								defaultFont);
end


function Unit(value)
	return value*1.28;
end

function createSound()

	t = { SetStyle(DefaultStyle), BeginGroup()	};
	
	btnSoundOn = 
		SoundButton
		{
			name = "btnSoundOn",
			x = kMax - 50,
			y = 0,
			typeButton = "on",
			type = kPush,
			graphics = 
				{
					"buttons/sound/on/active",
					"buttons/sound/on/pushdown",
					"buttons/sound/on/rollover"
				},
		};

	btnSoundOff = 
		SoundButton
		{
			name = "btnSoundOff",
			x = kMax - 50,
			y = 0,
			typeButton = "off",
			type = kPush,
			graphics = 
				{
					"buttons/sound/off/active",
					"buttons/sound/off/pushdown",
					"buttons/sound/off/rollover"
				},
		};
		
	table.insert(t,btnSoundOn);
	table.insert(t,btnSoundOff);
	
	return Group(t);
end

function createSoundNext(aX, aY)

	t = { SetStyle(DefaultStyle), BeginGroup()	};
	
	btnSoundOn = 
		SoundButton
		{
			name = "btnSoundOn",
			x = aX,
			y = aY,
			typeButton = "on",
			type = kPush,
			graphics = 
				{
					"buttons/sound/on/active",
					"buttons/sound/on/pushdown",
					"buttons/sound/on/rollover"
				},
		};

	btnSoundOff = 
		SoundButton
		{
			name = "btnSoundOff",
			x = aX,
			y = aY,
			typeButton = "off",
			type = kPush,
			graphics = 
				{
					"buttons/sound/off/active",
					"buttons/sound/off/pushdown",
					"buttons/sound/off/rollover"
				},
		};
		
	table.insert(t,btnSoundOn);
	table.insert(t,btnSoundOff);
	
	return Group(t);
end