var TW = Class.create();

TW.prototype = {
  initialize : function() {
    this._waiting = new Array();
    this._forumNewsSeen = new Array();
    this._forumNewsSeen[0] = 0;
    this._forumNewsId = 0;
  },
  
  setWaiting : function(id, len) {
    this._waiting[id] = 1;
    if (len) window.setTimeout(this.clearWaiting.bind(this,id), len);
  },
  
  clearWaiting : function(id) {
    this._waiting[id] = 0;
  },
  
  isWaiting : function(id) {
    return this._waiting[id];
  },
  
  keepWaiting : function(fn) {
    window.setTimeout(fn, 500);
  },

  // Ajax stuff
  Ajax : Object.extend(new Object(), {

    checkMessageBox: function(t) {
      if (t.action && t.action == 'msgbox') {
        twmsgbox.show(t.title, t.msg);
	return true;
      }
      return false;      
    },

    postComment: function() {
      var c = $F('newscomment');
      if (c.length == 0) {
	twmsgbox.show("No Comment", "You did not enter a comment");
	return;
      }

      $('commentbtn').disabled=true;

      var qid = $F('quoteid');
      var nid = $F('newsid');
      var pg = $F('page');

      var query = $H({ quoteid: qid, newsid: nid, comment: c, page: pg, action: 'addcomment'});

      new Ajax.Request(baseurl + '/ajax/news.php', 
      { method: 'post', parameters: query, onSuccess: this.donePostComment.bindAsEventListener(this), onFail: function() { twmsgbox.show("Failed Posting", "The post failed");  $('commentbtn').disabled=false;} } );
    },
	
    donePostComment: function(r) {
      t = r.responseText.parseJSON();
      //if (t.action && t.action == 'msgbox') {
      //twmsgbox.show(t.title, t.msg);
      //	return;
      //}
      if (this.checkMessageBox(t)) {
	return;
      }

      if (t.newurl) {
	window.location=t.newurl;
	return;
      }
      
      $('comcon').innerHTML = t.html;
      $('post' + t.postid).scrollIntoView();

    },

    deleteComment: function(pid) {
      var query = $H({ postid: pid, action: 'deletecomment'});
      this.deleteid = pid;
      
      new Ajax.Request(baseurl + '/ajax/news.php',
      { method: 'post', parameters: query, onSuccess: this.doneDeleteComment.bindAsEventListener(this), onFail: function() { twmsgbox.show("Failed", "Delete Action Failed");  } } );

    },

    doneDeleteComment: function(r) {
      t = r.responseText.parseJSON();
      if (t.type == "ok") {
	if ($('cbody' + this.deleteid)) {
	  Effect.BlindUp('cbody' + this.deleteid);
	}
      }

      twmsgbox.show(t.title, t.msg);
    },

    showBumpers: function(nid, pg, fr) {
      var query = $H({ newsid: nid, page:pg, friends:fr, action: 'showbumpers2'});

      new Ajax.Request(baseurl + '/ajax/news.php',
      { method: 'post', parameters: query, onSuccess: this.doneShowBumpers.bindAsEventListener(this), onFail: function() { twmsgbox.show("Failed", "Failed to go to page " + pg); } } );

    },

    doneShowBumpers: function(r) {
      var t = r.responseText.parseJSON();
      if (this.checkMessageBox(t)) {
        return;
      }
      
      $('bumperslist').innerHTML = t.bumperslist;
      $('bumppagenav').innerHTML = t.pagenav;
    },

    voteQuoteUp : function(id) {
      new Effect.Fade($('quote' + id));
    
      var query = $H({ quoteid: id, action: 'voteup'});

      new Ajax.Request(baseurl + '/ajax/quotes.php', { method: 'post', parameters: query, onSuccess: this.doneQuoteVote.bindAsEventListener(this), onFail: function() { twmsgbox.show("Failed", "Your vote failed.  You Suck"); } } );
      
    },

    voteQuoteDown : function(id) {
      new Effect.Fade($('quote' + id));
    
      var query = $H({ quoteid: id, action: 'votedown'});

      new Ajax.Request(baseurl + '/ajax/quotes.php', { method: 'post', parameters: query, onSuccess: this.doneQuoteVote.bindAsEventListener(this), onFail: function() { twmsgbox.show("Failed", "Your vote failed.  You Suck"); } } );
    },

    doneQuoteVote : function(r) {
      var t = r.responseText.parseJSON();
      if (this.checkMessageBox(t)) {
        return;
      }
      
      new Insertion.Bottom('quotelist', t.newquote);
      $('userquotecount').innerHTML = t.usercount;
      new Effect.Appear($('quote' + t.quoteid));
    },

    getNewQuotes : function() {
      var divs = $('quotelist').descendants();
      var x;
      //for (x=0; x < divs.length; x++) {
      //	new Effect.Fade(divs[x]);
      //}
      $('quotelist').innerHTML = '';

      var query = $H({action: 'getnewquotes'});
      new Ajax.Request(baseurl + '/ajax/quotes.php', { method: 'post', parameters: query, onSuccess: this.doneGetNewQuotes.bindAsEventListener(this), onFail: function() { twmsgbox.show("Failed", "You suck at getting new quotes"); } } );

    },

    doneGetNewQuotes : function(r) {
      var t = r.responseText.parseJSON();
      if (this.checkMessageBox(t)) {
        return;
      }

      new Insertion.Bottom('quotelist', t.newquote);

      var divs = $('quotelist').descendants();
      var x;
      for (x=0; x < divs.length; x++) {
        new Effect.Appear(divs[x]);
      }

    },

    submitQuote: function() {
      var query = $H({action: 'submitquote', quote: $('newquote').value });
      new Ajax.Request(baseurl + '/ajax/quotes.php', { method: 'post', parameters: query, onSuccess: this.doneSubmitQuote.bindAsEventListener(this), onFail: function() { twmsgbox.show("Failed", "Your submission failed.  You Suck"); } } );

    },

    doneSubmitQuote : function(r) {
      var t = r.responseText.parseJSON();
      if (this.checkMessageBox(t)) {
	if (t.msg.match('submitted')) {
	  $('newquote').value = "";
	}

	return;
      }

    },

    getRSSNews : function(id) {
      var query = $H({action: 'getrssnews', rssnewsid: id });
      new Ajax.Request(baseurl + '/ajax/admin_news.php', { method: 'post', parameters: query, onSuccess: this.doneGetRSSNews.bindAsEventListener(this), onFail: function() { twmsgbox.show("Failed", "Your submission failed.  You Suck"); } } );
      
    },
    
    doneGetRSSNews : function(r) {
      var t = r.responseText.parseJSON();
      if (this.checkMessageBox(t)) {
        return;
      }

      $('rsstitle').value = t.title;
      $('rsshref').value = t.href;
      $('rssview').href = t.href;
      $('rssbody').value = t.summary;      
    }

  }),  


  // Forums stuff
  Forums : Object.extend(new Object(), {
    

    // Forum news stuff
    News : Object.extend(new Object(), {
      _seen : Array(0),
      _newsid : 0,
      _news: '',
      _delay: 5000,


      startNews: function(news) {
	this._news = news;

	this._upcoming = 0;
	this._promoted = 0;

	window.setTimeout(this.continueNews.bind(this), this._delay);
      },

      continueNews : function() {
	var un = $('forumnews').getElementsByTagName('div');
	var pn = $('forumnewspromoted').getElementsByTagName('div');


	if (un[2] && this._upcoming < this._news.upcoming.length) Effect.Fade(un[2].id);
	if (pn[2] && this._promoted < this._news.promoted.length) Effect.Fade(pn[2].id);
		
	window.setTimeout(this.showNews.bind(this), 1000);

      },

      showNews : function() {
	var un = $('forumnews').getElementsByTagName('div');
        var pn = $('forumnewspromoted').getElementsByTagName('div');

	var run_again = 0;

	if (this._upcoming < this._news.upcoming.length) {
	  new Insertion.Top('forumnews', this._news.upcoming[this._upcoming].html);
	  Effect.Appear('news' + this._news.upcoming[this._upcoming].id);
	  this._upcoming++;
	  run_again = 1;
	}


	if (this._promoted < this._news.promoted.length) {
          new Insertion.Top('forumnewspromoted', this._news.promoted[this._promoted].html);
          Effect.Appear('news' + this._news.promoted[this._promoted].id);
          this._promoted++;
	  run_again = 1;
        }

	if (run_again) {
	  window.setTimeout(this.continueNews.bind(this), this._delay);
	}
      }

      
    })
    // End forum news
    
  })
  // End forum
  
}

