/*	MapDotNet Common Drawing Library
	Copyright 2003, Specialized Technology Products, Inc. All Rights Reserved
	
	REVISION HISTORY:
	2/10/2004 Created	BFH
	7/29/2004 Cross Browser FDW
*/

/* adds a rectangle object to the supplied canvass */
/* the parentCanvas is the MapNavigator image DIV */
/* returns the new rectangle object */
/* opacity is supported by VML only */
function MDNAddRect(parentCanvas, left, top, width, height, newID, borderWidth, 
					borderColor, fillColor, opacity, useVML)
{
	var aRect;
	if (useVML)
	{
		aRect=document.createElement('v:Rect');
		parentCanvas.appendChild(aRect);
		aRect.id=newID;
		aRect.fill.opacity=opacity;
		aRect.stroked=true;
		aRect.style.position='absolute';
		aRect.style.width=parseInt(width)+'px';
		aRect.style.height=parseInt(height)+'px';
		aRect.style.left=parseInt(left)+'px';
		aRect.style.top=parseInt(top)+'px';
		
		if (fillColor!=null){aRect.fillColor=fillColor;}
		if (borderWidth>0){aRect.strokeweight=borderWidth;}
		if (borderColor!=null){aRect.strokecolor=borderColor;}
	}
	else
	{
		aRect=document.createElement('div');
		parentCanvas.appendChild(aRect);
		aRect.id=newID;
		aRect.style.borderStyle='solid';
		aRect.style.visibility='visible';
		aRect.style.overflow='hidden';
		aRect.style.position='absolute';
		aRect.style.width=parseInt(width)+'px';
		aRect.style.height=parseInt(height)+'px';
		aRect.style.left=parseInt(left)+'px';
		aRect.style.top=parseInt(top)+'px';
		
		if (fillColor!=null){aRect.style.backgroundColor=fillColor;}
		if (borderWidth>0)
		{
		    aRect.style.borderWidth=parseInt(borderWidth)+'px';
		}
		if (borderColor!=null){aRect.style.borderColor=borderColor;}
			
		if (opacity>0)
		{ 
		    MDNSetOpacity(aRect, opacity*100);
		}
	}
	return aRect;
}

/* draws a polyline or polygon on the supplied DIV canvas */
/* id is only needed to locate VML shapes, not needed for non-VML mode */
function MDNDrawPolyShape(parentCanvas, pointsStr, xArray, yArray, color, weight, closePoly, id, useVML)
{
	var shape;
	if (useVML)
	{
		/* if poly is set to closed, and there are at least three points, add endpoint */
		var endPoint='';
		if (xArray.length>=3 && closePoly)
		{
			var endPoint = '' + xArray[0] + ',' + yArray[0];
		}

		/* draw the polyline
		   Note: the odd fact that points must be set before appending the child shape
		   Note VML quirk that you cannot set "filled" to true and set the opacity */
		shape=document.createElement('v:polyline');
		shape.id=id;
		shape.stroked=true;
		shape.strokeColor=color;
		shape.points=pointsStr.value + endPoint;
		shape.strokeWeight=weight;		
		parentCanvas.appendChild(shape);
		shape.style.left=0;
		shape.style.right=0;
		shape.style.position='absolute';
		shape.style.zIndex=1001;
		shape.filled=false;
		
        shape.onselectstart=function(){return false;};
		shape.ondrag=function(){return false;};
	}
	else
	{
		var xA, yA;	
		if (xArray.length>=3 && closePoly)
		{
			ex=new Array();
			ex[0]=xArray[0];
			ey=new Array();
			ey[0]=yArray[0];
			xA=xArray.concat(ex);
			yA=yArray.concat(ey);
		}
		else
		{
			xA=xArray;
			yA=yArray;
		}
		if (xA.length>1)
		{
			var g=new jsGraphics(parentCanvas.id);
			g.setColor(color);
			g.setStroke(weight);
			g.drawPolyline(xA, yA);
			g.paint();
		}
	}
}

