/*
 showPreviewDay = function(){
  t=arguments[0];
  pd=document.getElementById('previewDay');
  pd.innerHTML=t.parentNode.parentNode.getElementsByTagName('div')[1].innerHTML;
  pd.style.display='block';
  pd.style.left=(t.parentNode.parentNode.offsetLeft+15)+'px';
 }

*/
function Dia( dia, mes )
{
  this.date = new Date();
  this.link = null;
  this.image = null;
  this.title = null;
  this.headline = null;
  
  this.container = document.getElementById("cal").getElementsByTagName("ul")[0];
  this.preview = document.getElementById("previewDay");
  this.li = document.createElement("li");
  
  this.date.setFullYear( 2009 );
  this.date.setMonth( mes - 1 );
  this.date.setDate( dia );
  this.date.setHours(0);
  this.date.setMinutes(0);
  this.date.setSeconds(0);
  
  this.getDataFromJSON = function()
  {
    if( typeof days != "undefined" )
    {
      for( var i in days )
      {
        if( days[i].date == this.getStringDate() )
        {
          this.link = days[i].link;
          this.image = days[i].image;
          this.title = days[i].title;
          this.headline = days[i].headline;
          break;
        }
      }
    }
  }
  
  this.getTypeDay = function()
  {
    var weekDays = new Array();
    weekDays[0] = { "name" : "domingo", "smallName" : "dom" };
    weekDays[1] = { "name" : "segunda", "smallName" : "seg" };
    weekDays[2] = { "name" : "terca", "smallName" : "ter" };
    weekDays[3] = { "name" : "quarta", "smallName" : "qua" };
    weekDays[4] = { "name" : "quinta", "smallName" : "qui" };
    weekDays[5] = { "name" : "sexta", "smallName" : "sex" };
    weekDays[6] = { "name" : "sabado", "smallName" : "sab" };
    
    return weekDays[ this.date.getDay() ];
  }
  
  this.setClassLi = function()
  {
    addClass( this.li, this.getTypeDay().name );
  }
  
  this.getStringDate = function()
  {
    var day = (this.date.getDate().toString().length==1)?"0"+this.date.getDate().toString():this.date.getDate().toString();
    var month = ((this.date.getMonth()+1).toString().length==1)?"0"+(this.date.getMonth()+1).toString():(this.date.getMonth()+1).toString(); 
    
    return  day+"/"+month;
  }
  
  this.getTitleDay = function()
  {
    var h5 = document.createElement("h5");
    var span = document.createElement("span");
    var a = (this.link != null)?"<a href=\"{link}\" onmouseover=\"calendar.links[this.href].showPreviewDay( this )\" onmouseout=\"calendar.links[this.href].hidePreviewDay()\" >":"<span>";
    var txt = document.createTextNode(" ");
    
    addClass( span, "dia" );
    
    span.innerHTML = this.getTypeDay().smallName;
    
    if (this.link != null) 
      a = a.replace("{link}", this.link);
    
    h5.appendChild( span ); 
    h5.appendChild( txt );
    h5.appendChild(document.createTextNode(this.getStringDate()));
		a+="<h5>"+h5.innerHTML+"</h5>"+((this.link != null)?"</a>":"</span>");

    b=document.createElement('div');
    b.innerHTML=a;
    return b;
  }
  
  this.showPreviewDay = function( obj )
  {
    this.preview.appendChild( this.getPreviewDay() );
    this.preview.style.left = obj.parentNode.parentNode.offsetLeft.toString() +  "px";
    this.preview.style.display = "block";
  }
  
  this.hidePreviewDay = function()
  {
    this.preview.innerHTML = "";
    this.preview.style.display = "none"; 
  }
  
  this.getPreviewDay = function()
  {
    var div1 = document.createElement("div");
    var div2 = document.createElement("div");
    var img = document.createElement("img");
    var h4 = document.createElement("h4");
    var p = document.createElement("p");
    var a = document.createElement("a");
    
    addClass( div1, "preview" );
    addClass( div2, "wrap" );
    
    img.src = this.image + "?ts=" + new Date().getTime();
    img.alt = this.title;
    img.title = this.title;
    /*img.style.display = "none";
    img.onload = function()
    {
      img.style.display = "inline";
    }*/
    
    h4.innerHTML = this.getStringDate() + " " + this.title;
    
    a.innerHTML = this.headline;
    a.href = this.link;
    
    div1.appendChild( div2 );
    div2.appendChild( img );
    div2.appendChild( h4 );
    div2.appendChild( p );
    p.appendChild( a );
    
    return div1;
  }
  
  this.setLi = function()
  {
    this.getDataFromJSON();
    this.setClassLi();
    this.li.appendChild( this.getTitleDay() );
    if( this.link == null )
    {
    	addClass( this.li, "off" );
    }
  }
  
  this.append = function()
  {
    this.container.appendChild( this.li );
  }
  
  this.show = function()
  {
    this.setLi();
    this.append();
  }
}
