/*
 * class BirdHouse
 *
 * JavaScript class to work with PHP BirdHouse class and scripts
 * for easy Twitter integration
 *
 * Requires: 
 *  jQuery        (http://jquery.com)
 *  CooQuery      (http://plugins.jquery.com/project/cooquery)
 *
 *  TODO: Eliminate the above dependencies.
 *
 * Constructor params:
 *  consumer_key - Twitter API consumer_key (not sure if this is needed... yet)
 *  callback_path - path where callback scripts are located
 */
function BirdHouse(params) {
  if (typeof params == 'object') {
    if (params.consumer_key != undefined) {
      this.consumer_key = params.consumer_key;
    }
    if (params.popup != undefined) {
      this.popup = params.popup;
    } else {
      this.popup = true;
    }
    if (params.callback_path != undefined) {
      this.callback_path = window.location.protocol + "//" +  params.callback_path;
    } else {
      this.callback_path = '';  // default is local directory
    }
  }
  

  /*
   * isConnected()
   *
   * returns false if not connected
   * sets and returns userID if connected 
   */
  this.isConnected = function() {
    if ($.readCookie('BirdHouse')) {
      return $.readCookie('BirdHouse');
    } else {
      return false;
    }
  }

  /*
   * logon(callback)
   * 
   * callback - function to execution when logon window exits
   *
   * opens pop-up with Twitter authentication stuff
   */
  this.logon = function(callback, checked) {
      if (this.popup) {
          var win = this.popupWindow(this.callback_path+'logon.php', 800);
          $().unbind('BHclose');
          $(win).bind('BHclose', callback);
      } else {
          var url = this.callback_path+'logon.php?canvas=1';
          if (checked) {
              url += '&checked=1';
          }
          window.location = url; 
      } 
  }

  /*
   * logout()
   *
   * logs out of Twitter and removes BirdHouse cookies
   */
  this.logout = function() {}

  /*
   * popupWindow(url,w,h)
   *
   * pops up window!!
   */
  this.popupWindow = function(url,w,h) {
    if (url == undefined) {
      return; // no url, so what are we doing here?
    }
    if (w == undefined) {
      w = 500;
    }
    if (h == undefined) {
      h = 500;
    }
    var winSettings = "width="+w+",height="+h;
    var keeper = window.open(url, "birdhouse", winSettings, true); 

    if (keeper) {
      return keeper; 
    } else {
      return null;
    }
  } 
}

