// Window Vars
var topZ = 1;
nextID = 0;
// Mouse methods
var dragObjTitle = null;
var dragOffsetX = 0;
var dragOffsetY = 30;
var dragAdj = 34;
// Fade Vars
var fadeInDelay = 50;
var fadeOutDelay = 50;
var intervalDelay = 500;
var closeDelay = 50;
// Array Vars
var maxArray = 50;          // max controls on page to track status
var idArray = new Array(maxArray);
var mouseArray = new Array(2);
//
var closeOUT;
var closeOVER;
var closeLOCKED;
var int_ID;
//
if (LocalServer) {
    //alert("Local");
    closeOUT = '/BO/tooltips/images/closeout.png';
    closeOVER = '/BO/tooltips/images/closeover.png';
    closeLOCKED = '/BO/tooltips/images/locked.png';
}
else {
    //alert("Online");
    closeOUT = 'http://www.buyeropinions.com/tooltips/images/closeout.png';
    closeOVER = 'http://www.buyeropinions.com/tooltips/images/closeover.png';
    closeLOCKED = 'http://www.buyeropinions.com/tooltips/images/locked.png';
}
//
// Inialize div id array with default values...
initArray();
var IE = document.all?true:false;
if (!IE) {
    // NonIE
    document.addEventListener("mousemove", function(e) { return getMouseXY(e) }, true);
}
else {
    // IE
    document.attachEvent("onmousemove", function(e) { return getMouseXY(e) });
}
//---------------------------------------------------------------------------
// Interval Functions
//---------------------------------------------------------------------------
//  Use the following html to see array contents at will... 
//     <a href="#" onclick="javascript:showArray();">See Array...</a>
//
function initArray() {
    // Init the array
    for (i=0; i <maxArray; i++) {
        idArray[i] = new Array(2)
        idArray[i][0] = ""; // default: control name (empty)
        idArray[i][1] = 0;  // default: interval id is 0
        idArray[i][2] = 0;  // default: unlocked
    } 
}
function showArray() {
    for (i=0; i <maxArray; i++) {
        var output = 'Row ' + String(i) + '= ' + idArray[i][0] + ',' + idArray[i][1] + ',' + idArray[i][2] + '<br>'
        document.write(output);
    }
}
function findDivInSlots(idText) {
    // finds a slot for div tag specified. Return is 1 based array record, NOT 0 
    var slotFound = 0;
    // 1.  See if div already exists in array. If so, use existing slot!
    for (i=0; i<maxArray; i++) {
        // See if this control already exists in idArray
        if (idArray[i][0] == idText) {
            slotFound = i + 1;
            i=maxArray;   // end search                      
        }        
    }
    return slotFound;
}
function cleanSlot(idText) {
    // clears intervalid in array
    var slotFound = findDivInSlots(idText);
    if (slotFound > 0) {
        idArray[slotFound-1][1] = 0;
    }
}
function saveInSlot(idText,iid) {
    // finds an empty slot in array and fills it
    //alert("saveInSlot: " + idText + "," + iid);
    var slotFound = findDivInSlots(idText);
    //alert("[saveInSlot] Found Slot: " + slotFound);
    if (slotFound == 0) {
        // div not found in slot array 
        for (i=0; i<maxArray; i++) {
            if (idArray[i][0] == "") {
                // Found empty slot, save in slot       
                idArray[i][0] = idText;
                idArray[i][1] = iid;
                slotFound = i;
                //alert("Slot Found: " + String(i));
                i=maxArray;   // end search
            }
        }    
    }
    else {
        // div found so store new interval id in preexist slot
        idArray[slotFound-1][1] = iid;
    }
    return slotFound;
    //alert("[saveInSlot] Stored Interval id: " + iid + " in slot: " + String(slotFound));
}
function endInterval(idText) {
    //alert("[endInterval] div: " + idText);
    var returnCode;
    var interval_Id = findIntervalId(idText)
    //alert("found interval id: " + interval_Id);
    if (interval_Id > 0) {
        window.clearInterval(interval_Id);
        cleanSlot(idText);
        returnCode = 0;    	
    }
    return returnCode;
}
function findIntervalId(idText) {
    // find the info for the specified name of div tag
    // changes the logical 0 based to 1 based
    var iId = 0;
    for (i=0; i <maxArray; i++) {
        if (idArray[i][0] == idText) {
            // Found div tag, copy into globals            
            iId = idArray[i][1];
            i = maxArray;          
        }
    }
    return iId;         
}
function lockTooltip(idText, locked) {
    
    var slotFound = findDivInSlots(idText);
    if (slotFound > 0) {    // Slot was found
        idArray[slotFound-1][2] = locked;
        //if (locked==0) alert(idText + " is Unlocked");
        //else alert(idText + " is Locked");
    }
}
function getLockedStatus(idText) {
    var slotStatus = 0;
    var slotFound = findDivInSlots(idText);
    if (slotFound > 0) {    // Slot was found
        slotStatus = idArray[slotFound-1][2]   
    }
    return slotStatus;    
}
//---------------------------------------------------------------------------
// Div Functions
//---------------------------------------------------------------------------
function showTooltip(divId,divSize,canMove,canFade) {
    //alert(divId);
    var idHead = document.getElementById(divId);
    var contentId = document.getElementById(divId+"content");

    if (!idHead) {
        var status;
        var statusText;
        var newdiv1;
        newdiv1 = document.createElement("div");
        newdiv1.id = divId;
        newdiv1.className = "Frame" + String(divSize);
        //alert(String(mouseArray[0]));
        newdiv1.style.left = AddPx(mouseArray[0]+75);
        newdiv1.style.top = AddPx(mouseArray[1]-15);
        if (canMove) {
             closeOUTBtn = closeOUT;
             closeOVERBtn = closeOVER;                    
            newdiv1.title='Click left mouse button and hold down to Drag...';
            newdiv1.style.zIndex = topZ;
            newdiv1.innerHTML = WriteNewDiv('<img id="Img$2" class="Close$3" ' +
                'src="' + closeOUT + '" ' +
                'onmouseover="javascript:toggleIcon(this.id,1);" ' +
                'onmouseout="javascript:toggleIcon(this.id,0);" ' +
                'onclick="javascript:closeTooltip(\'$1\');" ' +        
                'border="false" /> ', divId,nextID,divSize);
            // IE doesn't support addEventListener, so check for its presence
            if (!IE) {
                // firefox, etc.
                newdiv1.addEventListener("mousemove", function(e) { return mouseMove(e,divId) }, true);
                newdiv1.addEventListener("mousedown", function(e) { return mouseDown(e,divSize) }, true);
                newdiv1.addEventListener("mouseup", function(e)   { return mouseUp(e) }, true);
                newdiv1.addEventListener("mouseover", function(e)  { return pauseTimer(e,newdiv1.id) }, true);
                newdiv1.addEventListener("mouseout", function(e)  { return resumeTimer(e,newdiv1.id) }, true);                
            }
            else {
                // IE
                newdiv1.attachEvent("onmousemove", function(e) { return mouseMove(e,divId) });
                newdiv1.attachEvent("onmousedown", function(e) { return mouseDown(e,divSize) });
                newdiv1.attachEvent("onmouseup", function(e) { return mouseUp(e) });
                newdiv1.attachEvent("onmouseover", function(e) { return pauseTimer(e,newdiv1.id) });
                newdiv1.attachEvent("onmouseout", function(e) { return resumeTimer(e,newdiv1.id) });                                               
            }                                 
        }
        else {
            // CANT MOVE TOOLTIP
            //newdiv1.style.cursor='auto';
            newdiv1.style.zIndex = topZ;
            newdiv1.innerHTML = WriteNewDiv('<img id="Img$2" class="Locked$3" ' +
                'src="' + closeLOCKED + '" ' +               
                'onclick="javascript:closeTooltip(\'$1\');" ' +        
                'border="false" /> ', divId,nextID,divSize); 
            // Can't move div but allow pauseTimer if hovered
            // IE doesn't support addEventListener, so check for its presence
            if (!IE) {
                // firefox, etc.
                newdiv1.addEventListener("mouseover", function(e)  { return pauseTimer(e,newdiv1.id) }, true);
                newdiv1.addEventListener("mouseout", function(e)  { return resumeTimer(e,newdiv1.id) }, true);                
            }
            else {
                // IE
                newdiv1.attachEvent("onmouseover", function(e) { return pauseTimer(e,newdiv1.id) });
                newdiv1.attachEvent("onmouseout", function(e) { return resumeTimer(e,newdiv1.id) });                                               
            }        
                                   
        }                  
        document.body.appendChild(newdiv1);
        //
        var newdiv2;
        newdiv2 = document.createElement("div");
        newdiv2.id = divId + "content";
        newdiv2.className = "Content" + String(divSize);
        newdiv2.style.left = AddPx(mouseArray[0]+75);
        newdiv2.style.top = AddPx(mouseArray[1]+19);
        newdiv2.style.zIndex = topZ;
        var contentSource = document.getElementById(divId + "_content");
        if (contentSource) {
            newdiv2.innerHTML = contentSource.innerHTML;
        }        
        if (canMove) {
            if (!IE) {
                // firefox, etc.
                newdiv2.addEventListener("mousedown", function(e) { return contentMouseDown(e) }, true);
                newdiv2.addEventListener("mouseover", function(e)  { return pauseTimer(e,newdiv1.id) }, true);
                newdiv2.addEventListener("mouseout", function(e)  { return resumeTimer(e,newdiv1.id) }, true);
            }
            else {
                // IE
                newdiv2.attachEvent("onmousedown", function(e) { return contentMouseDown(e) }); 
                newdiv2.attachEvent("onmouseover", function(e) { return pauseTimer(e,newdiv1.id) });
                newdiv2.attachEvent("onmouseout", function(e) { return resumeTimer(e,newdiv1.id) });               
            }
        }
        else {
            // Can't move div but allow pauseTimer if hovered
            // IE doesn't support addEventListener, so check for its presence
            if (newdiv2.addEventListener) {
                // firefox, etc.
                newdiv2.addEventListener("mouseover", function(e)  { return pauseTimer(e,newdiv1.id) }, true);
                newdiv2.addEventListener("mouseout", function(e)  { return resumeTimer(e,newdiv1.id) }, true);                
            }
            else {
                // IE
                newdiv2.attachEvent("onmouseover", function(e) { return pauseTimer(e,newdiv1.id) });
                newdiv2.attachEvent("onmouseout", function(e) { return resumeTimer(e,newdiv1.id) });                                               
            }                
        }
        document.body.appendChild(newdiv2);
        
        // Save away the content DIV into the title DIV for 
        // later access, and vice versa
        newdiv1.content = newdiv2;
        newdiv2.titlediv = newdiv1;

        startTooltip(newdiv1.id,newdiv2.id,canFade);
    }
    topZ += 2;
    // If you want you can check when these two are greater than
    // a certain number and then rotate them back to 100,100...
    nextID += 2;     
    if (idHead) {       
        if (idHead.style.visibility=='hidden') {
            idHead.style.zIndex = topZ;
            idHead.style.left = AddPx(mouseArray[0]+75);
            idHead.style.top = AddPx(mouseArray[1]-15);
            //
            contentId.style.zIndex = topZ+1;
            contentId.style.left = AddPx(mouseArray[0]+75);
            contentId.style.top = AddPx(mouseArray[1]+19);  
            startTooltip(divId,divId+"content",canFade);
        }
    }   
}
function ShowMenu(divId,divSize,canFade) {
    //alert(divId);
    var divObject = document.getElementById(divId);
    var divObjectContent = document.getElementById(divId+'content');
    if (divObject && divObjectContent) {
        if (divObject.addEventListener) {
            // firefox, etc.
            divObject.addEventListener("mouseover", function(e)  { return pauseTimer(e,divObject.id) }, true);
            divObject.addEventListener("mouseout", function(e)  { return resumeTimer(e,divObject.id) }, true);
            divObjectContent.addEventListener("mouseover", function(e)  { return pauseTimer(e,divObject.id) }, true);
            divObjectContent.addEventListener("mouseout", function(e)  { return resumeTimer(e,divObject.id) }, true);                            
        }
        else {
            // IE
            divObject.attachEvent("onmouseover", function(e) { return pauseTimer(e,divObject.id) });
            divObject.attachEvent("onmouseout", function(e) { return resumeTimer(e,divObject.id) });                                               
            divObjectContent.attachEvent("onmouseover", function(e) { return pauseTimer(e,divObject.id) });
            divObjectContent.attachEvent("onmouseout", function(e) { return resumeTimer(e,divObject.id) });                                               
        }
        if (mouseArray[0]+20 > document.body.clientWidth-100) {
            //alert("Too Far: " + mouseArray[0]+20);            
            divObject.style.left = AddPx(mouseArray[0]-250);            
            topZ = 9999999;
        }
        else {
            topZ = 9999999;
            divObject.style.left = AddPx(mouseArray[0]+20);
        }        
        divObject.style.top = AddPx(mouseArray[1]-18);
        divObject.style.zIndex = topZ;
        startTooltip(divObject.id,divObjectContent.id,canFade);                            
    }
    topZ += 2;
    nextID += 2;     
}
function Tooltip(divId,divSize,canFade) {
    //alert(divId);
    var divObject = document.getElementById(divId);
    var divObjectContent = document.getElementById(divId+'content');
    if (divObject && divObjectContent) {
        if (divObject.addEventListener) {
            // firefox, etc.
            divObject.addEventListener("mouseover", function(e)  { return pauseTimer(e,divObject.id) }, true);
            divObject.addEventListener("mouseout", function(e)  { return resumeTimer(e,divObject.id) }, true);
            divObjectContent.addEventListener("mouseover", function(e)  { return pauseTimer(e,divObject.id) }, true);
            divObjectContent.addEventListener("mouseout", function(e)  { return resumeTimer(e,divObject.id) }, true);                            
        }
        else {
            // IE
            divObject.attachEvent("onmouseover", function(e) { return pauseTimer(e,divObject.id) });
            divObject.attachEvent("onmouseout", function(e) { return resumeTimer(e,divObject.id) });                                               
            divObjectContent.attachEvent("onmouseover", function(e) { return pauseTimer(e,divObject.id) });
            divObjectContent.attachEvent("onmouseout", function(e) { return resumeTimer(e,divObject.id) });                                               
        }
        //alert("Left Coord:" + mouseArray[0]+20 + "  Screen: " + document.body.clientWidth);
        if (mouseArray[0]+20 > document.body.clientWidth-100) {
            //alert("Too Far: " + mouseArray[0]+20);            
            divObject.style.left = AddPx(mouseArray[0]-250);            
            topZ = 9999999;
        }
        else {
            divObject.style.left = AddPx(mouseArray[0]+20);
            topZ = 9999999;
        }
        divObject.style.top = AddPx(mouseArray[1]-100);
        divObject.style.zIndex = topZ;
        startTooltip(divObject.id,divObjectContent.id,canFade);
    }
    topZ += 2;
    nextID += 2;     
}
//---------------------------------------------------------------------------
// Tooltip Functions
//---------------------------------------------------------------------------
function startTooltip(idText1,idText2,canFade) {
    //fadeMenu(idText1,idText2,canFade);
    endInterval(idText1);
    endInterval(idText2);
    var command =  'delayIn("' + String(idText1) + '","' + String(idText2) + '",' + String(canFade) + ')';
    // this works... command = 'alert("Hello");';
    //alert(command);
    window.clearInterval(int_ID);   
	int_ID = window.setInterval(command, 300);
}
function delayIn(idText1,idText2,canFade) {
	//alert("openTooltip: " + idText);
	endInterval(idText1);
	endInterval(idText2);
	window.clearInterval(int_ID);
	var idHead = document.getElementById(idText1);
	if (idHead) {
        idHead.style.visibility = 'visible';
        idHead.style.display = 'block';
        idHead.style.filter = 'alpha(opacity=0)';
    }
	idHead = document.getElementById(idText2);
	if (idHead) {
        idHead.style.visibility = 'visible';
        idHead.style.display = 'block';
        idHead.style.filter = 'alpha(opacity=0)';
    }    
    if (canFade) { 
        opacity(idText1, 0, 100, fadeInDelay); 
        opacity(idText2, 0, 100, fadeInDelay); 
    }
	else { 
	    opacity(idText1, 99, 100, fadeInDelay); 
	    opacity(idText2, 0, 100, fadeInDelay); 
	}
}
function OLDstartTooltip(idText,canFade) {
	//alert("openTooltip: " + idText);
	endInterval(idText);
	var idHead = document.getElementById(idText);
	if (idHead) {
        idHead.style.visibility = 'visible';
        idHead.style.display = 'block';
        idHead.style.filter = 'alpha(opacity=0)';
    }
    if (canFade) { opacity(idText, 0, 100, fadeInDelay); }
	else { opacity(idText, 99, 100, fadeInDelay); }
}
function hideTooltip(idText,canFade) {
    //alert("hideTooltip: " + idText);
    endInterval(idText);
    window.clearInterval(int_ID);    
    var command =  'delayHideTooltip("' + String(idText) + '",' + String(canFade) + ')';
    // this works... command = 'alert("Hello");';
    //alert(command);   
	var interval_Id = window.setInterval(command, intervalDelay);
	//alert("IntervalId: " + interval_Id);
	var slotId = saveInSlot(idText,interval_Id);
	//alert("[hideTooltip] Array Slot: " + String(slotId) + " = Interval Id: " + String(idArray[slotId][1]));
}
function HideMenu(idText,canFade) {
    //alert("hideTooltip: " + idText);
    endInterval(idText); 
    window.clearInterval(int_ID);   
    var command =  'delayHideTooltip("' + String(idText) + '",' + String(canFade) + ')';
    // this works... command = 'alert("Hello");';
    //alert(command);   
	var interval_Id = window.setInterval(command, intervalDelay);
	//alert("IntervalId: " + interval_Id);
	var slotId = saveInSlot(idText,interval_Id);
	//alert("[hideTooltip] Array Slot: " + String(slotId) + " = Interval Id: " + String(idArray[slotId][1]));
}
function delayHideTooltip(idText,canFade) {
    //alert("[delayHideTooltip] Div Name: " + idText + " Fade: " + canFade);
    var idHead = document.getElementById(idText);
    var idContent = document.getElementById(idText + "content");
    var slotStatus = getLockedStatus(idText);
    //alert("Slot Status: " + slotStatus);
    if (slotStatus == 0) {
        if (canFade) { 
            if (idContent) { opacity(idContent.id,100,0,fadeOutDelay); }
            if (idHead) { opacity(idHead.id,100,0,fadeOutDelay); }
        }
        else {
            if (idContent) { opacity(idContent.id,1,0,fadeOutDelay); }
            if (idHead) { opacity(idHead.id,1,0,fadeOutDelay); }
        }    	
    }
	endInterval(idHead.id);
}
function pauseTimer(ev,idText) {     
    //alert("pauseTimer: " + idText);
    var idContent = document.getElementById(idText+"content"); 
    if (idContent) {  
        idContent.style.backgroundColor = '#FFFFFF';
        endInterval(idText); 
    }
}
function resumeTimer(ev,idText) { 
    //alert("resumeTimer: " + idText);
    var idContent = document.getElementById(idText+"content");
    if (idContent) {   
        idContent.style.backgroundColor = '#EEEEEE';    
        hideTooltip(idText); 
    }
}
function closeTooltip(idText) {
    //alert("[closeTooltip] div: " + idText);
	var idHead = document.getElementById(idText);
	if (idHead) {
        idHead.style.visibility = 'hidden';
        idHead.style.display = 'none';
        idHead.style.filter = 'alpha(opacity=0)';
    }
    //
    var idContent = document.getElementById(idText + "content"); 
    if (idContent) { 
        idContent.style.visibility = 'hidden';
        idContent.style.display = 'none';
        idContent.style.filter = 'alpha(opacity=0)';
    }
    //        
    lockTooltip(idText, 0);
    var exitCode = endInterval(idText);
    if (exitCode==1) { alert("Can't close interval"); }
}
//---------------------------
function toggleIcon(idObject, icon) {
    //alert("ID: " + idObject.id + " ICON: " + icon);
    var ctrl = document.getElementById(idObject)
    if (icon == 0) {ctrl.src = closeOUT;}
    else {ctrl.src = closeOVER;}
}
//---------------------------------------------------------------------------
// Mouse Functions
//---------------------------------------------------------------------------
function getMouseXY(evt) {
    var coords = {left:0, top:0};
    if (evt.pageX) {
        // NON-IE
        coords.left = evt.pageX;
        coords.top = evt.pageY;
    }
    else if (evt.clientX) {
        // IE
        if (document.body) {
            coords.left = evt.clientX + document.body.scrollLeft - document.body.clientLeft;
            coords.top = evt.clientY + document.body.scrollTop - document.body.clientTop;
            // include html element space, if applicable
            if (document.body.parentElement && document.body.parentElement.clientLeft) {
                var bodParent = document.body.parentElement;
                coords.left += bodParent.scrollLeft - bodParent.clientLeft;
                coords.top += bodParent.scrollTop - bodParent.clientTop;
            }            
        }
    }
    mouseArray[0] = coords.left;
    mouseArray[1] = coords.top;     
    //return coords;
}    
function contentMouseDown(e) {
    // Move the window to the front
    // Use a handy trick for IE vs FF
    var dragContent = e.srcElement || e.currentTarget;
    if ( ! dragContent.id.match("Content1")) {  //TODO
        dragContent = findParentTagById(dragContent, "Content1");   //TODO
    }
    if (dragContent) {
        dragContent.style.zIndex = topZ;
        dragContent.titlediv.style.zIndex = topZ;
        topZ++;
    }
}
function mouseDown(e, divSize) {
    // These first two lines are written to handle both FF and IE
    var curElem = e.srcElement || e.target;
    var dragTitle = e.currentTarget || findParentDiv(curElem);
    if (dragTitle) {
        if (dragTitle.className != 'Frame' + String(divSize)) {
            return;
        }
    }    
    // Start the drag, but first make sure neither is null
    if (curElem && dragTitle) {    
        // Attach the document handlers. We don't want these running all the time.
        addDocumentHandlers(true);    
        // Move this window to the front.
        dragTitle.style.zIndex = topZ;
        dragTitle.content.style.zIndex = topZ;
        topZ++;    
        // Check if it's the button. If so, don't drag.
        if (curElem.className != "Close" + String(divSize)) {           
            // Save away the two objects
            dragObjTitle = dragTitle;
            
            // Calculate the offset
            dragOffsetX = e.clientX - dragTitle.offsetLeft;
            dragOffsetY = e.clientY - dragTitle.offsetTop;
                
            // Don't let the default actions take place
            if (e.preventDefault) {
                e.preventDefault();
            }
            else {
                document.onselectstart = function () { return false; };
                e.cancelBubble = true;
                return false;
            }
        }
    }        
}
function mouseMove(e,idText) {
    // If not null, then we're in a drag
    if (dragObjTitle) {
        //alert(idText);
        lockTooltip(idText, 1);                      
        if (!e.preventDefault) {
            // This is the IE version for handling a strange
            // problem when you quickly move the mouse
            // out of the window and let go of the button.
            if (e.button == 0) {
                finishDrag(e);
                return;
            }
        }    
        dragObjTitle.style.left = AddPx(e.clientX - dragOffsetX);
        dragObjTitle.style.top = AddPx(e.clientY - dragOffsetY);
        dragObjTitle.content.style.left = AddPx(e.clientX - dragOffsetX);
        dragObjTitle.content.style.top = AddPx(e.clientY - dragOffsetY + dragAdj);
        if (e.preventDefault) {
            e.preventDefault();
        }
        else {
            e.cancelBubble = true;
            return false;
        }  
    }
}
function mouseUp(e) {
    if (dragObjTitle) {
        finishDrag(e);
    }
}
function finishDrag(e) {
    var finalX = e.clientX - dragOffsetX;
    var finalY = e.clientY - dragOffsetY;
    if (finalX < 0) { finalX = 0 };
    if (finalY < 0) { finalY = 0 };

    dragObjTitle.style.left = AddPx(finalX);
    dragObjTitle.style.top = AddPx(finalY);
    dragObjTitle.content.style.left = AddPx(finalX);
    dragObjTitle.content.style.top = AddPx(finalY + dragAdj);
    
    // Done, so reset to null
    dragObjTitle = null;
    addDocumentHandlers(false);
    if (e.preventDefault) {
        e.preventDefault();
    }
    else {
        document.onselectstart = null;
        e.cancelBubble = true;
        return false;
    }
}
//---------------------------------------------------------------------------
// Handler Functions
//---------------------------------------------------------------------------
function addDocumentHandlers(addOrRemove) {
    if (addOrRemove) {
        if (!IE) {
            // firefox, etc.
            document.addEventListener("mousedown", function(e) { return mouseDown(e) }, true);
            document.addEventListener("mousemove", function(e) { return mouseMove(e) }, true);
            document.addEventListener("mouseup", function(e) { return mouseUp(e) }, true);
        }
        else {
            // IE
            document.onmousedown = function() { mouseDown(window.event) } ;
            document.onmousemove = function() { mouseMove(window.event) } ;
            document.onmouseup = function() { mouseUp(window.event) } ;
        }
    }
    else {
        if (!IE) {
            // firefox, etc.
            remove.addEventListener("mousedown", function(e) { return mouseDown(e) }, true);
            remove.addEventListener("mousemove", function(e) { return mouseMove(e) }, true);
            remove.addEventListener("mouseup", function(e) { return mouseUp(e) }, true);
        }
        else {
            // IE
            // Be careful here. If you have other code that sets these events,
            // you'll want this code here to restore the values to your other handlers,
            // rather than just clear them out.
            document.onmousedown = null;
            document.onmousemove = null;
            document.onmouseup = null;
        }
    }
}
//---------------------------------------------------------------------------
// Core Functions
//---------------------------------------------------------------------------
function WriteNewDiv() {
    len = arguments.length;
    if (len == 0) { return; }
    if (len == 1) { return arguments[0]; }
    
    var result;
    var regexstr;
    var replstr;
    var formatstr;
    var re;
    
    result = "";
    regexstr = "";
    replstr = "";
    formatstr = arguments[0];
    
    for (var i=1; i<arguments.length; i++) {
        replstr += String(i+100) + arguments[i]  + String(i + 100);
        regexstr += String(i+100) + "(.*)" + String(i+100);
    }
    re = new RegExp(regexstr);
    var result;
    result = replstr.replace(re, formatstr);
    return result;
}
function AddPx(num) {
    return String(num) + "px";
}

