﻿--[[---------------------------------------------------------------------------

Copyright (c) 2008, 2009 by K. Scott Piel 
All Rights Reserved

E-mail: < kscottpiel@gmail.com >
Web:    < http://www.scottpiel.com >

This file is part of nUI.

	The copyright for all material provided within the nUI software package 
	("nUI") is held by Kenneth Scott Piel. Except as stated herein, none of 
	the material may be copied, reproduced, distributed, republished, 
	downloaded, displayed, posted or transmitted in any form or by any means, 
	including, but not limited to, electronic, mechanical, photocopying, 
	recording, or otherwise, without the prior written permission of 
	the copyright holder. Permission is granted to display, copy, distribute 
	and download the materials on this Site for personal, non-commercial use 
	only provided you do not modify the materials and that you retain all 
	copyright and other proprietary notices contained in the materials. You 
	also may not, without the copyright holder's permission, "mirror" any 
	material contained in nUI on any other server. This permission terminates 
	automatically if you breach any of these terms or conditions. Upon 
	termination, you will immediately destroy any downloaded and printed 
	materials. Any unauthorized use of any material contained in nUI may 
	violate copyright laws, trademark laws, the laws of privacy and publicity, 
	and communications regulations and statutes.
	
	nUI is packaged in four distributable versions known as "nUI Release",
	"nUI+", "nUI Development" and "nUI PTR Beta" -- Redistribution for these
	versions is governed by the following terms...
	
	1) Redistribution of the nUI Release (aka nUI Lite) version is permitted under 
	   the following terms... Permission is hereby	granted for unlimited free 
	   and open distribution of "nUI Release" / "nUI Lite" by anyone in any 
	   form and by any means provided the nUI Release distribution contents 
	   are not altered in any way, are distributed in full with all copyright 
	   statements and licensing terms included and intact and that any 
	   interface the end user is provided for the purpose of downloading nUI 
	   includes a plainly visible and functioning link to nUI's official web 
	   site at http://www.nUIaddon.com and a plainly visible notice that nUI 
	   accepts user donations with a working link to nUI's donation page at 
	   http://www.nUIaddon.com/donate.html
	   
	2) Permission is hereby granted for distribution of the "nUI+", "nUI+
	   Development" and "nUI+ PTR Beta" versions of nUI via the online download
	   service at http://www.wowinterface.com and the copyright holder's own
	   web site http://www.nuiaddon.com -- The end user is granted permission
	   to download any nUI package from these two web sites for their personal
	   use under the same terms and conditions as nUI Release but are prohibited
	   from sharing the contents of these packages via any means in any form
	   with anyone other than via direct transfer with immediate friends and
	   family members. Distribution of nUI+, nUI+ Development or nUI+ PTR Beta
	   via any other means by any other entity in any other form is strictly 
	   prohibited without the copyright holder's express written permission
	   explicitly granting such distribution rights specifically to that entity.
	   
	3) Deep-linking and leeching of nUI distributions is strictly prohibited. 
	   Any individual or entity who wishes to offer downloads of nUI 
	   distributions must either host the legal and unmodified distribution 
	   on their own servers to be distributed at their own expense using their 
	   own bandwidth or they must link the user back to the official download 
	   page on the third party provider's servers from which the user can 
	   initiate the download. Use of any download link or mechanism which 
	   initiates a download of any nUI distribution from a third party 
	   distribtion site that bypasses the official content and download pages 
	   or advertisements of that third party site is strictly prohibited
       without the express written consent of that site.
       
    See the included files "nUI_RELEASE_LICENSE.txt" and "nUI_PLUS_LICENSE.txt"
    for the complete terms of nUI's licensing terms.
	   
    nUI is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    enclosed license for more details.
	
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

--]]---------------------------------------------------------------------------

if not nUI_Data then nUI_Data = {}; end
if not nUI_Data.MoverAnchors then nUI_Data.MoverAnchors = {}; end
if not nUI_Profile then nUI_Profile = {}; end;

nUI_Profile.nUI_Movers = {};

