
/**
 *  A collection of Javascript utilities for the Mayan Calendar
 *  Copyright © 2006 God N Locomotive Works & Ivan Van Laningham
 *    All Rights Reserved
 */
var org;
if(!org){
  org={};
} else if(typeof org != "object"){
  throw new Error("org already exists and is not an object!");
}
if(!org.pauahtun){
  org.pauahtun={};
} else if(typeof org.pauahtun != "object"){
  throw new Error("org.pauahtun already exists and is not an object!");
}
if(!org.pauahtun.GodN){
  org.pauahtun.GodN={
    isNav:false,
    isIE:false,
    imageSource:"http://www.pauahtun.org/",
    browserType:function(){
      if (parseInt(navigator.appVersion)>=4) {
        if(navigator.appName=="Netscape") {
          org.pauahtun.GodN.isNav=true;
        } else {
          org.pauahtun.GodN.isIE=true;
        }
      }
    },
    newMail:function(display){
      sps=display.split(' at ');
      if(sps.length<2){
        sps=display.split('@');
      }
      if(sps.length<2){
        document.write(display);
        return
      }
      name=sps[0].replace(' ','');
      domain=sps[1].replace(' ','');
      document.write('<a href=mailto:'+name+'@'+domain+'>'+display+'</a>');
    },
    safemail:function(name,domain,display){
      name=name.replace(' ','');
      domain=domain.replace(' ','');
      displayed=(typeof(display)=="undefined")?name+"@"+domain:display
      document.write('<a href=mailto:'+name+'@'+domain+'>'+displayed+'</a>');
    },
    mayaDate:function(aDate){
      if(aDate!=undefined){
        this.lastmod=0;
        this.lastmoddate=aDate;
      }else{
        this.lastmod=document.lastModified;
        this.lastmoddate=new Date(Date.parse(this.lastmod));
      }
      this.modDate=new Date();
      this.lastYear=this.modDate.getFullYear();  // For copyright purposes.  That is, even if the doc was last modified
                                                 // in 1999, say, we can still print out "Copyright (c) 1998-2006"
      this.modSeconds=this.lastmoddate.getTime();
      this.modSeconds=this.modSeconds/1000.0;
      this.mayaday=Math.round(this.modSeconds);
      this.mayaday/=86400;
      this.mayaday+=2440588;
      this.mayaday=Math.floor(this.mayaday);
      this.mayaday-=584285;
	  this.mayaday+=2;
      this.longcount=new Array(5);
      this.longcount[0]=Math.floor(this.mayaday/144000);
      tmp=this.mayaday-(this.longcount[0]*144000);
      this.longcount[1]=Math.floor(tmp/7200);
      tmp=tmp-(this.longcount[1]*7200);
      this.longcount[2]=Math.floor(tmp/360);
      tmp=tmp-(this.longcount[2]*360);
      this.longcount[3]=Math.floor(tmp/20);
      tmp=tmp-(this.longcount[3]*20);
      this.longcount[4]=Math.floor(tmp);
      this.glord=((this.longcount[3]*2)+this.longcount[4])%9;
      if (this.glord==0) {
        this.glord=9;
      }
      this.haab=(this.mayaday+348)%365;
      if(this.haab<0) {
        this.haab=365+this.haab;
        this.haab%=365;
      }
      this.trecena=(this.mayaday+4)%13;
      if(this.trecena<0) {
        this.trecena=(13+this.trecena)
        this.trecena%-13;
      }
      if(this.trecena==0) {
        this.trecena=13;
      }
      this.veintena=this.mayaday%20; (this.mayaday+19)%20;
      if(this.veintena<0) {
        this.veintena=20+this.veintena;
        this.veintena%=20;
      }
      this.haabmonth=Math.floor(this.haab/20);
      this.haabday=this.haab-(this.haabmonth*20);
      this.fontcolor="";
      this.fontend="";
      this.st8=this.st819();
      this.calRound=0;
      this.tzolkin=0;
      this.p260();
    }

  };
}
org.pauahtun.GodN.mayaDate.prototype.tzolkinDays=["Ajpu","Imox","Iq'","Aq'ab'al","K'at","K'an","Keme",
    "K'ej","Q'anil","Toj","Tz'i","B'atz'","E'","Aj'","Ish",
    "Ta'ikin","Ajmaq","No'j","Tijax","Kawoq"
  ];