/* draws a circle, the first point is the center and the second determines radius (p2-p1) */
function MDNAddCircle(parentCanvas, x, y, radius, color, weight, id, useVML)
{
	var shape;
	if (useVML)
	{
		shape=document.createElement('v:oval');
		shape.id=id;
		shape.stroked=true;
		shape.strokeColor=color;
		shape.strokeWeight=weight;
		shape.style.width=radius*2;
		shape.style.height=radius*2;
		parentCanvas.appendChild(shape);
		shape.style.position='absolute';
		shape.style.left=x-radius;
		shape.style.top=y-radius;
		shape.style.zIndex=1001;
		shape.filled=false;
	}
	else
	{
		var g=new jsGraphics(parentCanvas.id);
		g.setColor(color);
		g.setStroke(weight);
		g.drawOval(x-radius, y-radius, radius*2, radius*2);
		g.paint();
	}
}

/* clear canvas of any polylines or polygons */
/* id of shape is only required if using VML */
function MDNClearShapesFromCanvas(parentCanvas, id, useVML)
{
    var shapes;
	if (useVML)
	{
		shapes = parentCanvas.childNodes;
	}
	else
	{
	    shapes = parentCanvas.getElementsByTagName('DIV');
	}  
		
	var idx;
	if (shapes != null)
	{
		for (idx=0; idx<shapes.length; idx++)
		{
			if ((shapes[idx].tagName == 'DIV' && (shapes[idx].id==null || shapes[idx].id=='')) || shapes[idx].id==id)
			{
				shapes[idx].parentNode.removeChild(shapes[idx]);
				idx--;
			}
		}
	}
}

/* returns truncated and padded numeric based on precision */
function MDNTruncateAndPad(val, precision)
{
	var p=Math.pow(10, precision);
	val=(parseInt(val * p))/p;

	var strArray=('' + val).split('.');
	if (strArray.length>1)
	{
		while(strArray[1].length<precision)
			strArray[1]+='0';
		strArray[1]='.'+strArray[1];
	}
	else
	{
		strArray[1]='';
	}
	return strArray[0]+strArray[1];
}

/* Resizes an element in a cross browser compatible manner */
function MDNElementResizeTo(element, newWidth, newHeight )
{
    if (element)
    {
	    if( element.style ) { element = element.style; }
	    if( element.resizeTo ) {
	      element.resizeTo( newWidth, newHeight );
	    }
	    var noPx = document.childNodes ? 'px' : 0;
	    element.width = newWidth + noPx;
	    element.pixelWidth = newWidth;
	    element.height = newHeight + noPx;
	    element.pixelHeight = newHeight;
	}
}

/* Resizes an element in a cross browser compatible manner */
/* Uses a delta to each dimension rather than an absolute size */
/* will not update element dimension if '%' */
function MDNElementResizeBy(element, deltaWidth, deltaHeight )
{
      if (element)
      {
	    if( element.style ) { element = element.style; }
	    var noPx = document.childNodes ? 'px' : 0;
    	
	    if (element.width && (' '+element.width).indexOf('%')==-1)
	    {
	        var w = parseInt(element.width);
	        w = w + deltaWidth;
	        if (w > 0)
	        {
	            element.width = w + noPx;
	            element.pixelWidth = w;
	        }
	    }
    	
	    if (element.height && (' '+element.height).indexOf('%')==-1)
	    {
	        var h = parseInt(element.height);
	        h = h + deltaHeight;
	        if (h > 0)
	        {
	            element.height = h + noPx;
	            element.pixelHeight = h;
	        }
	    }
	}
}


// general purpose function to return viewable client width */
function MDNGetClientWidth() 
{ 
    if (window.innerWidth)
    {
        return window.innerWidth;
    }  
    else if (document.documentElement && document.documentElement.clientWidth != 0)
    {
        return document.documentElement.clientWidth;
    }
    else if (document.body)
    {
        return document.body.clientWidth;
    }      
    return 0;
}

// general purpose function to return viewable client height
function MDNGetClientHeight()
{
    if (window.self && self.innerHeight)
    {
        return self.innerHeight;
    }
    if (document.documentElement && document.documentElement.clientHeight)
    {
        return document.documentElement.clientHeight;
    }
    if (document.body && document.body.clientHeight)
    {
        return document.body.clientHeight;
    }
    return 0;
}

