﻿function CalendarJS() {
  this.now = new Date();
  this.dayname = ["日","一","二","三","四","五","六"];
  this.monthname = ["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"];
  this.dayspermonth = [31,28,31,30,31,30,31,31,30,31,30,31];
  this.tooltip = ["前一月","後一月"];
  this.parEl = null;
  this.ev = null;
  this.mb_id = null;
  
  this.init = function( id , mbid ,iyy , imm) {
    this.mb_id = mbid;
    //this.date = this.now.getDate();
    this.month = this.mm = this.now.getMonth();
    this.year = this.yy = this.now.getFullYear();
    if (parseInt(iyy))this.yy= parseInt(iyy);
    if (parseInt(imm))this.mm= parseInt(imm) -1;
    this.parEl = document.getElementById( id );
    this.get_event(this.yy,this.mm);
  }
  
  this.removeElements = function( Obj ) {
    for (var i= Obj.childNodes.length -1 ; i>=0 ; i--)
    {
      Obj.removeChild(Obj.childNodes[i]);
    }  
    return Obj;
  }
    
  this.show = function() {
    this.parEl = this.removeElements( this.parEl );
    
    this.parEl.appendChild( this.createHeadYear() );
    this.parEl.appendChild( this.createHeadMonth() );
    
    var table = document.createElement("table");
    table.width="100%";
    table.border="0";
    table.cellpadding="0";
    table.cellspacing="0";
    table.className = "date";
    table.appendChild( this.createTableHead() );
    table.appendChild( this.createTableBody() );
    this.parEl.appendChild( table );
  }

  this.createHeadYear = function() {
    var d = document.createElement("div");
    d.id = "year";
    var g = document.createElement("img");
    g.src = "images/arrRedL.gif";
    g.Instanz = this;
    g.onclick = function() { this.Instanz.switchYear("prev"); };
    try { g.style.cursor = "pointer"; } catch(e){ g.style.cursor = "hand"; }
    d.appendChild(g);

    var a = document.createElement("a");
    if (this.mb_id == "")
      a.href = "newevent_01index.asp?yyyy=" + this.yy 
    else  
      a.href = "blog_eventlist.asp?mb_id=" + this.mb_id + "&yyyy=" + this.yy
    a.appendChild(document.createTextNode( " " + this.yy + " " ));
    d.appendChild(a);
    
    var g = document.createElement("img");
    g.src = "images/arrRedR.gif";
    g.Instanz = this;
    g.onclick = function() { this.Instanz.switchYear("next"); };
    try { g.style.cursor = "pointer"; } catch(e){ g.style.cursor = "hand"; }
    d.appendChild(g);      
    
    return d;
  }

  this.createHeadMonth = function() {
    var d = document.createElement("div");
    d.id = "month";
    var g = document.createElement("img");
    g.src = "images/arrRedL.gif";
    g.Instanz = this;
    g.onclick = function() { this.Instanz.switchMonth("prev"); };
    try { g.style.cursor = "pointer"; } catch(e){ g.style.cursor = "hand"; }
    d.appendChild(g);
    

    var a = document.createElement("a");
    if (this.mb_id == "")
      a.href = "newevent_01index.asp?yyyy=" + this.yy + "&mm=" + (this.mm+1)
    else  
      a.href = "blog_eventlist.asp?mb_id=" + this.mb_id + "&yyyy=" + this.yy + "&mm=" + (this.mm+1)
    a.appendChild(document.createTextNode( " " + this.monthname[this.mm] + " " ));
    d.appendChild(a);

    
    var g = document.createElement("img");
    g.src = "images/arrRedR.gif";
    g.Instanz = this;
    g.onclick = function() { this.Instanz.switchMonth("next"); };
    try { g.style.cursor = "pointer"; } catch(e){ g.style.cursor = "hand"; }
    d.appendChild(g);      
    
    return d;
  }

  
  this.createTableHead = function() {
    var thead = document.createElement("thead");
    tr = document.createElement('tr');
    for (var i=0; i<this.dayname.length; i++)
    {
      tr.appendChild( this.getCell("th", this.dayname[i] , null) );
    }  
    thead.appendChild(tr);
    return thead;
  }
  
  this.createTableBody = function() {
    var sevendaysaweek = 0;
    var begin = new Date(this.yy, this.mm, 1);
    var firstday = begin.getDay();
    if ((this.yy%4==0) && ((this.yy%100!=0) || (this.yy%400==0)))
      this.dayspermonth[1] = 29;
    else  
      this.dayspermonth[1] = 28; 
    
    var tbody = document.createElement("tbody");
    var tr = document.createElement('tr');
    var c=1;
    

    //補月初空格
    for (var i=0; i<firstday; i++, sevendaysaweek++)
      tr.appendChild( this.getCell( "td", " " , null ) );

    for (var i=1; i<=this.dayspermonth[this.mm]; i++, sevendaysaweek++){
      if (this.dayname.length == sevendaysaweek){
        tbody.appendChild( tr );
        tr = document.createElement('tr');
        c++;
        sevendaysaweek = 0;
      }
      //if (i==this.date && this.mm==this.month && this.yy==this.year && (sevendaysaweek == 5 || sevendaysaweek == 6))
      //  tr.appendChild( this.getCell( "td",i, null ) );     // today & 六or日
      //else if (i==this.date && this.mm==this.month && this.yy==this.year)
      //  tr.appendChild( this.getCell( "td",i, null) );      // today
      //else if (sevendaysaweek == 5 || sevendaysaweek == 6)  // 六or日
      //  tr.appendChild( this.getCell( "td",i null ) );
      //else
      //  tr.appendChild( this.getCell( "td",i, null ) );
      
      //if (i==this.date && this.mm==this.month && this.yy==this.year)
      //  tr.appendChild( this.getCell( "td",'['+i+']', null) );      // today
      //else
      
      if(this.ev == undefined || this.ev[i] == undefined)
      {
        tr.appendChild( this.getCell( "td", i , null ) );
      }
      else  
      {  
        var ti="";
        var aday = this.ev[i];
        
        for(var j=0; j<aday.length ; j++)
        {
          //ae.mb_id
          //ae.mb_nn
          //ae.be_sn
          //ae.be_ti //標題
          //ae.be_dt //日期
          //ae.be_tm //時間
          var ae = aday[j];
          //alert(ae.be_ti);
          ti += (j+1) + "." + ae.be_ti + " "
        }

        if(ti=="")
        {
          tr.appendChild( this.getCell( "td", i , null ) );
        }  
        else
        {  
          if (this.mb_id == "")
            var s = "<a href='newevent_01index.asp?yyyy=" + this.yy + "&mm=" + (this.mm+1) + "&dd="+ i + "' title='" + ti + "' >" + i + "</a>";
          else
            var s = "<a href='blog_eventlist_day.asp?mb_id=" + this.mb_id + "&be_dt=" + this.yy+"/"+(this.mm+1)+"/"+ i + "' title='" + ti + "' >" + i + "</a>";
          tr.appendChild( this.getCell2( "td", s , null ) );
        }  
      }  
      
    }

    //補月底空格
    for (var i=sevendaysaweek; i<this.dayname.length; i++)
      tr.appendChild( this.getCell( "td", " ", null  ) );
    tbody.appendChild( tr );
      
    //補月底空格
    while (c < 6 ) 
    { 
      var tr = document.createElement('tr'); 
      c++;
      for(i=0;i<7;i++)
        tr.appendChild( this.getCell( "td", "　" , null ) );
      tbody.appendChild(tr);
    }  

    return tbody;
    
  }
  
  this.getCell = function(tag, str, cssClass) {
    var El = document.createElement( tag );
    El.appendChild(document.createTextNode(str));
    El.align = "center";
    if (cssClass != null)
      El.className = cssClass;
    return El;
  }
  
  this.getCell2 = function(tag, str, cssClass) {
    var El = document.createElement( tag );
    El.innerHTML=str;
    if (cssClass != null)
      El.className = cssClass;
    return El;
  }  
  
  this.switchMonth = function( s ){
    switch (s) {
      case "prev": 
        this.yy = (this.mm == 0)?this.yy-1:this.yy;
        this.mm = (this.mm == 0)?11:this.mm-1;
      break;
      
      case "next":
        this.yy = (this.mm == 11)?this.yy+1:this.yy;
        this.mm = (this.mm == 11)?0:this.mm+1;
      break;
    }
    this.get_event(this.yy,this.mm);
  }
  
  this.switchYear = function( s ){
    switch (s) {
      case "prev": 
        this.yy = (this.yy == this.year - 10)?this.yy:this.yy-1;
      break;
      
      case "next":
        this.yy = (this.yy == this.year + 10)?this.yy:this.yy+1;
      break;
    }
    this.get_event(this.yy,this.mm);
  }  


  this.get_event = function(yy,mm)
  {
    var dt = new Date();
    sUrl = 'get_event.asp?mb_id='+ this.mb_id + '&yyyy='+yy+'&mm='+(mm+1)+'&t=' + dt.getTime();
    var xmlhttp = false;
    xmlhttp = (window.XMLHttpRequest)?new window.XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.open("GET", sUrl,false);
    xmlhttp.send(null);

    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
    {
      var json = xmlhttp.responseText;
      this.ev = eval ("(" + json + ")");
      this.show();
    }
    else
    {
      alert('伺服器錯誤 ' + xmlhttp.status );
    }
  }  

}