local ProfileCounter = nUI_Profile.nUI_Movers;
local MoverAnchors   = nUI_Data.MoverAnchors;

-------------------------------------------------------------------------------
-- frame mover event management

nUI_Movers           = CreateFrame( "Frame", "nUI_MoverEvents", WorldFrame );
nUI_Movers.FrameList = {};

local function onMoversEvent()

--	nUI_ProfileStart( ProfileCounter, "onMoversEvent", event );
	
	if event == "PLAYER_LOGOUT" then
		
		for mover_name in pairs( nUI_Data.MoverAnchors ) do
			
			if not nUI_Data.MoverAnchors[mover_name].user_placed then
				nUI_Data.MoverAnchors[mover_name] = nil;
			end
		end
		
	elseif event == "VARIABLES_LOADED" then

		if not nUI_Data.MoverAnchors then 
			nUI_Data.MoverAnchors = {}; 
		end		

		MoverAnchors = nUI_Data.MoverAnchors;

		VehicleSeatIndicator:SetScale( nUI_Options.mountScale or 1 );
					
	elseif event == "PLAYER_LOGIN" then
			
		nUI:setScale();
		
		-- initialize the slash command handler for selecting the mover state
		
		local option = nUI_SlashCommands[nUI_SLASHCMD_MOVERS];
		
		nUI_SlashCommands:setHandler( option.command,
		
			function()

				if not nUI_Movers.enabled then nUI_Movers.enabled = true;
				else nUI_Movers.enabled = false;
				end
			
				DEFAULT_CHAT_FRAME:AddMessage( (option.message):format(nUI_Movers.enabled and nUI_L["|cFF00FF00ENABLED|r"] or nUI_L["|cFFFF0000DISABLED|r"]), 1, 0.83, 0 );
				
				for i in pairs( nUI_Movers.FrameList ) do
					
					nUI_Movers:enableDrag( nUI_Movers.FrameList[i], nUI_Movers.enabled );
					
				end
			end
		);
		
		-- initialize the slash command handler for sizing the vehicle seat indicator
		
		local option = nUI_SlashCommands[nUI_SLASHCMD_MOUNTSCALE];
		
		nUI_SlashCommands:setHandler( option.command,
			
			function( msg, arg1 )
				
				local scale = tonumber( arg1 or "1" );
				
				if not scale or scale < 0.5 or scale > 1.5 then

					DEFAULT_CHAT_FRAME:AddMessage( nUI_L["nUI: [ %s ] is not a valid special mount indicator display scale. The scale must be a number between 0.5 and 1.5"]:format( arg1 or "<nil>" ), 1, 0.83, 0 );

				elseif nUI_Options.mountScale ~= scale then

					nUI_Options.mountScale = scale;
					VehicleSeatIndicator:SetScale( scale );
					
					DEFAULT_CHAT_FRAME:AddMessage( (option.message):format( scale ), 1, 0.83, 0 );

				end
			end
		);	
				
		-- create a mover for the capture bars
		
		local frame = WorldStateCaptureBar1;
		local mover = nUI_Data.MoverAnchors["nUI_CaptureBarMover"];
		
		nUI_CaptureBarMover = CreateFrame( "Frame", "nUI_CaptureBarMover", UIParent );
		nUI_CaptureBarMover:SetWidth( 173 );
		nUI_CaptureBarMover:SetHeight( 26 );
		
		if not mover then
			nUI_CaptureBarMover:SetPoint( "TOPRIGHT", UIParent, "TOPRIGHT", -10, -20 );
		else
			for i in pairs( mover.Point ) do
				local point = mover.Point[i];
				nUI_CaptureBarMover:SetPoint( point.anchor, point.relative_to, point.relative_pt, point.xOfs, point.yOfs );
			end
		end
		
		nUI_CaptureBarMover:SetScript( "OnEvent", 
			function()	

--				nUI_ProfileStart( ProfileCounter, "CaptureBarMover.OnEvent", event );
	
				if event == "UPDATE_WORLD_STATES" then
					local last_frame;
					
					for i=1,NUM_EXTENDED_UI_FRAMES do
						local frame = _G["WorldStateCaptureBar"..i];				
						
						if not frame.fCache then nUI_Movers:lockFrame( frame, true, nil ); end					
						
						frame:fCache_ClearAllPoints();
						
						if i == 1 then
							frame:fCache_SetPoint( "CENTER", nUI_CaptureBarMover, "CENTER", 0, 0 );
						else
							frame:fCache_SetPoint( "TOP", last_frame, "BOTTOM", 0, 0 );
						end
						
						last_frame = frame;
					end
				end
				
--				nUI_ProfileStop();
				
			end
		);
	
		nUI_Movers:lockFrame( nUI_CaptureBarMover, true, nUI_L["Capture Bar"] );
		nUI_CaptureBarMover:RegisterEvent( "UPDATE_WORLD_STATES" );	
		
		-- Bliz frames we want the player to be able to move any time
	
		LoadAddOn( "Blizzard_AchievementUI" );
		LoadAddOn( "Blizzard_AuctionUI" );
		LoadAddOn( "Blizzard_Calendar" );
		LoadAddOn( "Blizzard_CraftUI" );
		LoadAddOn( "Blizzard_GuildBankUI" );
		LoadAddOn( "Blizzard_InspectUI" );
		LoadAddOn( "Blizzard_TalentUI" );
		LoadAddOn( "Blizzard_TradeSkillUI" );
	
		if AchievementFrame then nUI_Movers:movableFrame( AchievementFrame ); end
		if AuctionFrame then nUI_Movers:movableFrame( AuctionFrame ); end
		if CalendarFrame then nUI_Movers:movableFrame( CalendarFrame ); end
		if CraftFrame then nUI_Movers:movableFrame( CraftFrame ); end
		if GuildBankFrame then nUI_Movers:movableFrame( GuildBankFrame ); end
		if InspectFrame then nUI_Movers:movableFrame( InspectFrame ); end
		if PlayerTalentFrame then nUI_Movers:movableFrame( PlayerTalentFrame ); end
		if TradeSkillFrame then nUI_Movers:movableFrame( TradeSkillFrame ); end
		
		nUI_Movers:movableFrame( ArenaFrame );
		nUI_Movers:movableFrame( BattlefieldFrame );
		nUI_Movers:movableFrame( ChannelFrame, FriendsFrame );
		nUI_Movers:movableFrame( ChatConfigFrame );
		nUI_Movers:movableFrame( FriendsFrame );
		nUI_Movers:movableFrame( GossipFrame );
		nUI_Movers:movableFrame( GuildRegistrarFrame );
		nUI_Movers:movableFrame( HonorFrame, CharacterFrame );
		nUI_Movers:movableFrame( LFGParentFrame ); -----------
		nUI_Movers:movableFrame( MailFrame );
		nUI_Movers:movableFrame( MerchantFrame );
		nUI_Movers:movableFrame( PaperDollFrame, CharacterFrame );
		nUI_Movers:movableFrame( PetitionFrame );
		nUI_Movers:movableFrame( PetStableFrame );
		nUI_Movers:movableFrame( PVPFrame );
		nUI_Movers:movableFrame( QuestFrame );
		nUI_Movers:movableFrame( QuestLogFrame );
		nUI_Movers:movableFrame( RaidFrame, FriendsFrame );
		nUI_Movers:movableFrame( ReputationFrame, CharacterFrame );
		nUI_Movers:movableFrame( SendMailFrame, MailFrame );
		nUI_Movers:movableFrame( SkillFrame, CharacterFrame );
		nUI_Movers:movableFrame( SpellBookFrame );
		nUI_Movers:movableFrame( TaxiFrame );
		nUI_Movers:movableFrame( TradeFrame );
	
		-- standard Bliz frames we want to  move
		
		MirrorTimer1:ClearAllPoints();
		MirrorTimer1:SetPoint( "TOP", nUI_TopBars, "BOTTOM", 0, -5 );
		nUI_Movers:lockFrame( MirrorTimer1, true, nUI_L["Timer Bar"] );
				
		GroupLootFrame1:ClearAllPoints();
		GroupLootFrame1:SetPoint( "TOP", MirrorTimer1, "BOTTOM", 0, -5 );
		nUI_Movers:lockFrame( GroupLootFrame1, true, nUI_L["Group Loot Frame"].." 1" );
		
		GroupLootFrame2:ClearAllPoints();
		GroupLootFrame2:SetPoint( "TOP", GroupLootFrame1, "BOTTOM", 0, -5 );
		nUI_Movers:lockFrame( GroupLootFrame2, true, nUI_L["Group Loot Frame"].." 2" );
		
		GroupLootFrame3:ClearAllPoints();
		GroupLootFrame3:SetPoint( "TOP", GroupLootFrame2, "BOTTOM", 0, -5 );
		nUI_Movers:lockFrame( GroupLootFrame3, true, nUI_L["Group Loot Frame"].." 3" );
		
		GroupLootFrame4:ClearAllPoints();
		GroupLootFrame4:SetPoint( "TOP", GroupLootFrame3, "BOTTOM", 0, -5 );
		nUI_Movers:lockFrame( GroupLootFrame4, true, nUI_L["Group Loot Frame"].." 4" );
		
		if MultiCastActionBarFrame then
			MultiCastActionBarFrame:SetParent( UIParent );
			MultiCastActionBarFrame:ClearAllPoints();
			MultiCastActionBarFrame:SetPoint( "BOTTOM", nUI_Dashboard, "TOP", 0, 10 );
			nUI_Movers:lockFrame( MultiCastActionBarFrame, true, "Shaman Totem Bar" );
		end
		
		if not WatchFrame then
		
			if QuestWatchFrame then
				QuestWatchFrame:ClearAllPoints();
				QuestWatchFrame:SetPoint( "TOPLEFT", nUI_TopBars, "BOTTOMLEFT", 10, 120 * nUI.vScale );	
				nUI_Movers:lockFrame( QuestWatchFrame, true, nUI_L["Watched Quests"] );
			end
				
			if QuestTimerFrame then
				QuestTimerFrame:ClearAllPoints();
				QuestTimerFrame:SetPoint( "TOP", nUI_TopBars, "BOTTOM", 0, -5 );
				nUI_Movers:lockFrame( QuestTimerFrame, true, nUI_L["Quest Timer"] );
			end
			
		end
			
		DurabilityFrame:ClearAllPoints();
		DurabilityFrame:SetPoint( "BOTTOMRIGHT", UIParent, "RIGHT", -5, 0 );	
		nUI_Movers:lockFrame( DurabilityFrame, true, nUI_L["Equipment Durability"] );
	
		WorldStateAlwaysUpFrame:ClearAllPoints();
		WorldStateAlwaysUpFrame:SetPoint( "TOPLEFT", nUI_CaptureBarMover, "BOTTOMLEFT", 0, -10 );
		nUI_Movers:lockFrame( WorldStateAlwaysUpFrame, true, nUI_L["PvP Objectives"] );
	
		VehicleSeatIndicator:ClearAllPoints();
		VehicleSeatIndicator:SetPoint( "BOTTOMRIGHT", DurabilityFrame, "TOPRIGHT", 0, 5 );
		nUI_Movers:lockFrame( VehicleSeatIndicator, true, nUI_L["Vehicle Seat Indicator"] );
		
		VoiceChatTalkers:ClearAllPoints();
		VoiceChatTalkers:SetPoint( "BOTTOMRIGHT", VehicleSeatIndicator, "TOPRIGHT", 0, 5 );
		nUI_Movers:lockFrame( VoiceChatTalkers, true, nUI_L["Voice Chat Talkers"] );
		
		LoadAddOn( "QuestHelper" );
		
		if QuestWatchFrame and AchievementWatchFrame then
			if IsAddOnLoaded( "QuestHelper" ) then
				AchievementWatchFrame:ClearAllPoints();
				AchievementWatchFrame:SetPoint( "TOPLEFT", QuestHelperQuestWatchFrame, "BOTTOMLEFT", 0, -10 );
				nUI_Movers:lockFrame( AchievementWatchFrame, true, nUI_L["Watched Achievments"] );
			else
				AchievementWatchFrame:ClearAllPoints();
				AchievementWatchFrame:SetPoint( "TOPLEFT", QuestWatchFrame, "BOTTOMLEFT", 0, -10 );
				nUI_Movers:lockFrame( AchievementWatchFrame, true, nUI_L["Watched Achievments"] );
			end
		end
	end
	
