// sjcDHTML.js

//====================
detect = new function() {
//====================
	//-----------
	// properties
	//-----------
  this.userAgent = (navigator!=null) ? navigator.userAgent.toLowerCase() : "";
	//-----------------------------------
  // minimal broadphase browser detects
	//-----------------------------------
	this.msie = /msie/.test( this.userAgent ) && !/opera/.test( this.userAgent );

	this.webkit = /webkit/.test( this.userAgent );
	this.safari = /safari\//.test( this.userAgent );
  this.chrome = /chrome\//.test( this.userAgent );

  this.opera = /opera/.test( this.userAgent );

  this.mozilla = /mozilla/.test( this.userAgent ) && !/(compatible|webkit)/.test( this.userAgent );
  this.gecko = /gecko\//.test( this.userAgent ) && !/(compatible|webkit)/.test( this.userAgent );
  this.firefox = /firefox\//.test( this.userAgent );
  this.netscape = /navigator\//.test( this.userAgent );
}


// Create a DHTML layer
function createLayer(name, inleft, intop, width, height, visible, content) {
  document.writeln('<div id="' + name + '" style="position:absolute; overflow:hidden; left:' + inleft + 'px; top:' + intop + 'px; width:' + width + 'px; height:' + height + 'px;' + '; z-index:1; visibility:' + (visible ? 'visible;' : 'hidden;') +  '">');
  document.writeln(content);
  document.writeln('</div>');
}

// get the layer object called "name"
function getLayer(name) {
	var theObj = document.getElementById(name);
	if (theObj!=null) {
		return theObj.style
	 }  else {
	    return(null);
	 }
}


function isVisible(name) {
	var layer = getLayer(name);
	if (!layer) return false;
  return ((layer.visibility == "show") || (layer.visibility == "visible"));
}


function moveLayer(name, x, y) {		
	var layer = getLayer(name);
	if (layer) {
		layer.left = x + "px";
		layer.top  = y + "px";
	}
}

function setLayerBackgroundColor(name, color) {		
 	var layer = getLayer(name);
	if (layer) layer.backgroundColor = color;
}

function hideLayer(name) {		
	var layer = getLayer(name);		
  if (layer) layer.visibility = "hidden";
}

function showLayer(name) {		
	var layer = getLayer(name);		
	if (layer) layer.visibility = "visible";
}


function clipLayer2(name, clipleft, cliptop, clipright, clipbottom) {		
	var layer = getLayer(name);		
	if (layer) layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
}

function clipLayer(name, clipleft, cliptop, clipright, clipbottom) {		
	var layer = getLayer(name);
	if (!layer) return;
	var newWidth = clipright - clipleft + 1;
	var newHeight = clipbottom - cliptop + 1;
	layer.height = newHeight;
	layer.width	= newWidth;
	layer.top	= cliptop  + "px";
	layer.left	= clipleft + "px";
}



function replaceLayerContent(name, content) {
	var theObj = document.getElementById(name);
	if (theObj!=null) {	  
		theObj.innerHTML = content;	
	}
}


function toggleOVMap() {
	ovmap.visible = !ovmap.visible;
	if (ovmap.visible) {
		putExtentOnOVMap();
		showLayer("ovLayer");
	} else {
		hideLayer("ovLayer");
    hideLayer("ovzoombox");
	}
	var layer = getLayer("theTop");
	if (layer!=null) layer.cursor = theCursor;
}


function putExtentOnOVMap() {
  var ppmx = i2Width / ovextent.width; // pixels per map x
  var ppmy = i2Height / ovextent.height; // pixels per map y
	var vleft = (extent.minx - ovextent.minx) * ppmx;
	var vright = (extent.maxx - ovextent.minx) * ppmx;
	var vtop = (ovextent.maxy - extent.maxy) * ppmy;
	var vbottom = (ovextent.maxy - extent.miny) * ppmy;
  clipLayer("ovzoombox", vleft, vtop, vright, vbottom);
  showLayer("ovzoombox");
}

function boxIt(theLeft,theTop,theRight,theBottom) {
  clipLayer("zoombox", theLeft, theTop, theRight, theBottom);
  showLayer("zoombox");
}

function resizeAllDHTML(force) {
  var mWidth = getMapWidth();
  var mHeight = getMapHeight();
  if (!force)
  	if ((document.theImage.width == mWidth) && (document.theImage.height == mHeight))
    	return false;
  iWidth = mWidth; //document.theImage.width;
  iHeight = mHeight; //document.theImage.height;
  moveLayer("theTop",0,0);
  clipLayer2("theTop",0,0,iWidth,iHeight);
  pixel.width = iWidth;
  pixel.height = iHeight;
  moveLayer("theMap",0,0);
  clipLayer2("theMap",0,0,iWidth,iHeight);
  theImage.width = iWidth;
  theImage.height = iHeight;
  var loadBannerLeft = parseInt((mWidth - 150)/2);
  var loadBannerTop = parseInt((mHeight - 18)/2);
  moveLayer("LoadMap",loadBannerLeft,loadBannerTop);
  moveLayer("LoadData",loadBannerLeft,loadBannerTop);
  return true;
}

function showRetrieveData() { showLayer("LoadData"); }
function hideRetrieveData() { hideLayer("LoadData"); }
function showRetrieveMap() { showLayer("LoadMap"); }
function hideRetrieveMap() { hideLayer("LoadMap"); }

function getMapWidth () {
	var mapFrameWidth = window.innerWidth;
	if (mapFrameWidth == null) {
		mapFrameWidth = window.document.body.clientWidth;
	}
	return mapFrameWidth;
}

function getMapHeight () {
	var mapFrameHeight = window.innerHeight;
	if (mapFrameHeight == null) {
		mapFrameHeight = window.document.body.clientHeight;
	}
	return mapFrameHeight;
}

function setCursorURL(divLayer,custom,builtin) {
	var dObj = getLayer(divLayer);
	if (dObj!=null) {
    // IE7,IE8,FF3 all correctly support custom url cursors
    // Safari4,Chrome1.0.154.65 support url cursors, but don't respect the hotspot (so are unusable)
    // Opera9.6 doesn't support
    //
    if (detect.msie || detect.firefox) {
      dObj.cursor = "url(cursors/" + custom + ".cur), " + builtin;
    } else {
    	dObj.cursor = builtin;
    }
    theCursor = dObj.cursor;
	}
}

// reposition map image after a pan
function resetPanImage(){
	moveLayer("theMap",0,0);
	clipLayer2("theMap",0,0,iWidth,iHeight);
  window.setTimeout('showLayer("theMap");',100);
  document.theImage.onload = null;
}
