/**
 * Copyright (C) 2006 by FGCZ.
 * http://www.snyke.net
 *
 * @author	Christian Decker <decker.christian@gmail.com>
 *
 * 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
 */
// ==========================================================================
/**
 * Class that represents the calendar.
 */
var Calendar = Class.create();
Calendar.prototype = {
	uri: "7cghno42lleqpbihmoi5qiikm8%40group.calendar.google.com",
	orderby: "starttime",
	singleEvents: true,
	futureEvents: true,
	sortOrder: "ascending",
	hourSeparator : ":",
	dateSeparator : "/",
	startEndSeparator : " -> ",
	wholeDayText : "",
	entries: {},
	title : "",
	initialize: function(u){
		if(u){this.uri = u;}
	},
	setIframe: function(string) {
	    this.iFrame = string;
	},
	buildURL: function(){
		var str = "http://www.google.com/calendar/feeds/";
		str += this.uri;
		str += "/public/full-noattendees?orderby=" + this.orderby;
		str += "&alt=json-in-script&callback=window.activeCalendar.parseFeed&";
		str += "singleevents=" + this.singleEvents;
		str += "&sortorder=" + this.sortOrder;
		str += "&futureevents=" + this.futureEvents;
		return str;
	},
	
	loadFeed: function(u){
	    $("wait").show();
	    if(u){this.uri = u;}
		window.activeCalendar = this;
		var headTag = document.getElementsByTagName('head')[0]; 
		var script = document.createElement("script");
		script.src = this.buildURL();
		script.language = "JavaScript";
		headTag.appendChild(script);
	},
	
	formatDate: function(sdt,fulls,edt,fulle) {	
		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 = "";
        if (sdt.getDate() == edt.getDate() && sdt.getMonth() == edt.getMonth() && sdt.getFullYear() == edt.getFullYear() ) {
            res += sdt.getDate() + this.dateSeparator + months[sdt.getMonth()] + this.dateSeparator + sdt.getFullYear();
            res += " " + sdt.getHours() + this.hourSeparator + (sdt.getMinutes()<10?"0" + sdt.getMinutes():sdt.getMinutes());
            res += "  -  ";
            res += " " + edt.getHours() + this.hourSeparator + (edt.getMinutes()<10?"0" + edt.getMinutes():sdt.getMinutes());
        } 
        else {
            res += "" + sdt.getDate() + this.dateSeparator + months[sdt.getMonth()] + this.dateSeparator + sdt.getFullYear();
            if (sdt.getHours() == 0 && sdt.getMinutes() == 0) {
                 res += this.wholeDayText;
            } else {
                res += " " + sdt.getHours() + this.hourSeparator + (sdt.getMinutes()<10?"0" + sdt.getMinutes():sdt.getMinutes());
            }                        
            if (edt.getHours() == 0 && edt.getMinutes() == 0) {
                if (edt.getDate() == sdt.getDate() + 1) {
                    
                } else {
                    edt.setDate(edt.getDate()-1);
                    res += this.startEndSeparator + edt.getDate() + this.dateSeparator + months[edt.getMonth()] + this.dateSeparator + edt.getFullYear();
                    res += this.wholeDayText;
                }
            } else {
                res += this.startEndSeparator + edt.getDate() + this.dateSeparator + months[edt.getMonth()] + this.dateSeparator + edt.getFullYear();
                res += " " + edt.getHours() + this.hourSeparator + (edt.getMinutes()<10?"0" + edt.getMinutes():edt.getMinutes());
            }                                            
        }
	    return res;
	},

	parseFeed: function(json){
		this.title = json.feed.title.$t;
		e = json.feed.entry;
		for(var i=0;i < e.length;i++){
			this.entries[i] = new EventEntry(e[i])
		}
		try {
			this.onsuccess(this);
      } catch (e) {}
	},
	
	onsuccess: function(c){	
	    $('content').innerHTML = this.iFrame;
	    for(k in c.entries){
            try{
		        var entry = c.entries[k];		    
		        var eventDiv = $('content');
		        var div = document.createElement("div");		    
		        var str = "<div class='title2 pointer' onclick=javascript:$('cal_content"+k+"').toggle();>" + rep(this.formatDate(entry.startDate,entry.fullStartDay,entry.endDate,entry.fullEndDay)) + ": " + rep(entry.title) + " </div>";
		        str += "<div id='cal_content"+k+"' style='display:none'>";
		        if(entry.content)
			        str += "<div class='what'>" + rep(entry.content) + "</div>";
		        if(entry.location)
		            str += "<div class='where'>&nbsp;&nbsp;&nbsp;" + rep(entry.location) + "</div>";
		        str += "</div>";
		        div.innerHTML = str;
		        eventDiv.appendChild(div);
                Element.hide(eventDiv); 
		        new Effect.Appear(eventDiv);
		        $("wait").hide();
	        }catch(e){
		        alert(e.description);
	        }
	    }	
	}
};


// ==========================================================================
var EventEntry = Class.create();
EventEntry.prototype = {
	title: null,
	content: null,
	startDate: null,
	endDate: null,
	location: null,
	fullStartDay: false,
	fullEndDay: false,
	initialize: function(entry){
		this.title = entry.title.$t;
		this.content = entry.content.$t;
		this.startDate = DateConverter.rfc3339toDate(entry.gd$when[0].startTime);
		this.endDate = DateConverter.rfc3339toDate(entry.gd$when[0].endTime);
		if ((this.startDate.getHours() == 00) && (this.startDate.getMinutes() == 00)) {
		    this.fullStartDay=true;
		}
		if ((this.endDate.getHours() == 00) && (this.endDate.getMinutes() == 00)) {
		    this.fullEndDay=true;
		}
		this.location = entry.gd$where[0].valueString;
	}
};