function findParentDiv(obj) {
    while (obj) {
        if (obj.tagName.toUpperCase() == "DIV") {
            return obj;
        }
        
        if (obj.parentElement) {
            obj = obj.parentElement;
        }
        else {
            return null;
        }
    }
    return null;
}
function findParentTagById(obj, parentname) {
    while (obj) {
        if (obj.id.match(parentname)) {
            return obj;
        }
        
        if (obj.parentElement) {
            obj = obj.parentElement;
        }
        else {
            return null;
        }
    }
    return null;
}
//---------------------------------------------------------------------------
// Fade in and out functions for Tooltips
//---------------------------------------------------------------------------
function opacity(idObject, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;
	var object = document.getElementById(idObject).style; 
	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + idObject + "',1)",(timer * speed));
			timer++;				
		}
	} 
	else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++) {
			setTimeout("changeOpac(" + i + ",'" + idObject + "',0)",(timer * speed));
			timer++;
		}
	}
}
//change the opacity for different browsers
function changeOpac(opacity, idObject, direction) {
	var object = document.getElementById(idObject).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
    if (direction==1 && object.opacity==0) {
        // direction=1 when fading out... 
        object.display = 'none';
        object.visibility = 'hidden';
    }	
}
function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	//make image transparent
	changeOpac(0, imageid, 0);
	//make new image
	document.getElementById(imageid).src = imagefile;
	//fade in image
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "',0)",(timer * speed));
		timer++;
	}
}
function currentOpac(idObject, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;	
	//if the element has an opacity set, get it
	if(document.getElementById(idObject).style.opacity < 100) {
		currentOpac = document.getElementById(idObject).style.opacity * 100;
	}
	//call for the function that changes the opacity
	opacity(idObject, currentOpac, opacEnd, millisec)
}
function shiftOpacity(idObject, millisec) {
	//if an element is invisible, make it visible, else make it invisible
	if(document.getElementById(idObject).style.opacity == 0) {
		opacity(idObject, 0, 100, millisec);
	} else {	
		opacity(idObject, 100, 0, millisec);
	}
}
