﻿/***********************************************
* TimeHelper: maps coordinates to time cells
*
************************************************/
function TimeHelper(calendar)
{
       var tableGrid = calendar.firstChild.lastChild.previousSibling;
       var tbls  = tableGrid.getElementsByTagName("table");
       var table = null;
       this._tables = new Array();
       var y = 0;
       
       for (var i = 0; i < tbls.length; i++)
       {
          if(tbls[i].getAttribute("dayCellTable"))
          {
             this._tables[y++] = tbls[i];
          }        
       }
       this._timeRuller = document.getElementById("timeRullerTbl" + calendar.id);
       this._tds = this._tables[0].getElementsByTagName("TD");
       this._topY = findPosY(this._tds[0].style.top);       
//       if (IsIE() | IsIE7())
//       {
          this._height = this._tds[this._tds.length - 1].offsetHeight;       
//       }
//       else
//       { 
//          this._height = 17;
//       }
       
       this._bottomY = this._height * this._tds.length + 24; 
       var tableRows = tableGrid.rows; 
       this._scroll = tableRows[tableRows.length - 1].cells[0].firstChild;
       this._offsetHours = tableRows.length == 1 ? 0 : -1;
}

TimeHelper._instance = new Array();
TimeHelper._current = null;

TimeHelper.getInstance = function(calendar) 
{
   if(TimeHelper._instance[calendar.id] == null || TimeHelper._instance[calendar.id]._scroll.parentNode == null)
   {
      TimeHelper._instance[calendar.id] = new TimeHelper(calendar);
   }
   
   TimeHelper._current = TimeHelper._instance[calendar.id];
   return TimeHelper._instance[calendar.id];         
}

TimeHelper.prototype.ReInit = function(calendar)
{
   TimeHelper._instance[calendar.id] = new TimeHelper(calendar.id);
}

TimeHelper.prototype.getTopLimit = function()
{
   return TimeHelper._current._topY;
}

TimeHelper.prototype.getBottomLimit = function()
{   
   return TimeHelper._current._bottomY;
}


TimeHelper.prototype.posToTime = function(y)
{
   var idx = (y) / (2 * TimeHelper._current._height + 1); 
   var h = Math.abs(Math.floor(idx));
   if (h < 0)
   {
       h = 0;
   }
   var m = ((Math.abs(idx)) - h) % 2 >= 0.45 ? ":30" : ":00";
   var time = Math.ceil(h) + m;
   if (time == "24:00")
   {
       return "23:30";
   }
   return time;
}

TimeHelper.prototype.indexToTime = function(y)
{
   var idx = y / 2; 
   if (idx >= 23 )
   {
       idx = 22;  
   }
   
   var m = y % 2 > 0 ? ":30" : ":00";

   
   return idx + m;
}

TimeHelper.prototype.posToDate = function(x)
{
   var dayTable = TimeHelper._current.getDayTblFromX(x);
   return dayTable.getAttribute('date');
}

TimeHelper.prototype.getTimeCellOverPos = function(y)
{
   var idx = Math.ceil(y / TimeHelper._current._height); 
   var topY = idx * TimeHelper._current._height;
   return [ topY, topY + this._height];
}

TimeHelper.prototype.timeToPosY = function(time)
{
   var i = time.indexOf(':');
   
   var minutes = time.substring(i+1, time.length);
   
   var delta = TimeHelper._current._height / 30;
   
    if(minutes >= 30)
   {
      minutes = minutes - 30;
   }
   
   var idx = TimeHelper._current.getIndex(time);
      
   return (idx * (TimeHelper._current._height + 0.5) -1) + minutes * delta;
}

TimeHelper.prototype.timeToPosX = function(parentNode)
{
   return findPosX(parentNode);
}

TimeHelper.prototype.getDate = function( eventElement )
{
   var parent = eventElement.parentNode;

   while( parent != null)
   {
      if( parent.getAttribute("DayTable") != null )
      {
          return parent.getAttribute("date");
      } 

      parent = parent.parentNode;
   }

   return null;
}


TimeHelper.prototype.getMinHeight = function(parentNode)
{
   return TimeHelper._current._tds[0].offsetHeight;
}