--	nUI_ProfileStop();
	
end

nUI_Movers:SetScript( "OnEvent", onMoversEvent );
nUI_Movers:RegisterEvent( "PLAYER_LOGIN" );
nUI_Movers:RegisterEvent( "VARIABLES_LOADED" );
nUI_Movers:RegisterEvent( "PLAYER_LOGOUT" );

-------------------------------------------------------------------------------

function nUI_Movers:movableFrame( frame, parent )
	
--	nUI_ProfileStart( ProfileCounter, "movableFrame" );
	
	frame:EnableMouse( true );
	frame:SetMovable( true );
	frame:RegisterForDrag( "LeftButton" );
	
	frame:SetScript( "OnEnter", 
		function()
			if not InCombatLockdown() then
				GameTooltip:SetOwner( frame );
				GameTooltip:SetText( nUI_L["Left-click and drag to move this frame"] );
				GameTooltip:Show();
			end
		end
	);
	
	frame:SetScript( "OnLeave",
		function()
			GameTooltip:Hide();
		end
	);
	
	frame:SetScript( "OnDragStart",
		function()			
			if not InCombatLockdown() then
				frame.is_moving = true;
				if parent then parent:StartMoving() 
				else frame:StartMoving();
				end
			end
		end
	);
	
	frame:SetScript( "OnDragStop",
		function()
			if frame.is_moving then
				frame.is_moving = false;
				if parent then parent:StopMovingOrSizing();
				else frame:StopMovingOrSizing();
				end
			end
		end
	);
			
