/**
 * Copyright (C) 2006 by Renaud Delbru.
 * http://www.paponie.fr
 *
 * @author	Renaud Delbru <renaud.delbru@deri.org>
 *
 * GCalendar is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Foobar; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
// ==========================================================================
window.onload = initCal;

var calGP = null;
var calHP = null;

function initCal(){
	calGP = new GPCalendar();  
	calGP.onsuccess = displayEventsGP;
  calGP.loadFeed();
  
  calHP = new HPCalendar();  
  calHP.onsuccess = displayEventsHP;
  calHP.loadFeed();
}

function displayEventsGP(c){
	for(k in c.entries){
		addEventGP(c.entries[k]);
	}
  
  Element.hide('eventLoad');
	Effect.Appear('events');
}

function addEventGP(entry){
	try{
		var li = document.createElement("li");
    var str = formatDateBasic(entry.startDate);
    str += " - " + formatDateBasic(entry.endDate);
        
    li.innerHTML = str;
        
    if(entry.title == "Disponible"){
      var ul1 = $('GPDisponibilityList');
		  ul1.appendChild(li);
		}
//    else {
//      var ul2 = $('GPNonDisponibilityList');
//      ul2.appendChild(li);
//    }
	}catch(e){
		alert("Adding entry error: " + e.description);
	}
}

function displayEventsHP(c){
  for(k in c.entries){
    addEventHP(c.entries[k]);
  }
}

function addEventHP(entry){
  try{
    var li = document.createElement("li");
    var str = formatDateBasic(entry.startDate);
    str += " - " + formatDateBasic(entry.endDate);
        
    li.innerHTML = str;
        
    if(entry.title == "Disponible"){
      var ul1 = $('HPDisponibilityList');
      ul1.appendChild(li);
    }
//     else {
//      var ul2 = $('HPNonDisponibilityList');
//      ul2.appendChild(li);
//    }
  }catch(e){
    alert("Adding entry error: " + e.description);
  }
}

function formatDateBasic(dt){
	var months = {0: '01',1:'02',2:'03',3:'04',4:'05',5:'06',6:'07',7:'08',8:'09',9: '10',10:'11',11:'12'};
	var res = dt.getDate() + "/" + months[dt.getMonth()] + "/" + dt.getFullYear();
	return res;
}

function formatDateHuman(dt){
	var months = {0: 'January',1:'February',2:'March',3:'April',4:'May',5:'June',6:'July',7:'August',8:'September',9: 'October',10:'November',11:'December'};
	var res = dt.getDate() + ". " + months[dt.getMonth()] + " " + dt.getFullYear();
	res += " " + dt.getHours() + ":" + (dt.getMinutes()<10?"0" + dt.getMinutes():dt.getMinutes());
	return res;
}