TimeHelper.prototype.getIndex = function(time)
{   
   var i = time.indexOf(':');
   
   var minutes = time.substring(i+1, time.length);
  
   var minIndex = 0;
   if( minutes.length > 1)
   {
      if( minutes >= 30)
      {
         minIndex = 1;
      }
   }
   var hours = time.substring(0, i);

   if( hours.indexOf("0") == 0 && hours != "0" )
   {
      hours = time.substring(1,2);
   }   

   return hours * 2 + minIndex;
}


TimeHelper.prototype.getDayTblFromX = function(x)
{
   var rulerTblWidth = TimeHelper._current._timeRuller == null? 0 :TimeHelper._current._timeRuller.offsetWidth ;  
   var dayTblWidth = TimeHelper._current._tables[0].offsetWidth;
   
   var idx = Math.ceil( (x - rulerTblWidth) / dayTblWidth);
   
   if (idx > 7)
   {
      return TimeHelper._current._tables[6];
   }
   else if (idx <=0)
   {
      return TimeHelper._current._tables[0];
   }

   return  TimeHelper._current._tables[idx-1];
}

function createPhantom(element)
{
   var phantom = element.cloneNode(true);
      
   phantom.id = element.id +"_phanton";    
   phantom.style.zIndex = 100;
   if( element.style.width )
   {
      phantom.style.width = element.style.width; 
   }
   else
   {
      phantom.style.width = element.offsetWidth + "px"; 
   }
   
   convertPosition(element,phantom);
   
   if( IsIE() == true)
   {
      phantom.runtimeStyle.filter = "alpha(opacity=20);";
   }
   else 
   {
      phantom.style.MozOpacity = 1/5;
   }
   phantom.style.display = "none";   
   
   return phantom;
}

function adaptToBrowser()
{
   // NOTE(IE BUG) Using css property overflow-y:scroll on iframes in IE7 causes it 
   // to display an empty gap on the right side of the frame. 
   if(IsIE7() == true)
   {
      var spacer = _$('ie7gapFix');
      if( spacer != null)
      {
         spacer.style.width = "36px";
      }
   }
   
   var monthSpacer = _$('idDummySpacer');     
   if(IsIE() != true && monthSpacer!=null)
   {
      monthSpacer.style.display = 'none';
   }

   var frame = document.getElementById('calendarFrame');

   if( monthSpacer != null && frame!=null && 
       frame.contentWindow.document.documentElement.clientWidth < frame.offsetWidth)
   {
      if( IsIE7() == true)
      {
         monthSpacer.style.width = "36px";
      }
      if(IsIE() != true)
      {
         monthSpacer.style.display = 'inline';
      }

   }
}

function setWeekScrollPos(element)
{
   adaptToBrowser();

   if(element == null)
   {
      return;
   }
   
   var frame = _$(element.id);
   if( frame != null )
   {
      frame.contentWindow.scrollBy(0,250);
   }
}

function convertPosition(element, phantom)
{
   if(element == null)
   {
      return;
   }
   
   phantom.style.top = findAbsPosY(element)+"px";
   phantom.style.left = findAbsPosX(element)+"px";
   phantom.style.position = "absolute";
}


function findPosX(element)
{
   var curleft = -2;
    
   if(element != null && element.x)
   {
      curleft += element.x;
   }
   return curleft;
}


function findPosY(element)
{
   var curtop = 0;

   if(element != null && element.y)
   {
      curtop += element.y;
   }
   return curtop;
}
 

function findAbsPosX(element)
{
   var curleft = 0;
    
   if(element.offsetParent != null)
   {
      while(true) 
      {
         curleft += element.offsetLeft;

         if(element.offsetParent == null)
         {
            break;
         }

         element = element.offsetParent;
      }
    }
    else if(element.x)
    {
       curleft += element.x;
    }

   return curleft;
}


function findAbsPosY(element)
{   
   var curtop = 0;// element.cells[2].firstChild.firstChild.cells[0].firstChild.cells[0].clientHeight;
      
   if(element.offsetParent)
   {
      //curtop -= element.cells[1].clientHeight;
      //element = element.offsetParent;
      while(true)
      {
         curtop += element.offsetTop;


         if(!element.offsetParent)
         {
            break;
         }
         element = element.offsetParent;
      }
   }
   else if(element.y)
   {
      curtop += element.y;
   }
   return curtop;
}

 