--	nUI_ProfileStop();
	
end

-------------------------------------------------------------------------------

function nUI_Movers:lockFrame( frame, mode, overlay )

--	nUI_ProfileStart( ProfileCounter, "lockFrame" );
	
	local new_mover = false;
	
	-- if we don't already have a frame cache for this frame, initialize it
	
	if not frame.fCache then	

		new_mover                   = true;		
		frame.fCache                = {};		
		frame.fCache_SetAllPoints   = frame.SetAllPoints;
		frame.fCache_SetPoint       = frame.SetPoint;
		frame.fCache_ClearAllPoints = frame.ClearAllPoints;

	end
	
	-- lock the frame to prevent anyone else from moving it
	
	if not frame.fCache.locked and mode then
		
		frame.fCache.locked  = true;
		frame.SetAllPoints   = function() end;
		frame.SetPoint       = function() end;
		frame.ClearAllPoints = function() end;
	
	-- make the frame moveable again
	
	elseif frame.fCache.locked and not mode then

		frame.fCache.locked  = false;
		frame.SetAllPoints   = frame.fCache_SetAllPoints;
		frame.SetPoint       = frame.fCache_SetPoint;
		frame.ClearAllPoints = frame.fCache_ClearAllPoints;
		
	end

	-- if this is a new mover, set up the overlay for it
	
	if new_mover then
		
		local frame_name = frame:GetName();
		local mover      = nUI_Data.MoverAnchors[frame_name];		

		-- if this frame has a visible overlay, then add it to the list of 
		-- movers we enable/disable on the "/nui movers" slash command

		if overlay then
			if not nUI_Movers.FrameList[frame_name] then 
				nUI_Movers.FrameList[frame_name] = frame; 
			end
		end
		
		-- record the default location for the frame
		
		frame.fCache.DefaultPoints = {};
		
		for i=1,frame:GetNumPoints() do
			
			local point = {};

			point.anchor, point.relative_to, point.relative_pt, point.xOfs, point.yOfs = frame:GetPoint( i );
			point.relative_to = point.relative_to and point.relative_to.GetName and point.relative_to:GetName() or point_relative_to;
			
			frame.fCache.DefaultPoints[i] = point;
			
		end
		
		-- create a default mover in the config if we don't have one yet
		
		if not mover or not mover.user_placed then
			
			mover = {};
			mover.Point = {};
			mover.user_placed = false;
			
			for i in pairs( frame.fCache.DefaultPoints ) do
				mover.Point[i] = frame.fCache.DefaultPoints[i];
			end
			
			nUI_Data.MoverAnchors[frame_name] = mover;
			
		end

		frame.fCache.label = overlay;	
		frame.fCache.drag  = true;
		
		-- create an overlay mover frame to show the user and/or for dragging the frame
	
		frame.fCache.overlay = CreateFrame( "Button", frame:GetName().."_MoverOverlay", frame:GetParent() );
		
		frame.fCache.overlay:SetFrameStrata( "HIGH" );
		frame.fCache.overlay:SetFrameLevel( frame:GetFrameLevel()+5 );
		
		frame.fCache.overlay:SetScale( frame:GetScale() );
		frame.fCache.overlay:SetWidth( max( frame:GetWidth(), 25 ) );
		frame.fCache.overlay:SetHeight( max( frame:GetHeight(), 25 ) );

		for i in pairs( mover.Point ) do
			local point = mover.Point[i];
			frame.fCache.overlay:SetPoint( point.anchor, point.relative_to, point.relative_pt, point.xOfs, point.yOfs );
		end
		
		frame.fCache.overlay:EnableMouse( true );
		frame.fCache.overlay:RegisterForDrag();
		frame.fCache.overlay:RegisterForClicks();
		frame.fCache.overlay:SetScript( "OnDragStart", 
			function() 
--				nUI_ProfileStart( ProfileCounter, "OnDragStart" ); 
				nUI_Movers:dragFrame( frame, true ); 
--				nUI_ProfileStop();
			end 
		);
		frame.fCache.overlay:SetScript( "OnDragStop", 
			function() 
--				nUI_ProfileStart( ProfileCounter, "OnDragStop" ); 
				nUI_Movers:dragFrame( frame, false ); 
--				nUI_ProfileStop();
			end 
		);
		frame.fCache.overlay:SetScript( "OnClick", 
			function() 
--				nUI_ProfileStart( ProfileCounter, "OnClick" ); 
				nUI_Movers:resetFrame( frame ); 
--				nUI_ProfileStop();
			end 
		);
		frame.fCache.overlay:SetMovable( true );

		-- if we want the mover to be visible, creat a texture for it
		
		if overlay then
			
			frame.fCache.overlay:SetBackdrop(
				{
					bgFile   = "Interface\\AddOns\\nUI\\Layouts\\Default\\Art\\nUI_BevelboxBg.blp", 
					edgeFile = "Interface\\AddOns\\nUI\\Layouts\\Default\\Art\\nUI_BevelboxBorder.blp", 
					tile     = true, 
					tileSize = 1, 
					edgeSize = 10, 
					insets   = {left = 0, right = 0, top = 0, bottom = 0},
				}
			);
		
			frame.fCache.overlay:SetBackdropColor( 1, 0.75, 0.75, 1 );
			frame.fCache.overlay:SetBackdropBorderColor( 1, 0.75, 0.75, 1 );
			
			frame.fCache.overlay.texture = frame.fCache.overlay:CreateTexture();
			frame.fCache.overlay.texture:SetAllPoints( frame.fCache.overlay );
			frame.fCache.overlay.texture:SetTexture( 1, 0.5, 0.5, 0.5 );
		
		end

		-- lock the frame to the overlay
		
		frame:fCache_ClearAllPoints();
		
		for i in pairs( mover.Point ) do
			local point = mover.Point[i];
			frame:fCache_SetPoint( point.anchor, frame.fCache.overlay, point.anchor, 0, 0 );
		end
		
		-- disable any other movers that may be attached to the frame
		
		frame:RegisterForDrag();
		frame:SetScript( "OnDragStart", nil );
		frame:SetScript( "OnDragStop", nil );

		-- disable dragging
		
		self:enableDrag( frame, false );
	end	
	