org.pauahtun.GodN.mayaDate.prototype.haabMonths=["Pohp","Wo","Sip","Sots","Sek","Xul","Yaxk'in","Mol",
    "Ch'en","Yax", "Sac","Keh", "Mak","K'ank'in", "Muwan","Pax",
    "K'ayab","Kumk'u", "Wayeb"
  ];
                                             //    0   1   2   3   4   5   6   7   8   9  10 11
org.pauahtun.GodN.mayaDate.prototype._l819coefs=[360,648,675,396,549,333,108,522,612,774,738,18];
org.pauahtun.GodN.mayaDate.prototype.fontColor=function(clr){
  this.fontcolor="<font color="+clr+">";
  this.fontend="</font>";
}
org.pauahtun.GodN.mayaDate.prototype.toString=function(){
  return this.fontcolor+this.LC()+"&nbsp;  "+this.CR()+" ["+this.G()+"]"+this.fontend;
}
org.pauahtun.GodN.mayaDate.prototype.LC=function(){
  return this.longcount[0]+"."+this.longcount[1]+"."+this.longcount[2]+"."+this.longcount[3]+"."+this.longcount[4];
}
org.pauahtun.GodN.mayaDate.prototype.G=function(){
  return "G"+this.glord;
}
org.pauahtun.GodN.mayaDate.prototype.CR=function(){
  return this.trecena+" "+this.tzolkinDays[this.veintena]+" "+this.haabday+" "+this.haabMonths[this.haabmonth];
}
org.pauahtun.GodN.mayaDate.prototype.gregorian=function(){
  return this.lastmoddate.toLocaleString();
}
org.pauahtun.GodN.mayaDate.prototype.getFullYear=function(){
  return this.lastmoddate.getFullYear();
}
org.pauahtun.GodN.mayaDate.prototype.st819=function(){
  var l=this.longcount;
  l.reverse()
  var j=0;
  var c=1
  var e=3;        // 819 Day station for 0.0.0.0.0
  for(var i=0;i<l.length;i++){
    if(j==0){
        c=1;
    }else if(j==1){
        c=20;
    }else{
        c=this._l819coefs[(j-2)%12];
    }
    e=(e+(l[i]*c))%819;
    j=j+1;
  }
  l.reverse()
  this.st8=e;
  return e
}
org.pauahtun.GodN.integer=function(f){
  if(f>0.0){
    return Math.floor(f);
  }
  return Math.ceil(f);
}
org.pauahtun.GodN.mayaDate.prototype.julday=function(mm,id,iyyy){
  //"""Arguments:  month, day, year and dayofweek are all integers, and
  //are expected to be in the Gregorian calendar
  //Return value:  Julian day #
  if(mm==undefined){
    mm=this.lastmoddate.getMonth();
    id=this.lastmoddate.getDate();
    iyyy=this.lastmoddate.getFullYear();
  }
  mm=mm+1;
  if(mm>2){
    jy=iyyy;
    jm=mm+1;
  }else{
    jy=iyyy-1;
    jm=mm+13;
  }
  tm=0.0; // Time of day, decimal, for future use.
  jul=org.pauahtun.GodN.integer(Math.floor(365.25*jy)+Math.floor(30.6001*jm)+(id+1720995.0+tm));
  ja=org.pauahtun.GodN.integer(0.01*jy);
  jul=org.pauahtun.GodN.integer(jul+(2-ja+(org.pauahtun.GodN.integer(0.25*ja))));
  return jul;
  var daysleft = 2456283 - jul;
}
org.pauahtun.GodN.modulo=function(x,y) {
  var k = x % y;
  return (k!=0&&(k>0^y>0)&&isFinite(y))?k+y:k;
}
org.pauahtun.GodN.mayaDate.prototype.p260=function(){
    // """Arguments:  integer trecena, v
    // Return value:  integer representing the position in the tzolk'in (a number
    // ranging from 0 to 259) from the given values of the trecena T and
    // veintena v."""
    var c12=-3;
    var m1=13;
    var m2=20;
    var u1=this.trecena-1;
    var u2=this.veintena-1;
    if(u2<0){
      u2=19;
    }
    var m=m1*m2;
    var v1=org.pauahtun.GodN.modulo(u1,m1);
    var v2=org.pauahtun.GodN.modulo(((u2-v1)*c12),m2);
    var u=(v2*m1)+v1;
    this.tzolkin=u;
    return u;
}
org.pauahtun.GodN.frameModified=function(firstYear,copyrighter,mailto,fTitle) {
  wmd=new org.pauahtun.GodN.mayaDate();
  lastYear=wmd.getFullYear();
  if(firstYear==undefined||firstYear=="") {
    firstYear=wmd.getFullYear();
  }
  if(copyrighter==undefined) {
    copyrighter="Your Name Here";
  }
  if(mailto==undefined) {
    mailto="your name at your.domain";
  }
  document.write("<p align=center><font size=1>");
  document.write((fTitle==undefined?"Contents of this frame":fTitle)+"—Revised:<br>"+wmd);
  document.write("<br>("+wmd.gregorian()+")\n<br>");
  if(firstYear!=-1) {
    document.write("Copyright &copy; ");
    if(firstYear==lastYear) {
      document.write(firstYear);
    } else {
      document.write(firstYear,"-",lastYear);
    }
  }
  document.write(" "+copyrighter+"<br>");
  if(firstYear!=-1) {
    document.write("All Rights Reserved<br>");
  }
  org.pauahtun.GodN.newMail(mailto);
  document.write("<br><font size=3></p>\n");
}
org.pauahtun.GodN.mayamodified=function(firstYear,copyrighter,mailto) {
  wmd=new org.pauahtun.GodN.mayaDate();
  lastYear=wmd.getFullYear();
  if(firstYear==undefined||firstYear=="") {
    firstYear=wmd.getFullYear();
  }
  if(copyrighter==undefined) {
    copyrighter="Your Name Here";
  }
  if(mailto==undefined) {
    mailto="your name at your.domain";
  }
  document.write("<p align=center><font size=1>");
  document.write("URL of this page—Revised:<br>"+wmd);
  document.write("<br>("+wmd.gregorian()+")\n<br>");
  if(firstYear!=-1) {
    document.write("Copyright &copy; ");
    if(firstYear==lastYear) {
      document.write(firstYear);
    } else {
      document.write(firstYear,"-",lastYear);
    }
  }
  document.write(" "+copyrighter+"<br>");
  if(firstYear!=-1) {
    document.write("All Rights Reserved<br>");
  }
  org.pauahtun.GodN.newMail(mailto);
  document.write("<br><font size=3></p>\n");
}
org.pauahtun.GodN.additionalMaterials=function(cpr,y,str,em) {
  lastmod=document.lastModified;
  lastmoddate=Date.parse(lastmod);
  var modDate;
  if (lastmoddate == 0){
    modDate=new Date();
  } else {
    modDate=new Date(lastmoddate);
  }
  if((cpr==undefined)||(cpr=="")){
    return;
  }
  if((y==undefined)||(y=="")){
    y=modDate.getFullYear();
  }
  if((str==undefined)||(str=="")){
    str="Additional materials ";
  }
  document.write("<p align=center><font size=1>");
  document.write(str+"Copyright &copy; "+y);
  if(y<modDate.getFullYear()){
    document.write("-"+modDate.getFullYear());
  }
  document.write(" "+cpr+"<br>\n");
    today=new Date();
  document.write("All Rights Reserved<br>");
  if((em!=undefined)&&(em!="")){
    org.pauahtun.GodN.newMail(em);
    document.write("<br>");
  }
  document.write("<font size=3></p>\n");
}
org.pauahtun.GodN.todaysDate=function(){
  today=new org.pauahtun.GodN.mayaDate(new Date());
  document.write(today);
}
