// BAND data
// ---------------------------------
// {
//  id:         bandId,
//  name:       "Band Name",
//  genre:      "Genre",
//  bio:        "Detailed Band Bio",
//  hometown:   "City, ST",
//  mp3:        "http://mp3.url",
//  image:      "http://pic.url",
//  showcases:  [
//      {
//          club:       "Club Name"
//          date:       "Day, March N",
//          time:       "N:00 P.M."
//      },
//      {
//          club:       "Club Name"
//          date:       "Day, March N",
//          time:       "N:00 P.M."
//      }
//  ]
// }
// ---------------------------------

// VENUE data
// ---------------------------------
// {
//  name:       "Club Name",
//  address:    "123 Street Nm",
//  age_policy: "(age policy)",
//  latitude:   latitude,
//  longitude:  longitude,
//  dates: {
//      "2007-03-12": {
//          "8:00 P.M.": {
//              id:         bandId,
//              name:       bandName,
//          },
//          "9:00 P.M.": {
//              id:         bandId,
//              name:       bandName,
//          }
//      },
//      "2007-03-13": {
//          "8:00 P.M.": {
//              id:         bandId,
//              name:       bandName,
//          },
//          "9:00 P.M.": {
//              id:         bandId,
//              name:       bandName,
//          }
//      }
//  }
// }
// ---------------------------------

var sxswdata = {
    // this will get the detailed band info (as described above) for the band with the given id
    getBand : function(id, callback) {
        jQuery.getJSON("/sxswinfoapi/band/" + id, callback);
    },

    // this will get a sorted list of band data objects whose name starts with the given letter
    bandListAlpha : function(letter, callback) {
        jQuery.getJSON("/sxswinfoapi/bandsalpha/" + (letter ? letter : "all"), callback);
    },

  	// this will get a sorted list of band data objects whose name starts with the given letter
    bandListAlphaHTML : function(letter, callback) {
        jQuery.get("/sxswinfoapi/bandsalpha-html/" + (letter ? letter : "all"), callback);
    },

    // this will get the list of showcases, organized by venue and then time
    scheduleByVenue : function(date, callback) {
        jQuery.getJSON("/sxswinfoapi/byvenue/" + date, callback);
    },

    // this will get the list of showcases, organized by time and then venue
    scheduleByTime : function(date, callback) {
        jQuery.getJSON("/sxswinfoapi/bytime/" + date, callback);
    },

    // this will get the list of showcases, organized by venue and then time
    scheduleByVenueHTML : function(date, callback, numPages, page) {
        // use timer to paginate data and call callback once per chunk...
        jQuery.get("/sxswinfoapi/byvenue-html/" + date + "/" + numPages + "/" + page, callback);
    },

    // this will get the list of showcases, organized by time and then venue
    scheduleByTimeHTML : function(date, callback, numPages, page) {
        jQuery.get("/sxswinfoapi/bytime-html/" + date + "/" + numPages + "/" + page, callback);
    },

    getCal : function(email, password, callback) {
        jQuery.get("http://calendar.sxsw.com/iCal-" + password + "-" + email + ".ics",
                function(data) {
                    var items = {};
                    var showcases = data.split("BEGIN:VEVENT");
                    showcases.shift();
                    showcases.forEach(function(showcase) {
                        var date = null;
                        var item = {};
                        showcase.split("\n").forEach(function(line) {
                            if (line.match(/.*SUMMARY:(.*)$/)) {
                                item["band"] = line.replace(/.*SUMMARY:(.*)$/, "$1");
                            }
                            if (line.match(/.*LOCATION:([^(]+) \(.*/)) {
                                item["location"] = line.replace(/.*LOCATION:([^(]+) \(.*/, "$1");
                            }
                            if (line.match(/.*URL;.*\/(\d+)\.html.*/)) {
                                item["id"] = line.replace(/.*URL;.*\/(\d+)\.html.*/, "$1");
                            }
                            if (line.match(/.*DTSTART;TZID=US\/Central:(\w+).*/)) {
                                date = line.replace(
                                        /.*DTSTART;TZID=US\/Central:(\d{4})(\d{2})(\d{2})T\d{6}.*/,
                                        "$1-$2-$3");
                                item["time"] = line.replace(
                                        /.*DTSTART;TZID=US\/Central:\d{8}T(\d{2})(\d{2})\d{2}.*/, 
                                        "$1:$2");
                            }
                        });
                        if (!items[date]) {
                            items[date] = []
                        }
                        items[date].push(item);
                    });
                    callback(items);
                })
    }
}