
function MiniCalendar()
{
};


// "Static" Variables
//////////////////////////////////////////////////////////////////////////////////////////////////
//   Variables defined here are used by all minicalendar instances on the page.

// id of the popup div used by all mini calendars.
MiniCalendar.popupDiv = "fb_calSmallGrid_popup";

//////////////////////////////////////////////////////////////////////////////////////////////////


// "Static" Methods
//////////////////////////////////////////////////////////////////////////////////////////////////
//   Functions defined here are used by all minicalendar instances on the page.

MiniCalendar.createMiniCalendar = function(name)
{
    if (!document.getElementById(MiniCalendar.popupDiv))
    {
        MiniCalendar.ie4=document.all;
        MiniCalendar.ns6=document.getElementById&&!document.all;

        if (MiniCalendar.ie4||MiniCalendar.ns6){
            var poppaDiv = document.createElement('div');
            poppaDiv.setAttribute('id', MiniCalendar.popupDiv);
            poppaDiv.setAttribute('class', MiniCalendar.popupDiv);
            document.body.appendChild(poppaDiv);
        }
    }
    var my_calendar = new MiniCalendar();
    my_calendar['name'] = name;
    return my_calendar;
}


// "Object" Variables
//////////////////////////////////////////////////////////////////////////////////////////////////
//    Variables defined here are used as unique copies in each instance of the mini calendar.
//    they are defined with these defaults, and may be modified after being instanciated
//    without effecting other mini calendars.

var BV = parseFloat(navigator.appVersion.indexOf("MSIE")>0?navigator.appVersion.split(";")[1].substr(6):navigator.appVersion);
var BN = navigator.appName;
var IE=(BN.indexOf('Explorer')!=-1);

// tooltip disappear speed onMouseout (in milliseconds)
MiniCalendar.prototype.disappeardelay = 250;

// vertical offset of tooltip from anchor link
MiniCalendar.prototype.vertical_offset = (IE && BV < 7) ? "-7px" : "-7px";
// horizontal offset of tooltip from anchor link
MiniCalendar.prototype.horizontal_offset = (IE && BV < 7) ? "-13px" : "-13px";
// Pixels above cursor box should appear if at bottom of page
MiniCalendar.prototype.reverse_vertical_offset = (IE && BV < 7) ? 13 : 13;
// Pixels to right of cursor box should appear if at right of page
MiniCalendar.prototype.reverse_horizontal_offset = (IE && BV < 7) ? 47 : 47;

// label for the event description
MiniCalendar.prototype.decriptionLabel = "<span class='eventLabel'>Event:&nbsp;</span>";
// label for the datetime
MiniCalendar.prototype.dateTimeLabel = "<span class='eventLabel'><span class='date'>Date</span><span class='date'> &amp; </span><span class='time'>Time</span>:&nbsp;</span>";
// lable for the location
MiniCalendar.prototype.locationLabel = "<span class='eventLabel'>Location:&nbsp;</span>";
// From edge of popup in pixels
MiniCalendar.prototype.arrowIndent = 20;
// Width of arrow background graphic in pixels
MiniCalendar.prototype.arrowWidth = 20;


// "Object" Methods
//////////////////////////////////////////////////////////////////////////////////////////////////

// Opens a window to the FullGridView for this calendar/set of calendars.
MiniCalendar.prototype.goToFullGridView = function()
{
    window.open(this.response_url);
}//end goToFullGridView

//////////////////////////////////////////////////////////////////////////////////////////////////

// Gets the position offset for an object.
MiniCalendar.prototype.getposOffset = function(what, offsettype)
{
    var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
    var parentEl=what.offsetParent;
    while (parentEl!=null){
        totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
        parentEl=parentEl.offsetParent;
    }
    return totaloffset;
}//end getposOffset

//////////////////////////////////////////////////////////////////////////////////////////////////

