var offsetfromcursorX=0; //Customize x offset of tooltip
var offsetfromcursorY=0; //Customize y offset of tooltip

var offsetdivfrompointerX=10; //Customize x offset of tooltip DIV relative to pointer image
var offsetdivfrompointerY=14; //Customize y offset of tooltip DIV relative to pointer image. Tip: Set it to (height_of_pointer_image-1).

document.write('<div id="dhtmltooltip"></div>'); //write out tooltip DIV

//write out divs to hold pointers
document.write('<div id="upper_left_arrow">upperleft</div>');
document.write('<div id="upper_right_arrow">upperright</div>');
document.write('<div id="lower_left_arrow">lowerleft</div>');
document.write('<div id="lower_right_arrow">lowerright</div>');
//getting ie and ns6 variables
var ie=document.all;
var ns6=document.getElementById && !document.all;

//initially don't want to show
var enabletip=false;
var needposition=false;
if (ie||ns6) {
        var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : "";
}
var pointerobj;

function ietruebody(){
        return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

//function that actually enables the tooltip
function ddrivetip(thetext, thewidth, thecolor){
        if (ns6||ie){
                if (typeof thewidth!="undefined") {
                        tipobj.style.width=thewidth+"px";
                }
                if (typeof thecolor!="undefined" && thecolor!="") {
                        tipobj.style.backgroundColor=thecolor;
                }
                tipobj.innerHTML=thetext;
                enabletip=true;
                needposition=true;
                return false;
        }
}

//event called on mouse movement
function positiontip(e){
        //if tip is enabled
        if (enabletip && needposition){
                //hide old arrow
                pointerobj.style.visibility="hidden";

                //get current position
                var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
                var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;

                //Find out how close the mouse is to the corner of the window
                var winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20;
                var winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight-20;

                //get edge information
                var rightedge=ie&&!window.opera? winwidth-event.clientX-offsetfromcursorX : winwidth-e.clientX-offsetfromcursorX;
                var bottomedge=ie&&!window.opera? winheight-event.clientY-offsetfromcursorY : winheight-e.clientY-offsetfromcursorY;
                var leftedge=(offsetfromcursorX<0)? offsetfromcursorX*(-1) : -1000;

                //variable to see if right edge has been reached
                var right=false;

                //if the horizontal distance isn't enough to accomodate the width of the context menu
                if (rightedge<tipobj.offsetWidth){
                        //move the horizontal position of the menu to the left by it's width
                        tipobj.style.left=curX-tipobj.offsetWidth+"px";
                        right = true;

                        //place on right side of tip object
                        pointerobj.style.left=curX-offsetfromcursorX-(2*offsetdivfrompointerX)+"px";
                }
                else if (curX<leftedge) {
                        tipobj.style.left="5px";
                }
                else{
                        //position the horizontal position of the menu where the mouse is positioned
                        tipobj.style.left=curX+offsetfromcursorX-offsetdivfrompointerX+"px";

                        //place on left side of tip object
                        pointerobj.style.left=curX+offsetfromcursorX+"px";
                }

                //if bottom needs to be displayed
                if (bottomedge<tipobj.offsetHeight){
                        //initial height for tip object
                        tipobj.style.top=curY-tipobj.offsetHeight-offsetfromcursorY+"px";

                        //if right display lower right, otherwise display left
                        if(right) {
                                pointerobj=document.all? document.all["lower_right_arrow"] : document.getElementById? document.getElementById("lower_right_arrow") : "";
                        }
                        else {
                                pointerobj=document.all? document.all["lower_left_arrow"] : document.getElementById? document.getElementById("lower_left_arrow") : "";
                        }

                        //account for shadow if IE
                        if(ie) {
                                //comment out if not using shadow in IE
                                pointerobj.style.top=curY-offsetdivfrompointerY-5+"px";
                        }
                        //account for 3 pixels lost by drawing tip for FF
                        else {
                                pointerobj.style.top=curY-offsetdivfrompointerY+3+"px";
                        }
                }
                //otherwise display upper ones
                else{
                        //initial height for tip object
                        tipobj.style.top=curY+offsetfromcursorY+offsetdivfrompointerY+"px";

                        //if right, display upper right, if not, display left
                        if(right) {
                                pointerobj=document.all? document.all["upper_right_arrow"] : document.getElementById? document.getElementById("upper_right_arrow") : "";
                        }
                        else {
                                pointerobj=document.all? document.all["upper_left_arrow"] : document.getElementById? document.getElementById("upper_left_arrow") : "";
                        }

                        //upper position of pointer object
                        pointerobj.style.top=curY+offsetfromcursorY+"px";
                }

                //match background color of pointer to div
                var kids = pointerobj.childNodes;
                for(var i=0; i<kids.length; i++) {
                        //if ie style...
                        if(tipobj.currentStyle) {
                                kids[i].style.backgroundColor = tipobj.currentStyle['backgroundColor'];
                        }
                        //otherwise mozilla?
                        else if(window.getComputedStyle) {
                                if(kids[i].className != 'arrow_tip') {
                                        kids[i].style.backgroundColor = document.defaultView.getComputedStyle(tipobj,null).getPropertyValue('background-color');
                                }
                        }
                        else {
                                //other style...
                        }
                }
                //set all to visible
                tipobj.style.visibility="visible";
                pointerobj.style.visibility="visible";

        needposition = false;
        }
}

//hides tooltip
function hideddrivetip(){
        if (ns6||ie){
                //disable tip
                enabletip=false;
                needposition=false;

                //hide all objects
                tipobj.style.visibility="hidden";
                pointerobj.style.visibility="hidden";
                tipobj.style.left="-1000px";
                tipobj.style.backgroundColor=''
                tipobj.style.width=''
                tipobj.style.height=''
                tipobj.style.textAlign=''
        }
}

//function initially draws arrows using only CSS
function draw_arrows()
  {
  var div = document.getElementById('upper_left_arrow');
  var h = '';
  var end = 15;

  //upper left pointer
  if(!ie)
    {
    //initial tip for FF aarow
    h += "<DIV class='arrow_tip' style='width: 1px;'></DIV>";
    h += "<DIV class='arrow_tip' style='width: 2px;'></DIV>";
    h += "<DIV class='arrow_tip' style='width: 3px;'></DIV>";
    end = 12;
    }

  //printing out main triangle
  for(var i=0; i<end; i++)
    {
    h += "<DIV class='arrow' style='width: " + (i) + "px;'>";
    h += "</DIV>";
    }
  div.innerHTML = h;

        //lower left pointer
        div = document.getElementById('lower_left_arrow');
        h = '';

        //main triangle
        for(var i=end; i>0; i--) {
                h += "<DIV class='arrow' style='width: " + (i) + "px;'>";
                h += "</DIV>";
        }

        if(!ie) {
                //tip for FF arrow
                h += "<DIV class='arrow_tip' style='width: 3px;'></DIV>";
                h += "<DIV class='arrow_tip' style='width: 2px;'></DIV>";
                h += "<DIV class='arrow_tip' style='width: 1px;'></DIV>";
        }
        div.innerHTML = h;

        //lower right pointer
        div = document.getElementById('lower_right_arrow');
        h = '';

        //main triangle
        for(var i=end; i>0; i--) {
                h += "<DIV class='arrow' style='border-left-width: 2px; border-right-width: 1px; width: " + (i) + "px;'>";
                h += "</DIV>";
        }

        if(!ie) {
                //tip for FF arrow
                h += "<DIV class='arrow_tip' style='width: 3px;'></DIV>";
                h += "<DIV class='arrow_tip' style='width: 2px;'></DIV>";
                h += "<DIV class='arrow_tip' style='width: 1px;'></DIV>";
        }
        div.innerHTML = h;

        //upper right pointer
        div = document.getElementById('upper_right_arrow');
        h = '';
        if(!ie) {
                //tip for FF arrow
                h += "<DIV class='arrow_tip' style='width: 1px;'></DIV>";
                h += "<DIV class='arrow_tip' style='width: 2px;'></DIV>";
                h += "<DIV class='arrow_tip' style='width: 3px;'></DIV>";
        }

        //main triangle
        for(var i=0; i<end; i++) {
                h += "<DIV class='arrow' style='border-left-width: 2px; border-right-width: 1px; width: " + (i) + "px;'>";
                h += "</DIV>";
        }
        div.innerHTML = h;

        //initially select upper left
        pointerobj=document.all? document.all["upper_left_arrow"] : document.getElementById? document.getElementById("upper_left_arrow") : ""
}

//initially draw out arrows
draw_arrows();

//set onmousemove event to position tip
document.onmousemove=positiontip;

// Ajax stuff
var isWorking = false;
/* use these two for my calendar link tooltip */
function cal_request(event_date) {
        if (!isWorking && cal_http) {
                document.getElementById('dhtmltooltip').style.height = '';
                document.getElementById('dhtmltooltip').style.textAlign = 'left';
                cal_http.open('get', 'http://www.enjoybirmingham.com/get_event.php?t=' + event_date);
                cal_http.onreadystatechange = handle_cal_response;
                isWorking = true;
                cal_http.send(null);
                return '<em><b>Please wait...</b><br>Retrieving today\'s events.</em>';
          }
        else
          {
          if (!cal_http)
            {
            return '<em><b>Something bad happened</b></em>';
            }
          else
            {
            return '<em><b>Something poop happened</b></em>';
            }
          }
}

function handle_cal_response()
  {
  if(cal_http.readyState == 4)
    {
    if (cal_http.responseText.indexOf('invalid') == -1)
      {
      var response = cal_http.responseText;
      var tooltipEl = document.getElementById('dhtmltooltip');
      tooltipEl.innerHTML = '<div class="bubble_box"><div class="bubble_title">' +
                            '<a name="close" onclick="hideddrivetip();">' +
                            '<img src="rsc/img/close_up.gif" title="close" alt="close">' +
                            '</a>' +
                            '</div>\n' +
                            response +
                            '</div>';
     needposition = true;

     isWorking = false;
     }
    }
}

/* primary function to create request object */
function create_cal_RequestObject() {
        var ro;
        var browser = navigator.appName;
        if (browser == "Microsoft Internet Explorer") {
                ro = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
                ro = new XMLHttpRequest();
        }
        return ro;
}

var cal_http = create_cal_RequestObject();