UIPanelWindows["CosmosFeatureFrame"] = { area = "left", pushable = 3,	whileDead = 1 };

local COSMOSFEATUREFRAME_PAGESIZE = 14;

local CosmosFeatureFrame_Offset = 0;

function CosmosButton_OnHide()
	UpdateMicroButtons();
	PlaySound("igSpellBookClose");
end

function CosmosButtons_UpdateColor()
	local value = nil;
	for id = 1, 14 do
		value = CosmosMaster_Buttons[id+CosmosFeatureFrame_Offset];
		if ( value ) then
			local test_function = value[CSM_TESTFUNCTION];
			if (test_function ~= nil) then
				local icon = getglobal("CosmosFeaturesButton"..id);
				local iconTexture = getglobal("CosmosFeaturesButton"..id.."IconTexture");
				
				if (test_function() == false) then
					icon:Disable();
					iconTexture:SetVertexColor(1.00, 0.00, 0.00);
				else
					icon:Enable();
					iconTexture:SetVertexColor(1.00, 1.00, 1.00);
				end
			end
		end
	end
end

function CosmosButton_OnShow()
	UpdateMicroButtons();
	CosmosFeaturesTitleText:SetText(TEXT(COSMOS_FEATURES_TITLE));
	CreditText1:SetText(TEXT(HANULARI_CREDIT_TEXT1));
	CreditText2:SetText(TEXT(HANULARI_CREDIT_TEXT2));
	CreditText3:SetText(TEXT(HANULARI_CREDIT_TEXT3));
	CreditText4:SetText(TEXT(HANULARI_CREDIT_TEXT4));
	PlaySound("igSpellBookOpen");
	CosmosButtons_UpdateColor();
end

function CosmosButton_OnEnter()	
	local value = CosmosMaster_Buttons[this:GetID()+CosmosFeatureFrame_Offset];
	if (value) then
		GameTooltip:SetOwner(this, "ANCHOR_RIGHT");
		GameTooltip:SetText(value[CSM_LONGDESCRIPTION], 1.0, 1.0, 1.0);
		return;
	end
end

function CosmosButton_OnClick()	
	local value = CosmosMaster_Buttons[this:GetID()+CosmosFeatureFrame_Offset];
	if (value) then
		local f = value[CSM_CALLBACK];
		this:SetChecked(0);
		f();
		return;
	end
end

function CosmosButton_UpdateButton()
	for i = 1, 14, 1 do
		local icon = getglobal("CosmosFeaturesButton"..i);
		local iconTexture = getglobal("CosmosFeaturesButton"..i.."IconTexture");
		local iconName = getglobal("CosmosFeaturesButton"..i.."Name");
		local iconDescription = getglobal("CosmosFeaturesButton"..i.."OtherName");
		
		icon:Hide();
		iconTexture:Hide();
		iconName:Hide();
		iconDescription:Hide();
	end
	local id = 0;
	local value = 0;
	for i = 1, 14 do
		id = i + CosmosFeatureFrame_Offset;
		
		value = CosmosMaster_Buttons[id];
		if ( not value ) then
			break;
		end
		
		local icon = getglobal("CosmosFeaturesButton"..i);
		local iconTexture = getglobal("CosmosFeaturesButton"..i.."IconTexture");
		local iconName = getglobal("CosmosFeaturesButton"..i.."Name");
		local iconDescription = getglobal("CosmosFeaturesButton"..i.."OtherName");
		
		icon:Show();
		icon:Enable();
		iconTexture:Show();
		iconTexture:SetTexture(value[CSM_ICON]);
		iconName:Show();
		iconName:SetText(value[CSM_NAME]);
		iconDescription:Show();
		iconDescription:SetText(value[CSM_DESCRIPTION]);

	end
	CosmosFeatureFrame_UpdatePageArrows();
end

