var startDragSmall = null;
var endDragSmall = null;
var LastRectDraw = null;


function copy_obj(o) {
    var c = new Object();

    for (var e in o) {
      c[e] = o[e];
    }
    return c;
}


function DrawRect(obj, RectDraw)
{

    var MapSmallWidth = parseInt(obj.mapSmall.width);

    if (RectDraw.X+RectDraw.Width>MapSmallWidth)
    {
        RectDraw.Width = MapSmallWidth - RectDraw.X;
        if (RectDraw.X>MapSmallWidth)
            RectDraw.X = MapSmallWidth;
    }
    else RectDraw.Width= LastRectDraw.WidthDef;
    if (RectDraw.Y+RectDraw.Height>MapSmallWidth)
    {
        RectDraw.Height = MapSmallWidth - RectDraw.Y;
        if (RectDraw.Y>MapSmallWidth)
            RectDraw.Y = MapSmallWidth;
    }
    else RectDraw.Height= LastRectDraw.WidthDef;
    if (RectDraw.X<0)
    {       
        RectDraw.Width = (RectDraw.Width) + RectDraw.X;
        RectDraw.X=0;
    }
    if (RectDraw.Y<0)
    {
        RectDraw.Height = (RectDraw.Height) + RectDraw.Y;
        RectDraw.Y=0;
    }
    if (RectDraw.Width<0)
        RectDraw.Width = 0;
    if (RectDraw.Height<0)
        RectDraw.Height = 0;
    jgSmallMap.clear();
    jgSmallMap.setColor("#ff0000"); // red
    jgSmallMap.setStroke(2);
    jgSmallMap.drawRect(RectDraw.X,RectDraw.Y,RectDraw.Width,RectDraw.Height);
    jgSmallMap.paint();
}


function SmallMap_OnMouseOut(event,obj)
{

}


function SmallMap_MouseDown(event,obj){
//alert("SmallMap_MouseDown");
	var e = event || window.event;	
    startDragSmall=null;	
    endDragSmall=null;
	if(obj.zoomEnded==1 && obj.mapReady==1){
		startDragSmall=SharpMap_GetRelativePosition(e.clientX,e.clientY,obj.mapSmall);
		LastRectDraw.X = startDragSmall.x;
		LastRectDraw.Y = startDragSmall.y;
		LastRectDraw.X = LastRectDraw.X - LastRectDraw.Width/2;
		LastRectDraw.Y = LastRectDraw.Y - LastRectDraw.Height/2;
		DrawRect(obj, LastRectDraw)
	}
}


/* MouseUp - Occurs during a drag event or when doing a click */ 
function SmallMap_MouseUp(event,obj){
var e = event || window.event;
    
if (!SharpMap_IsDefined(endDragSmall))
{
	endDragSmall=SharpMap_GetRelativePosition(e.clientX,e.clientY,obj.mapSmall); 
}
if(obj.zoomEnded==1 && obj.mapReady==1 && SharpMap_IsDefined(startDragSmall) && SharpMap_IsDefined(endDragSmall))
{
    var MapSmallWidth = parseInt(obj.mapSmall.width);
    var dx=endDragSmall.x-startDragSmall.x;
    var dy=endDragSmall.y-startDragSmall.y;
    var RectDraw = copy_obj(LastRectDraw);
    RectDraw.X = LastRectDraw.X + dx;
    RectDraw.Y = LastRectDraw.Y + dy;
    RectDraw.X = RectDraw.X + (LastRectDraw.WidthDef/2);
    RectDraw.Y = RectDraw.Y + (LastRectDraw.WidthDef/2);
    RectDraw.Width = LastRectDraw.Width;
    RectDraw.Height = LastRectDraw.Height;
    var x = RectDraw.X*(obj.maxZoom/MapSmallWidth)+obj.defMinX;
    var y = obj.defMaxY-RectDraw.Y*(obj.maxZoom/MapSmallWidth);
    GoToMap(obj, x , y);
}
                                   
    startDragSmall=null;	
    endDragSmall=null;
}


function SmallMap_MapMouseOver(event,obj){
//alert("SharpMap_MapMouseOver");

    if(SharpMap_IsDefined(startDragSmall)) {
            var e = event || window.event;
            endDragSmall=SharpMap_GetRelativePosition(e.clientX,e.clientY,obj.mapSmall);                                                                                                   
            var dx=endDragSmall.x-startDragSmall.x;
            var dy=endDragSmall.y-startDragSmall.y;         
            var RectDraw = copy_obj(LastRectDraw);
            RectDraw.X = LastRectDraw.X + dx;
            RectDraw.Y = LastRectDraw.Y + dy;
            RectDraw.Width = LastRectDraw.Width;
            RectDraw.Height = LastRectDraw.Height;
            DrawRect(obj,RectDraw);
       	}

}


function SharpMap_RedrawSmallMap(obj, dx, dy)
{
    var MapSmallWidth = parseInt(obj.mapSmall.width);
    var Width = obj.zoom/obj.maxZoom * MapSmallWidth;
    if (Width<6)
        Width = 6;
    var Height = Width;
    var WidthDef = Width;
    var center = SharpMap_GetCenter(obj);
    var X = (center.x - obj.defMinX )/(obj.maxZoom/MapSmallWidth);
    var Y = (obj.defMaxY - center.y)/(obj.maxZoom/MapSmallWidth);  
    if (dx!=null && dy!=null)
    {     
        var ScreenWidth = parseInt(obj.container.style.width.substring(0,obj.container.style.width.length-2));
        X = ((center.x - dx*(obj.zoom/ScreenWidth)) - obj.defMinX  )/(obj.maxZoom/MapSmallWidth);
        Y = (obj.defMaxY - (center.y + dy*(obj.zoom/ScreenWidth)))/(obj.maxZoom/MapSmallWidth); 
        
    }   
    X = X-(Width/2);
    Y = Y-(Height/2);  
    
    if (X+Width>MapSmallWidth)
    {
        Width = MapSmallWidth - X;
        if (X>MapSmallWidth)
            X = MapSmallWidth;
    }
    if (Y+Height>MapSmallWidth)
    {
        Height = MapSmallWidth - Y;
        if (Y>MapSmallWidth)
            Y = MapSmallWidth;
    }
    if (X<0)
    {       
        Width = (Width) + X;
        X=0;
    }
    if (Y<0)
    {
        Height = (Height) + Y;
        Y=0;
    }
    if (Width<0)
        Width = 0;
    if (Height<0)
        Height = 0;
    //alert(obj.minX+" "+ obj.maxY+" "+ center.x+" "+ center.y+" "+ obj.maxZoom+" "+X+" "+Y  );
    jgSmallMap.clear();
	jgSmallMap.setColor("#ff0000"); // red
	jgSmallMap.setStroke(2);
	jgSmallMap.drawRect(X,Y,Width,Height);
	jgSmallMap.paint();
	LastRectDraw = new Object(); 
	LastRectDraw.X = X; 
	LastRectDraw.Y = Y;
	LastRectDraw.Width = Width;
	LastRectDraw.Height = Height;
	LastRectDraw.WidthDef = WidthDef;
}