var $TW = new TW();

Effect.FadeKeepSpace = function(element) {
  var oldOpacity = Element.getInlineOpacity(element);
  var options = Object.extend({
  from: Element.getOpacity(element) || 1.0,
  to:   0.0,
  afterFinishInternal: function(effect) { with(Element) { 
    if(effect.options.to!=0) return;
    blank(effect.element);
    setStyle(effect.element, {opacity: oldOpacity}); }}
  }, arguments[1] || {});
  return new Effect.Opacity(element,options);
}

Effect.AppearKeepSpace = function(element) {
  var options = Object.extend({
  from: (Element.getStyle(element, 'visibility') == 'hidden' ? 0.0 : Element.getOpacity(element) || 0.0),
  to:   1.0,
  beforeSetup: function(effect) { with(Element) {
    setOpacity(effect.element, effect.options.from);
    unblank(effect.element); }}
  }, arguments[1] || {});
  return new Effect.Opacity(element,options);
}

Object.extend(Element, {blank:function(){for(var i=0;i<arguments.length;i++){var element=$(arguments[i]);element.style.visibility='hidden';}},unblank:function(){for(var i=0;i<arguments.length;i++){var element=$(arguments[i]);element.style.visibility='visible';}}});


String.prototype.parseJSON = function () {
  try {
    return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
						       this.replace(/"(\\.|[^"\\])*"/g, ''))) &&
            eval('(' + this + ')');
    } catch (e) {
        return false;
    }
};