function CosmosFeatureFrame_UpdatePageArrows()
	local numValues = 0;
	if ( CosmosMaster_Buttons ) then
		numValues = table.getn(CosmosMaster_Buttons);
	end
	if ( CosmosFeatureFrame_Offset <= 0 ) then
		CosmosFeatureFramePrevPageButton:Disable();
	else
		CosmosFeatureFramePrevPageButton:Enable();
	end
	if ( ( CosmosFeatureFrame_Offset + COSMOSFEATUREFRAME_PAGESIZE ) >= numValues ) then
		CosmosFeatureFrameNextPageButton:Disable();
	else
		CosmosFeatureFrameNextPageButton:Enable();
	end
end

function CosmosFeatureFrame_PrevPageButton_OnClick()
	if ( ( CosmosFeatureFrame_Offset - COSMOSFEATUREFRAME_PAGESIZE ) >= 0 ) then
		CosmosFeatureFrame_Offset = CosmosFeatureFrame_Offset - COSMOSFEATUREFRAME_PAGESIZE;
	end
	CosmosButton_UpdateButton();
end

function CosmosFeatureFrame_NextPageButton_OnClick()
	local numValues = table.getn(CosmosMaster_Buttons);
	if ( ( CosmosFeatureFrame_Offset + COSMOSFEATUREFRAME_PAGESIZE ) < numValues ) then
		CosmosFeatureFrame_Offset = CosmosFeatureFrame_Offset + COSMOSFEATUREFRAME_PAGESIZE;
	end
	CosmosButton_UpdateButton();
end

function CosmosMinimapButton_OnLoad()
	this:RegisterEvent("VARIABLES_LOADED");
	CosmosMinimapButton:SetFrameLevel(MinimapZoomIn:GetFrameLevel());
end

function CosmosMinimapButton_OnEvent(event)
	if (event == "VARIABLES_LOADED") then
		if (CosmosMinimapButton_OffsetX) and (CosmosMinimapButton_OffsetY) then
			this:SetPoint("CENTER", "Minimap", "CENTER", CosmosMinimapButton_OffsetX, CosmosMinimapButton_OffsetY);
		end
	end
end

function CosmosMinimapButton_OnMouseDown()
	if (IsControlKeyDown()) then
		if ( arg1 == "RightButton" ) then
			--wait for reset
		else
			this.isMoving = true;
		end
	end
end

function CosmosMinimapButton_OnMouseUp()
	if (this.isMoving) then
		this.isMoving = false;
		CosmosMinimapButton_OffsetX = this.currentX;
		CosmosMinimapButton_OffsetY = this.currentY;
	elseif (MouseIsOver(CosmosMinimapButton)) then
		if (IsControlKeyDown()) and ( arg1 == "RightButton" ) then
			CosmosMinimapButton_Reset();
		else
			ToggleCosmosFeatureFrame();
		end
	end
end

function CosmosMinimapButton_OnHide()
	this.isMoving = false;
end

function CosmosMinimapButton_OnUpdate()
	if (this.isMoving) then
		local mouseX, mouseY = GetCursorPosition();
		local centerX, centerY = Minimap:GetCenter();
		local scale = Minimap:GetEffectiveScale();
		mouseX = mouseX / scale;
		mouseY = mouseY / scale;
		local radius = (Minimap:GetWidth()/2) + (this:GetWidth()/3);
		local x = math.abs(mouseX - centerX);
		local y = math.abs(mouseY - centerY);
		local xSign = 1;
		local ySign = 1;
		if not (mouseX >= centerX) then
			xSign = -1;
		end
		if not (mouseY >= centerY) then
			ySign = -1;
		end
		--Sea.io.print(xSign*x,", ",ySign*y);
		local angle = math.atan(x/y);
		x = math.sin(angle)*radius;
		y = math.cos(angle)*radius;
		this.currentX = xSign*x;
		this.currentY = ySign*y;
		this:SetPoint("CENTER", "Minimap", "CENTER", this.currentX, this.currentY);
	end
end

function CosmosMinimapButton_Reset()
	CosmosMinimapButton:ClearAllPoints();
	CosmosMinimapButton_OffsetX = -62;
	CosmosMinimapButton_OffsetY = -49;
	CosmosMinimapButton:SetPoint("CENTER", "Minimap", "CENTER", CosmosMinimapButton_OffsetX, CosmosMinimapButton_OffsetY);
end