function getTotalScrollTop(element)
{
   var total = element.scrollTop;
    
   if(element.offsetParent)
   {
      while(true)
      {
         total += element.scrollTop;
         if(!element.offsetParent)
         {
            break;
         }
         element = element.offsetParent;
      }
   }

   return total;
}

function getTotalScrollLeft(element)
{
   var total = element.scrollLeft;
    
   if(element.offsetParent)
   {
      while(true)
      {
         total += element.scrollLeft;
         if(!element.offsetParent)
         {
            break;
         }
         element = element.offsetParent;
      }
   }

   return total;
}

function AbsToRelX(absX, viewPointElement)
{
   var x = findAbsPosX(viewPointElement);
   return absX - x;
}

function AbsToRelY(absY, viewPointElement)
{
   var y = findAbsPosY(viewPointElement);
   return absY - y;
}

function _$(elementID)
{
   var element = window.document.getElementById(elementID);
   if( element == null)
   {
      element = window.parent.document.getElementById(elementID); 
      if(element == null)
      {
         if (  document.getElementById('calendarFrame') != null )
         element = document.getElementById('calendarFrame').contentWindow.document.getElementById(elementID);
      }
   }

   return element;
}

function getFormatDate(date)
{ 
   if (date.length == 4)
   {
      date = "0" + date;
   }
   return date;
}

function addHours(time, hour)
{
   var hours = parseInt(hour) + parseInt(time.substring(0, time.indexOf(':')));
   var minute = time.substring(time.indexOf(':') + 1, time.length);            
   if (hours < 0)
   {
       hours = 0; 
       minute = "00";
   }
   return getFormatDate(hours + ":" + minute);   
}

function getTodayDate()
{ 
   var date = new Date();
   var month = (date.getMonth()+1) < 10 ? "0" + (date.getMonth()+1) : (date.getMonth()+1);
   var day = date.getDate() < 10? "0" + date.getDate() : date.getDate();

   return  date.getYear()+ "/" + month  + "/" + day;
}

function IsIE()
{
   return window.navigator.appVersion.indexOf("MSIE") != -1;
}


function IsIE7()
{
   if( IsIE()!= true)
   {
      return;
   }
 
   return document.documentElement && typeof document.documentElement.style.maxHeight!="undefined";
}


function getDivElements(classnames)
{
    var divs = document.getElementsByTagName("div");
    var calendars = new Array();
    var index = 0;
    for (i = 0; i < divs.length; ++i)
    { 
        if (classnames[divs[i].className] != null)
        {
            calendars[index] = divs[i];
            ++index;
        }
    }
    return calendars;
}

function GetGridForEvent(event)
{
    var grid = (event.target) ? event.target : event.srcElement;
    while (grid != null && grid.id != "idGrid")
    {   
        grid = grid.parentNode;
    }
    return grid;
}

function GetEventGrid(element)
{
   var tables = element.getElementsByTagName("table");
   for (i = 0; i < tables.length; ++i)
   {
      if (tables[i].className != "")
      {
         return tables[i];
      }
   }
   return 
}

function FormatTimeToIndex(time)
{
    var charIdx = time.indexOf(":");
    var hours   = time.substring(0, charIdx);
    var minutes = time.substring(charIdx + 1, time.length);
    var startIndex = hours * 2;

    if(minutes >= 30)
    {
       startIndex++;
    }
    return startIndex;
}

/**********************************************
*  Enumerations
***********************************************/

DaysOfWeek = 
{
   Monday:    1,
   Tuesday:   2,
   Wednesday: 4,
   Thursday:  8,
   Friday:    16,
   Saturday:  32,
   Sunday:    64 
};

Recurrence =
{
   Once:    0,
   Daily:   1,
   Weekly:  2,
   Monthly: 3,
   Yearly:  4
};

Sequence = 
{
   None:   0,
   First:  1,
   Second: 2,
   Third:  3,
   Fourth: 4, 
   Last:   5
};


Months = 
{
   January :  1,
   February:  2,
   March:     3,
   April:     4,
   May:       5,
   June:      6,
   Jule:      7,
   August:    8,
   September: 9,
   October:   10,
   November:  11,
   December:  12
};