// Shows or hides the tooltip
MiniCalendar.prototype.showhide = function(obj, e, visible, hidden, tipwidth)
{
    if (MiniCalendar.ie4||MiniCalendar.ns6){
        dropmenuobj.style.left=dropmenuobj.style.top=-500;
    }
    if (tipwidth!=""){
        dropmenuobj.widthobj=dropmenuobj.style;
        dropmenuobj.widthobj.width=tipwidth;
    }
    if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover"){
        obj.visibility=visible;
    }
    else if (e.type=="click"){
        obj.visibility=hidden;
    }
}//end showhide

//////////////////////////////////////////////////////////////////////////////////////////////////

// IE Compatibility test.
MiniCalendar.prototype.iecompattest = function()
{
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}//end iecompattest

//////////////////////////////////////////////////////////////////////////////////////////////////

// Checks if the minicalendar is clear of the browser edge.
MiniCalendar.prototype.clearbrowseredge = function(obj, whichedge)
{
    var edgeoffset=(whichedge=="rightedge")? parseInt(this.horizontal_offset)*-1 : parseInt(this.vertical_offset)*-1;
    if (whichedge=="rightedge"){
        var windowedge=MiniCalendar.ie4 && !window.opera? this.iecompattest().scrollLeft+this.iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15;
        dropmenuobj.contentmeasure=dropmenuobj.offsetWidth;
        if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure){
            edgeoffset=dropmenuobj.contentmeasure - this.reverse_horizontal_offset;
            this.edgeCheck += 1;
        }
    }
    else{
        var windowedge=MiniCalendar.ie4 && !window.opera? this.iecompattest().scrollTop+this.iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18;
        dropmenuobj.contentmeasure=dropmenuobj.offsetHeight;
        if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure){
            edgeoffset=dropmenuobj.contentmeasure + this.reverse_vertical_offset;
            this.edgeCheck += 2;
        }
    }
    return edgeoffset;
}//end clearbrowseredge

//////////////////////////////////////////////////////////////////////////////////////////////////

// Creates the tooltip for display from the events list sent in.
MiniCalendar.prototype.fixedtooltip = function(obj, e, tipwidth, eventsList)
{
    if (window.event){
        event.cancelBubble=true;
    }
    else if (e.stopPropagation){
        e.stopPropagation();
    }
    this.clearhidetip();
    dropmenuobj=document.getElementById? document.getElementById(MiniCalendar.popupDiv) : MiniCalendar.popupDiv;
    popupContents  = '<div id="fb_calSmallGridPopup_arrowTop"><img src="/core/img/spacer.gif" /></div>';
    popupContents += '<div id="fb_calSmallGridPopup_eventInfo">';
    for (var idx=0; idx < eventsList.length; idx++)
    {
        popupContents += '<div id="fb_calSmallGridPopupEventInfo_description_'+idx+'" class="fb_calSmallGridPopupEventInfo_description">'+this.decriptionLabel+eventsList[idx]['title']+'</div>';
        popupContents += '<div id="fb_calSmallGridPopupEventInfo_dateTime_'+idx+'" class="fb_calSmallGridPopupEventInfo_dateTime">';
        popupContents += '<span class="fb_calSmallGridPopupEventInfo_date">'+this.dateTimeLabel+eventsList[idx]['date']+'</span>';
        popupContents += '<span class="fb_calSmallGridPopupEventInfo_time">'+eventsList[idx]['time']+'</span>';
        popupContents += '</div>';
        popupContents += '<div id="fb_calSmallGridPopupEventInfo_location_'+idx+'" class="fb_calSmallGridPopupEventInfo_location">'+this.locationLabel+eventsList[idx]['location']+'</div>';
    }//endfor
    popupContents += '</div>';
    popupContents += '<div id="fb_calSmallGridPopup_arrowBot"><img src="/core/img/spacer.gif" /></div>';
    dropmenuobj.innerHTML=popupContents;

    if (MiniCalendar.ie4||MiniCalendar.ns6){
        this.showhide(dropmenuobj.style, e, "visible", "hidden", tipwidth);
        dropmenuobj.x=this.getposOffset(obj, "left");
        dropmenuobj.y=this.getposOffset(obj, "top");
        this.edgeCheck = 0;
        dropmenuobj.style.left=dropmenuobj.x-this.clearbrowseredge(obj, "rightedge")+"px";
        dropmenuobj.style.top=dropmenuobj.y-this.clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px";

        arrowLeftPos  = this.arrowIndent;
        arrowRightPos = dropmenuobj.offsetWidth - this.arrowIndent - this.arrowWidth;
        switch(this.edgeCheck){
            case 0:
              document.getElementById('fb_calSmallGridPopup_arrowTop').style.backgroundPosition = arrowLeftPos + 'px 0';
              break;
            case 1:
              document.getElementById('fb_calSmallGridPopup_arrowTop').style.backgroundPosition = arrowRightPos + 'px 0';
              break;
            case 2:
              document.getElementById('fb_calSmallGridPopup_arrowBot').style.backgroundPosition = arrowLeftPos + 'px 0';
              break;
            case 3:
              document.getElementById('fb_calSmallGridPopup_arrowBot').style.backgroundPosition = arrowRightPos + 'px 0';
              break;
        } /* end switch */
        if(this.edgeCheck < 2){
            document.getElementById('fb_calSmallGridPopup_arrowBot').style.display = 'none';
            document.getElementById('fb_calSmallGridPopup_arrowTop').style.display = 'block';
        }
        else{
            document.getElementById('fb_calSmallGridPopup_arrowTop').style.display = 'none';
            document.getElementById('fb_calSmallGridPopup_arrowBot').style.display = 'block';
        }
    }
}//end fixedtooltip

