/*
** AnderAdvies JavaScript
*/

var AA = new Object();

AA.Quote = {
 element  : undefined,
 url      : '/home/quote/',
 throbber : undefined,
 
 fetch : function() {
  this.wrap.innerHTML = '';
  this.throbber.on();
  new Ajax(this.url,{
   update:this.wrap,
   onComplete:this.finish.bind(this)
  }).request();
 },
 finish : function() {
  this.throbber.off();
 },
 init : function() {
  this.element = $('quote');
  if (!this.element) return;
  this.wrap = this.element.getElement('div');
  if (!this.wrap) return;
  if (location.href.indexOf('/vrijdaganders/') != -1) this.url = '/vrijdaganders/quote/';
  this.throbber = new Throbber(this.wrap);
  this.trigger = new Element('img',{
   'src' : (document.body.id == 'va' ? '/images/arrow-va.gif' : '/images/arrow.gif'),
   'id' : 'more',
   'alt' : 'Meer',
   'title' : 'Nog een citaat',
   'events' : {
    'click' : this.fetch.bind(this)
   },
   'styles' : {
    'cursor' : 'pointer'
   }
  }).injectInside(this.element);
 }
}

AA.setName = function() {
  $$('h2, div.searchresults p, div.searchresults h3 a').each(function(el){
    el.setHTML(el.innerHTML.replace('AnderAdvies','<em>Ander</em>Advies'));
  });
}


AA.pdfs = function() {
	$$('a.download').each(function(el){
		el.setAttribute('target','_blank');
	});
}

var Throbber = new Class({
 initialize : function (target) {
  this.target  = $(target);
  this.status  = 0;
  this.img     = new Element('img');
  this.img.src = arguments[1] || '/images/throbber.gif';
 },

 on : function() {
  if (this.status == 0) {
   this.target.appendChild(this.img);
   this.status = 1;
  }
 },

 off : function() {
  if (this.status == 1) {
   try { $(this.img).remove(); } catch(x) { }
   this.status = 0;
  }
 },

 toggle : function() {
  if (this.status)
   this.off();
  else
   this.on();
 }
});

window.addEvent('domready',AA.Quote.init.bind(AA.Quote));
window.addEvent('domready',AA.setName);
window.addEvent('domready',AA.pdfs);