var ai1ec = (function(){
    var API = {}, json = [], events = [];
    
    //Categorie di eventi
    API.cats = {
        single: 34,
        repeat: 87
    };

    var getCleanId = function(uid){
        var initChar = uid.indexOf('-'),
            endChar = uid.indexOf('@');

        return uid.substr(initChar+1, uid.length - uid.substr(endChar -1).length - initChar );
    }; 

    //Parser di un evento dell'array tornato da getXmlData
    API.event = {
        getId : function(event) {
            return event.uid ? getCleanId(event.uid) : null;
        },
        
        getTitle : function(event) {
            return event.summary || null;
        },
    
        getDescription : function(event) {
            return event.description || "";
        },
    
        getInitDate : function(event) {
            return event.dtstart && event.dtstart._ ? event.dtstart._ : "";
        },
    
        getEndDate : function(event) {
            return event.dtend && event.dtend._ ? event.dtend._ : "";
        },
    
        getCats : function(event) {
            return event.categories || "";
        },
        
        getTags : function(event) {
            return event["x-tags"] && event["x-tags"]._ ? event["x-tags"]._ : "";
        },
    
        getUrl : function(event) {
            return event.url && event.url.$ && event.url.$.uri ? event.url.$.uri : "";
        },
        
        getContact : function(event) {
            return event.contact || "";
        },
        
        getGeo : function(event) {
            return event.geo || "";
        },
        
        //Regole per gli eventi ripetitivi
        getRule : function(event) {
            return event.rrule || "";
        },
		
		getImage : function(event){
			return event['x-wp-images-url'] || "";
		},
    };
    
    var getXmlData = function(url, catId, callback) {
        $.ajax({
            url: url,
            dataType: 'xml',
            method: 'GET',
            success: function(res) {
                json[catId] = $.xml2json(res);
                events[catId] = json[catId]["#document"].vcalendar.vevent;                
                console.log(events[catId]);
                return callback(events[catId]);
            },
            error: function(err){
                console.log("Err: status ", err.status);
                return callback(events[catId]);
            }
        });
    };
    
    var getDefaultUrl = function(catId){
        catId = catId || API.cats.single;
        
        return API.remote.site + API.remote.q + "&" + API.remote.catq + catId + "&" + API.remote.xmlq;
     }
    
    //Oggetto che gestisce attività remote
    API.remote = {
        site : 'http://www.ecn.org/xm24',
        q : '/?plugin=all-in-one-event-calendar&controller=ai1ec_exporter_controller&action=export_events',
        post: 'ai1ec_post_ids=',
        catq : 'ai1ec_cat_ids=',
        xmlq : 'xml=true',
        win : 'window=true',
        //post_ids
        
        //Torna un array con tutti gli eventi organizzati da xm24, un array vuoto se errore.
        getAllEvents : function(catId, callback) {
            var url = getDefaultUrl(catId);
            getXmlData(url, catId, callback);
        }, 

        //Torna un array con gli eventi tra -30 e +30 giorni da oggi, un array vuoto se errore.
        getLastEvents : function(catId, callback) {
            var url = getDefaultUrl(catId) + '&' + API.remote.win;
            getXmlData(url, catId, callback);
        }, 

    }
    
    return API;
})();
