/*
    This file contains javascript methods used by Connect Daily for 
    rubber band selection operations.

    Connect Daily Web Calendar Software

    Copyright 2007, MH Software, Inc. All Rights Reserved
    5023 W 120th Ave #311, Broomfield CO 80020
    +1 303 438 9585

    Last Edit By: "$Author: gsexton $"
    Last Checkin: "$Date: 2008/09/17 20:24:29 $"
    Revision #  : "$Revision: 1.2 $"
*/

var thisBand={};

/**
 * Return the position of a specific object.
 * 
 * @param obj The object to locate.
 * 
 * @result Object with properties xOffset and yOffset containing the position.
 */
function findPos(obj)
{
    var result={
        xOffset:0,
        yOffset:0
    };

    if (obj.offsetParent)
        while (obj) {
            result.xOffset+=obj.offsetLeft;
            result.yOffset+=obj.offsetTop;
            obj = obj.offsetParent;
        }
    else if (obj.x) {
        result.xOffset+=obj.x;
        result.yOffset+=obj.y;
    }
    return result;
}

/**
 * Get the scroll position x/y offset that's in effect for the current viewport.
 * 
 * @return Object with xOffset property and yOffset property containing the 
 * offset values.
 */
function getScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if ( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if ( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if ( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    var result={
        xOffset:scrOfX,
        yOffset:scrOfY
    };
    return result;
}

function setRubberHandlersIE(Obj,enable){
    
    if (enable) {
        Obj.setCapture();
        Obj.attachEvent("onmousemove",sizeRubber);
        Obj.attachEvent("onmouseup",endRubber);
    } else {
        Obj.detachEvent("onmousemove",sizeRubber);
        Obj.detachEvent("onmouseup",endRubber);
        Obj.releaseCapture();
    }
}
function setRubberHandlersGeneric(Obj,enable){
    
    if (enable) {
        Obj.addEventListener("mousemove",sizeRubber,false);
        //   We want the mouse up on the doc so that even if the 
        // mouse is not on the div when the mouseup happens, the
        // event still fires.
        document.addEventListener("mouseup",endRubber,false);
    } else {
        Obj.removeEventListener("mousemove",sizeRubber,false);
        document.removeEventListener("mouseup",endRubber,false);
    }
} 
function setRubberHandlers(Obj,enable) {
    if (Obj.addEventListener) {
        setRubberHandlersGeneric(Obj,enable);
    } else {
        setRubberHandlersIE(Obj,enable);
    }
}

function sizeRubber(event){
    var iCurX,iCurY,iDivX,iDivY,width,height;
    var oScroll=getScrollXY();
    iDivX=thisBand.xAnchor;
    iDivY=thisBand.yAnchor;
    iCurX=event.clientX+oScroll.xOffset;
    iCurY=event.clientY+oScroll.yOffset;

    if (iCurX<thisBand.xAnchor) {
        iDivX=iCurX;
        width=thisBand.xAnchor-iDivX-2;
    } else {
        iDivX=thisBand.xAnchor;
        width=iCurX-iDivX-2;
    }
    if (iCurY<thisBand.yAnchor) {
        iDivY=iCurY;
        height=thisBand.yAnchor-iDivY-2;
    } else {
        iDivY=thisBand.yAnchor;
        height=iCurY-iDivY-2;
    }
    
    with (thisBand.divRubber) {
        style.left=(iDivX-thisBand.parentX)+"px";
        style.top=(iDivY-thisBand.parentY)+"px";
        if (height>0) {
            style.height=height+"px";
        }
        if (width>0) {
            style.width=width+"px";
        }
    }
    thisBand.width=width;
    thisBand.height=height;
}



function endRubber(event){

    setRubberHandlers(thisBand.divParent,false);
    var rubberPos=findPos(thisBand.divRubber);
    thisBand.divParent.removeChild(thisBand.divRubber);
    thisBand.divRubber=null;
    thisBand.xOffset=rubberPos.xOffset;
    thisBand.yOffset=rubberPos.yOffset;
    thisBand.callBackFunction(thisBand);
    thisBand={};
}
/**
 * Start a rubber band selection.
 * 
 * @param obj The ID of the parent object that the rubber band is drawn within.
 * @param callBack The user callback function to invoke when the mouseup event
 * happens. The callback function should accept one argument, and will receive 
 * the thisband object.
 * @param event The event object.
 */
function startRubber(obj,callBack,event){
    thisBand.divParent=document.getElementById(obj);
    thisBand.divRubber=document.createElement("div");
    var parentPos=findPos(thisBand.divParent);
    thisBand.parentX=parentPos.xOffset;
    thisBand.parentY=parentPos.yOffset;
    thisBand.width=0;
    thisBand.height=0;
    thisBand.callBackFunction=callBack;
    var oScroll=getScrollXY();
    with (thisBand.divRubber) {
        id="divRubber";
        className="Rubber";
        thisBand.xAnchor=event.clientX+oScroll.xOffset-2;
        style.left=thisBand.xAnchor-thisBand.parentX;
        thisBand.yAnchor=event.clientY+oScroll.yOffset;
        style.top=thisBand.yAnchor-thisBand.parentY;
        style.width="1px";
        style.height="1px";
    }
    thisBand.divParent.appendChild(thisBand.divRubber);
    setRubberHandlers(thisBand.divParent,true);
}

function stopBubble(event){
	if (event.stopPropagation) {
		event.stopPropagation();
	} else {
		event.cancelBubble=true;
	}
}