// gets an elements absolute position and sets expandos for x, y, w, h (cross-browser)
function MDNGetElementAbsolutePos(el) 
{
    var x,w,y,h; 
    if (document.getBoxObjectFor) 
    { 
        x = MDNFireFoxTrueX(el); 
        y = MDNFireFoxTrueY(el); 
        var bo = document.getBoxObjectFor(el); 
        w = bo.width;
        h = bo.height;
    } 
    else if (el.getBoundingClientRect) 
    { 
        var rect = el.getBoundingClientRect(); 
        x = rect.left; 
        w = rect.right - rect.left;
        y = rect.top;
        h = rect.bottom - rect.top;
    } 
    el.x = x;
    el.y = y;
    el.w = w;
    el.h = h;
}

// get X for a control accounting for scrolling
function MDNFireFoxTrueX(ctrl)
{
    var x = ctrl.offsetLeft;
    // add border width
    if (ctrl.style && ctrl.style.borderLeftWidth)
    {
        var w = parseInt(ctrl.style.borderLeftWidth);
        if (!isNaN(w))
        {
            x += w;
        }
    }
    if (window.pageXOffset) {x -= window.pageXOffset;}
    else if (document.body && document.body.scrollLeft) {x -= document.body.scrollLeft;}
    else if (document.documentElement && document.documentElement.scrollLeft) {x -= document.documentElement.scrollLeft;}
    ctrl = ctrl.offsetParent;
    while (ctrl) {x += ctrl.offsetLeft; if (ctrl != document.body && ctrl != document.documentElement && ctrl.scrollLeft) {x -= ctrl.scrollLeft;} ctrl = ctrl.offsetParent;}
    return x;
}

// get Y for a control accounting for scrolling
function MDNFireFoxTrueY(ctrl)
{
    var y = ctrl.offsetTop;
    // add border width
    if (ctrl.style && ctrl.style.borderTopWidth)
    {
        var w = parseInt(ctrl.style.borderTopWidth);
        if (!isNaN(w))
        {
            y += w;
        }
    }
    if (window.pageXOffset) {y -= window.pageYOffset;}
    else if (document.body && document.body.scrollTop) {y -= document.body.scrollTop;}
    else if (document.documentElement && document.documentElement.scrollTop) {y -= document.documentElement.scrollTop;}
    ctrl = ctrl.offsetParent;
    while (ctrl) {y += ctrl.offsetTop; if (ctrl != document.body && ctrl != document.documentElement && ctrl.scrollTop) {y -= ctrl.scrollTop;} ctrl = ctrl.offsetParent;}
    return y;
}


// fades in from start to end opacity by the delta amount (0-transparent, 100 fully opaque)
function MDNFadeIn(objId,startOpac,endOpac,delta) 
{
    obj = document.getElementById(objId);
    if (startOpac<=100&&endOpac<=100&&Math.abs((obj.style.opacity*100)-endOpac)>(delta-1)) 
    {
      MDNSetOpacity(obj, startOpac);
      if (startOpac<endOpac)
      {
        startOpac += delta;
        window.setTimeout("MDNFadeIn('"+objId+"',"+startOpac+","+endOpac+","+delta+")", 100);
      }
    }
}

// fades out from start to end opacity by the delta amount (0-transparent, 100 fully opaque)
function MDNFadeOut(objId,startOpac,endOpac,delta) 
{
    obj = document.getElementById(objId);
    if (startOpac>=0&&endOpac>=0&&Math.abs((obj.style.opacity*100)-endOpac)>(delta-1)) 
    {
      MDNSetOpacity(obj, startOpac);
      if (startOpac>endOpac)
      {
        startOpac -= delta;
        window.setTimeout("MDNFadeOut('"+objId+"',"+startOpac+","+endOpac+","+delta+")", 100);
      }
    }
}

// cross-browser opacity set function
function MDNSetOpacity(obj, opacity) 
{
    opacity = (opacity>=100)?99.99:opacity;
    opacity = (opacity<0)?0:opacity;
    
    obj.style.opacity = opacity/100;
    obj.style.filter = "alpha(opacity:"+opacity+")";
    obj.style.KHTMLOpacity = opacity/100;
    obj.style.MozOpacity = opacity/100;
}


// prevent an event from propagating
function MDNEmptyEvent(event)
{
    event = event | window.event;
    
    if(event!=null)
    {
		event.returnValue=false;
		if(event.preventDefault)
		{
		    event.preventDefault();
	    }
	    if(event.stopPropagation)
	    {
	        event.stopPropagation();
	    }
	    if(typeof(event.cancelBubble)!='undefined')
	    {
	        event.cancelBubble=true;
	    }
    }
	return false;
}