var bIE4 = (navigator.appVersion.indexOf("MSIE 4")>0)
var g_ActiveWidget = null
function RectPosition(el,sWhich) {
var iPos= el["offset" + sWhich]
while (el.offsetParent!=null) {
el = el.offsetParent
iPos+=el["offset" + sWhich]
}
return iPos
}
function Rect_Recalc() {
this.top = RectPosition(this.el,"Top")
this.left = RectPosition(this.el,"Left")
this.right = this.left + this.el.offsetWidth
this.bottom = this.top + this.el.offsetHeight
return this
}
function Rect(el) {
this.el = el
this.recalc()
}
Rect.prototype.recalc = Rect_Recalc
function Rect_Init(rect) {
rect.top = rect.left = 9999999
rect.right = rect.bottom = 0
}
function Region_Recalc() {
Rect_Init(this.rect)
var intRectCount  = this.rectList.length
for (var i=0;i<intRectCount;i++)  {
this.rectList[i].recalc()
Rect_Extents(this.rectList[i],this.rect)
}
}
function Region(elTarget) {
this.type = "Region"
this.rectList = new Array
this.rect = new Object
Rect_Init(this.rect)
this.target = elTarget
}
Region.prototype.addRect = Region_AddRect
Region.prototype.recalc = Region_Recalc
function Region_AddRect(objRect) {
this.rectList[this.rectList.length] = objRect
Rect_Extents(objRect,this.rect)
}
function Rect_Extents(objSrcRect,objDestRect) {
if ((objSrcRect.top<objDestRect.top)) objDestRect.top = objSrcRect.top
if ((objSrcRect.right>objDestRect.right)) objDestRect.right = objSrcRect.right
if ((objSrcRect.bottom>objDestRect.bottom)) objDestRect.bottom = objSrcRect.bottom
if ((objSrcRect.left<objDestRect.left)) objDestRect.left = objSrcRect.left
}
function RegionList_AddRegion(objRegion) {
if (objRegion.type=="Region") {
this.regions[this.regions.length] = objRegion
Rect_Extents(objRegion.rect,this)
}
}
function RegionList_Recalc() {
Rect_Init(this)
var intRegionCount = this.regions.length
for (var i=0;i<intRegionCount;i++) {
var objRegion = this.regions[i]
objRegion.recalc()
Rect_Extents(objRegion.rect,this)
}
this.scrollHeight = document.body.scrollHeight
this.scrollWidth = document.body.scrollWidth
}
function RegionList_Dump() {
var w = window.open()
for (var i=0;i<this.regions.length;i++) {
w.document.write("Total Rects: " + this.regions[i].rectList.length + "<BR>")
w.document.write(this.regions[i].rect.top + ":" + this.regions[i].rect.left + ":" + this.regions[i].rect.right + ":" + this.regions[i].rect.bottom + "<HR>")
}
}
function RegionList_FindTargetRegion(intTop,intLeft) {
var intRegions = this.regions.length;
for (var i=0;i<intRegions;i++) {
var objRegion = this.regions[i]
if ((intTop>=objRegion.rect.top) && (intTop<=objRegion.rect.bottom) && (intLeft>=objRegion.rect.left) && (intLeft<=objRegion.rect.right))
return objRegion
}
return null
}
function RegionList() {
this.regions = new Array()
Rect_Init(this)
this.scrollHeight = document.body.scrollHeight
this.scrollWidth = document.body.scrollWidth
}
RegionList.prototype.addRegion = RegionList_AddRegion
RegionList.prototype.findTargetRegion = RegionList_FindTargetRegion
RegionList.prototype.recalc = RegionList_Recalc
RegionList.prototype.dump = RegionList_Dump
function Widget_CheckTarget() {
var iLeft = this.src.offsetLeft + this.hotspotX
var iTop = this.src.offsetTop + this.hotspotY
var objTarget=null
if ((!((iTop<this.regionList.top) || (iTop>this.regionList.bottom))) && (!((iLeft<this.regionList.left) || (iLeft>this.regionList.right))) &&
!((this.lastTarget) && (iTop>=this.lastTarget.rect.top) && (iTop<=this.lastTarget.rect.bottom) && (iLeft>=this.lastTarget.rect.left) && (iLeft<=this.lastTarget.rect.right)))  {
objTarget = this.regionList.findTargetRegion(iTop,iLeft)
if ((objTarget) && (((this.lastTarget) && (this.lastTarget.target!=objTarget.target)) || (!this.lastTarget))) {
if ((this.lastTarget) && (this.onrelease))
this.onrelease(this,this.lastTarget)
if (this.onacquire)
this.onacquire(this,objTarget)
}
if (!objTarget)
if ((this.lastTarget) && (this.onrelease))
this.onrelease(this,this.lastTarget)
this.lastTarget = objTarget
}
return objTarget
}
function Widget_Recalc() {
this.regionList.recalc()
if (this.onrecalc)
this.onrecalc(this)
this.checkTarget()
}
function Widget_Show(intTop,intLeft) {
if (intTop) this.src.style.pixelTop = intTop
if (intLeft) this.src.style.pixelLeft = intLeft
this.src.style.display = "block"
this.visible = true
this.recalc()
if (this.onend)
this.onend(this,this.lastTarget)
}
function Widget_Hide() {
if ((this.lastTarget) && (this.onrelease))
this.onrelease(this,this.lastTarget)
this.lastTarget=null
if ((this.onhide) && (this.visible))
this.onhide()
this.visible=false
this.src.style.display = "none"
}
function Widget_StopEvent() {
event.cancelBubble=true
return true
}
function Widget_CenterAt(iTop,iLeft) {
if (iTop) {
if (this.hotspotX==0)
this.src.style.pixelTop = iTop - (this.src.offsetHeight/2)
else
this.src.style.pixelTop = iTop - this.hotspotY
}
if (iLeft)
if (this.hotspotY==0)
this.src.style.pixelLeft = iLeft - (this.src.offsetWidth/2)
else
this.src.style.pixelLeft = iLeft - this.hotspotX
if (bIE4)  {
g_ActiveWidget = this
setTimeout("Widget_FinishMove()",0)
}
else {
this.checkTarget()
if (this.onend)
this.onend(this,this.lastTarget)
}
}
function Widget_FinishMove() {
if (g_ActiveWidget) {
g_ActiveWidget.checkTarget()
if (g_ActiveWidget.onend)
g_ActiveWidget.onend(g_ActiveWidget,g_ActiveWidget.lastTarget)
}
}
function Widget_DisableDrag() {
wDrag.stopDrag = true
}
function Widget_EnableDrag() {
wDrag.stopDrag = false
}
function Widget_CancelEvent() {
return false
}
function Widget(objRegions,objWidget,hotspotX,hotspotY,fAcquire,fRelease,fonHide,fonstart,fonend,fonrecalc) {
this.regionList = objRegions
this.src = objWidget
this.visible = false
this.lastTarget=null
for (var i=0;i<objWidget.all.length;i++) {
var elCheck = objWidget.all[i]
if ((elCheck.tagName=="INPUT") || (elCheck.tagName=="BUTTON") || (elCheck.tagName=="A") || (elCheck.tagName=="SELECT")) {
elCheck.onmousemove =  elCheck.onselectstart = Widget_StopEvent
elCheck.onfocus = Widget_DisableDrag
elCheck.onblur = Widget_EnableDrag
elCheck.widget = this
}
if ((elCheck.className=="allowDrag") || (elCheck.useMap)) elCheck.widget = this
}
objWidget.widget = this
this.lastTarget = null
this.hotspotX = hotspotX
this.hotspotY = hotspotY
this.onacquire = fAcquire
this.onrelease = fRelease
this.onhide = fonHide
this.onstart = fonstart
this.onend = fonend
this.onrecalc = fonrecalc
}
Widget.prototype.show = Widget_Show
Widget.prototype.hide = Widget_Hide
Widget.prototype.recalc = Widget_Recalc
Widget.prototype.checkTarget = Widget_CheckTarget
Widget.prototype.centerAt = Widget_CenterAt
function widgetCheckTarget() {
if ((!wDrag.stopDrag) && (event.button==1)) { // Mouse button is down
if (!wDrag.src) {  // Check if drag is starting
var elWidget = parentWidget(event.srcElement)
if (elWidget) {
if ((elWidget) && (elWidget.onstart))
elWidget.onstart(elWidget,elWidget.lastTarget)
wDrag.src=elWidget.src
wDrag.offsetX = event.clientX +document.body.scrollLeft - RectPosition(elWidget.src,"Left")
wDrag.offsetY = event.clientY +document.body.scrollTop - RectPosition(elWidget.src,"Top")
if ((elWidget.regionList.scrollHeight != document.body.scrollHeight) || (elWidget.regionList.scrollWidth != document.body.scrollWidth))
elWidget.recalc()
if (document.body.setCapture)
document.body.setCapture()
}
}
else {
with (wDrag.src) {
var newLeft = event.clientX + document.body.scrollLeft - wDrag.offsetX
if (newLeft  < widget.regionList.left- widget.hotspotX) newLeft= widget.regionList.left - widget.hotspotX
else if (newLeft > widget.regionList.right - widget.hotspotX) newLeft = widget.regionList.right - widget.hotspotX
style.pixelLeft = newLeft
var newTop = event.clientY + document.body.scrollTop - wDrag.offsetY
if (newTop < widget.regionList.top - widget.hotspotY) newTop=widget.regionList.top - widget.hotspotY
else if (newTop > widget.regionList.bottom - widget.hotspotY) newTop=widget.regionList.bottom - widget.hotspotY
style.pixelTop = newTop
var newScrollTop = offsetTop-document.body.clientHeight+ offsetHeight
if (offsetTop+offsetHeight>document.body.scrollTop + document.body.clientHeight)
document.body.scrollTop = offsetTop-document.body.clientHeight+ offsetHeight
else if (offsetTop<document.body.scrollTop)
document.body.scrollTop = offsetTop
if (offsetLeft+offsetWidth>document.body.scrollLeft + document.body.clientWidth)
document.body.scrollLeft = offsetLeft-document.body.clientWidth+ offsetWidth
else if (offsetLeft<document.body.scrollLeft)
document.body.scrollLeft = offsetLeft
var objTarget = widget.checkTarget()
}
}
}
else
if (wDrag.src)
widgetStopDrag()
return true
}
var wDrag = new Object // Used to track the widget being dragged
wDrag.stopDrag = false
function Widget_Offset(elWidget,el,sWhich) {
var iPos= el["offset" + sWhich]
while (el!=elWidget) {
iPos+=el["offset" + sWhich]
el = el.offsetParent
}
return iPos
}
function widgetStopDrag() {
if (wDrag.src)  {
if (document.body.releaseCapture)
document.body.releaseCapture()
if ((event.type=="mouseup") && (wDrag.src.widget) && (wDrag.src.widget.onend))
wDrag.src.widget.onend(wDrag.src.widget,wDrag.src.widget.lastTarget)
}
wDrag.src = null
}
function BuildColumnTargets(elTable,intStart) {
var objRegions = new RegionList()
var objList = new Array()
if (intStart==null) intStart=0
if (elTable.tagName=="TABLE") {
for (var j=intStart;j<elTable.rows[0].cells.length;j++) {
elTable.all.tags("COL")[j]._index = j
objList[j] = new Region(elTable.all.tags("COL")[j])
objList[j].addRect(new Rect(elTable.rows[0].cells[j]))
}
for (var j=intStart;j<elTable.rows[elTable.rows.length-1].cells.length;j++)
objList[j].addRect(new Rect(elTable.rows[elTable.rows.length-1].cells[j]))
for (var i=intStart;i<objList.length;i++)
objRegions.addRegion(objList[i])
}
return objRegions
}
function BuildRowTargets(elTable) {
var objRegions = new RegionList()
var objList
if (elTable.tagName=="TABLE")
for (var i=1;i<elTable.rows.length;i++) {
objList = new Region(elTable.rows[i])
objList.addRect(new Rect(elTable.rows[i]))
objRegions.addRegion(objList)
}
return objRegions
}
function parentWidget(el) {
while ((el) && (el.widget==null))
el = el.parentElement
return el ? el.widget : null
}
document.onmousemove = widgetCheckTarget
document.onmouseup = widgetStopDrag
