/**
 * Copyright (c) 2008 Google Inc.
 *
 * You are free to copy and use this sample.
 * License can be found here: http://code.google.com/apis/ajaxsearch/faq/#license
*/

/**
 * @fileoverview A feed gadget based on the AJAX Feed API.
 * @author dcollison@google.com (Derek Collison)
 */

/**
 * GFdynamicFeedControl
 * @param {String} feed The feed URL.
 * @param {String|Object} container Either the id string or the element itself.
 * @param {Object} options Options map.
 * @constructor
 */
function GFdynamicFeedControl(feedUrls, page) {

 	// the feeds.
 	this.feeds = [];
 	this.results = [];
	this.options = [{'page':page,'numResults':(page==='home')?30:90}];

  if (typeof feedUrls == 'string') {
    this.feeds.push({url:feedUrls});
  } else if (typeof feedUrls == 'object') {
    for (var i=0; i<feedUrls.length; i++) {
      var entry = feedUrls[i];
      var o = {};
      var feedUrl;
      if (typeof entry == 'string') {
        o.url = feedUrls[i];
      } else if (typeof entry == 'object') {
        o = feedUrls[i];
	if (o && o.title) {
	  var s = o.title;
	  o.title = s.replace(/</g,'&lt;').replace(/>/g, '&gt;');
	}
      }
      this.feeds.push(o);
    }
  }
  this.setup_();

}

/**
 * Basic setup.
 * @private
 */
GFdynamicFeedControl.prototype.setup_ = function() {

  // Browser fun.
  if (window.ActiveXObject) {
    this.ie = this[window.XMLHttpRequest ? 'ie7' : 'ie6'] = true;
  } else if (document.childNodes && !document.all && !navigator.taintEnabled) {
    this.safari = true;
  } else if (document.getBoxObjectFor != null) {
    this.gecko = true;
  }

  for (var i = 0; i < this.feeds.length; i++) {
    var feed = new google.feeds.Feed(this.feeds[i].url);
    feed.setNumEntries(this.options[0].numResults);
    feed.load(this.bind_(this.feedLoaded_, i));
  }
};

/**
 * Helper method to bind this instance correctly.
 * @param {Object} method function/method to bind.
 * @return {Function}
 * @private
 */
GFdynamicFeedControl.prototype.bind_ = function(method) {
  var self = this;
  var opt_args = [].slice.call(arguments, 1);
  return function() {
    var args = opt_args.concat([].slice.call(arguments));
    return method.apply(self, args);
  }
};

/**
 * Callback associated with the AJAX Feed api after load.
 * @param {Object} result Loaded result.
 * @private
 */
 GFdynamicFeedControl.prototype.feedLoaded_ = function(index, result) {
	if(this.options[0].page == 'home'){
		getCurrentSchedule.init(result.feed.entries);
	} else if(this.options[0].page == 'schedule'){
		getWeeklySchedule.init(result.feed.entries);
	}
}
