﻿
function Overview(pTargetMapBase, WndWidth, WndHeight, dicScale, hAlign, vAlign)
{
	var eImgWidth = pTargetMapBase.getImageWidth();
	var eImgHeight = pTargetMapBase.getImageHeight();
	
	var pNode = pTargetMapBase.getHPackage();
	
	var m_pWndDiv = pNode.ownerDocument.createElement("div");
	pNode.appendChild(m_pWndDiv);
	m_pWndDiv.style.position = "absolute";
	if (hAlign)
		m_pWndDiv.style.left =0;
	else
		m_pWndDiv.style.right =0;
	
	if (vAlign)
		m_pWndDiv.style.top =0;
	else
		m_pWndDiv.style.bottom =0;

	m_pWndDiv.style.backgroundColor='white';
	m_pWndDiv.style.borderColor='white';
	m_pWndDiv.style.width=WndWidth-10;
	m_pWndDiv.style.height=WndHeight-10;
	m_pWndDiv.style.border = "inset 1px";
	m_pWndDiv.style.overflow = "hidden";

	var CtrlDiv = m_pWndDiv.ownerDocument.createElement("img");
	m_pWndDiv.appendChild(CtrlDiv);
	CtrlDiv.style.position = "absolute";
	CtrlDiv.src = 'images/eyeclose.gif';
	if (hAlign)
		CtrlDiv.style.left=0;
	else
		CtrlDiv.style.right=0 ;
	if (vAlign)
		CtrlDiv.style.top=0;
	else
		CtrlDiv.style.bottom=0;
		
	CtrlDiv.style.zIndex = 99999;
	CtrlDiv.status = true;
	CtrlDiv.alt='Close';
	CtrlDiv.src = 'images/eyeclose.gif';
	
	var tCtrl = null;
	CtrlDiv.onclick=function()
	{
	 	var imove = function(tX, tY)
	 	{
			divright = parseInt(m_pWndDiv.style.width)-10;
			divbottom = parseInt(m_pWndDiv.style.height)-10;
	 		divXOffset = tX - divright;
	 		divYOffset = tY - divbottom;
	 		if(Math.abs(divXOffset)<10 || Math.abs(divYOffset)<10)
	 		{
				m_pWndDiv.style.width = tX;
				m_pWndDiv.style.height = tY;
				tCtrl = null;
	 		}
	 		else
	 		{
				m_pWndDiv.style.width = divright + divXOffset/2;
				m_pWndDiv.style.height = divbottom + divYOffset/2;
				tCtrl = setTimeout(function() {imove(tX, tY);}, 50);
	 		}
	 	}
		if (tCtrl!=null)
			clearTimeout(tCtrl);
		tCtrl = null;
		if(CtrlDiv.status = !CtrlDiv.status)
		{
			CtrlDiv.alt='Close';
			CtrlDiv.src = 'images/eyeclose.gif';
			imove(WndWidth, WndHeight);
		}
		else
		{
			CtrlDiv.alt='Open';
			CtrlDiv.src = 'images/eyeopen.gif';
			imove(parseInt(CtrlDiv.offsetWidth)+2, parseInt(CtrlDiv.offsetHeight)+2);
		}
	}

	var imgw = 16;
	var imgh = 16;
	var m_pMapDiv = pNode.ownerDocument.createElement("div");
	m_pWndDiv.appendChild(m_pMapDiv);
	m_pMapDiv.style.position = "absolute";
	m_pMapDiv.style.overflow = "hidden";
	if (hAlign)
		m_pMapDiv.style.left = imgw;
	else
		m_pMapDiv.style.right = imgw-15;
		
	if (vAlign)
		m_pMapDiv.style.top = imgh;
	else
		m_pMapDiv.style.bottom = imgh-15;
		
	m_pMapDiv.style.width = WndWidth - imgw - 3;
	m_pMapDiv.style.height = WndHeight - imgh - 3;
	m_pMapDiv.style.border = "solid 1px";
	m_pMapDiv.style.borderColor="#797979";
	
 	var m_pOverMapBase = new MapBase(m_pMapDiv, 0, 0, "100%", "100%", eImgWidth, eImgHeight);
 	m_pOverMapBase.putName("Overview");
 	m_pOverMapBase.putMapMaxLevel(pTargetMapBase.getMapMaxLevel());

	
	this.CurrentScale = m_pOverMapBase.getMapLevel;
	this.CenterX = m_pOverMapBase.getCenterX;
	this.CenterY = m_pOverMapBase.getCenterY;
	this.CenterXY = function(){return new MapPoint(m_pOverMapBase.getCenterX(),m_pOverMapBase.getCenterY());}
	this.SelectAreaHide = function() {m_IndexObj.style.visibility = "hidden";}
	this.SelectAreaShow = function() {m_IndexObj.style.visibility = "visible";}
	this.getTarget = function(){return pTargetMapBase;}
	this.getMapBase = function(){return m_pOverMapBase;}
	
	var m_pIndexCuboid = new IndexCuboid(this);

	this.UpdateElement = function()
	{
		if (CtrlDiv.status)
			m_pIndexCuboid.UpdateElement();
	}
	this.RebuildElement = function()
	{
		m_pIndexCuboid.RebuildElement();
		var e_Scale = Math.max(pTargetMapBase.getMapLevel() - dicScale, 0);
		if(e_Scale == 0)
		{
			e_Scale = 5;
		}
		m_pOverMapBase.MoveToAnimation(new MapPoint(pTargetMapBase.getCenterX(), pTargetMapBase.getCenterY()), e_Scale);
	}
    this.MapTo=function(cx,cy)
	{
	    pTargetMapBase.MoveMapTo(cx, cy);
		pTargetMapBase.RefreshMap(true, true);		
	}
	pTargetMapBase.AddElement(this);
}