--	nUI_ProfileStop();
	
end

-------------------------------------------------------------------------------

function nUI_Movers:enableDrag( frame, mode )

	if mode then

		if not frame.fCache.moving and not frame.fCache.drag then
			
			frame.fCache.drag = true;
			frame.fCache.overlay:SetWidth( max( frame:GetWidth(), 25 ) );
			frame.fCache.overlay:SetHeight( max( frame:GetHeight(), 25 ) );
			frame.fCache.overlay:SetScale( frame:GetScale() );
			frame.fCache.overlay:Show();
			frame.fCache.overlay:RegisterForDrag( "LeftButton" );
			frame.fCache.overlay:RegisterForClicks( "RightButtonUp" );
			
			frame.fCache.overlay:SetScript( "OnEnter", 
				function() 
					
--					nUI_ProfileStart( ProfileCounter, "OnEnter" ); 
					
					GameTooltip:SetOwner( frame.fCache.overlay ); 
					GameTooltip:SetText( nUI_L["Left click and drag to move <frame label>"]:format( frame.fCache.label ), 1, 0.83, 0 ); 
					
					if nUI_Data.MoverAnchors[frame:GetName()].user_placed then
						GameTooltip:AddLine( nUI_L["Right click to reset to the default location"], 1, 0.83, 0 );
					end
					
					GameTooltip:Show();
					
--					nUI_ProfileStop();
				end 
			);
			
			frame.fCache.overlay:SetScript( "OnLeave", 
				function() 
--					nUI_ProfileStart( ProfileCounter, "OnLeave" ); 
					GameTooltip:Hide(); 
--					nUI_ProfileStop();
				end 
			);
			
		end
			
	else
		
		if not frame.fCache.moving and frame.fCache.drag then
			
			frame.fCache.drag = false;
			frame.fCache.overlay:Hide();
			frame.fCache.overlay:RegisterForClicks();
			frame.fCache.overlay:RegisterForDrag();
			frame.fCache.overlay:SetScript( "OnEnter", function() end );
			frame.fCache.overlay:SetScript( "OnLeave", function() end );
		end		
	end
