/* poll.js - Extends fs_ajax.js
 * Stuff to investigate:
 *
 * http://dev.rubyonrails.org/ticket/4060 - A recent patch for OO (base.js)
 *
 * http://dev.rubyonrails.org/ticket/5459 - Better OO support for Prototype, but
 * in a Ruby like fashion.
 *
 * http://dean.edwards.name/ - Base.js homepage (as per first url)
 *
 * Have chosen to use a method discussed here:
 *
 * http://encytemedia.com/blog/articles/2006/05/23/prototype-inheritance-madness
 *
 */

/* Global cookie functions */
var Cookie = {
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    }
  return (document.cookie = escape(name) + '=' + escape(value || '') + expire);  
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled=='boolean') {
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') = '1');
  }
};

var Poll = Class.create();
// I know this looks freaky, but this is how Moo.fx does it - yucky stuff!
// Will be replacing this when Prototype upgrades its inheritance handling...
Object.extend(Object.extend(Poll.prototype,fs_ajax.prototype), {
	_parent:fs_ajax.prototype,
	debugMode:false,
	choiceId:"",
	choiceValue:"",
	initialize: function(ajaxUrl,outputId,pollName) {
	    // call the parents initialize with params
 	    this._parent.initialize.call(this,{ajaxUrl:ajaxUrl,outputId:outputId,name:pollName});
 	    // Check to see if the user has submitted before
      	if (Cookie.get(this.name+"_poll_submitted")=="true") {
            this.choiceValue=Cookie.get(this.name+"_poll_choice");
            this.choiceId=Cookie.get(this.name+"_poll_id");
 	        this.update();
 	        // Indicate previous poll choice
 		}
	},
	submit: function(formId) {
	    // Validate the form and submit an AJAX request
	    // check to see if the submission is complete
	    var pollForm=$(formId);
	    // Get the radio buttons from the poll
	    var submit_poll_choices=Form.getInputs(pollForm,"radio",this.name+"_choice");
	    // Find the checked radio
		var checked_choice=submit_poll_choices.find(function(poll_choice){
			return($F(poll_choice));
		});
		if (checked_choice!=null) {
		    // Maintain client state
		    this.debug("submitting choice: "+checked_choice.value);
			Cookie.set(this.name+"_poll_choice",checked_choice.value,365);
			this.choiceValue=checked_choice.value;
			Cookie.set(this.name+"_poll_id",checked_choice.id,365);
			this.choiceId=checked_choice.id;
			// Update server state by calling base class's submit
			this._parent.submit.call(this,formId);
		} // do nothing...
	},
	success: function(formId) {
	    Cookie.set(this.name+"_poll_submitted","true", 365);
	    this.animateOut();
	},
	highlightChoice: function() {
		
	    if (this.choiceValue!="") {
	        // Add a class to the appropriate field
	        var chosen=$(this.choiceId);
			chosen.addClassName("fs_poll_chosen");
			
	    }
	},
	animateOut: function() {
		this.debug("animateOut");
		new Effect.Opacity(this.outputId,{queue:{position:'end',scope:this.name},fps:60,duration:0.5,from:1.0,to:0,afterFinish:this.animateIn.bind(this)});
		// To form a queue for our animation, we use the polls name
	},
	animateIn: function() {
	    Effect.Transitions.exponential = function(pos) {
 			return 1-Math.pow(1-pos,2);
		}
		this.debug("animateIn");
		new Effect.Opacity(this.outputId,{queue:{position:'end',scope:this.name},fps:60,duration:0.5,from:0,to:1.0});
		//new Effect.Opacity(this.outputId,{fps:60,duration:0.5,from:0,to:1.0,queue:{position:'end',scope:this.name}});
		this._parent.success.call(this);
		// Grow the bars
		output=$(this.outputId);
		
		var bars=output.getElementsByClassName('fs_poll_bar');
		new Effect.Grow(bars[0],{queue:{position:'end',scope:this.name,limit:5},transition:Effect.Transitions.exponential,direction:'left',fps:60,duration:0.5});
		new Effect.Grow(bars[1],{queue:{position:'end',scope:this.name,limit:5},transition:Effect.Transitions.exponential,direction:'left',fps:60,duration:0.5});
		new Effect.Grow(bars[2],{queue:{position:'end',scope:this.name,limit:5},transition:Effect.Transitions.exponential,direction:'left',fps:60,duration:0.5});
		new Effect.Grow(bars[3],{queue:{position:'end',scope:this.name,limit:5},transition:Effect.Transitions.exponential,direction:'left',fps:60,duration:0.5});
		this.highlightChoice();
	}
});
