﻿
function _getInt(str,i,len)
{
  return str.substring(i, i+len);
}


function GetDT(TheDT)
{
  if(TheDT == null || TheDT=="")
    return new Date();
  var pos = 0;
  var year = _getInt(TheDT,pos,4);
  pos+=4;
  if(TheDT.charAt(pos) == '-')
    pos++;
  var month = _getInt(TheDT,pos,2);
  pos+=2;
  if(TheDT.charAt(pos) == '-')
    pos++;
  var day = _getInt(TheDT,pos,2);
  pos+=2;
  if(TheDT.charAt(pos) == 'T')
    pos++;
  var hour = _getInt(TheDT,pos,2);
  pos+=2;
  if(TheDT.charAt(pos) == ':')
    pos++;
  var min = _getInt(TheDT,pos,2);
  pos+=2;
  if(TheDT.charAt(pos) == ':')
    pos++;
  var sec = _getInt(TheDT,pos,2);

//  alert(TheDT+" > "+year + " " + month + " " + day + " " + hour + " " + min + " " + sec);  
  if(hour == 0 && min == 0 && sec == 0)
    return new Date(year, month-1, day);
  else
      return new Date(year,month-1,day,hour,min,sec);
}

function MicroFormatObjectBinding(displayDiv, microFormatObject, inactiveStyleClassName, activeStyleClassName)
{
    var webClip;
    var self = this;
    
    this.updateDisplayAndWebClipData = function()
    {
        //alert(displayDiv.id);
        webClip = new LiveClipboardContent();
        //displayDiv.innerHTML = microFormatObject.HTML;
        if(displayDiv != null && displayDiv.id == "calendarHcal1")
        {
            if(microFormatObject.Summary != null)
            {
              document.forms["eventform"].eventtitle.value = microFormatObject.Summary;
            }
            if(microFormatObject.DtStart != null)
            {
              var TheDate = GetDT(microFormatObject.DtStart);
              document.forms["eventform"].eventstart.value = TheDate.toLocaleString();
            }
            if(microFormatObject.DtEnd != null)
            {
              var TheDate = GetDT(microFormatObject.DtEnd);
              document.forms["eventform"].eventend.value = TheDate.toLocaleString();
            }
        }
        webClip.data.formats[0] = new DataFormat();
        webClip.data.formats[0].type = microFormatObject.formatType;
        webClip.data.formats[0].contentType = "application/xhtml+xml";
        webClip.data.formats[0].items = new Array(1);
        webClip.data.formats[0].items[0] = new DataItem();
        webClip.data.formats[0].items[0].xmlData = microFormatObject.xmlData;
    }
    
    this.onActive = function()
    {
        if (displayDiv.childNodes[0] && (displayDiv.childNodes[0].className == microFormatObject.formatRootClassName))
            displayDiv.className = activeStyleClassName;
    }
    
    this.onInactive = function()
    {
        displayDiv.className = inactiveStyleClassName;
    }
    
    this.onCopy = function()
    {     
        return webClip;
    }
    
    this.onPaste = function(clipData)
    {
        for (var i = 0; i < clipData.data.formats.length; i++)
        {
            if ((clipData.data.formats[i].contentType = "application/xhtml+xml") && (clipData.data.formats[i].type == microFormatObject.formatType) && (clipData.data.formats[i].items.length > 0) && (clipData.data.formats[i].items[0].xmlData))
            {
                microFormatObject.initFromXml(clipData.data.formats[i].items[0].xmlData);
                self.updateDisplayAndWebClipData();
                
                // For now, just look at the first microFormatObject found.
                return;
            }
        }
        
        // No matching formats found -- this could mean cut/delete -- so clear the associated data.
        microFormatObject.clearProps();
        self.updateDisplayAndWebClipData();
    }            
    
    self.updateDisplayAndWebClipData();
}