end

-------------------------------------------------------------------------------

function nUI_Movers:dragFrame( frame, mode )
	
--	nUI_ProfileStart( ProfileCounter, "dragFrame" ); 
	
	if mode and frame.fCache.drag and not frame.fCache.moving then
		
		frame.fCache.overlay:StartMoving();
		frame.fCache.moving = true;
		GameTooltip:Hide();
		
	elseif not mode and frame.fCache.drag and frame.fCache.moving then
		
		local mover   = nUI_Data.MoverAnchors[frame:GetName()];
		local overlay = frame.fCache.overlay;
		
		overlay:StopMovingOrSizing();
		frame.fCache.moving = false;

		mover.Point = {};
		mover.user_placed = true;
		
		for i=1,overlay:GetNumPoints() do
			
			local point = {};

			point.anchor, point.relative_to, point.relative_pt, point.xOfs, point.yOfs = overlay:GetPoint( i );
			point.relative_to = point.relative_to and point.relative_to:GetName() or point_relative_to;
			
			mover.Point[i] = point;
			
		end
	end
	
--	nUI_ProfileStop();
	
end

-------------------------------------------------------------------------------

function nUI_Movers:resetFrame( frame )
	
--	nUI_ProfileStart( ProfileCounter, "resetFrame" ); 
	
	local mover       = nUI_Data.MoverAnchors[frame:GetName()];
	local overlay     = frame.fCache.overlay;

	for i in pairs( frame.fCache.DefaultPoints ) do
		mover.Point[i] = frame.fCache.DefaultPoints[i];
	end

	mover.user_placed = false;
	
	-- lock the frame to the overlay
	
	frame.fCache.overlay:ClearAllPoints();
	
	for i in pairs( mover.Point ) do
		
		local point = mover.Point[i];						
		frame.fCache.overlay:SetPoint( point.anchor, point.relative_to, point.relative_pt, point.xOfs, point.yOfs );
		
	end
	
--	nUI_ProfileStop();
	
end