//////////////////////////////////////////////////////////////////////////////////////////////////

// Hides the tooltip.
MiniCalendar.prototype.hidetip = function(e)
{
    if (typeof dropmenuobj!="undefined"){
        if (MiniCalendar.ie4||MiniCalendar.ns6){
            dropmenuobj.style.visibility="hidden";
        }
    }
}//end hidetip

//////////////////////////////////////////////////////////////////////////////////////////////////

// Delays the hiding of the tooltip.
MiniCalendar.prototype.delayhidetip = function()
{
    if (MiniCalendar.ie4||MiniCalendar.ns6){
        delayhide=setTimeout(this.name+".hidetip()",this.disappeardelay);
    }
}//end delayhidetip

//////////////////////////////////////////////////////////////////////////////////////////////////

// Clears the hide timeout.
MiniCalendar.prototype.clearhidetip = function()
{
    if (typeof delayhide!="undefined"){
        clearTimeout(delayhide);
    }
}//end clearhidetip

//////////////////////////////////////////////////////////////////////////////////////////////////

// Sets the month selected and calls the server for new data for display.
// Also defines the function that replaces the old data with the new.
MiniCalendar.prototype.selectMonth = function(date_string)
{
    var container = this.name + '_container';
    var calendar_string = document.getElementById(this.name+'_calendars').value;
    var calendar_title = document.getElementById(this.name+'_title').value
    var raw_param_array = {
        'ajaxsvc' : 'mini_calendar_grid',
        'mini_cal' : this.name,
        'selected_month' : date_string,
        'selected_calendars' : calendar_string,
        'title' : calendar_title
        };
    var callback = function(response_status, response_text, response_xml)
    {
        //alert('status: "' + response_status + '", xml: "' + response_xml + '", text: "' + response_text + '"' );
        if (response_status == 200)
        {
            var oldNode = document.getElementById(container);
            if (oldNode)
            {
                oldNode.innerHTML = response_text;
            }
        }
    }
    processAjaxRequest('/Ajax.ashx', raw_param_array, callback, false);
}//end selectMonth

//////////////////////////////////////////////////////////////////////////////////////////////////
