// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
Request.prototype._send = Request.prototype.send
Request.implement({

  auth_token: function(){
    return AUTH_TOKEN;
  },

  // This is more verbose than is ideal, but I don't see a better place
  // to hook this functionality in
  send: function(options){
    var type = $type(options);
    if (type == 'string' || type == 'element') options = {data: options};

    var old = this.options;
    options = $extend({data: old.data, url: old.url, method: old.method}, options);

    switch ($type(options.data)){
      case 'element': options.data = $(options.data).toQueryString(); break;
      case 'object': case 'hash': options.data = Hash.toQueryString(options.data);
    }

    // If this isn't a get request add the authenticity_token
    if (options.method != 'get' || options.method != 'GET')
      options.data = options.data+'&authenticity_token='+this.auth_token();

    // Call the original send
    this._send(options)
  }

});

Element.implement({
	flash: function(to,from,reps,prop,dur) {

		//defaults
		if(!reps) { reps = 1; }
		if(!prop) { prop = 'background-color'; }
		if(!dur) { dur = 250; }

		//create effect
		var effect = new Fx.Tween(this, {
				duration: dur,
				link: 'chain'
			})

		//do it!
		for(x = 1; x <= reps; x++)
		{
			effect.start(prop,from,to).start(prop,to,from);
		}
	}
});
var twitterRoll = function() {
  var total = $$('#tweet_mid .twitter_div').length;
  var active_tweet = $$('#tweet_mid .active_tweet');
  var current_num = active_tweet[0].id.split("_")[2]
  if ((current_num*1+1) == (total*1)) {
    var next = 'twitter_div_0';
  } else {
    var next = 'twitter_div_' + (current_num*1 + 1);
  }
  $(next).addClass('active_tweet');
  active_tweet.removeClass('active_tweet');
  active_tweet.fade('out');
  $(next).fade('in');
}
window.addEvent('domready', function(){
  var tweet1 = $('twitter_div_0');
  if (tweet1) {
    twitterRoll.periodical(7000)
  }

  var map_req = new Request.HTML({
    update: 'map_container'
  });

	$$('div.slider.advanced').each(function(el, i){
		var slider = new Slider(el, el.getElement('.knob'), {
			steps: ELECTION_MAP_STEPS,  // Steps from 0 to 255
			wheel: true, // Using the mousewheel is possible too
			onChange: function() {
				//map_req.get('/maps/'+this.step+'/slide');
				// ajax is too slow, we need to preload
				var the_current = $('map_'+current_map);
				var new_current = $('map_'+ELECTION_MAPS[this.step-1]);
        if (the_current && new_current) {
  				the_current.setStyle('display', 'none');
	  			current_map = ELECTION_MAPS[this.step-1];
          new_current.setStyle('display', 'block');
	  		}
				// also change date and description
			}
		});
	});

	var drop = $('drop');
	if (drop) {
	  var slide = new Fx.Slide(drop);
	  var drop_foot = $('drop_foot');
	  slide.hide();
	  drop_foot.addEvent('click', function(e) {
      slide.toggle().chain(function() {
        if (drop_foot.getStyle('background-image').indexOf('drop.png') != -1) {
          drop_foot.setStyle('background-image', 'url(/images/dropout.png)');
        } else {
          drop_foot.setStyle('background-image', 'url(/images/drop.png)');
        }
      });
	  });
	}

  var booklight = $('booklight');
  if (booklight) {
    SqueezeBox.initialize({
        size: {x: 793, y: 477}
    });
    SqueezeBox.assign($('booklight_vbanner'));
    SqueezeBox.assign($('booklight_vbanner2'));
  }


});