function IndexCuboid(pOverview)
{
	var pTargetMapBase = pOverview.getTarget();
	var pOverMapBase = pOverview.getMapBase();
	//var m_bMapAction = false;
	var m_bOverAction = false;
	var m_pMoveTool = new MapMoveEx(false, false);
	
	var pOverNode = pOverMapBase.getHPackage();
	var m_IndexObj = pOverNode.ownerDocument.createElement("div");			
	pOverNode.appendChild(m_IndexObj);
	m_IndexObj.style.position = "absolute";
	m_IndexObj.style.overflow = "hidden";
	m_IndexObj.style.width =40;
	m_IndexObj.style.height = 40;
	m_IndexObj.style.left =15;
	m_IndexObj.style.top =15;
	m_IndexObj.style.border = "inset 2px red";
//	hObj.style.MozOpacity = 0.15; // FF ㄡ


//	var hObj = m_IndexObj.ownerDocument.createElement("div");
//	m_IndexObj.appendChild(hObj);
//	hObj.style.filter='Alpha(Opacity=15)';
//	hObj.style.backgroundColor = 'blue';
//	hObj.style.width ='100%';
//	hObj.style.height = '100%';
	
	var MouseCurX ;
	var MouseCurY ;
	var pMouseMove = function(tEvent)
	{	    
		//var ox = tEvent.screenX - MouseCurX;
		//var oy = tEvent.screenY - MouseCurY;
		var ox = MouseCurX - tEvent.screenX ; // FF ㄡ
		var oy = MouseCurY - tEvent.screenY ; // FF ㄡ
		MouseCurX = tEvent.screenX;
		MouseCurY = tEvent.screenY;		
		pTargetMapBase.MoveMapTo(pTargetMapBase.getCenterX()+pOverMapBase.ToMapDistX(ox), pTargetMapBase.getCenterY()+pOverMapBase.ToMapDistY(oy));
		pTargetMapBase.RefreshMap(true, false);
	};
	var pMouseUp = FuncAdapter(this, function(tEvent)
	{
		DetachEvent(m_IndexObj, "mousemove", pMouseMove, true);
		DetachEvent(m_IndexObj, "mouseup", pMouseUp, false);
		pTargetMapBase.RefreshMap(true, true);
	});
	var pMouseDown = function(tEvent)
	{	    
		MouseCurX = tEvent.screenX;
		MouseCurY = tEvent.screenY;
		AttachEvent(m_IndexObj, "mousemove", pMouseMove, true);
		AttachEvent(m_IndexObj, "mouseup", pMouseUp, false);
	};
	AttachEvent(m_IndexObj, "mousedown", pMouseDown, false);
	
	this.UpdateElement = function()
	{	    
	    //alert(m_pMoveTool.IsMapAction());
	    
		if (m_pMoveTool.IsMapAction())
		{		
			if (m_bOverAction)
				return;
				
			m_bOverAction = true
			
			LTc.X = parseInt(m_IndexObj.style.left) + parseInt(m_IndexObj.style.width)/2;
			LTc.Y = parseInt(m_IndexObj.style.top) + parseInt(m_IndexObj.style.height)/2;
			LT = pOverMapBase.ToMapPoint(LTc.X, LTc.Y);
			pTargetMapBase.MoveMapTo(LT.X, LT.Y);	
			pTargetMapBase.RefreshMap(true, false);
			m_bOverAction = false;
		}
		else
		{
		//m_bOverAction = true
						
			var LT = pTargetMapBase.ToMapPoint(0,0);
			var RB = pTargetMapBase.ToMapPoint(pTargetMapBase.getClientWidth(), pTargetMapBase.getClientHeight());
			LTc = pOverMapBase.FromMapPoint(LT.X, LT.Y);
			RBc = pOverMapBase.FromMapPoint(RB.X, RB.Y);
			m_IndexObj.style.left = LTc.X ;
			m_IndexObj.style.top = LTc.Y; 
			m_IndexObj.style.width = RBc.X - LTc.X;
			m_IndexObj.style.height =  RBc.Y - LTc.Y;
		}
	}
	this.RebuildElement = function()
	{
		if (m_pMoveTool.IsMapAction())
		{
			if (m_bOverAction)
				return;
			m_bOverAction = true
			pTargetMapBase.RefreshMap(true, true);
			m_bOverAction = false;
		}
	}
	pOverMapBase.SelectMapTool(m_pMoveTool);
	pOverMapBase.AddElement(this);
}
